Esempio n. 1
0
 public static object StorageAddInVersion(object trigger)
 {
     return(StorageExcelHelper.ExecuteExcelFunction(() =>
     {
         Assembly currentAssembly = Assembly.GetExecutingAssembly();
         AssemblyInformationalVersionAttribute infoVersionAttribute =
             currentAssembly.GetCustomAttribute <AssemblyInformationalVersionAttribute>();
         return infoVersionAttribute.InformationalVersion;
     }));
 }
Esempio n. 2
0
                Category = AddIn.ExcelFunctionCategory, IsThreadSafe = false, IsVolatile = false, IsExceptionSafe = true)] // TODO turn IsThreadSafe to true and use ConcurrentDictionary?
 public static object SubscribeResultProperty(string objectHandle, string propertyName, object returnedWhilstWaiting)
 {
     return(StorageExcelHelper.ExecuteExcelFunction(() =>
     {
         const string functionName = nameof(SubscribeResultProperty);
         return ExcelAsyncUtil.Observe(functionName, new [] { objectHandle, propertyName, returnedWhilstWaiting }, () =>
         {
             ExcelCalcWrapper wrapper = _calcWrappers[objectHandle];
             var excelObservable = new CalcWrapperResultPropertyObservable(wrapper, propertyName, returnedWhilstWaiting);
             return excelObservable;
         });
     }));
 }
Esempio n. 3
0
                Category = AddIn.ExcelFunctionCategory, IsThreadSafe = false, IsVolatile = false, IsExceptionSafe = true)] // TODO turn IsThreadSafe to true and use ConcurrentDictionary?
 public static object SubscribeStatus(string name)
 {
     return(StorageExcelHelper.ExecuteExcelFunction(() =>
     {
         const string functionName = nameof(SubscribeStatus);
         return ExcelAsyncUtil.Observe(functionName, name, () =>
         {
             ExcelCalcWrapper wrapper = _calcWrappers[name];
             var excelObserver = new CalcWrapperStatusObservable(wrapper);
             return excelObserver;
         });
     }));
 }
Esempio n. 4
0
 public static object StorageValueIntrinsic(
     [ExcelArgument(Name = ExcelArg.ValDate.Name, Description = ExcelArg.ValDate.Description)] DateTime valuationDate,
     [ExcelArgument(Name = ExcelArg.StorageStart.Name, Description = ExcelArg.StorageStart.Description)] DateTime storageStart,
     [ExcelArgument(Name = ExcelArg.StorageEnd.Name, Description = ExcelArg.StorageEnd.Description)] DateTime storageEnd,
     [ExcelArgument(Name = ExcelArg.Ratchets.Name, Description = ExcelArg.Ratchets.Description)] object storageConstraints,
     [ExcelArgument(Name = ExcelArg.RatchetInterpolation.Name, Description = ExcelArg.RatchetInterpolation.Description)] string injectWithdrawInterpolation,
     [ExcelArgument(Name = ExcelArg.InjectionCost.Name, Description = ExcelArg.InjectionCost.Description)] double injectionCostRate,
     [ExcelArgument(Name = ExcelArg.CmdtyConsumedInject.Name, Description = ExcelArg.CmdtyConsumedInject.Description)] double cmdtyConsumedOnInjection,
     [ExcelArgument(Name = ExcelArg.WithdrawalCost.Name, Description = ExcelArg.WithdrawalCost.Description)] double withdrawalCostRate,
     [ExcelArgument(Name = ExcelArg.CmdtyConsumedWithdraw.Name, Description = ExcelArg.CmdtyConsumedWithdraw.Description)] double cmdtyConsumedOnWithdrawal,
     [ExcelArgument(Name = ExcelArg.Inventory.Name, Description = ExcelArg.Inventory.Description)] double currentInventory,
     [ExcelArgument(Name = ExcelArg.ForwardCurve.Name, Description = ExcelArg.ForwardCurve.Description)] object forwardCurve,
     [ExcelArgument(Name = ExcelArg.InterestRateCurve.Name, Description = ExcelArg.InterestRateCurve.Description)] object interestRateCurve,
     [ExcelArgument(Name = ExcelArg.NumGridPoints.Name, Description = ExcelArg.NumGridPoints.Description)] object numGlobalGridPoints,
     [ExcelArgument(Name = ExcelArg.NumericalTolerance.Name, Description = ExcelArg.NumericalTolerance.Description)] object numericalTolerance)     // TODO add granularity
 {
     return(StorageExcelHelper.ExecuteExcelFunction(() =>
                                                    TrinomialStorageValuationIntrinsic <Day>(valuationDate, storageStart, storageEnd, storageConstraints, injectWithdrawInterpolation,
                                                                                             injectionCostRate, cmdtyConsumedOnInjection, withdrawalCostRate,
                                                                                             cmdtyConsumedOnWithdrawal, currentInventory, forwardCurve, interestRateCurve, numGlobalGridPoints,
                                                                                             numericalTolerance).NetPresentValue));
 }
Esempio n. 5
0
                Category = AddIn.ExcelFunctionCategory, IsThreadSafe = false, IsVolatile = false, IsExceptionSafe = true)] // TODO turn IsThreadSafe to true and use ConcurrentDictionary?
 public static object CreateStorage(
     [ExcelArgument(Name = "Storage_name", Description = "Name of storage object to create.")] string name,
     [ExcelArgument(Name = ExcelArg.StorageStart.Name, Description = ExcelArg.StorageStart.Description)] DateTime storageStart,
     [ExcelArgument(Name = ExcelArg.StorageEnd.Name, Description = ExcelArg.StorageEnd.Description)] DateTime storageEnd,
     [ExcelArgument(Name = ExcelArg.Ratchets.Name, Description = ExcelArg.Ratchets.Description)] object ratchets,
     [ExcelArgument(Name = ExcelArg.RatchetInterpolation.Name, Description = ExcelArg.RatchetInterpolation.Description)] string ratchetInterpolation,
     [ExcelArgument(Name = ExcelArg.InjectionCost.Name, Description = ExcelArg.InjectionCost.Description)] double injectionCostRate,
     [ExcelArgument(Name = ExcelArg.CmdtyConsumedInject.Name, Description = ExcelArg.CmdtyConsumedInject.Description)] double cmdtyConsumedOnInjection,
     [ExcelArgument(Name = ExcelArg.WithdrawalCost.Name, Description = ExcelArg.WithdrawalCost.Description)] double withdrawalCostRate,
     [ExcelArgument(Name = ExcelArg.CmdtyConsumedWithdraw.Name, Description = ExcelArg.CmdtyConsumedWithdraw.Description)] double cmdtyConsumedOnWithdrawal,
     [ExcelArgument(Name = ExcelArg.NumericalTolerance.Name, Description = ExcelArg.NumericalTolerance.Description)] object numericalToleranceIn)
 {
     return(StorageExcelHelper.ExecuteExcelFunction(() =>
     {
         double numericalTolerance = StorageExcelHelper.DefaultIfExcelEmptyOrMissing(numericalToleranceIn, 1E-10, "Numerical_tolerance");
         CmdtyStorage <Day> storage = StorageExcelHelper.CreateCmdtyStorageFromExcelInputs <Day>(storageStart,
                                                                                                 storageEnd, ratchets, ratchetInterpolation, injectionCostRate, cmdtyConsumedOnInjection,
                                                                                                 withdrawalCostRate, cmdtyConsumedOnWithdrawal, numericalTolerance);
         _storageObjects[name] = storage;
         return name;
     }));
 }
Esempio n. 6
0
 public static object StorageIntrinsicValue(
     DateTime valuationDate,
     DateTime storageStart,
     DateTime storageEnd,
     object injectWithdrawConstraints,
     [ExcelArgument(Name = ExcelArg.RatchetInterpolation.Name, Description = ExcelArg.RatchetInterpolation.Description)] string injectWithdrawInterpolation,
     double injectionCostRate,
     double cmdtyConsumedOnInjection,
     double withdrawalCostRate,
     double cmdtyConsumedOnWithdrawal,
     double currentInventory,
     object forwardCurve,
     object interestRateCurve,
     object numGlobalGridPoints,             // TODO excel argument says default is 100
     object numericalTolerance,
     [ExcelArgument(Name = "Granularity")] object granularity)
 {
     return(StorageExcelHelper.ExecuteExcelFunction(() =>
                                                    IntrinsicStorageVal <Day>(valuationDate, storageStart, storageEnd, injectWithdrawConstraints, injectWithdrawInterpolation,
                                                                              injectionCostRate, cmdtyConsumedOnInjection, withdrawalCostRate,
                                                                              cmdtyConsumedOnWithdrawal,
                                                                              currentInventory, forwardCurve, interestRateCurve, numGlobalGridPoints, numericalTolerance).NetPresentValue));
 }
Esempio n. 7
0
        public static object StorageIntrinsicDecisionProfile(
            DateTime valuationDate,
            DateTime storageStart,
            DateTime storageEnd,
            object injectWithdrawConstraints,
            [ExcelArgument(Name = ExcelArg.RatchetInterpolation.Name, Description = ExcelArg.RatchetInterpolation.Description)] string injectWithdrawInterpolation,
            double injectionCostRate,
            double cmdtyConsumedOnInjection,
            double withdrawalCostRate,
            double cmdtyConsumedOnWithdrawal,
            double currentInventory,
            object forwardCurve,
            object interestRateCurve,
            object numGlobalGridPoints, // TODO excel argument says default is 100
            object numericalTolerance,
            [ExcelArgument(Name = "Granularity")] object granularity)
        {
            return(StorageExcelHelper.ExecuteExcelFunction(() =>
            {
                IntrinsicStorageValuationResults <Day> valuationResults = IntrinsicStorageVal <Day>(valuationDate, storageStart, storageEnd,
                                                                                                    injectWithdrawConstraints, injectWithdrawInterpolation,
                                                                                                    injectionCostRate, cmdtyConsumedOnInjection, withdrawalCostRate,
                                                                                                    cmdtyConsumedOnWithdrawal,
                                                                                                    currentInventory, forwardCurve, interestRateCurve, numGlobalGridPoints, numericalTolerance);

                var resultArray = new object[valuationResults.StorageProfile.Count, 3];

                for (int i = 0; i < resultArray.GetLength(0); i++)
                {
                    resultArray[i, 0] = valuationResults.StorageProfile.Indices[i].Start;
                    resultArray[i, 1] = valuationResults.StorageProfile[i].InjectWithdrawVolume;
                    resultArray[i, 2] = valuationResults.StorageProfile[i].CmdtyConsumed;
                }

                return resultArray;
            }));
        }
Esempio n. 8
0
                       Category = AddIn.ExcelFunctionCategory, IsThreadSafe = false, IsVolatile = false, IsExceptionSafe = true)] // TODO turn IsThreadSafe to true and use ConcurrentDictionary?
        public static object StorageValueThreeFactor(
            [ExcelArgument(Name = "Name", Description = "Name of cached object to create.")] string name,
            [ExcelArgument(Name = ExcelArg.StorageHandle.Name, Description = ExcelArg.StorageHandle.Description)] string storageHandle,
            [ExcelArgument(Name = ExcelArg.ValDate.Name, Description = ExcelArg.ValDate.Description)] DateTime valuationDate,
            [ExcelArgument(Name = ExcelArg.Inventory.Name, Description = ExcelArg.Inventory.Description)] double currentInventory,
            [ExcelArgument(Name = ExcelArg.ForwardCurve.Name, Description = ExcelArg.ForwardCurve.Description)] object forwardCurve,
            [ExcelArgument(Name = ExcelArg.InterestRateCurve.Name, Description = ExcelArg.InterestRateCurve.Description)] object interestRateCurve,
            [ExcelArgument(Name = ExcelArg.SpotVol.Name, Description = ExcelArg.SpotVol.Description)] double spotVol,
            [ExcelArgument(Name = ExcelArg.SpotMeanReversion.Name, Description = ExcelArg.SpotMeanReversion.Description)] double spotMeanReversion,
            [ExcelArgument(Name = ExcelArg.LongTermVol.Name, Description = ExcelArg.LongTermVol.Description)] double longTermVol,
            [ExcelArgument(Name = ExcelArg.SeasonalVol.Name, Description = ExcelArg.SeasonalVol.Description)] double seasonalVol,
            [ExcelArgument(Name = ExcelArg.DiscountDeltas.Name, Description = ExcelArg.DiscountDeltas.Description)] bool discountDeltas,
            [ExcelArgument(Name = ExcelArg.SettleDates.Name, Description = ExcelArg.SettleDates.Description)] object settleDatesIn,
            [ExcelArgument(Name = ExcelArg.NumSims.Name, Description = ExcelArg.NumSims.Description)] int numSims,
            [ExcelArgument(Name = ExcelArg.BasisFunctions.Name, Description = ExcelArg.BasisFunctions.Description)] string basisFunctionsIn,
            [ExcelArgument(Name = ExcelArg.Seed.Name, Description = ExcelArg.Seed.Description)] object seedIn,
            [ExcelArgument(Name = ExcelArg.ForwardSimSeed.Name, Description = ExcelArg.ForwardSimSeed.Description)] object fwdSimSeedIn,
            [ExcelArgument(Name = ExcelArg.NumGridPoints.Name, Description = ExcelArg.NumGridPoints.Description)] object numGlobalGridPointsIn,
            [ExcelArgument(Name = ExcelArg.NumericalTolerance.Name, Description = ExcelArg.NumericalTolerance.Description)] object numericalTolerance,
            [ExcelArgument(Name = ExcelArg.ExtraDecisions.Name, Description = ExcelArg.ExtraDecisions.Description)] object extraDecisions)
        {
            return(StorageExcelHelper.ExecuteExcelFunction(() =>
            {
                _calcWrappers[name] = ExcelCalcWrapper.CreateCancellable((cancellationToken, onProgress) =>
                {
                    // TODO provide alternative method for interpolating interest rates
                    Func <Day, double> interpolatedInterestRates =
                        StorageExcelHelper.CreateLinearInterpolatedInterestRateFunc(interestRateCurve, ExcelArg.InterestRateCurve.Name);

                    Func <Day, Day, double> discountFunc = StorageHelper.CreateAct65ContCompDiscounter(interpolatedInterestRates);
                    Day valDate = Day.FromDateTime(valuationDate);
                    Func <Day, Day> settleDateRule = StorageExcelHelper.CreateSettlementRule(settleDatesIn, ExcelArg.SettleDates.Name);

                    CmdtyStorage <Day> storage = _storageObjects[storageHandle];
                    int numGlobalGridPoints = StorageExcelHelper.DefaultIfExcelEmptyOrMissing(numGlobalGridPointsIn, ExcelArg.NumGridPoints.Default,
                                                                                              ExcelArg.NumGridPoints.Name);

                    string basisFunctionsText = basisFunctionsIn.Replace("x_st", "x0").Replace("x_lt", "x1").Replace("x_sw", "x2");

                    var lsmcParamsBuilder = new LsmcValuationParameters <Day> .Builder
                    {
                        Storage = storage,
                        CurrentPeriod = valDate,
                        Inventory = currentInventory,
                        ForwardCurve = StorageExcelHelper.CreateDoubleTimeSeries <Day>(forwardCurve, ExcelArg.ForwardCurve.Name),
                        DiscountFactors = discountFunc,

                        DiscountDeltas = discountDeltas,
                        BasisFunctions = BasisFunctionsBuilder.Parse(basisFunctionsText),

                        ExtraDecisions = StorageExcelHelper.DefaultIfExcelEmptyOrMissing(extraDecisions, 0, ExcelArg.ExtraDecisions.Name),
                        CancellationToken = cancellationToken,
                        OnProgressUpdate = onProgress,
                        GridCalc = FixedSpacingStateSpaceGridCalc.CreateForFixedNumberOfPointsOnGlobalInventoryRange(storage, numGlobalGridPoints),
                        NumericalTolerance = StorageExcelHelper.DefaultIfExcelEmptyOrMissing(numericalTolerance, LsmcValuationParameters <Day> .Builder.DefaultNumericalTolerance, ExcelArg.NumericalTolerance.Description),
                        SettleDateRule = settleDateRule
                    };

                    // TODO test that this works with expired storage
                    Day endDate = new[] { valDate, storage.EndPeriod }.Max();
                    var threeFactorParams =
                        MultiFactorParameters.For3FactorSeasonal(spotMeanReversion, spotVol, longTermVol, seasonalVol, valDate, endDate);

                    // TODO better error messages if seedIn and fwdSimSeedIn cannot be cast
                    int?seed = StorageExcelHelper.IsExcelEmptyOrMissing(seedIn) ? (int?)null : (int)(double)seedIn;
                    int?fwdSimSeed = StorageExcelHelper.IsExcelEmptyOrMissing(fwdSimSeedIn) ? (int?)null : (int)(double)fwdSimSeedIn;

                    lsmcParamsBuilder.SimulateWithMultiFactorModelAndMersenneTwister(threeFactorParams, numSims, seed, fwdSimSeed);

                    return LsmcStorageValuation.WithNoLogger.Calculate(lsmcParamsBuilder.Build());
                });
                return name;
            }));
        }