public void ReturnsExpectedPortfolioTarget( Language language, decimal maxDrawdownPercent, bool invested, decimal unrealizedProfit, decimal absoluteHoldingsCost, bool shouldLiquidate) { var security = new Mock <Equity>( Symbols.AAPL, SecurityExchangeHours.AlwaysOpen(TimeZones.NewYork), new Cash(CashBook.AccountCurrency, 0, 1), SymbolProperties.GetDefault(CashBook.AccountCurrency), ErrorCurrencyConverter.Instance ); security.Setup(m => m.Invested).Returns(invested); var holding = new Mock <EquityHolding>(security.Object, new IdentityCurrencyConverter(CashBook.AccountCurrency)); holding.Setup(m => m.UnrealizedProfit).Returns(unrealizedProfit); holding.Setup(m => m.AbsoluteHoldingsCost).Returns(absoluteHoldingsCost); security.Object.Holdings = holding.Object; var algorithm = new QCAlgorithmFramework(); algorithm.SetPandasConverter(); algorithm.Securities.Add(Symbols.AAPL, security.Object); if (language == Language.Python) { using (Py.GIL()) { const string name = nameof(MaximumDrawdownPercentPerSecurity); var instance = Py.Import(name).GetAttr(name).Invoke(maxDrawdownPercent.ToPython()); var model = new RiskManagementModelPythonWrapper(instance); algorithm.SetRiskManagement(model); } } else { var model = new MaximumDrawdownPercentPerSecurity(maxDrawdownPercent); algorithm.SetRiskManagement(model); } var targets = algorithm.RiskManagement.ManageRisk(algorithm, null).ToList(); if (shouldLiquidate) { Assert.AreEqual(1, targets.Count); Assert.AreEqual(Symbols.AAPL, targets[0].Symbol); Assert.AreEqual(0, targets[0].Quantity); } else { Assert.AreEqual(0, targets.Count); } }
/// <summary> /// Adds a new risk management model /// </summary> /// <param name="riskManagement">Model defining how risk is managed to add</param> public void AddRiskManagement(PyObject riskManagement) { IRiskManagementModel model; if (!riskManagement.TryConvert(out model)) { model = new RiskManagementModelPythonWrapper(riskManagement); } AddRiskManagement(model); }
/// <summary> /// Sets the risk management model /// </summary> /// <param name="riskManagement">Model defining how risk is managed</param> public void SetRiskManagement(PyObject riskManagement) { IRiskManagementModel model; if (riskManagement.TryConvert(out model)) { SetRiskManagement(model); } else { RiskManagement = new RiskManagementModelPythonWrapper(riskManagement); } }
public void ReturnsExpectedPortfolioTarget( TrailingStopRiskManagementModelTestParameters parameters) { var decimalPrices = System.Array.ConvertAll(parameters.Prices, x => (decimal)x); var security = new Mock <Security>( Symbols.AAPL, SecurityExchangeHours.AlwaysOpen(TimeZones.NewYork), new Cash(Currencies.USD, 1000m, 1m), SymbolProperties.GetDefault(Currencies.USD), ErrorCurrencyConverter.Instance, RegisteredSecurityDataTypesProvider.Null, new SecurityCache() ); security.CallBase = true; security.Object.FeeModel = new ConstantFeeModel(0); var holding = new SecurityHolding(security.Object, new IdentityCurrencyConverter(Currencies.USD)); holding.SetHoldings(parameters.InitialPrice, parameters.Quantity); security.Object.Holdings = holding; var algorithm = new QCAlgorithm(); algorithm.SetPandasConverter(); algorithm.Securities.Add(Symbols.AAPL, security.Object); if (parameters.Language == Language.Python) { using (Py.GIL()) { const string name = nameof(TrailingStopRiskManagementModel); var instance = Py.Import(name).GetAttr(name).Invoke(parameters.MaxDrawdownPercent.ToPython()); var model = new RiskManagementModelPythonWrapper(instance); algorithm.SetRiskManagement(model); } } else { var model = new TrailingStopRiskManagementModel(parameters.MaxDrawdownPercent); algorithm.SetRiskManagement(model); } var quantity = parameters.Quantity; for (int i = 0; i < decimalPrices.Length; i++) { var price = decimalPrices[i]; security.Object.SetMarketPrice(new Tick(DateTime.Now, security.Object.Symbol, price, price)); security.Setup((m => m.Invested)).Returns(parameters.InvestedArray[i]); var targets = algorithm.RiskManagement.ManageRisk(algorithm, null).ToList(); var shouldLiquidate = parameters.ShouldLiquidateArray[i]; if (shouldLiquidate) { Assert.AreEqual(1, targets.Count); Assert.AreEqual(Symbols.AAPL, targets[0].Symbol); Assert.AreEqual(0, targets[0].Quantity); } else { Assert.AreEqual(0, targets.Count); } if (shouldLiquidate || parameters.ChangePosition[i]) { // Go from long to short or viceversa holding.SetHoldings(price, quantity = -quantity); } } }
public void ReturnsExpectedPortfolioTarget( Language language, decimal maxDrawdownPercent, bool[] investedArray, double[] uppInputsDouble, bool[] shouldLiquidateArray) { var unrealizedProfitPercentArray = System.Array.ConvertAll(uppInputsDouble, x => (decimal)x); var security = new Mock <Equity>( Symbols.AAPL, SecurityExchangeHours.AlwaysOpen(TimeZones.NewYork), new Cash(Currencies.USD, 0, 1), SymbolProperties.GetDefault(Currencies.USD), ErrorCurrencyConverter.Instance, RegisteredSecurityDataTypesProvider.Null, new SecurityCache(), Exchange.UNKNOWN ); var holding = new Mock <EquityHolding>(security.Object, new IdentityCurrencyConverter(Currencies.USD)); var algorithm = new QCAlgorithm(); algorithm.SetPandasConverter(); algorithm.Securities.Add(Symbols.AAPL, security.Object); if (language == Language.Python) { using (Py.GIL()) { const string name = nameof(TrailingStopRiskManagementModel); var instance = Py.Import(name).GetAttr(name).Invoke(maxDrawdownPercent.ToPython()); var model = new RiskManagementModelPythonWrapper(instance); algorithm.SetRiskManagement(model); } } else { var model = new TrailingStopRiskManagementModel(maxDrawdownPercent); algorithm.SetRiskManagement(model); } for (int i = 0; i < investedArray.Length; i++) { var invested = investedArray[i]; var unrealizedProfitPercent = unrealizedProfitPercentArray[i]; var shouldLiquidate = shouldLiquidateArray[i]; security.Setup(m => m.Invested).Returns(invested); holding.Setup(m => m.UnrealizedProfitPercent).Returns(unrealizedProfitPercent); security.Object.Holdings = holding.Object; var targets = algorithm.RiskManagement.ManageRisk(algorithm, null).ToList(); if (shouldLiquidate) { Assert.AreEqual(1, targets.Count); Assert.AreEqual(Symbols.AAPL, targets[0].Symbol); Assert.AreEqual(0, targets[0].Quantity); } else { Assert.AreEqual(0, targets.Count); } } }