Skip to content

Commit 6d1b727

Browse files
committed
- Updated sample c# project to version 0.1.0-beta2.
- Added C# code samples to main README.md.
1 parent ec4f3d5 commit 6d1b727

File tree

4 files changed

+120
-14
lines changed

4 files changed

+120
-14
lines changed

README.md

+105-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ Valuation and optimisation of commodity storage. Still in early stages of develo
1111
* [Getting Started](#getting-started)
1212
* [Installing C# API](#installing-c-api)
1313
* [Installing Excel Add-In](#installing-excel-add-in)
14+
* [Using the C# API](#using-the-c-api)
15+
* [Creating the Storage Object](#creating-the-storage-object)
16+
* [Calculating the Intrinsic Value](#calculating-the-intrinsic-value)
1417
* [Building](#building)
1518
* [One Factor Trinomial Tree Model](#one-factor-trinomial-tree-method-critique-and-rationale)
1619
* [License](#license)
@@ -39,7 +42,7 @@ Both approaches solve the optimsation problem using backward induction across a
3942
### Installing C# API
4043
For use from C# install the NuGet package Cmdty.Storage.
4144
```
42-
PM> Install-Package Cmdty.Storage -Version 0.1.0-beta1
45+
PM> Install-Package Cmdty.Storage -Version 0.1.0-beta2
4346
```
4447

4548
### Installing Excel Add-In
@@ -57,6 +60,107 @@ Within the latest build select, Artifacts > drop, then download either Cmdty.Sto
5760

5861
Examples of the Excel functions can be found in [samples/excel/storage_samples.xlsx](https://github.com/cmdty/storage/raw/master/samples/excel/storage_samples.xlsx).
5962

63+
## Using the C# API
64+
65+
### Creating the Storage Object
66+
In order for storage capacity to be valued, first an instance of the class CmdtyStorage
67+
needs to be created. The code sample below shows how the fluent builder API can be used
68+
to achieve this.
69+
70+
``` c#
71+
const double constantMaxInjectRate = 5.26;
72+
const double constantMaxWithdrawRate = 14.74;
73+
const double constantMaxInventory = 1100.74;
74+
const double constantMinInventory = 0.0;
75+
const double constantInjectionCost = 0.48;
76+
const double constantWithdrawalCost = 0.74;
77+
78+
CmdtyStorage<Day> storage = CmdtyStorage<Day>.Builder
79+
.WithActiveTimePeriod(new Day(2019, 9, 1), new Day(2019, 10, 1))
80+
.WithConstantInjectWithdrawRange(-constantMaxWithdrawRate, constantMaxInjectRate)
81+
.WithConstantMinInventory(constantMinInventory)
82+
.WithConstantMaxInventory(constantMaxInventory)
83+
.WithPerUnitInjectionCost(constantInjectionCost, injectionDate => injectionDate)
84+
.WithNoCmdtyConsumedOnInject()
85+
.WithPerUnitWithdrawalCost(constantWithdrawalCost, withdrawalDate => withdrawalDate)
86+
.WithNoCmdtyConsumedOnWithdraw()
87+
.WithNoCmdtyInventoryLoss()
88+
.WithNoCmdtyInventoryCost()
89+
.MustBeEmptyAtEnd()
90+
.Build();
91+
```
92+
93+
The above example is quite simple, with most parameters being constant, but much more complicated storage objects can be created. Once the Cmdty.Storage package has been installed,
94+
a good way to discover the flexibility in the API is to look at the IntelliSense suggestions in
95+
Visual Studio.
96+
97+
### Calculating the Intrinsic Value
98+
The following example shows how to calculate the intrinsic value of the storage, including
99+
the optimal intrinsic inject/withdraw decision profile.
100+
101+
``` c#
102+
var currentPeriod = new Day(2019, 9, 15);
103+
const double lowerForwardPrice = 56.6;
104+
const double forwardSpread = 87.81;
105+
double higherForwardPrice = lowerForwardPrice + forwardSpread;
106+
107+
var forwardCurveBuilder = new TimeSeries<Day, double>.Builder();
108+
109+
foreach (var day in new Day(2019, 9, 15).EnumerateTo(new Day(2019, 9, 22)))
110+
{
111+
forwardCurveBuilder.Add(day, lowerForwardPrice);
112+
}
113+
114+
foreach (var day in new Day(2019, 9, 23).EnumerateTo(new Day(2019, 10, 1)))
115+
{
116+
forwardCurveBuilder.Add(day, higherForwardPrice);
117+
}
118+
119+
const double startingInventory = 50.0;
120+
121+
IntrinsicStorageValuationResults<Day> valuationResults = IntrinsicStorageValuation<Day>
122+
.ForStorage(storage)
123+
.WithStartingInventory(startingInventory)
124+
.ForCurrentPeriod(currentPeriod)
125+
.WithForwardCurve(forwardCurveBuilder.Build())
126+
.WithCmdtySettlementRule(day => day.First<Month>().Offset(1).First<Day>().Offset(5))
127+
.WithDiscountFactorFunc(day => 1.0)
128+
.WithFixedGridSpacing(10.0)
129+
.WithLinearInventorySpaceInterpolation()
130+
.WithNumericalTolerance(1E-12)
131+
.Calculate();
132+
133+
Console.WriteLine("Calculated intrinsic storage NPV: " + valuationResults.NetPresentValue.ToString("F2"));
134+
Console.WriteLine();
135+
Console.WriteLine("Decision profile:");
136+
Console.WriteLine(valuationResults.DecisionProfile.FormatData("F2", -1));
137+
```
138+
139+
When run, the above code prints the following to the console.
140+
141+
```
142+
Calculated intrinsic storage NPV: 10827.21
143+
144+
Decision profile:
145+
Count = 16
146+
2019-09-15 5.26
147+
2019-09-16 5.26
148+
2019-09-17 5.26
149+
2019-09-18 5.26
150+
2019-09-19 5.26
151+
2019-09-20 5.26
152+
2019-09-21 5.26
153+
2019-09-22 5.26
154+
2019-09-23 -14.74
155+
2019-09-24 -14.74
156+
2019-09-25 0.00
157+
2019-09-26 -14.74
158+
2019-09-27 -14.74
159+
2019-09-28 -14.74
160+
2019-09-29 -14.74
161+
2019-09-30 -3.64
162+
```
163+
60164
## Building
61165
Build scripts use [cake](https://github.com/cake-build/cake) and require [the .NET Core SDK](https://dotnet.microsoft.com/download) to be installed on the Windows machine performing the build.
62166

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
</PropertyGroup>
77

88
<ItemGroup>
9-
<PackageReference Include="Cmdty.Storage" Version="0.1.0-alpha2020010602" />
9+
<PackageReference Include="Cmdty.Storage" Version="0.1.0-beta2" />
1010
</ItemGroup>
1111

1212
</Project>

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

+13-11
Original file line numberDiff line numberDiff line change
@@ -31,23 +31,25 @@ namespace Cmdty.Storage.Samples.Intrinsic
3131
{
3232
class Program
3333
{
34-
private const double ConstantMaxInjectRate = 5.26;
35-
private const double ConstantMaxWithdrawRate = 14.74;
36-
private const double ConstantMaxInventory = 1100.74;
37-
private const double ConstantMinInventory = 0.0;
38-
private const double ConstantInjectionCost = 0.48;
39-
private const double ConstantWithdrawalCost = 0.74;
34+
4035

4136
static void Main(string[] args)
4237
{
38+
const double constantMaxInjectRate = 5.26;
39+
const double constantMaxWithdrawRate = 14.74;
40+
const double constantMaxInventory = 1100.74;
41+
const double constantMinInventory = 0.0;
42+
const double constantInjectionCost = 0.48;
43+
const double constantWithdrawalCost = 0.74;
44+
4345
CmdtyStorage<Day> storage = CmdtyStorage<Day>.Builder
4446
.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)
47+
.WithConstantInjectWithdrawRange(-constantMaxWithdrawRate, constantMaxInjectRate)
48+
.WithConstantMinInventory(constantMinInventory)
49+
.WithConstantMaxInventory(constantMaxInventory)
50+
.WithPerUnitInjectionCost(constantInjectionCost, injectionDate => injectionDate)
4951
.WithNoCmdtyConsumedOnInject()
50-
.WithPerUnitWithdrawalCost(ConstantWithdrawalCost, withdrawalDate => withdrawalDate)
52+
.WithPerUnitWithdrawalCost(constantWithdrawalCost, withdrawalDate => withdrawalDate)
5153
.WithNoCmdtyConsumedOnWithdraw()
5254
.WithNoCmdtyInventoryLoss()
5355
.WithNoCmdtyInventoryCost()

samples/csharp/NuGet.config

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@
44
<!--To inherit the global NuGet package sources remove the <clear/> line below -->
55
<clear />
66
<add key="NuGet.org (v3)" value="https://api.nuget.org/v3/index.json" />
7-
<add key="Cmdty" value="https://pkgs.dev.azure.com/cmdty/_packaging/cmdty/nuget/v3/index.json" />
7+
<!--<add key="Cmdty" value="https://pkgs.dev.azure.com/cmdty/_packaging/cmdty/nuget/v3/index.json" />-->
88
</packageSources>
99
</configuration>

0 commit comments

Comments
 (0)