예제 #1
0
        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 = SecurityChanges.Added(_algorithm.Securities.Values.ToArray());

            _algorithm.PortfolioConstruction.OnSecuritiesChanged(_algorithm, changes);
        }
예제 #2
0
        private void SetPortfolioConstruction(Language language)
        {
            _algorithm.SetPortfolioConstruction(new BLOPCM(new UnconstrainedMeanVariancePortfolioOptimizer()));
            if (language == Language.Python)
            {
                try
                {
                    using (Py.GIL())
                    {
                        var name     = nameof(BLOPCM);
                        var instance = PythonEngine.ModuleFromString(name, GetPythonBLOPCM()).GetAttr(name).Invoke();
                        var model    = new PortfolioConstructionModelPythonWrapper(instance);
                        _algorithm.SetPortfolioConstruction(model);
                    }
                }
                catch (Exception e)
                {
                    Assert.Ignore(e.Message);
                }
            }

            var changes = SecurityChanges.Added(_algorithm.Securities.Values.ToList().ToArray());

            _algorithm.PortfolioConstruction.OnSecuritiesChanged(_algorithm, changes);
        }
예제 #3
0
        /// <summary>
        /// Adds a new subscription to provide data for the specified security.
        /// </summary>
        /// <param name="universe">The universe the subscription is to be added to</param>
        /// <param name="security">The security to add a subscription for</param>
        /// <param name="config">The subscription config to be added</param>
        /// <param name="utcStartTime">The start time of the subscription</param>
        /// <param name="utcEndTime">The end time of the subscription</param>
        /// <returns>True if the subscription was created and added successfully, false otherwise</returns>
        public bool AddSubscription(Universe universe, Security security, SubscriptionDataConfig config, DateTime utcStartTime, DateTime utcEndTime)
        {
            // create and add the subscription to our collection
            var subscription = CreateSubscription(universe, security, config, utcStartTime, utcEndTime);

            // for some reason we couldn't create the subscription
            if (subscription == null)
            {
                Log.Trace("Unable to add subscription for: " + security.Symbol.ToString());
                return(false);
            }

            Log.Trace("LiveTradingDataFeed.AddSubscription(): Added " + security.Symbol.ToString());

            _subscriptions.TryAdd(subscription);

            // send the subscription for the new symbol through to the data queuehandler
            // unless it is custom data, custom data is retrieved using the same as backtest
            if (!subscription.Configuration.IsCustomData)
            {
                _dataQueueHandler.Subscribe(_job, new[] { security.Symbol });
            }

            // keep track of security changes, we emit these to the algorithm
            // as notifications, used in universe selection
            _changes += SecurityChanges.Added(security);

            UpdateFillForwardResolution();

            return(true);
        }
예제 #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 = SecurityChanges.Added(_algorithm.Securities.Values.ToArray());

            algorithm.PortfolioConstruction.OnSecuritiesChanged(_algorithm, changes);
        }
예제 #5
0
        private void SetPortfolioConstruction(Language language, QCAlgorithm algorithm)
        {
            algorithm.SetPortfolioConstruction(new InsightWeightingPortfolioConstructionModel());
            if (language == Language.Python)
            {
                using (Py.GIL())
                {
                    var name     = nameof(InsightWeightingPortfolioConstructionModel);
                    var instance = Py.Import(name).GetAttr(name).Invoke();
                    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 = SecurityChanges.Added(_algorithm.Securities.Values.ToArray());

            algorithm.PortfolioConstruction.OnSecuritiesChanged(_algorithm, changes);
        }
        private void SetPortfolioConstruction(Language language)
        {
            _algorithm.SetPortfolioConstruction(new EqualWeightingPortfolioConstructionModel());
            if (language == Language.Python)
            {
                try
                {
                    using (Py.GIL())
                    {
                        var name     = nameof(EqualWeightingPortfolioConstructionModel);
                        var instance = Py.Import(name).GetAttr(name).Invoke();
                        var model    = new PortfolioConstructionModelPythonWrapper(instance);
                        _algorithm.SetPortfolioConstruction(model);
                    }
                }
                catch (Exception e)
                {
                    Assert.Ignore(e.Message);
                }
            }

            var changes = SecurityChanges.Added(_algorithm.Securities.Values.ToArray());

            _algorithm.PortfolioConstruction.OnSecuritiesChanged(_algorithm, changes);
        }
예제 #7
0
        /// <summary>
        /// Adds a new subscription to provide data for the specified security.
        /// </summary>
        /// <param name="universe">The universe the subscription is to be added to</param>
        /// <param name="security">The security to add a subscription for</param>
        /// <param name="utcStartTime">The start time of the subscription</param>
        /// <param name="utcEndTime">The end time of the subscription</param>
        public bool AddSubscription(Universe universe, Security security, DateTime utcStartTime, DateTime utcEndTime)
        {
            _fillForwardResolution.Value = ResolveFillForwardResolution(_algorithm);

            var subscription = CreateSubscription(universe, _resultHandler, security, utcStartTime, utcEndTime, _fillForwardResolution);

            if (subscription == null)
            {
                // subscription will be null when there's no tradeable dates for the security between the requested times, so
                // don't even try to load the data
                return(false);
            }

            Log.Debug("FileSystemDataFeed.AddSubscription(): Added " + security.Symbol.ID + " Start: " + utcStartTime + " End: " + utcEndTime);

            _subscriptions.AddOrUpdate(subscription.Security.Symbol, subscription);

            // prime the pump, run method checks current before move next calls
            //PrimeSubscriptionPump(subscription, true);

            _changes += SecurityChanges.Added(security);

            _fillForwardResolution.Value = ResolveFillForwardResolution(_algorithm);

            return(true);
        }
예제 #8
0
        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 = SecurityChanges.Added(_algorithm.Securities.Values.ToArray());

            algorithm.PortfolioConstruction.OnSecuritiesChanged(_algorithm, changes);
        }
예제 #9
0
        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, SecurityChanges.Added(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, SecurityChanges.Added(aapl));

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

            Assert.DoesNotThrow(() => algorithm.PortfolioConstruction.CreateTargets(algorithm, insights));
        }
        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(SecurityChanges.Added(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);
        }
예제 #11
0
        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, SecurityChanges.Added(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);
        }
예제 #12
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 = SecurityChanges.Added(_algorithm.Securities.Values.ToArray());

            _algorithm.PortfolioConstruction.OnSecuritiesChanged(_algorithm, changes);
        }
예제 #13
0
        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(SecurityChanges.Added(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();
        }
        public void DailySampleValueBasedOnMarketHour(bool extendedMarketHoursEnabled)
        {
            var referenceDate = new DateTime(2020, 11, 25);
            var resultHandler = new TestLiveTradingResultHandler
            {
                // market is open
                InitialSampleTime = referenceDate.AddHours(10)
            };

            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(SecurityChanges.Added(aapl));

            algo.Portfolio.CashBook["USD"].AddAmount(1000);
            algo.Portfolio.InvalidateTotalPortfolioValue();
            resultHandler.Sample(referenceDate.AddHours(15));

            Assert.IsFalse(resultHandler.Charts.ContainsKey("Strategy Equity"), "Should not sample on the same start date");

            // will be ignored based on 'extendedMarketHoursEnabled'
            algo.Portfolio.CashBook["USD"].AddAmount(10000);
            algo.Portfolio.InvalidateTotalPortfolioValue();
            resultHandler.Sample(referenceDate.AddHours(22));

            Assert.IsFalse(resultHandler.Charts.ContainsKey("Strategy Equity"), "Should not sample on the same start date");

            resultHandler.Sample(referenceDate.AddHours(24));
            Assert.IsTrue(resultHandler.Charts.ContainsKey("Strategy Equity"), "Expect sample of date change");

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

            resultHandler.Exit();
        }
        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 = SecurityChanges.Added(Algorithm.Securities.Values.ToArray());

            Algorithm.PortfolioConstruction.OnSecuritiesChanged(Algorithm, changes);
        }
예제 #16
0
        /// <summary>
        /// Adds a new subscription to provide data for the specified security.
        /// </summary>
        /// <param name="request">Defines the subscription to be added, including start/end times the universe and security</param>
        /// <returns>True if the subscription was created and added successfully, false otherwise</returns>
        public bool AddSubscription(SubscriptionRequest request)
        {
            if (_subscriptions.Contains(request.Configuration))
            {
                // duplicate subscription request
                return(false);
            }

            // create and add the subscription to our collection
            var subscription = request.IsUniverseSubscription
                ? CreateUniverseSubscription(request)
                : CreateSubscription(request);

            // for some reason we couldn't create the subscription
            if (subscription == null)
            {
                Log.Trace("Unable to add subscription for: " + request.Configuration);
                return(false);
            }

            Log.Trace("LiveTradingDataFeed.AddSubscription(): Added " + request.Configuration);

            _subscriptions.TryAdd(subscription);

            // send the subscription for the new symbol through to the data queuehandler
            // unless it is custom data, custom data is retrieved using the same as backtest
            if (!subscription.Configuration.IsCustomData)
            {
                _dataQueueHandler.Subscribe(_job, new[] { request.Security.Symbol });
            }

            // keep track of security changes, we emit these to the algorithm
            // as notifications, used in universe selection
            if (!request.IsUniverseSubscription)
            {
                _changes += SecurityChanges.Added(request.Security);
            }

            UpdateFillForwardResolution();

            return(true);
        }
예제 #17
0
        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, SecurityChanges.Added(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]));
        }
예제 #18
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, SecurityChanges.Added(security));

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

            Assert.DoesNotThrow(() => algorithm.PortfolioConstruction.CreateTargets(algorithm, insights));
        }
예제 #19
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, SecurityChanges.Added(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));
            }
        }
예제 #20
0
        /// <summary>
        /// Adds a new subscription to provide data for the specified security.
        /// </summary>
        /// <param name="universe">The universe the subscription is to be added to</param>
        /// <param name="security">The security to add a subscription for</param>
        /// <param name="utcStartTime">The start time of the subscription</param>
        /// <param name="utcEndTime">The end time of the subscription</param>
        /// <returns>True if the subscription was created and added successfully, false otherwise</returns>
        public bool AddSubscription(Universe universe, Security security, DateTime utcStartTime, DateTime utcEndTime)
        {
            // create and add the subscription to our collection
            var subscription = CreateSubscription(universe, security, utcStartTime, utcEndTime);

            // for some reason we couldn't create the subscription
            if (subscription == null)
            {
                Log.Trace("Unable to add subscription for: " + security.Symbol);
                return(false);
            }

            Log.Trace("LiveTradingDataFeed.AddSubscription(): Added " + security.Symbol);

            _subscriptions[new SymbolSecurityType(subscription)] = subscription;

            // send the subscription for the new symbol through to the data queuehandler
            // unless it is custom data, custom data is retrieved using the same as backtest
            if (!subscription.Configuration.IsCustomData)
            {
                _dataQueueHandler.Subscribe(_job, new Dictionary <SecurityType, List <string> >
                {
                    { security.Type, new List <string> {
                          security.Symbol
                      } }
                });
            }

            // keep track of security changes, we emit these to the algorithm
            // as notifications, used in universe selection
            _changes += SecurityChanges.Added(security);

            // update our fill forward resolution setting
            _fillForwardResolution.Value = ResolveFillForwardResolution(_algorithm);

            return(true);
        }
        public void NoRebalanceFunction()
        {
            PortfolioConstructionModel.RebalanceOnSecurityChanges = false;

            var constructionModel = new TestPortfolioConstructionModel();

            Assert.IsTrue(constructionModel.IsRebalanceDueWrapper(new DateTime(2020, 1, 1), new Insight[0]));
            Assert.IsTrue(constructionModel.IsRebalanceDueWrapper(new DateTime(2019, 1, 1), 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, SecurityChanges.Added(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]));

            PortfolioConstructionModel.RebalanceOnSecurityChanges = true;
        }
        public void PythonDoesNotImplementDetermineTargetPercent()
        {
            var algorithm = new AlgorithmStub();

            using (Py.GIL())
            {
                dynamic model = PythonEngine.ModuleFromString(
                    "TestPCM",
                    @"

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

from QuantConnect.Algorithm.Framework.Portfolio import *

class PyPCM(EqualWeightingPortfolioConstructionModel):
    def __init__(self):
        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(SecurityChanges.Added(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);
            }
        }