/// <summary> /// Gets a new buying power model for the security, returning the default model with the security's configured leverage. /// For cash accounts, leverage = 1 is used. /// </summary> /// <param name="security">The security to get a buying power model for</param> /// <returns>The buying power model for this brokerage/security</returns> public virtual IBuyingPowerModel GetBuyingPowerModel(Security security) { var leverage = GetLeverage(security); IBuyingPowerModel model; switch (security.Type) { case SecurityType.Crypto: model = new CashBuyingPowerModel(); break; case SecurityType.Forex: case SecurityType.Cfd: model = new SecurityMarginModel(leverage, RequiredFreeBuyingPowerPercent); break; case SecurityType.Option: model = new OptionMarginModel(RequiredFreeBuyingPowerPercent); break; case SecurityType.FutureOption: model = new FuturesOptionsMarginModel(RequiredFreeBuyingPowerPercent, (Option)security); break; case SecurityType.Future: model = new FutureMarginModel(RequiredFreeBuyingPowerPercent, security); break; default: model = new SecurityMarginModel(leverage, RequiredFreeBuyingPowerPercent); break; } return(model); }
public void NonAccountCurrency_GetMaintenanceMarginRequirement() { var algorithm = new QCAlgorithm(); algorithm.SubscriptionManager.SetDataManager(new DataManagerStub(algorithm)); algorithm.Portfolio.SetAccountCurrency("EUR"); algorithm.Portfolio.SetCash(10000); algorithm.Portfolio.SetCash(Currencies.USD, 0, 0.88m); // For this symbol we dont have history var ticker = QuantConnect.Securities.Futures.Financials.EuroDollar; const decimal price = 1.2345m; var time = new DateTime(2013, 1, 1); var futureSecurity = algorithm.AddFuture(ticker); futureSecurity.SetMarketPrice(new Tick { Value = price, Time = time }); futureSecurity.Holdings.SetHoldings(1.5m, 1); var buyingPowerModel = new FutureMarginModel(); // 625 overnight / (1.5 price * 1 quantity * 0.88 rate * 2500 multiplier) ~= 0.18939 var res = buyingPowerModel.GetMaintenanceMarginRequirement(futureSecurity); Assert.AreEqual(0.18939, (double)res, 0.00001); }
private static IBuyingPowerModel GetModel( Security security, out FutureMarginModel futureMarginModel, SecurityPortfolioManager portfolio = null, TimeKeeper timeKeeper = null ) { futureMarginModel = security.BuyingPowerModel as FutureMarginModel; return(new BuyingPowerModelComparator(futureMarginModel, new SecurityPositionGroupBuyingPowerModel(), portfolio, timeKeeper )); }
public void MarginWithNoFutureOptionHoldings() { const decimal price = 1.2345m; var time = new DateTime(2020, 10, 14); var expDate = new DateTime(2021, 3, 19); var tz = TimeZones.NewYork; // For this symbol we dont have any history, but only one date and margins line var ticker = QuantConnect.Securities.Futures.Indices.SP500EMini; var future = Symbol.CreateFuture(ticker, Market.CME, expDate); var symbol = Symbol.CreateOption(future, Market.CME, OptionStyle.American, OptionRight.Call, 2550m, new DateTime(2021, 3, 19)); var optionSecurity = new Option( SecurityExchangeHours.AlwaysOpen(tz), new SubscriptionDataConfig(typeof(TradeBar), symbol, Resolution.Minute, tz, tz, true, false, false), new Cash(Currencies.USD, 0, 1m), new OptionSymbolProperties(SymbolProperties.GetDefault(Currencies.USD)), ErrorCurrencyConverter.Instance, RegisteredSecurityDataTypesProvider.Null ); optionSecurity.Underlying = new Future( SecurityExchangeHours.AlwaysOpen(tz), new SubscriptionDataConfig(typeof(TradeBar), future, Resolution.Minute, tz, tz, true, false, false), new Cash(Currencies.USD, 0, 1m), new OptionSymbolProperties(SymbolProperties.GetDefault(Currencies.USD)), ErrorCurrencyConverter.Instance, RegisteredSecurityDataTypesProvider.Null ); optionSecurity.Underlying.SetMarketPrice(new Tick { Value = price, Time = time }); optionSecurity.Underlying.Holdings.SetHoldings(1.5m, 1); var futureBuyingPowerModel = new FutureMarginModel(security: optionSecurity.Underlying); var futureOptionBuyingPowerModel = new FuturesOptionsMarginModel(futureOption: optionSecurity); // we don't hold FOPs! Assert.AreEqual(0m, futureOptionBuyingPowerModel.GetMaintenanceMargin(optionSecurity)); Assert.AreNotEqual(0m, futureBuyingPowerModel.GetMaintenanceMargin(optionSecurity.Underlying)); Assert.AreNotEqual(0m, futureOptionBuyingPowerModel.GetInitialMarginRequirement(optionSecurity, 10)); Assert.AreEqual( futureBuyingPowerModel.GetInitialMarginRequirement(optionSecurity.Underlying, 10) * FuturesOptionsMarginModel.FixedMarginMultiplier, futureOptionBuyingPowerModel.GetInitialMarginRequirement(optionSecurity, 10)); }
public void NonAccountCurrency_GetBuyingPower(decimal nonAccountCurrencyCash) { var algorithm = new QCAlgorithm(); algorithm.SubscriptionManager.SetDataManager(new DataManagerStub(algorithm)); algorithm.Portfolio.SetAccountCurrency("EUR"); algorithm.Portfolio.SetCash(10000); algorithm.Portfolio.SetCash(Currencies.USD, nonAccountCurrencyCash, 0.88m); // For this symbol we dont have history var ticker = QuantConnect.Securities.Futures.Financials.EuroDollar; var futureSecurity = algorithm.AddFuture(ticker); var buyingPowerModel = new FutureMarginModel(); var quantity = buyingPowerModel.GetBuyingPower(new BuyingPowerParameters( algorithm.Portfolio, futureSecurity, OrderDirection.Buy)); Assert.AreEqual(10000m + algorithm.Portfolio.CashBook[Currencies.USD].ValueInAccountCurrency, quantity.Value); }
public void TestMarginForSymbolWithNoHistory() { const decimal price = 1.2345m; var time = new DateTime(2016, 1, 1); var expDate = new DateTime(2017, 1, 1); var tz = TimeZones.NewYork; // For this symbol we dont have any history at all var ticker = "NOT-A-SYMBOL"; var symbol = Symbol.CreateFuture(ticker, Market.USA, expDate); var futureSecurity = new Future(SecurityExchangeHours.AlwaysOpen(tz), new SubscriptionDataConfig(typeof(TradeBar), symbol, Resolution.Minute, tz, tz, true, false, false), new Cash(CashBook.AccountCurrency, 0, 1m), new OptionSymbolProperties(SymbolProperties.GetDefault(CashBook.AccountCurrency))); futureSecurity.SetMarketPrice(new Tick { Value = price, Time = time }); futureSecurity.Holdings.SetHoldings(1.5m, 1); var marginModel = new FutureMarginModel(); Assert.AreEqual(0m, marginModel.GetMaintenanceMargin(futureSecurity)); }
public void TestMarginForSymbolWithHistory() { const decimal price = 1.2345m; var time = new DateTime(2013, 1, 1); var expDate = new DateTime(2017, 1, 1); var tz = TimeZones.NewYork; // For this symbol we dont have history var ticker = Futures.Financials.EuroDollar; var symbol = Symbol.CreateFuture(ticker, Market.USA, expDate); var futureSecurity = new Future(SecurityExchangeHours.AlwaysOpen(tz), new SubscriptionDataConfig(typeof(TradeBar), symbol, Resolution.Minute, tz, tz, true, false, false), new Cash(CashBook.AccountCurrency, 0, 1m), new OptionSymbolProperties(SymbolProperties.GetDefault(CashBook.AccountCurrency))); futureSecurity.SetMarketPrice(new Tick { Value = price, Time = time }); futureSecurity.Holdings.SetHoldings(1.5m, 1); var marginModel = new FutureMarginModel(); Assert.AreEqual(625m, marginModel.GetMaintenanceMargin(futureSecurity)); // now we move forward to exact date when margin req changed time = new DateTime(2014, 06, 13); futureSecurity.SetMarketPrice(new Tick { Value = price, Time = time }); Assert.AreEqual(725m, marginModel.GetMaintenanceMargin(futureSecurity)); // now we fly beyond the last line of the history file (currently) to see how margin model resolves future dates time = new DateTime(2016, 06, 04); futureSecurity.SetMarketPrice(new Tick { Value = price, Time = time }); Assert.AreEqual(585m, marginModel.GetMaintenanceMargin(futureSecurity)); }