Skip to content

Commit 3a26455

Browse files
committed
Added samples project for Trinomial Tree valuation
1 parent 6d1b727 commit 3a26455

File tree

6 files changed

+154
-5
lines changed

6 files changed

+154
-5
lines changed

samples/csharp/Cmdty.Storage.Samples.Intrinsic/Cmdty.Storage.Samples.Intrinsic.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
5-
<TargetFramework>netcoreapp2.1</TargetFramework>
5+
<TargetFramework>netcoreapp3.1</TargetFramework>
66
</PropertyGroup>
77

88
<ItemGroup>

samples/csharp/Cmdty.Storage.Samples.Intrinsic/Program.cs

+3-4
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@ namespace Cmdty.Storage.Samples.Intrinsic
3131
{
3232
class Program
3333
{
34-
35-
3634
static void Main(string[] args)
3735
{
3836
const double constantMaxInjectRate = 5.26;
@@ -82,8 +80,8 @@ static void Main(string[] args)
8280
.WithStartingInventory(startingInventory)
8381
.ForCurrentPeriod(currentPeriod)
8482
.WithForwardCurve(forwardCurveBuilder.Build())
85-
.WithCmdtySettlementRule(day => day.First<Month>().Offset(1).First<Day>().Offset(5))
86-
.WithDiscountFactorFunc(day => 1.0)
83+
.WithCmdtySettlementRule(day => day.First<Month>().Offset(1).First<Day>().Offset(5)) // Commodity is settled on the 5th day of the next month
84+
.WithDiscountFactorFunc(day => 1.0) // Assumes to discounting
8785
.WithFixedGridSpacing(10.0)
8886
.WithLinearInventorySpaceInterpolation()
8987
.WithNumericalTolerance(1E-12)
@@ -94,6 +92,7 @@ static void Main(string[] args)
9492
Console.WriteLine("Decision profile:");
9593
Console.WriteLine(valuationResults.DecisionProfile.FormatData("F2", -1));
9694

95+
Console.WriteLine("Press any key to exit");
9796
Console.ReadKey();
9897
}
9998
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>netcoreapp3.1</TargetFramework>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<PackageReference Include="Cmdty.Storage" Version="0.1.0-beta2" />
10+
</ItemGroup>
11+
12+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
#region License
2+
// Copyright (c) 2019 Jake Fowler
3+
//
4+
// Permission is hereby granted, free of charge, to any person
5+
// obtaining a copy of this software and associated documentation
6+
// files (the "Software"), to deal in the Software without
7+
// restriction, including without limitation the rights to use,
8+
// copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
// copies of the Software, and to permit persons to whom the
10+
// Software is furnished to do so, subject to the following
11+
// conditions:
12+
//
13+
// The above copyright notice and this permission notice shall be
14+
// included in all copies or substantial portions of the Software.
15+
//
16+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17+
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18+
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19+
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20+
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21+
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22+
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23+
// OTHER DEALINGS IN THE SOFTWARE.
24+
#endregion
25+
26+
using System;
27+
using Cmdty.TimePeriodValueTypes;
28+
using Cmdty.TimeSeries;
29+
30+
namespace Cmdty.Storage.Samples.Trinomial
31+
{
32+
class Program
33+
{
34+
static void Main(string[] args)
35+
{
36+
const double constantMaxInjectRate = 5.26;
37+
const double constantMaxWithdrawRate = 14.74;
38+
const double constantMaxInventory = 1100.74;
39+
const double constantMinInventory = 0.0;
40+
const double constantInjectionCost = 0.48;
41+
const double constantWithdrawalCost = 0.74;
42+
43+
CmdtyStorage<Day> storage = CmdtyStorage<Day>.Builder
44+
.WithActiveTimePeriod(new Day(2019, 9, 1), new Day(2019, 10, 1))
45+
.WithConstantInjectWithdrawRange(-constantMaxWithdrawRate, constantMaxInjectRate)
46+
.WithConstantMinInventory(constantMinInventory)
47+
.WithConstantMaxInventory(constantMaxInventory)
48+
.WithPerUnitInjectionCost(constantInjectionCost, injectionDate => injectionDate)
49+
.WithNoCmdtyConsumedOnInject()
50+
.WithPerUnitWithdrawalCost(constantWithdrawalCost, withdrawalDate => withdrawalDate)
51+
.WithNoCmdtyConsumedOnWithdraw()
52+
.WithNoCmdtyInventoryLoss()
53+
.WithNoCmdtyInventoryCost()
54+
.MustBeEmptyAtEnd()
55+
.Build();
56+
57+
var currentPeriod = new Day(2019, 9, 15);
58+
59+
const double lowerForwardPrice = 56.6;
60+
const double forwardSpread = 87.81;
61+
62+
double higherForwardPrice = lowerForwardPrice + forwardSpread;
63+
64+
var forwardCurveBuilder = new TimeSeries<Day, double>.Builder();
65+
66+
foreach (var day in new Day(2019, 9, 15).EnumerateTo(new Day(2019, 9, 22)))
67+
{
68+
forwardCurveBuilder.Add(day, lowerForwardPrice);
69+
}
70+
71+
foreach (var day in new Day(2019, 9, 23).EnumerateTo(new Day(2019, 10, 1)))
72+
{
73+
forwardCurveBuilder.Add(day, higherForwardPrice);
74+
}
75+
76+
TimeSeries<Month, Day> cmdtySettlementDates = new TimeSeries<Month, Day>.Builder
77+
{
78+
{new Month(2019, 9), new Day(2019, 10, 20) }
79+
}.Build();
80+
81+
const double interestRate = 0.025;
82+
83+
// Trinomial tree model parameters
84+
const double spotPriceMeanReversion = 5.5;
85+
const double onePeriodTimeStep = 1.0 / 365.0;
86+
87+
TimeSeries<Day, double> spotVolatility = new TimeSeries<Day, double>.Builder
88+
{
89+
{new Day(2019, 9, 15), 0.975},
90+
{new Day(2019, 9, 16), 0.97},
91+
{new Day(2019, 9, 17), 0.96},
92+
{new Day(2019, 9, 18), 0.91},
93+
{new Day(2019, 9, 19), 0.89},
94+
{new Day(2019, 9, 20), 0.895},
95+
{new Day(2019, 9, 21), 0.891},
96+
{new Day(2019, 9, 22), 0.89},
97+
{new Day(2019, 9, 23), 0.875},
98+
{new Day(2019, 9, 24), 0.872},
99+
{new Day(2019, 9, 25), 0.871},
100+
{new Day(2019, 9, 26), 0.870},
101+
{new Day(2019, 9, 27), 0.869},
102+
{new Day(2019, 9, 28), 0.868},
103+
{new Day(2019, 9, 29), 0.867},
104+
{new Day(2019, 9, 30), 0.866},
105+
{new Day(2019, 10, 1), 0.8655}
106+
}.Build();
107+
108+
const double startingInventory = 50.0;
109+
110+
TreeStorageValuationResults<Day> valuationResults = TreeStorageValuation<Day>
111+
.ForStorage(storage)
112+
.WithStartingInventory(startingInventory)
113+
.ForCurrentPeriod(currentPeriod)
114+
.WithForwardCurve(forwardCurveBuilder.Build())
115+
.WithOneFactorTrinomialTree(spotVolatility, spotPriceMeanReversion, onePeriodTimeStep)
116+
.WithMonthlySettlement(cmdtySettlementDates)
117+
.WithAct365ContinuouslyCompoundedInterestRate(settleDate => interestRate)
118+
.WithFixedGridSpacing(10.0)
119+
.WithLinearInventorySpaceInterpolation()
120+
.WithNumericalTolerance(1E-12)
121+
.Calculate();
122+
123+
Console.WriteLine("Calculated storage NPV: " + valuationResults.NetPresentValue.ToString("F2"));
124+
Console.WriteLine();
125+
126+
Console.WriteLine("Press any key to exit");
127+
Console.ReadKey();
128+
}
129+
}
130+
}

samples/csharp/Cmdty.Storage.Samples.sln

+6
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
1111
NuGet.config = NuGet.config
1212
EndProjectSection
1313
EndProject
14+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Cmdty.Storage.Samples.Trinomial", "Cmdty.Storage.Samples.Trinomial\Cmdty.Storage.Samples.Trinomial.csproj", "{7B3DA54B-7FDF-47FB-A18C-6C7A6F745E10}"
15+
EndProject
1416
Global
1517
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1618
Debug|Any CPU = Debug|Any CPU
@@ -21,6 +23,10 @@ Global
2123
{DCE9B12B-ED10-497A-8145-A25143CF96A7}.Debug|Any CPU.Build.0 = Debug|Any CPU
2224
{DCE9B12B-ED10-497A-8145-A25143CF96A7}.Release|Any CPU.ActiveCfg = Release|Any CPU
2325
{DCE9B12B-ED10-497A-8145-A25143CF96A7}.Release|Any CPU.Build.0 = Release|Any CPU
26+
{7B3DA54B-7FDF-47FB-A18C-6C7A6F745E10}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
27+
{7B3DA54B-7FDF-47FB-A18C-6C7A6F745E10}.Debug|Any CPU.Build.0 = Debug|Any CPU
28+
{7B3DA54B-7FDF-47FB-A18C-6C7A6F745E10}.Release|Any CPU.ActiveCfg = Release|Any CPU
29+
{7B3DA54B-7FDF-47FB-A18C-6C7A6F745E10}.Release|Any CPU.Build.0 = Release|Any CPU
2430
EndGlobalSection
2531
GlobalSection(SolutionProperties) = preSolution
2632
HideSolutionNode = FALSE
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
2+
<s:Boolean x:Key="/Default/UserDictionary/Words/=Trinomial/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>

0 commit comments

Comments
 (0)