|
| 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 | +} |
0 commit comments