private void SetPortfolioConstruction(Language language, PortfolioBias portfolioBias = PortfolioBias.LongShort)
        {
            _algorithm.SetPortfolioConstruction(new BLOPCM(new UnconstrainedMeanVariancePortfolioOptimizer(), portfolioBias));
            if (language == Language.Python)
            {
                try
                {
                    using (Py.GIL())
                    {
                        var name     = nameof(BLOPCM);
                        var instance = PythonEngine.ModuleFromString(name, GetPythonBLOPCM()).GetAttr(name).Invoke(((int)portfolioBias).ToPython());
                        var model    = new PortfolioConstructionModelPythonWrapper(instance);
                        _algorithm.SetPortfolioConstruction(model);
                    }
                }
                catch (Exception e)
                {
                    Assert.Ignore(e.Message);
                }
            }

            var changes = SecurityChangesTests.AddedNonInternal(_algorithm.Securities.Values.ToList().ToArray());

            _algorithm.PortfolioConstruction.OnSecuritiesChanged(_algorithm, changes);
        }
        private void SetPortfolioConstruction(Language language)
        {
            _algorithm.SetCurrentSlice(null);
            IPortfolioConstructionModel model;

            if (language == Language.CSharp)
            {
                model = new EqualWeightingAlphaStreamsPortfolioConstructionModel();
            }
            else
            {
                throw new NotImplementedException($"{language} not implemented");
            }
            _algorithm.SetPortfolioConstruction(model);

            foreach (var kvp in _algorithm.Portfolio)
            {
                kvp.Value.SetHoldings(kvp.Value.Price, 0);
            }
            _algorithm.Portfolio.SetCash(100000);
            SetUtcTime(new DateTime(2018, 4, 5));

            var changes = SecurityChangesTests.AddedNonInternal(_algorithm.Securities.Values.ToArray());

            _algorithm.PortfolioConstruction.OnSecuritiesChanged(_algorithm, changes);
        }
        public void NewSymbolPortfolioConstructionModelDoesNotThrow()
        {
            var algorithm = new QCAlgorithm();
            var timezone  = algorithm.TimeZone;

            algorithm.SetDateTime(new DateTime(2018, 8, 7).ConvertToUtc(timezone));
            algorithm.SetPortfolioConstruction(new NewSymbolPortfolioConstructionModel());

            var spySymbol = Symbols.SPY;
            var spy       = GetSecurity(spySymbol, Resolution.Daily);

            spy.SetMarketPrice(new Tick(algorithm.Time, spySymbol, 1m, 1m));
            algorithm.Securities.Add(spySymbol, spy);

            algorithm.PortfolioConstruction.OnSecuritiesChanged(algorithm, SecurityChangesTests.AddedNonInternal(spy));

            var insights = new[] { Insight.Price(spySymbol, Time.OneMinute, InsightDirection.Up, .1) };

            Assert.DoesNotThrow(() => algorithm.PortfolioConstruction.CreateTargets(algorithm, insights));

            algorithm.SetDateTime(algorithm.Time.AddDays(1));

            var aaplSymbol = Symbols.AAPL;
            var aapl       = GetSecurity(spySymbol, Resolution.Daily);

            aapl.SetMarketPrice(new Tick(algorithm.Time, aaplSymbol, 1m, 1m));
            algorithm.Securities.Add(aaplSymbol, aapl);

            algorithm.PortfolioConstruction.OnSecuritiesChanged(algorithm, SecurityChangesTests.AddedNonInternal(aapl));

            insights = new[] { spySymbol, aaplSymbol }
            .Select(x => Insight.Price(x, Time.OneMinute, InsightDirection.Up, .1)).ToArray();

            Assert.DoesNotThrow(() => algorithm.PortfolioConstruction.CreateTargets(algorithm, insights));
        }
示例#4
0
        private void SetPortfolioConstruction(Language language, QCAlgorithm algorithm, dynamic paramenter = null)
        {
            paramenter = paramenter ?? Resolution.Daily;
            algorithm.SetPortfolioConstruction(new ConfidenceWeightedPortfolioConstructionModel(paramenter));
            if (language == Language.Python)
            {
                using (Py.GIL())
                {
                    var name     = nameof(ConfidenceWeightedPortfolioConstructionModel);
                    var instance = Py.Import(name).GetAttr(name).Invoke(((object)paramenter).ToPython());
                    var model    = new PortfolioConstructionModelPythonWrapper(instance);
                    algorithm.SetPortfolioConstruction(model);
                }
            }

            foreach (var kvp in _algorithm.Portfolio)
            {
                kvp.Value.SetHoldings(kvp.Value.Price, 0);
            }
            _algorithm.Portfolio.SetCash(_startingCash);
            SetUtcTime(new DateTime(2018, 7, 31));

            var changes = SecurityChangesTests.AddedNonInternal(_algorithm.Securities.Values.ToArray());

            algorithm.PortfolioConstruction.OnSecuritiesChanged(_algorithm, changes);
        }
        private void SetPortfolioConstruction(Language language, QCAlgorithm algorithm, PortfolioBias bias = PortfolioBias.LongShort)
        {
            algorithm.SetPortfolioConstruction(new AccumulativeInsightPortfolioConstructionModel((Func <DateTime, DateTime>)null, bias));
            if (language == Language.Python)
            {
                using (Py.GIL())
                {
                    var name     = nameof(AccumulativeInsightPortfolioConstructionModel);
                    var instance = Py.Import(name).GetAttr(name).Invoke(((object)null).ToPython(), ((int)bias).ToPython());
                    var model    = new PortfolioConstructionModelPythonWrapper(instance);
                    algorithm.SetPortfolioConstruction(model);
                }
            }

            foreach (var kvp in _algorithm.Portfolio)
            {
                kvp.Value.SetHoldings(kvp.Value.Price, 0);
            }
            _algorithm.Portfolio.SetCash(_startingCash);
            SetUtcTime(new DateTime(2018, 7, 31));

            var changes = SecurityChangesTests.AddedNonInternal(_algorithm.Securities.Values.ToArray());

            algorithm.PortfolioConstruction.OnSecuritiesChanged(_algorithm, changes);
        }
        public void MultipleAlphaPositionAggregation(Language language, decimal totalPortfolioValueAlpha1, decimal totalPortfolioValueAlpha2, decimal freePortfolioValue)
        {
            SetPortfolioConstruction(language);

            _algorithm.Settings.FreePortfolioValue = freePortfolioValue;
            var alpha1 = _algorithm.AddData <AlphaStreamsPortfolioState>("9fc8ef73792331b11dbd5429a");
            var alpha2 = _algorithm.AddData <AlphaStreamsPortfolioState>("623b06b231eb1cc1aa3643a46");

            _algorithm.OnFrameworkSecuritiesChanged(SecurityChangesTests.AddedNonInternal(alpha1, alpha2));
            var symbol  = alpha1.Symbol;
            var symbol2 = alpha2.Symbol;
            var data    = _algorithm.History <AlphaStreamsPortfolioState>(symbol, TimeSpan.FromDays(1)).Last();

            AddSecurities(_algorithm, data);
            data.TotalPortfolioValue = totalPortfolioValueAlpha1;
            var position = data.PositionGroups.Single().Positions.Single();

            var data2 = (AlphaStreamsPortfolioState)data.Clone();

            data2.Symbol = symbol2;
            data2.TotalPortfolioValue = totalPortfolioValueAlpha2;
            data2.PositionGroups      =
                new List <PositionGroupState>
            {
                new PositionGroupState {
                    Positions =
                        new List <PositionState>
                    {
                        new PositionState
                        {
                            Quantity     = position.Quantity * -10,
                            Symbol       = position.Symbol,
                            UnitQuantity = 1
                        }
                    }
                }
            };

            _algorithm.SetCurrentSlice(new Slice(_algorithm.UtcTime, new List <BaseData> {
                data, data2
            }));

            var targets = _algorithm.PortfolioConstruction.CreateTargets(_algorithm, Array.Empty <Insight>()).ToList();

            Assert.AreEqual(1, targets.Count);
            Assert.AreEqual(position.Symbol, targets.Single().Symbol);

            var tvpPerAlpha  = (_algorithm.Portfolio.TotalPortfolioValue - freePortfolioValue) * 0.5m;
            var alpha1Weight = tvpPerAlpha / data.TotalPortfolioValue;
            var alpha2Weight = tvpPerAlpha / data2.TotalPortfolioValue;

            Assert.AreEqual((position.Quantity * alpha1Weight).DiscretelyRoundBy(1, MidpointRounding.ToZero)
                            + (position.Quantity * -10m * alpha2Weight).DiscretelyRoundBy(1, MidpointRounding.ToZero),
                            targets.Single().Quantity);
        }
        public void PortfolioBiasIsRespected(Language language, PortfolioBias bias)
        {
            SetPortfolioConstruction(language, _algorithm, bias);
            var now = new DateTime(2018, 7, 31);

            SetUtcTime(now.ConvertFromUtc(_algorithm.TimeZone));
            var appl = _algorithm.AddEquity("AAPL");

            appl.SetMarketPrice(new Tick(now, appl.Symbol, 10, 10));

            var spy = _algorithm.AddEquity("SPY");

            spy.SetMarketPrice(new Tick(now, spy.Symbol, 20, 20));

            var ibm = _algorithm.AddEquity("IBM");

            ibm.SetMarketPrice(new Tick(now, ibm.Symbol, 30, 30));

            var aig = _algorithm.AddEquity("AIG");

            aig.SetMarketPrice(new Tick(now, aig.Symbol, 30, 30));

            var qqq = _algorithm.AddEquity("QQQ");

            qqq.SetMarketPrice(new Tick(now, qqq.Symbol, 30, 30));

            var insights = new[]
            {
                new Insight(now, appl.Symbol, TimeSpan.FromDays(1), InsightType.Price, InsightDirection.Up, 0.1d, null),
                new Insight(now, spy.Symbol, TimeSpan.FromDays(1), InsightType.Price, InsightDirection.Down, -0.1d, null),
                new Insight(now, ibm.Symbol, TimeSpan.FromDays(1), InsightType.Price, InsightDirection.Down, 0d, null),
                new Insight(now, aig.Symbol, TimeSpan.FromDays(1), InsightType.Price, InsightDirection.Down, -0.1d, null),
                new Insight(now, qqq.Symbol, TimeSpan.FromDays(1), InsightType.Price, InsightDirection.Up, 0.1d, null)
            };

            _algorithm.PortfolioConstruction.OnSecuritiesChanged(_algorithm, SecurityChangesTests.AddedNonInternal(appl, spy, ibm, aig, qqq));

            var createdValidTarget = false;

            foreach (var target in _algorithm.PortfolioConstruction.CreateTargets(_algorithm, insights))
            {
                QuantConnect.Logging.Log.Trace($"{target.Symbol}: {target.Quantity}");
                if (target.Quantity == 0)
                {
                    continue;
                }

                createdValidTarget = true;
                Assert.AreEqual(Math.Sign((int)bias), Math.Sign(target.Quantity));
            }

            Assert.IsTrue(createdValidTarget);
        }
示例#8
0
        protected void SetPortfolioConstruction(Language language, PortfolioBias bias)
        {
            var model = GetPortfolioConstructionModel(language, Resolution.Daily, bias);

            _algorithm.SetPortfolioConstruction(model);

            foreach (var kvp in _algorithm.Portfolio)
            {
                kvp.Value.SetHoldings(kvp.Value.Price, 0);
            }

            var changes = SecurityChangesTests.AddedNonInternal(_algorithm.Securities.Values.ToArray());

            _algorithm.PortfolioConstruction.OnSecuritiesChanged(_algorithm, changes);
        }
        public void DailySampleValueBasedOnMarketHour(bool extendedMarketHoursEnabled)
        {
            var referenceDate = new DateTime(2020, 11, 25);
            var resultHandler = new LiveTradingResultHandler();

            resultHandler.Initialize(new LiveNodePacket(),
                                     new QuantConnect.Messaging.Messaging(),
                                     new Api.Api(),
                                     new BacktestingTransactionHandler());

            var algo = new AlgorithmStub(createDataManager: false);

            algo.SetFinishedWarmingUp();
            var dataManager = new DataManagerStub(new TestDataFeed(), algo);

            algo.SubscriptionManager.SetDataManager(dataManager);
            var aapl = algo.AddEquity("AAPL", extendedMarketHours: extendedMarketHoursEnabled);

            algo.PostInitialize();
            resultHandler.SetAlgorithm(algo, 100000);
            resultHandler.OnSecuritiesChanged(SecurityChangesTests.AddedNonInternal(aapl));

            // Add values during market hours, should always update
            algo.Portfolio.CashBook["USD"].AddAmount(1000);
            algo.Portfolio.InvalidateTotalPortfolioValue();

            resultHandler.Sample(referenceDate.AddHours(15));
            Assert.IsTrue(resultHandler.Charts.ContainsKey("Strategy Equity"));
            Assert.AreEqual(1, resultHandler.Charts["Strategy Equity"].Series["Equity"].Values.Count);

            var currentEquityValue = resultHandler.Charts["Strategy Equity"].Series["Equity"].Values.Last().y;

            Assert.AreEqual(101000, currentEquityValue);

            // Add value to portfolio, see if portfolio updates with new sample
            // will be changed to 'extendedMarketHoursEnabled' = true
            algo.Portfolio.CashBook["USD"].AddAmount(10000);
            algo.Portfolio.InvalidateTotalPortfolioValue();

            resultHandler.Sample(referenceDate.AddHours(22));
            Assert.AreEqual(2, resultHandler.Charts["Strategy Equity"].Series["Equity"].Values.Count);

            currentEquityValue = resultHandler.Charts["Strategy Equity"].Series["Equity"].Values.Last().y;
            Assert.AreEqual(extendedMarketHoursEnabled ? 111000 : 101000, currentEquityValue);

            resultHandler.Exit();
        }
示例#10
0
        protected void SetPortfolioConstruction(Language language, dynamic paramenter = null)
        {
            var model = GetPortfolioConstructionModel(language, paramenter ?? Resolution.Daily);

            Algorithm.SetPortfolioConstruction(model);

            foreach (var kvp in Algorithm.Portfolio)
            {
                kvp.Value.SetHoldings(kvp.Value.Price, 0);
            }
            Algorithm.Portfolio.SetCash(StartingCash);
            SetUtcTime(new DateTime(2018, 7, 31));

            var changes = SecurityChangesTests.AddedNonInternal(Algorithm.Securities.Values.ToArray());

            Algorithm.PortfolioConstruction.OnSecuritiesChanged(Algorithm, changes);
        }
        public void NoRebalanceFunction(Language language)
        {
            TestPortfolioConstructionModel constructionModel;

            if (language == Language.Python)
            {
                constructionModel = new TestPortfolioConstructionModel();
                using (Py.GIL())
                {
                    var func = PythonEngine.ModuleFromString(
                        "RebalanceFunc",
                        @"
from datetime import timedelta

def RebalanceFunc():
    return None"
                        ).GetAttr("RebalanceFunc");
                    constructionModel.SetRebalancingFunc(func.Invoke());
                }
            }
            else
            {
                constructionModel = new TestPortfolioConstructionModel();
            }

            Assert.IsTrue(constructionModel.IsRebalanceDueWrapper(new DateTime(2020, 1, 1), new Insight[0]));
            Assert.IsTrue(constructionModel.IsRebalanceDueWrapper(new DateTime(2020, 1, 2), new Insight[0]));
            Assert.IsTrue(constructionModel.IsRebalanceDueWrapper(new DateTime(2020, 1, 3), new Insight[0]));

            var security = new Security(Symbols.SPY,
                                        SecurityExchangeHours.AlwaysOpen(DateTimeZone.Utc),
                                        new Cash(Currencies.USD, 1, 1),
                                        SymbolProperties.GetDefault(Currencies.USD),
                                        new IdentityCurrencyConverter(Currencies.USD),
                                        new RegisteredSecurityDataTypesProvider(),
                                        new SecurityCache());

            constructionModel.OnSecuritiesChanged(null, SecurityChangesTests.AddedNonInternal(security));
            Assert.IsTrue(constructionModel.IsRebalanceDueWrapper(new DateTime(2020, 1, 1), new Insight[0]));
            constructionModel.OnSecuritiesChanged(null, SecurityChanges.None);
            Assert.IsTrue(constructionModel.IsRebalanceDueWrapper(new DateTime(2020, 1, 1), new Insight[0]));
        }
示例#12
0
        public void PortfolioBiasIsRespected(Language language, PortfolioBias bias)
        {
            SetPortfolioConstruction(language, bias);

            var appl = _algorithm.AddEquity("AAPL");
            var bac  = _algorithm.AddEquity("BAC");
            var spy  = _algorithm.AddEquity("SPY");
            var ibm  = _algorithm.AddEquity("IBM");
            var aig  = _algorithm.AddEquity("AIG");
            var qqq  = _algorithm.AddEquity("QQQ");

            foreach (var equity in new[] { qqq, aig, ibm, spy, bac, appl })
            {
                equity.SetMarketPrice(new Tick(_nowUtc, equity.Symbol, 30, 30));
            }

            var insights = new[]
            {
                new Insight(_nowUtc, appl.Symbol, TimeSpan.FromDays(1), InsightType.Price, InsightDirection.Up, -0.1d, null),
                new Insight(_nowUtc, spy.Symbol, TimeSpan.FromDays(1), InsightType.Price, InsightDirection.Down, -0.1d, null),
                new Insight(_nowUtc, ibm.Symbol, TimeSpan.FromDays(1), InsightType.Price, InsightDirection.Down, 0d, null),
                new Insight(_nowUtc, aig.Symbol, TimeSpan.FromDays(1), InsightType.Price, InsightDirection.Down, -0.1d, null),
                new Insight(_nowUtc, qqq.Symbol, TimeSpan.FromDays(1), InsightType.Price, InsightDirection.Up, -0.1d, null),
                new Insight(_nowUtc, bac.Symbol, TimeSpan.FromDays(1), InsightType.Price, InsightDirection.Down, -0.1d, null)
            };

            _algorithm.PortfolioConstruction.OnSecuritiesChanged(_algorithm, SecurityChangesTests.AddedNonInternal(appl, spy, ibm, aig, qqq, bac));

            foreach (var target in _algorithm.PortfolioConstruction.CreateTargets(_algorithm, insights))
            {
                QuantConnect.Logging.Log.Trace($"{target.Symbol}: {target.Quantity}");
                if (target.Quantity == 0)
                {
                    continue;
                }
                Assert.AreEqual(Math.Sign((int)bias), Math.Sign(target.Quantity));
            }
        }
示例#13
0
        public void DuplicateKeyPortfolioConstructionModelDoesNotThrow()
        {
            var algorithm = new QCAlgorithm();
            var timezone  = algorithm.TimeZone;

            algorithm.SetDateTime(new DateTime(2018, 8, 7).ConvertToUtc(timezone));
            algorithm.SetPortfolioConstruction(new DuplicateKeyPortfolioConstructionModel());

            var symbol = Symbols.SPY;

            var security = new Security(
                SecurityExchangeHours.AlwaysOpen(timezone),
                new SubscriptionDataConfig(
                    typeof(TradeBar),
                    symbol,
                    Resolution.Daily,
                    timezone,
                    timezone,
                    true,
                    false,
                    false
                    ),
                new Cash(Currencies.USD, 0, 1),
                SymbolProperties.GetDefault(Currencies.USD),
                ErrorCurrencyConverter.Instance,
                RegisteredSecurityDataTypesProvider.Null,
                new SecurityCache()
                );

            security.SetMarketPrice(new Tick(algorithm.Time, symbol, 1m, 1m));
            algorithm.Securities.Add(symbol, security);

            algorithm.PortfolioConstruction.OnSecuritiesChanged(algorithm, SecurityChangesTests.AddedNonInternal(security));

            var insights = new[] { Insight.Price(symbol, Time.OneMinute, InsightDirection.Up, .1) };

            Assert.DoesNotThrow(() => algorithm.PortfolioConstruction.CreateTargets(algorithm, insights));
        }
        public void PythonDoesNotImplementDetermineTargetPercent()
        {
            var algorithm = new AlgorithmStub();

            using (Py.GIL())
            {
                dynamic model = PyModule.FromString(
                    "TestPCM",
                    @"

from clr import AddReference
AddReference(""QuantConnect.Algorithm.Framework"")

from QuantConnect.Algorithm.Framework.Portfolio import *

class PyPCM(EqualWeightingPortfolioConstructionModel):
    def __init__(self):
        super().__init__()
        self.CreateTargets_WasCalled = False
        self.OnSecuritiesChanged_WasCalled = False
        self.ShouldCreateTargetForInsight_WasCalled = False
        self.IsRebalanceDue_WasCalled = False
        self.GetTargetInsights_WasCalled = False

    def CreateTargets(self, algorithm, insights):
        self.CreateTargets_WasCalled = True
        return super().CreateTargets(algorithm, insights)

    def OnSecuritiesChanged(self, algorithm, changes):
        self.OnSecuritiesChanged_WasCalled = True
        super().OnSecuritiesChanged(algorithm, changes)

    def ShouldCreateTargetForInsight(self, insight):
        self.ShouldCreateTargetForInsight_WasCalled = True
        return super().ShouldCreateTargetForInsight(insight)

    def IsRebalanceDue(self, insights, algorithmUtc):
        self.IsRebalanceDue_WasCalled = True
        return True

    def GetTargetInsights(self):
        self.GetTargetInsights_WasCalled = True
        return super().GetTargetInsights()
"
                    ).GetAttr("PyPCM").Invoke();

                var now          = new DateTime(2020, 1, 10);
                var wrappedModel = new PortfolioConstructionModelPythonWrapper(model);
                var aapl         = algorithm.AddEquity("AAPL");
                aapl.SetMarketPrice(new Tick(now, aapl.Symbol, 10, 10));
                algorithm.SetDateTime(now);

                wrappedModel.OnSecuritiesChanged(algorithm, new SecurityChanges(SecurityChangesTests.AddedNonInternal(aapl)));
                Assert.IsTrue((bool)model.OnSecuritiesChanged_WasCalled);

                var insight = new Insight(now, aapl.Symbol, TimeSpan.FromDays(1), InsightType.Price, InsightDirection.Down, null, null);
                var result  = wrappedModel.CreateTargets(algorithm, new[] { insight }).ToList();
                Assert.AreEqual(1, result.Count);
                Assert.IsTrue((bool)model.CreateTargets_WasCalled);
                Assert.IsTrue((bool)model.GetTargetInsights_WasCalled);
                Assert.IsTrue((bool)model.IsRebalanceDue_WasCalled);
                Assert.IsTrue((bool)model.ShouldCreateTargetForInsight_WasCalled);
            }
        }