public void BaroneAdesiWhaleyPortfolioTest() { const decimal price = 30.00m; const decimal underlyingPrice = 200m; const decimal underlyingVol = 0.25m; const decimal riskFreeRate = 0.01m; var tz = TimeZones.NewYork; var evaluationDate = new DateTime(2015, 2, 19); var equity = new Equity(SecurityExchangeHours.AlwaysOpen(tz), new SubscriptionDataConfig(typeof(TradeBar), Symbols.SPY, Resolution.Minute, tz, tz, true, false, false), new Cash(CashBook.AccountCurrency, 0, 1m), SymbolProperties.GetDefault(CashBook.AccountCurrency)); equity.SetMarketPrice(new Tick { Value = underlyingPrice }); equity.VolatilityModel = new DummyVolatilityModel(underlyingVol); var contract = new OptionContract(Symbols.SPY_C_192_Feb19_2016, Symbols.SPY) { Time = evaluationDate }; var optionCall = new Option(SecurityExchangeHours.AlwaysOpen(tz), new SubscriptionDataConfig(typeof(TradeBar), Symbols.SPY_C_192_Feb19_2016, Resolution.Minute, tz, tz, true, false, false), new Cash(CashBook.AccountCurrency, 0, 1m), new OptionSymbolProperties(SymbolProperties.GetDefault(CashBook.AccountCurrency))); optionCall.SetMarketPrice(new Tick { Value = price }); optionCall.Underlying = equity; var priceModel = OptionPriceModels.BaroneAdesiWhaley(); var results = priceModel.Evaluate(optionCall, null, contract); var callPrice = results.TheoreticalPrice; var impliedVolatility = results.ImpliedVolatility; var greeks = results.Greeks; Assert.Greater(price, callPrice); Assert.Greater(impliedVolatility, underlyingVol); // BS equation (inequality) var rightPart = greeks.Theta + riskFreeRate * underlyingPrice * greeks.Delta + 0.5m * underlyingVol * underlyingVol * underlyingPrice * underlyingPrice * greeks.Gamma; var leftPart = riskFreeRate * callPrice; Assert.GreaterOrEqual(Math.Round(leftPart, 4), Math.Round(rightPart, 4)); }
public void VolumeShareSlippageModel_HighVolumeTest() { // These are the default values for the VolumeShareSlippageModel var priceImpact = 0.1m; var volumeLimit = 0.025m; var model = new VolumeShareSlippageModel(); // High volume: volume > volumeLimit x order.Quantity var volume = 100; var volumeShare = _equityBuyOrder.Quantity / (decimal)volume; Assert.Greater(volume, volumeLimit * _equityBuyOrder.Quantity); _equity.SetMarketPrice(new TradeBar(DateTime.Now, Symbols.SPY, 100m, 100m, 100m, 100m, volume)); var expected = _equity.Price * priceImpact * volumeShare * volumeShare; var actual = model.GetSlippageApproximation(_equity, _equityBuyOrder); Assert.AreEqual(expected, actual); }
public void BlackScholesPortfolioTest() { const decimal price = 20.00m; const decimal underlyingPrice = 200m; const decimal underlyingVol = 0.15m; const decimal riskFreeRate = 0.01m; var tz = TimeZones.NewYork; var evaluationDate = new DateTime(2015, 2, 19); var SPY_C_192_Feb19_2016E = Symbol.CreateOption("SPY", Market.USA, OptionStyle.European, OptionRight.Call, 192m, new DateTime(2016, 02, 19)); // setting up underlying var equity = new Equity(SecurityExchangeHours.AlwaysOpen(tz), new SubscriptionDataConfig(typeof(TradeBar), Symbols.SPY, Resolution.Minute, tz, tz, true, false, false), new Cash(CashBook.AccountCurrency, 0, 1m), SymbolProperties.GetDefault(CashBook.AccountCurrency)); equity.SetMarketPrice(new Tick { Value = underlyingPrice }); equity.VolatilityModel = new DummyVolatilityModel(underlyingVol); // setting up European style option var contract = new OptionContract(SPY_C_192_Feb19_2016E, Symbols.SPY) { Time = evaluationDate }; var optionCall = new Option(SecurityExchangeHours.AlwaysOpen(tz), new SubscriptionDataConfig(typeof(TradeBar), SPY_C_192_Feb19_2016E, Resolution.Minute, tz, tz, true, false, false), new Cash(CashBook.AccountCurrency, 0, 1m), new OptionSymbolProperties(SymbolProperties.GetDefault(CashBook.AccountCurrency))); optionCall.SetMarketPrice(new Tick { Value = price }); optionCall.Underlying = equity; // running evaluation var priceModel = OptionPriceModels.BlackScholes(); var results = priceModel.Evaluate(optionCall, null, contract); var callPrice = results.TheoreticalPrice; var greeks = results.Greeks; // BS equation var rightPart = greeks.Theta + riskFreeRate * underlyingPrice * greeks.Delta + 0.5m * underlyingVol * underlyingVol * underlyingPrice * underlyingPrice * greeks.Gamma; var leftPart = riskFreeRate * callPrice; Assert.AreEqual((double)leftPart, (double)rightPart, 0.0001); }
public void AlphaStreamsSlippageModel_HardCodedEquityTest() { Symbol symbol = Symbol.Create("DGAZ", SecurityType.Equity, Market.USA); Equity equity = new Equity( symbol, SecurityExchangeHours.AlwaysOpen(TimeZones.NewYork), new Cash(Currencies.USD, 0, 1m), SymbolProperties.GetDefault(Currencies.USD), ErrorCurrencyConverter.Instance, RegisteredSecurityDataTypesProvider.Null ); equity.SetMarketPrice(new TradeBar(DateTime.Now, Symbols.SPY, 100m, 100m, 100m, 100m, 1)); Order equityBuyOrder = new MarketOrder(symbol, 1, DateTime.Now); var model = new AlphaStreamsSlippageModel(); var actual = model.GetSlippageApproximation(equity, equityBuyOrder); Assert.AreEqual(0.135m, actual); }
public void EvaluationDateWorksInPortfolioTest() { const decimal price = 30.00m; const decimal underlyingPrice = 200m; const decimal underlyingVol = 0.25m; const decimal riskFreeRate = 0.01m; var tz = TimeZones.NewYork; var evaluationDate1 = new DateTime(2015, 2, 19); var evaluationDate2 = new DateTime(2015, 2, 20); var equity = new Equity(SecurityExchangeHours.AlwaysOpen(tz), new SubscriptionDataConfig(typeof(TradeBar), Symbols.SPY, Resolution.Minute, tz, tz, true, false, false), new Cash(CashBook.AccountCurrency, 0, 1m), SymbolProperties.GetDefault(CashBook.AccountCurrency)); equity.SetMarketPrice(new Tick { Value = underlyingPrice }); equity.VolatilityModel = new DummyVolatilityModel(underlyingVol); var contract = new OptionContract(Symbols.SPY_C_192_Feb19_2016, Symbols.SPY) { Time = evaluationDate1 }; var optionCall = new Option(SecurityExchangeHours.AlwaysOpen(tz), new SubscriptionDataConfig(typeof(TradeBar), Symbols.SPY_C_192_Feb19_2016, Resolution.Minute, tz, tz, true, false, false), new Cash(CashBook.AccountCurrency, 0, 1m), new OptionSymbolProperties(SymbolProperties.GetDefault(CashBook.AccountCurrency))); optionCall.SetMarketPrice(new Tick { Value = price }); optionCall.Underlying = equity; var priceModel = OptionPriceModels.BaroneAdesiWhaley(); var results = priceModel.Evaluate(optionCall, null, contract); var callPrice1 = results.TheoreticalPrice; contract.Time = evaluationDate2; results = priceModel.Evaluate(optionCall, null, contract); var callPrice2 = results.TheoreticalPrice; Assert.Greater(callPrice1, callPrice2); }
public void Initialize() { _equity = new Equity( Symbols.SPY, SecurityExchangeHours.AlwaysOpen(TimeZones.NewYork), new Cash(CashBook.AccountCurrency, 0, 1m), SymbolProperties.GetDefault(CashBook.AccountCurrency)); _equity.SetMarketPrice(new TradeBar(DateTime.Now, Symbols.SPY, 100m, 100m, 100m, 100m, 1)); _equityBuyOrder = new MarketOrder(Symbols.SPY, 1, DateTime.Now); _forex = new Forex( Symbols.EURUSD, SecurityExchangeHours.AlwaysOpen(TimeZones.NewYork), new Cash(CashBook.AccountCurrency, 0, 1m), SymbolProperties.GetDefault(CashBook.AccountCurrency)); _forex.SetMarketPrice(new TradeBar(DateTime.Now, Symbols.EURUSD, 100m, 100m, 100m, 100m, 0)); _forexBuyOrder = new MarketOrder(Symbols.EURUSD, 1000, DateTime.Now); }
private static TestCaseData[] GetValueTestParameters() { const decimal delta = 1m; const decimal price = 1.2345m; const int quantity = 100; const decimal pricePlusDelta = price + delta; const decimal priceMinusDelta = price - delta; var tz = TimeZones.NewYork; var time = new DateTime(2016, 2, 4, 16, 0, 0).ConvertToUtc(tz); var equity = new Equity(SecurityExchangeHours.AlwaysOpen(tz), new SubscriptionDataConfig(typeof(TradeBar), Symbols.SPY, Resolution.Minute, tz, tz, true, false, false), new Cash(CashBook.AccountCurrency, 0, 1m), SymbolProperties.GetDefault(CashBook.AccountCurrency)); equity.SetMarketPrice(new Tick { Value = price }); var gbpCash = new Cash("GBP", 0, 1.46m); var properties = SymbolProperties.GetDefault(gbpCash.Symbol); var forex = new Forex(SecurityExchangeHours.AlwaysOpen(tz), gbpCash, new SubscriptionDataConfig(typeof(TradeBar), Symbols.EURGBP, Resolution.Minute, tz, tz, true, false, false), properties); forex.SetMarketPrice(new Tick { Value = price }); var eurCash = new Cash("EUR", 0, 1.12m); properties = new SymbolProperties("Euro-Bund", eurCash.Symbol, 10, 0.1m); var cfd = new Cfd(SecurityExchangeHours.AlwaysOpen(tz), eurCash, new SubscriptionDataConfig(typeof(TradeBar), Symbols.DE10YBEUR, Resolution.Minute, tz, tz, true, false, false), properties); cfd.SetMarketPrice(new Tick { Value = price }); var multiplierTimesConversionRate = properties.ContractMultiplier * eurCash.ConversionRate; return(new List <ValueTestParameters> { // equity orders new ValueTestParameters("EquityLongMarketOrder", equity, new MarketOrder(Symbols.SPY, quantity, time), quantity * price), new ValueTestParameters("EquityShortMarketOrder", equity, new MarketOrder(Symbols.SPY, -quantity, time), -quantity * price), new ValueTestParameters("EquityLongLimitOrder", equity, new LimitOrder(Symbols.SPY, quantity, priceMinusDelta, time), quantity * priceMinusDelta), new ValueTestParameters("EquityShortLimit Order", equity, new LimitOrder(Symbols.SPY, -quantity, pricePlusDelta, time), -quantity * pricePlusDelta), new ValueTestParameters("EquityLongStopLimitOrder", equity, new StopLimitOrder(Symbols.SPY, quantity, .5m * priceMinusDelta, priceMinusDelta, time), quantity * priceMinusDelta), new ValueTestParameters("EquityShortStopLimitOrder", equity, new StopLimitOrder(Symbols.SPY, -quantity, 1.5m * pricePlusDelta, pricePlusDelta, time), -quantity * pricePlusDelta), new ValueTestParameters("EquityLongStopMarketOrder", equity, new StopMarketOrder(Symbols.SPY, quantity, priceMinusDelta, time), quantity * priceMinusDelta), new ValueTestParameters("EquityLongStopMarketOrder", equity, new StopMarketOrder(Symbols.SPY, quantity, pricePlusDelta, time), quantity * price), new ValueTestParameters("EquityShortStopMarketOrder", equity, new StopMarketOrder(Symbols.SPY, -quantity, pricePlusDelta, time), -quantity * pricePlusDelta), new ValueTestParameters("EquityShortStopMarketOrder", equity, new StopMarketOrder(Symbols.SPY, -quantity, priceMinusDelta, time), -quantity * price), // forex orders new ValueTestParameters("ForexLongMarketOrder", forex, new MarketOrder(Symbols.EURGBP, quantity, time), quantity * price * forex.QuoteCurrency.ConversionRate), new ValueTestParameters("ForexShortMarketOrder", forex, new MarketOrder(Symbols.EURGBP, -quantity, time), -quantity * price * forex.QuoteCurrency.ConversionRate), new ValueTestParameters("ForexLongLimitOrder", forex, new LimitOrder(Symbols.EURGBP, quantity, priceMinusDelta, time), quantity * priceMinusDelta * forex.QuoteCurrency.ConversionRate), new ValueTestParameters("ForexShortLimit Order", forex, new LimitOrder(Symbols.EURGBP, -quantity, pricePlusDelta, time), -quantity * pricePlusDelta * forex.QuoteCurrency.ConversionRate), new ValueTestParameters("ForexLongStopLimitOrder", forex, new StopLimitOrder(Symbols.EURGBP, quantity, .5m * priceMinusDelta, priceMinusDelta, time), quantity * priceMinusDelta * forex.QuoteCurrency.ConversionRate), new ValueTestParameters("ForexShortStopLimitOrder", forex, new StopLimitOrder(Symbols.EURGBP, -quantity, 1.5m * pricePlusDelta, pricePlusDelta, time), -quantity * pricePlusDelta * forex.QuoteCurrency.ConversionRate), new ValueTestParameters("ForexLongStopMarketOrder", forex, new StopMarketOrder(Symbols.EURGBP, quantity, priceMinusDelta, time), quantity * priceMinusDelta * forex.QuoteCurrency.ConversionRate), new ValueTestParameters("ForexLongStopMarketOrder", forex, new StopMarketOrder(Symbols.EURGBP, quantity, pricePlusDelta, time), quantity * price * forex.QuoteCurrency.ConversionRate), new ValueTestParameters("ForexShortStopMarketOrder", forex, new StopMarketOrder(Symbols.EURGBP, -quantity, pricePlusDelta, time), -quantity * pricePlusDelta * forex.QuoteCurrency.ConversionRate), new ValueTestParameters("ForexShortStopMarketOrder", forex, new StopMarketOrder(Symbols.EURGBP, -quantity, priceMinusDelta, time), -quantity * price * forex.QuoteCurrency.ConversionRate), // cfd orders new ValueTestParameters("CfdLongMarketOrder", cfd, new MarketOrder(Symbols.DE10YBEUR, quantity, time), quantity * price * multiplierTimesConversionRate), new ValueTestParameters("CfdShortMarketOrder", cfd, new MarketOrder(Symbols.DE10YBEUR, -quantity, time), -quantity * price * multiplierTimesConversionRate), new ValueTestParameters("CfdLongLimitOrder", cfd, new LimitOrder(Symbols.DE10YBEUR, quantity, priceMinusDelta, time), quantity * priceMinusDelta * multiplierTimesConversionRate), new ValueTestParameters("CfdShortLimit Order", cfd, new LimitOrder(Symbols.DE10YBEUR, -quantity, pricePlusDelta, time), -quantity * pricePlusDelta * multiplierTimesConversionRate), new ValueTestParameters("CfdLongStopLimitOrder", cfd, new StopLimitOrder(Symbols.DE10YBEUR, quantity, .5m * priceMinusDelta, priceMinusDelta, time), quantity * priceMinusDelta * multiplierTimesConversionRate), new ValueTestParameters("CfdShortStopLimitOrder", cfd, new StopLimitOrder(Symbols.DE10YBEUR, -quantity, 1.5m * pricePlusDelta, pricePlusDelta, time), -quantity * pricePlusDelta * multiplierTimesConversionRate), new ValueTestParameters("CfdLongStopMarketOrder", cfd, new StopMarketOrder(Symbols.DE10YBEUR, quantity, priceMinusDelta, time), quantity * priceMinusDelta * multiplierTimesConversionRate), new ValueTestParameters("CfdLongStopMarketOrder", cfd, new StopMarketOrder(Symbols.DE10YBEUR, quantity, pricePlusDelta, time), quantity * price * multiplierTimesConversionRate), new ValueTestParameters("CfdShortStopMarketOrder", cfd, new StopMarketOrder(Symbols.DE10YBEUR, -quantity, pricePlusDelta, time), -quantity * pricePlusDelta * multiplierTimesConversionRate), new ValueTestParameters("CfdShortStopMarketOrder", cfd, new StopMarketOrder(Symbols.DE10YBEUR, -quantity, priceMinusDelta, time), -quantity * price * multiplierTimesConversionRate), }.Select(x => new TestCaseData(x).SetName(x.Name)).ToArray()); }
private static TestCaseData[] GetValueTestParameters() { const decimal delta = 1m; const decimal price = 1.2345m; const int quantity = 100; const decimal pricePlusDelta = price + delta; const decimal priceMinusDelta = price - delta; var tz = TimeZones.NewYork; var time = new DateTime(2016, 2, 4, 16, 0, 0).ConvertToUtc(tz); var equity = new Equity( SecurityExchangeHours.AlwaysOpen(tz), new SubscriptionDataConfig(typeof(TradeBar), Symbols.SPY, Resolution.Minute, tz, tz, true, false, false), new Cash(Currencies.USD, 0, 1m), SymbolProperties.GetDefault(Currencies.USD), ErrorCurrencyConverter.Instance, RegisteredSecurityDataTypesProvider.Null ); equity.SetMarketPrice(new Tick { Value = price }); var gbpCash = new Cash("GBP", 0, 1.46m); var properties = SymbolProperties.GetDefault(gbpCash.Symbol); var forex = new Forex( SecurityExchangeHours.AlwaysOpen(tz), gbpCash, new SubscriptionDataConfig(typeof(TradeBar), Symbols.EURGBP, Resolution.Minute, tz, tz, true, false, false), properties, ErrorCurrencyConverter.Instance, RegisteredSecurityDataTypesProvider.Null ); forex.SetMarketPrice(new Tick { Value = price }); var eurCash = new Cash("EUR", 0, 1.12m); properties = new SymbolProperties("Euro-Bund", eurCash.Symbol, 10, 0.1m, 1, string.Empty); var cfd = new Cfd( SecurityExchangeHours.AlwaysOpen(tz), eurCash, new SubscriptionDataConfig(typeof(TradeBar), Symbols.DE10YBEUR, Resolution.Minute, tz, tz, true, false, false), properties, ErrorCurrencyConverter.Instance, RegisteredSecurityDataTypesProvider.Null ); cfd.SetMarketPrice(new Tick { Value = price }); var multiplierTimesConversionRate = properties.ContractMultiplier * eurCash.ConversionRate; var option = new Option( SecurityExchangeHours.AlwaysOpen(tz), new SubscriptionDataConfig( typeof(TradeBar), Symbols.SPY_P_192_Feb19_2016, Resolution.Minute, tz, tz, true, false, false ), new Cash(Currencies.USD, 0, 1m), new OptionSymbolProperties(SymbolProperties.GetDefault(Currencies.USD)), ErrorCurrencyConverter.Instance, RegisteredSecurityDataTypesProvider.Null ); option.SetMarketPrice(new Tick { Value = price }); return(new List <ValueTestParameters> { // equity orders new ValueTestParameters("EquityLongMarketOrder", equity, new MarketOrder(Symbols.SPY, quantity, time), quantity * price), new ValueTestParameters("EquityShortMarketOrder", equity, new MarketOrder(Symbols.SPY, -quantity, time), -quantity * price), new ValueTestParameters("EquityLongLimitOrder", equity, new LimitOrder(Symbols.SPY, quantity, priceMinusDelta, time), quantity * priceMinusDelta), new ValueTestParameters("EquityShortLimit Order", equity, new LimitOrder(Symbols.SPY, -quantity, pricePlusDelta, time), -quantity * pricePlusDelta), new ValueTestParameters("EquityLongStopLimitOrder", equity, new StopLimitOrder(Symbols.SPY, quantity, .5m * priceMinusDelta, priceMinusDelta, time), quantity * priceMinusDelta), new ValueTestParameters("EquityShortStopLimitOrder", equity, new StopLimitOrder(Symbols.SPY, -quantity, 1.5m * pricePlusDelta, pricePlusDelta, time), -quantity * pricePlusDelta), new ValueTestParameters("EquityLongStopMarketOrder", equity, new StopMarketOrder(Symbols.SPY, quantity, priceMinusDelta, time), quantity * priceMinusDelta), new ValueTestParameters("EquityLongStopMarketOrder", equity, new StopMarketOrder(Symbols.SPY, quantity, pricePlusDelta, time), quantity * price), new ValueTestParameters("EquityShortStopMarketOrder", equity, new StopMarketOrder(Symbols.SPY, -quantity, pricePlusDelta, time), -quantity * pricePlusDelta), new ValueTestParameters("EquityShortStopMarketOrder", equity, new StopMarketOrder(Symbols.SPY, -quantity, priceMinusDelta, time), -quantity * price), new ValueTestParameters("EquityLongLimitIfTouchedOrder", equity, new LimitIfTouchedOrder(Symbols.SPY, quantity, 1.5m * pricePlusDelta, priceMinusDelta, time), quantity * priceMinusDelta), new ValueTestParameters("EquityShortLimitIfTouchedOrder", equity, new LimitIfTouchedOrder(Symbols.SPY, -quantity, .5m * priceMinusDelta, pricePlusDelta, time), -quantity * pricePlusDelta), // forex orders new ValueTestParameters("ForexLongMarketOrder", forex, new MarketOrder(Symbols.EURGBP, quantity, time), quantity * price * forex.QuoteCurrency.ConversionRate), new ValueTestParameters("ForexShortMarketOrder", forex, new MarketOrder(Symbols.EURGBP, -quantity, time), -quantity * price * forex.QuoteCurrency.ConversionRate), new ValueTestParameters("ForexLongLimitOrder", forex, new LimitOrder(Symbols.EURGBP, quantity, priceMinusDelta, time), quantity * priceMinusDelta * forex.QuoteCurrency.ConversionRate), new ValueTestParameters("ForexShortLimit Order", forex, new LimitOrder(Symbols.EURGBP, -quantity, pricePlusDelta, time), -quantity * pricePlusDelta * forex.QuoteCurrency.ConversionRate), new ValueTestParameters("ForexLongStopLimitOrder", forex, new StopLimitOrder(Symbols.EURGBP, quantity, .5m * priceMinusDelta, priceMinusDelta, time), quantity * priceMinusDelta * forex.QuoteCurrency.ConversionRate), new ValueTestParameters("ForexShortStopLimitOrder", forex, new StopLimitOrder(Symbols.EURGBP, -quantity, 1.5m * pricePlusDelta, pricePlusDelta, time), -quantity * pricePlusDelta * forex.QuoteCurrency.ConversionRate), new ValueTestParameters("ForexLongStopMarketOrder", forex, new StopMarketOrder(Symbols.EURGBP, quantity, priceMinusDelta, time), quantity * priceMinusDelta * forex.QuoteCurrency.ConversionRate), new ValueTestParameters("ForexLongStopMarketOrder", forex, new StopMarketOrder(Symbols.EURGBP, quantity, pricePlusDelta, time), quantity * price * forex.QuoteCurrency.ConversionRate), new ValueTestParameters("ForexShortStopMarketOrder", forex, new StopMarketOrder(Symbols.EURGBP, -quantity, pricePlusDelta, time), -quantity * pricePlusDelta * forex.QuoteCurrency.ConversionRate), new ValueTestParameters("ForexShortStopMarketOrder", forex, new StopMarketOrder(Symbols.EURGBP, -quantity, priceMinusDelta, time), -quantity * price * forex.QuoteCurrency.ConversionRate), new ValueTestParameters("ForexLongLimitIfTouchedOrder", forex, new LimitIfTouchedOrder(Symbols.EURGBP, quantity, 1.5m * priceMinusDelta, priceMinusDelta, time), quantity * priceMinusDelta * forex.QuoteCurrency.ConversionRate), new ValueTestParameters("ForexShortLimitIfTouchedOrder", forex, new LimitIfTouchedOrder(Symbols.EURGBP, -quantity, .5m * pricePlusDelta, pricePlusDelta, time), -quantity * pricePlusDelta * forex.QuoteCurrency.ConversionRate), // cfd orders new ValueTestParameters("CfdLongMarketOrder", cfd, new MarketOrder(Symbols.DE10YBEUR, quantity, time), quantity * price * multiplierTimesConversionRate), new ValueTestParameters("CfdShortMarketOrder", cfd, new MarketOrder(Symbols.DE10YBEUR, -quantity, time), -quantity * price * multiplierTimesConversionRate), new ValueTestParameters("CfdLongLimitOrder", cfd, new LimitOrder(Symbols.DE10YBEUR, quantity, priceMinusDelta, time), quantity * priceMinusDelta * multiplierTimesConversionRate), new ValueTestParameters("CfdShortLimit Order", cfd, new LimitOrder(Symbols.DE10YBEUR, -quantity, pricePlusDelta, time), -quantity * pricePlusDelta * multiplierTimesConversionRate), new ValueTestParameters("CfdLongStopLimitOrder", cfd, new StopLimitOrder(Symbols.DE10YBEUR, quantity, .5m * priceMinusDelta, priceMinusDelta, time), quantity * priceMinusDelta * multiplierTimesConversionRate), new ValueTestParameters("CfdShortStopLimitOrder", cfd, new StopLimitOrder(Symbols.DE10YBEUR, -quantity, 1.5m * pricePlusDelta, pricePlusDelta, time), -quantity * pricePlusDelta * multiplierTimesConversionRate), new ValueTestParameters("CfdLongStopMarketOrder", cfd, new StopMarketOrder(Symbols.DE10YBEUR, quantity, priceMinusDelta, time), quantity * priceMinusDelta * multiplierTimesConversionRate), new ValueTestParameters("CfdLongStopMarketOrder", cfd, new StopMarketOrder(Symbols.DE10YBEUR, quantity, pricePlusDelta, time), quantity * price * multiplierTimesConversionRate), new ValueTestParameters("CfdShortStopMarketOrder", cfd, new StopMarketOrder(Symbols.DE10YBEUR, -quantity, pricePlusDelta, time), -quantity * pricePlusDelta * multiplierTimesConversionRate), new ValueTestParameters("CfdShortStopMarketOrder", cfd, new StopMarketOrder(Symbols.DE10YBEUR, -quantity, priceMinusDelta, time), -quantity * price * multiplierTimesConversionRate), new ValueTestParameters("CfdShortLimitIfTouchedOrder", cfd, new LimitIfTouchedOrder(Symbols.DE10YBEUR, -quantity, 1.5m * pricePlusDelta, pricePlusDelta, time), -quantity * pricePlusDelta * multiplierTimesConversionRate), new ValueTestParameters("CfdLongLimitIfTouchedOrder", cfd, new LimitIfTouchedOrder(Symbols.DE10YBEUR, quantity, .5m * priceMinusDelta, priceMinusDelta, time), quantity * priceMinusDelta * multiplierTimesConversionRate), // equity/index option orders new ValueTestParameters("OptionLongMarketOrder", option, new MarketOrder(Symbols.SPY_P_192_Feb19_2016, quantity, time), quantity * price), new ValueTestParameters("OptionShortMarketOrder", option, new MarketOrder(Symbols.SPY_P_192_Feb19_2016, -quantity, time), -quantity * price), new ValueTestParameters("OptionLongLimitOrder", option, new LimitOrder(Symbols.SPY_P_192_Feb19_2016, quantity, priceMinusDelta, time), quantity * priceMinusDelta), new ValueTestParameters("OptionShortLimit Order", option, new LimitOrder(Symbols.SPY_P_192_Feb19_2016, -quantity, pricePlusDelta, time), -quantity * pricePlusDelta), new ValueTestParameters("OptionLongStopLimitOrder", option, new StopLimitOrder(Symbols.SPY_P_192_Feb19_2016, quantity, .5m * priceMinusDelta, priceMinusDelta, time), quantity * priceMinusDelta), new ValueTestParameters("OptionShortStopLimitOrder", option, new StopLimitOrder(Symbols.SPY_P_192_Feb19_2016, -quantity, 1.5m * pricePlusDelta, pricePlusDelta, time), -quantity * pricePlusDelta), new ValueTestParameters("OptionLongStopMarketOrder", option, new StopMarketOrder(Symbols.SPY_P_192_Feb19_2016, quantity, priceMinusDelta, time), quantity * priceMinusDelta), new ValueTestParameters("OptionLongStopMarketOrder", option, new StopMarketOrder(Symbols.SPY_P_192_Feb19_2016, quantity, pricePlusDelta, time), quantity * price), new ValueTestParameters("OptionShortStopMarketOrder", option, new StopMarketOrder(Symbols.SPY_P_192_Feb19_2016, -quantity, pricePlusDelta, time), -quantity * pricePlusDelta), new ValueTestParameters("OptionShortStopMarketOrder", option, new StopMarketOrder(Symbols.SPY_P_192_Feb19_2016, -quantity, priceMinusDelta, time), -quantity * price), new ValueTestParameters("OptionShortLimitIfTouchedOrder", option, new LimitIfTouchedOrder(Symbols.SPY_P_192_Feb19_2016, -quantity, 1.5m * pricePlusDelta, pricePlusDelta, time), -quantity * pricePlusDelta), new ValueTestParameters("OptionLongLimitIfTouchedOrder", option, new LimitIfTouchedOrder(Symbols.SPY_P_192_Feb19_2016, quantity, .5m * priceMinusDelta, priceMinusDelta, time), quantity * priceMinusDelta), new ValueTestParameters("OptionExerciseOrderPut", option, new OptionExerciseOrder(Symbols.SPY_P_192_Feb19_2016, quantity, time), quantity * option.Symbol.ID.StrikePrice), new ValueTestParameters("OptionAssignmentOrderPut", option, new OptionExerciseOrder(Symbols.SPY_P_192_Feb19_2016, -quantity, time), -quantity * option.Symbol.ID.StrikePrice), new ValueTestParameters("OptionExerciseOrderCall", option, new OptionExerciseOrder(Symbols.SPY_C_192_Feb19_2016, quantity, time), quantity * option.Symbol.ID.StrikePrice), new ValueTestParameters("OptionAssignmentOrderCall", option, new OptionExerciseOrder(Symbols.SPY_C_192_Feb19_2016, -quantity, time), -quantity * option.Symbol.ID.StrikePrice), }.Select(x => new TestCaseData(x).SetName(x.Name)).ToArray()); }
public void PutCallParityTest() { const decimal underlyingPrice = 200m; const decimal underlyingVol = 0.15m; const decimal riskFreeRate = 0.01m; var tz = TimeZones.NewYork; var evaluationDate = new DateTime(2015, 2, 19); var SPY_C_192_Feb19_2016E = Symbol.CreateOption("SPY", Market.USA, OptionStyle.European, OptionRight.Call, 192m, new DateTime(2016, 02, 19)); var SPY_P_192_Feb19_2016E = Symbol.CreateOption("SPY", Market.USA, OptionStyle.European, OptionRight.Put, 192m, new DateTime(2016, 02, 19)); // setting up underlying var equity = new Equity( SecurityExchangeHours.AlwaysOpen(tz), new SubscriptionDataConfig(typeof(TradeBar), Symbols.SPY, Resolution.Minute, tz, tz, true, false, false), new Cash(CashBook.AccountCurrency, 0, 1m), SymbolProperties.GetDefault(CashBook.AccountCurrency), ErrorCurrencyConverter.Instance ); equity.SetMarketPrice(new Tick { Value = underlyingPrice }); equity.VolatilityModel = new DummyVolatilityModel(underlyingVol); // setting up European style call option var contractCall = new OptionContract(SPY_C_192_Feb19_2016E, Symbols.SPY) { Time = evaluationDate }; var optionCall = new Option( SecurityExchangeHours.AlwaysOpen(tz), new SubscriptionDataConfig(typeof(TradeBar), SPY_C_192_Feb19_2016E, Resolution.Minute, tz, tz, true, false, false), new Cash(CashBook.AccountCurrency, 0, 1m), new OptionSymbolProperties(SymbolProperties.GetDefault(CashBook.AccountCurrency)), ErrorCurrencyConverter.Instance ); optionCall.Underlying = equity; // setting up European style put option var contractPut = new OptionContract(SPY_P_192_Feb19_2016E, Symbols.SPY) { Time = evaluationDate }; var optionPut = new Option( SecurityExchangeHours.AlwaysOpen(tz), new SubscriptionDataConfig(typeof(TradeBar), SPY_P_192_Feb19_2016E, Resolution.Minute, tz, tz, true, false, false), new Cash(CashBook.AccountCurrency, 0, 1m), new OptionSymbolProperties(SymbolProperties.GetDefault(CashBook.AccountCurrency)), ErrorCurrencyConverter.Instance ); optionPut.Underlying = equity; // running evaluation var priceModel = OptionPriceModels.BlackScholes(); var resultsCall = priceModel.Evaluate(optionCall, null, contractCall); var resultsPut = priceModel.Evaluate(optionPut, null, contractPut); var callPrice = resultsCall.TheoreticalPrice; var putPrice = resultsPut.TheoreticalPrice; // Put-call parity equation var rightPart = putPrice + underlyingPrice; // no yield var leftPart = callPrice + contractCall.Strike * (decimal)Math.Exp((double)-riskFreeRate); Assert.AreEqual((double)leftPart, (double)rightPart, 0.0001); }
public void GreekApproximationTest() { const decimal price = 20.00m; const decimal underlyingPrice = 190m; const decimal underlyingVol = 0.15m; var tz = TimeZones.NewYork; var evaluationDate = new DateTime(2016, 1, 19); var equity = new Equity( SecurityExchangeHours.AlwaysOpen(tz), new SubscriptionDataConfig(typeof(TradeBar), Symbols.SPY, Resolution.Minute, tz, tz, true, false, false), new Cash(CashBook.AccountCurrency, 0, 1m), SymbolProperties.GetDefault(CashBook.AccountCurrency), ErrorCurrencyConverter.Instance ); equity.SetMarketPrice(new Tick { Value = underlyingPrice }); equity.VolatilityModel = new DummyVolatilityModel(underlyingVol); var contract = new OptionContract(Symbols.SPY_P_192_Feb19_2016, Symbols.SPY) { Time = evaluationDate }; var optionPut = new Option( SecurityExchangeHours.AlwaysOpen(tz), new SubscriptionDataConfig( typeof(TradeBar), Symbols.SPY_P_192_Feb19_2016, Resolution.Minute, tz, tz, true, false, false ), new Cash(CashBook.AccountCurrency, 0, 1m), new OptionSymbolProperties(SymbolProperties.GetDefault(CashBook.AccountCurrency)), ErrorCurrencyConverter.Instance ); optionPut.SetMarketPrice(new Tick { Value = price }); optionPut.Underlying = equity; var priceModel = (QLOptionPriceModel)OptionPriceModels.CrankNicolsonFD(); priceModel.EnableGreekApproximation = false; var results = priceModel.Evaluate(optionPut, null, contract); var greeks = results.Greeks; Assert.AreEqual(greeks.Theta, 0); Assert.AreEqual(greeks.Rho, 0); Assert.AreEqual(greeks.Vega, 0); priceModel = (QLOptionPriceModel)OptionPriceModels.CrankNicolsonFD(); priceModel.EnableGreekApproximation = true; results = priceModel.Evaluate(optionPut, null, contract); greeks = results.Greeks; Assert.LessOrEqual(greeks.Theta, 0); Assert.AreNotEqual(greeks.Rho, 0); Assert.Greater(greeks.Vega, 0); }
private static TestCaseData[] GetValueTestParameters() { const decimal delta = 1m; const decimal price = 1.2345m; const int quantity = 100; const decimal pricePlusDelta = price + delta; const decimal priceMinusDelta = price - delta; var tz = TimeZones.NewYork; var time = new DateTime(2016, 2, 4, 16, 0, 0).ConvertToUtc(tz); var equity = new Equity(SecurityExchangeHours.AlwaysOpen(tz), new SubscriptionDataConfig(typeof(TradeBar), Symbols.SPY, Resolution.Minute, tz, tz, true, false, false), new Cash(CashBook.AccountCurrency, 0, 1m), SymbolProperties.GetDefault(CashBook.AccountCurrency)); equity.SetMarketPrice(new Tick {Value = price}); var gbpCash = new Cash("GBP", 0, 1.46m); var properties = SymbolProperties.GetDefault(gbpCash.Symbol); var forex = new Forex(SecurityExchangeHours.AlwaysOpen(tz), gbpCash, new SubscriptionDataConfig(typeof(TradeBar), Symbols.EURGBP, Resolution.Minute, tz, tz, true, false, false), properties); forex.SetMarketPrice(new Tick {Value= price}); var eurCash = new Cash("EUR", 0, 1.12m); properties = new SymbolProperties("Euro-Bund", eurCash.Symbol, 10, 0.1m, 1); var cfd = new Cfd(SecurityExchangeHours.AlwaysOpen(tz), eurCash, new SubscriptionDataConfig(typeof(TradeBar), Symbols.DE10YBEUR, Resolution.Minute, tz, tz, true, false, false), properties); cfd.SetMarketPrice(new Tick { Value = price }); var multiplierTimesConversionRate = properties.ContractMultiplier*eurCash.ConversionRate; return new List<ValueTestParameters> { // equity orders new ValueTestParameters("EquityLongMarketOrder", equity, new MarketOrder(Symbols.SPY, quantity, time), quantity*price), new ValueTestParameters("EquityShortMarketOrder", equity, new MarketOrder(Symbols.SPY, -quantity, time), -quantity*price), new ValueTestParameters("EquityLongLimitOrder", equity, new LimitOrder(Symbols.SPY, quantity, priceMinusDelta, time), quantity*priceMinusDelta), new ValueTestParameters("EquityShortLimit Order", equity, new LimitOrder(Symbols.SPY, -quantity, pricePlusDelta, time), -quantity*pricePlusDelta), new ValueTestParameters("EquityLongStopLimitOrder", equity, new StopLimitOrder(Symbols.SPY, quantity,.5m*priceMinusDelta, priceMinusDelta, time), quantity*priceMinusDelta), new ValueTestParameters("EquityShortStopLimitOrder", equity, new StopLimitOrder(Symbols.SPY, -quantity, 1.5m*pricePlusDelta, pricePlusDelta, time), -quantity*pricePlusDelta), new ValueTestParameters("EquityLongStopMarketOrder", equity, new StopMarketOrder(Symbols.SPY, quantity, priceMinusDelta, time), quantity*priceMinusDelta), new ValueTestParameters("EquityLongStopMarketOrder", equity, new StopMarketOrder(Symbols.SPY, quantity, pricePlusDelta, time), quantity*price), new ValueTestParameters("EquityShortStopMarketOrder", equity, new StopMarketOrder(Symbols.SPY, -quantity, pricePlusDelta, time), -quantity*pricePlusDelta), new ValueTestParameters("EquityShortStopMarketOrder", equity, new StopMarketOrder(Symbols.SPY, -quantity, priceMinusDelta, time), -quantity*price), // forex orders new ValueTestParameters("ForexLongMarketOrder", forex, new MarketOrder(Symbols.EURGBP, quantity, time), quantity*price*forex.QuoteCurrency.ConversionRate), new ValueTestParameters("ForexShortMarketOrder", forex, new MarketOrder(Symbols.EURGBP, -quantity, time), -quantity*price*forex.QuoteCurrency.ConversionRate), new ValueTestParameters("ForexLongLimitOrder", forex, new LimitOrder(Symbols.EURGBP, quantity, priceMinusDelta, time), quantity*priceMinusDelta*forex.QuoteCurrency.ConversionRate), new ValueTestParameters("ForexShortLimit Order", forex, new LimitOrder(Symbols.EURGBP, -quantity, pricePlusDelta, time), -quantity*pricePlusDelta*forex.QuoteCurrency.ConversionRate), new ValueTestParameters("ForexLongStopLimitOrder", forex, new StopLimitOrder(Symbols.EURGBP, quantity,.5m*priceMinusDelta, priceMinusDelta, time), quantity*priceMinusDelta*forex.QuoteCurrency.ConversionRate), new ValueTestParameters("ForexShortStopLimitOrder", forex, new StopLimitOrder(Symbols.EURGBP, -quantity, 1.5m*pricePlusDelta, pricePlusDelta, time), -quantity*pricePlusDelta*forex.QuoteCurrency.ConversionRate), new ValueTestParameters("ForexLongStopMarketOrder", forex, new StopMarketOrder(Symbols.EURGBP, quantity, priceMinusDelta, time), quantity*priceMinusDelta*forex.QuoteCurrency.ConversionRate), new ValueTestParameters("ForexLongStopMarketOrder", forex, new StopMarketOrder(Symbols.EURGBP, quantity, pricePlusDelta, time), quantity*price*forex.QuoteCurrency.ConversionRate), new ValueTestParameters("ForexShortStopMarketOrder", forex, new StopMarketOrder(Symbols.EURGBP, -quantity, pricePlusDelta, time), -quantity*pricePlusDelta*forex.QuoteCurrency.ConversionRate), new ValueTestParameters("ForexShortStopMarketOrder", forex, new StopMarketOrder(Symbols.EURGBP, -quantity, priceMinusDelta, time), -quantity*price*forex.QuoteCurrency.ConversionRate), // cfd orders new ValueTestParameters("CfdLongMarketOrder", cfd, new MarketOrder(Symbols.DE10YBEUR, quantity, time), quantity*price*multiplierTimesConversionRate), new ValueTestParameters("CfdShortMarketOrder", cfd, new MarketOrder(Symbols.DE10YBEUR, -quantity, time), -quantity*price*multiplierTimesConversionRate), new ValueTestParameters("CfdLongLimitOrder", cfd, new LimitOrder(Symbols.DE10YBEUR, quantity, priceMinusDelta, time), quantity*priceMinusDelta*multiplierTimesConversionRate), new ValueTestParameters("CfdShortLimit Order", cfd, new LimitOrder(Symbols.DE10YBEUR, -quantity, pricePlusDelta, time), -quantity*pricePlusDelta*multiplierTimesConversionRate), new ValueTestParameters("CfdLongStopLimitOrder", cfd, new StopLimitOrder(Symbols.DE10YBEUR, quantity,.5m*priceMinusDelta, priceMinusDelta, time), quantity*priceMinusDelta*multiplierTimesConversionRate), new ValueTestParameters("CfdShortStopLimitOrder", cfd, new StopLimitOrder(Symbols.DE10YBEUR, -quantity, 1.5m*pricePlusDelta, pricePlusDelta, time), -quantity*pricePlusDelta*multiplierTimesConversionRate), new ValueTestParameters("CfdLongStopMarketOrder", cfd, new StopMarketOrder(Symbols.DE10YBEUR, quantity, priceMinusDelta, time), quantity*priceMinusDelta*multiplierTimesConversionRate), new ValueTestParameters("CfdLongStopMarketOrder", cfd, new StopMarketOrder(Symbols.DE10YBEUR, quantity, pricePlusDelta, time), quantity*price*multiplierTimesConversionRate), new ValueTestParameters("CfdShortStopMarketOrder", cfd, new StopMarketOrder(Symbols.DE10YBEUR, -quantity, pricePlusDelta, time), -quantity*pricePlusDelta*multiplierTimesConversionRate), new ValueTestParameters("CfdShortStopMarketOrder", cfd, new StopMarketOrder(Symbols.DE10YBEUR, -quantity, priceMinusDelta, time), -quantity*price*multiplierTimesConversionRate), }.Select(x => new TestCaseData(x).SetName(x.Name)).ToArray(); }
public void ReturnsNoneIfNotWarmedUp() { const decimal underlyingPrice = 200m; const decimal underlyingVol = 0.15m; const decimal riskFreeRate = 0.01m; var tz = TimeZones.NewYork; var evaluationDate = new DateTime(2015, 2, 19); var SPY_C_192_Feb19_2016E = Symbol.CreateOption("SPY", Market.USA, OptionStyle.European, OptionRight.Call, 192m, new DateTime(2016, 02, 19)); var SPY_P_192_Feb19_2016E = Symbol.CreateOption("SPY", Market.USA, OptionStyle.European, OptionRight.Put, 192m, new DateTime(2016, 02, 19)); // setting up underlying var equity = new Equity( SecurityExchangeHours.AlwaysOpen(tz), new SubscriptionDataConfig(typeof(TradeBar), Symbols.SPY, Resolution.Minute, tz, tz, true, false, false), new Cash(Currencies.USD, 0, 1m), SymbolProperties.GetDefault(Currencies.USD), ErrorCurrencyConverter.Instance, RegisteredSecurityDataTypesProvider.Null ); equity.SetMarketPrice(new Tick { Value = underlyingPrice }); equity.VolatilityModel = new DummyVolatilityModel(underlyingVol); // setting up European style call option var contractCall = new OptionContract(SPY_C_192_Feb19_2016E, Symbols.SPY) { Time = evaluationDate }; var optionCall = new Option( SecurityExchangeHours.AlwaysOpen(tz), new SubscriptionDataConfig(typeof(TradeBar), SPY_C_192_Feb19_2016E, Resolution.Minute, tz, tz, true, false, false), new Cash(Currencies.USD, 0, 1m), new OptionSymbolProperties(SymbolProperties.GetDefault(Currencies.USD)), ErrorCurrencyConverter.Instance, RegisteredSecurityDataTypesProvider.Null ); optionCall.Underlying = equity; // setting up European style put option var contractPut = new OptionContract(SPY_P_192_Feb19_2016E, Symbols.SPY) { Time = evaluationDate }; var optionPut = new Option( SecurityExchangeHours.AlwaysOpen(tz), new SubscriptionDataConfig(typeof(TradeBar), SPY_P_192_Feb19_2016E, Resolution.Minute, tz, tz, true, false, false), new Cash(Currencies.USD, 0, 1m), new OptionSymbolProperties(SymbolProperties.GetDefault(Currencies.USD)), ErrorCurrencyConverter.Instance, RegisteredSecurityDataTypesProvider.Null ); optionPut.Underlying = equity; // running evaluation var volatilityModel = new Mock <IQLUnderlyingVolatilityEstimator>(); volatilityModel.SetupGet(s => s.IsReady).Returns(false); var priceModel = new QLOptionPriceModel(process => new AnalyticEuropeanEngine(process), volatilityModel.Object, null, null); var resultsCall = priceModel.Evaluate(optionCall, null, contractCall); var resultsPut = priceModel.Evaluate(optionPut, null, contractPut); Assert.AreEqual(OptionPriceModelResult.None, resultsCall); Assert.AreEqual(OptionPriceModelResult.None, resultsCall); }