Exemplo n.º 1
0
        public void Initialize()
        {
            var pythonPath = new DirectoryInfo("../../../Algorithm.Framework/Alphas");

            Environment.SetEnvironmentVariable("PYTHONPATH", pythonPath.FullName);

            _algorithm = new QCAlgorithmFramework();
            _algorithm.PortfolioConstruction = new NullPortfolioConstructionModel();
            _algorithm.HistoryProvider       = new SineHistoryProvider(_algorithm.Securities);
            _algorithm.SetStartDate(2018, 1, 4);
            _security = _algorithm.AddEquity(Symbols.SPY.Value, Resolution.Daily);
        }
        public void DoesNotReturnTargetsIfSecurityPriceIsZero(Language language)
        {
            var algorithm = new QCAlgorithmFramework();

            algorithm.AddEquity(Symbols.SPY.Value);
            algorithm.SetDateTime(DateTime.MinValue.ConvertToUtc(_algorithm.TimeZone));

            SetPortfolioConstruction(language, algorithm);

            var insights      = new[] { GetInsight(Symbols.SPY, InsightDirection.Up, algorithm.UtcTime) };
            var actualTargets = algorithm.PortfolioConstruction.CreateTargets(algorithm, insights);

            Assert.AreEqual(0, actualTargets.Count());
        }
Exemplo n.º 3
0
        public void SetsInsightGeneratedAndCloseTimes()
        {
            var eventFired = false;
            var algo       = new QCAlgorithmFramework();

            algo.SubscriptionManager.SetDataManager(new DataManagerStub(algo));
            algo.Transactions.SetOrderProcessor(new FakeOrderProcessor());
            algo.InsightsGenerated += (algorithm, data) =>
            {
                eventFired = true;
                var insights = data.Insights;
                Assert.AreEqual(1, insights.Count);
                Assert.IsTrue(insights.All(insight => insight.GeneratedTimeUtc != default(DateTime)));
                Assert.IsTrue(insights.All(insight => insight.CloseTimeUtc != default(DateTime)));
            };
            var security = algo.AddEquity("SPY");

            algo.SetUniverseSelection(new ManualUniverseSelectionModel());

            var alpha = new FakeAlpha();

            algo.SetAlpha(alpha);

            var construction = new FakePortfolioConstruction();

            algo.SetPortfolioConstruction(construction);

            var tick = new Tick
            {
                Symbol   = security.Symbol,
                Value    = 1,
                Quantity = 2
            };

            security.SetMarketPrice(tick);

            algo.OnFrameworkData(new Slice(new DateTime(2000, 01, 01), algo.Securities.Select(s => tick)));

            Assert.IsTrue(eventFired);
            Assert.AreEqual(1, construction.Insights.Count);
            Assert.IsTrue(construction.Insights.All(insight => insight.GeneratedTimeUtc != default(DateTime)));
            Assert.IsTrue(construction.Insights.All(insight => insight.CloseTimeUtc != default(DateTime)));
        }
Exemplo n.º 4
0
 /// <summary>
 /// Provides derived types a chance to initialize anything special they require
 /// </summary>
 protected virtual void InitializeAlgorithm(QCAlgorithmFramework algorithm)
 {
     _algorithm.SetStartDate(2018, 1, 4);
     _algorithm.AddEquity(Symbols.SPY.Value, Resolution.Daily);
 }
 protected override void InitializeAlgorithm(QCAlgorithmFramework algorithm)
 {
     algorithm.AddEquity("BAC");
     algorithm.AddEquity("AIG");
 }
Exemplo n.º 6
0
        public void OrdersAreSubmittedImmediatelyForTargetsToExecute(
            Language language,
            double[] historicalPrices,
            decimal openOrdersQuantity,
            int expectedOrdersSubmitted,
            decimal expectedTotalQuantity)
        {
            var actualOrdersSubmitted = new List <SubmitOrderRequest>();

            var time            = new DateTime(2018, 8, 2, 16, 0, 0);
            var historyProvider = new Mock <IHistoryProvider>();

            historyProvider.Setup(m => m.GetHistory(It.IsAny <IEnumerable <HistoryRequest> >(), It.IsAny <DateTimeZone>()))
            .Returns(historicalPrices.Select((x, i) =>
                                             new Slice(time.AddMinutes(i),
                                                       new List <BaseData>
            {
                new TradeBar
                {
                    Time   = time.AddMinutes(i),
                    Symbol = Symbols.AAPL,
                    Open   = Convert.ToDecimal(x),
                    High   = Convert.ToDecimal(x),
                    Low    = Convert.ToDecimal(x),
                    Close  = Convert.ToDecimal(x),
                    Volume = 100m
                }
            })));

            var algorithm = new QCAlgorithmFramework();

            algorithm.SubscriptionManager.SetDataManager(new DataManagerStub(algorithm));
            algorithm.SetPandasConverter();
            algorithm.SetHistoryProvider(historyProvider.Object);
            algorithm.SetDateTime(time.AddMinutes(5));

            var security = algorithm.AddEquity(Symbols.AAPL.Value);

            security.SetMarketPrice(new TradeBar {
                Value = 250
            });

            algorithm.SetFinishedWarmingUp();

            var orderProcessor = new Mock <IOrderProcessor>();

            orderProcessor.Setup(m => m.Process(It.IsAny <SubmitOrderRequest>()))
            .Returns((SubmitOrderRequest request) => new OrderTicket(algorithm.Transactions, request))
            .Callback((SubmitOrderRequest request) => actualOrdersSubmitted.Add(request));
            orderProcessor.Setup(m => m.GetOpenOrders(It.IsAny <Func <Order, bool> >()))
            .Returns(new List <Order> {
                new MarketOrder(Symbols.AAPL, openOrdersQuantity, DateTime.MinValue)
            });
            algorithm.Transactions.SetOrderProcessor(orderProcessor.Object);

            var model = GetExecutionModel(language);

            algorithm.SetExecution(model);

            var changes = new SecurityChanges(new[] { security }, Enumerable.Empty <Security>());

            model.OnSecuritiesChanged(algorithm, changes);

            var targets = new IPortfolioTarget[] { new PortfolioTarget(Symbols.AAPL, 10) };

            model.Execute(algorithm, targets);

            Assert.AreEqual(expectedOrdersSubmitted, actualOrdersSubmitted.Count);
            Assert.AreEqual(expectedTotalQuantity, actualOrdersSubmitted.Sum(x => x.Quantity));

            if (actualOrdersSubmitted.Count == 1)
            {
                var request = actualOrdersSubmitted[0];
                Assert.AreEqual(expectedTotalQuantity, request.Quantity);
                Assert.AreEqual(algorithm.UtcTime, request.Time);
            }
        }