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);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Initialise the data and resolution required, as well as the cash and start-end dates for your algorithm. All algorithms must initialized.
        /// </summary>
        public override void Initialize()
        {
            // Set requested data resolution
            UniverseSettings.Resolution = Resolution.Minute;

            SetStartDate(2013, 10, 07);  //Set Start Date
            SetEndDate(2013, 10, 11);    //Set End Date
            SetCash(100000);             //Set Strategy Cash

            // Find more symbols here: http://quantconnect.com/data
            // Forex, CFD, Equities Resolutions: Tick, Second, Minute, Hour, Daily.
            // Futures Resolution: Tick, Second, Minute
            // Options Resolution: Minute Only.

            // set algorithm framework models
            UniverseSelection     = new ManualUniverseSelectionModel(QuantConnect.Symbol.Create("SPY", SecurityType.Equity, Market.USA));
            Alpha                 = new ConstantAlphaModel(InsightType.Price, InsightDirection.Up, TimeSpan.FromMinutes(20), 0.025, null);
            PortfolioConstruction = new EqualWeightingPortfolioConstructionModel();
            Execution             = new ImmediateExecutionModel();
            RiskManagement        = new MaximumDrawdownPercentPerSecurity(0.01m);
        }