Example #1
0
        public override void Initialize()
        {
            SetCash(1000);
            SetStartDate(Config.GetValue <DateTime>("startDate", new DateTime(2017, 6, 12)));
            SetEndDate(Config.GetValue <DateTime>("endDate", new DateTime(2017, 7, 22)));

            if (IsOutOfSampleRun)
            {
                var startDate = new DateTime(year: 2016, month: 1, day: 1);
                SetStartDate(startDate);
                SetEndDate(startDate.AddMonths(oosPeriod));
                RuntimeStatistics["ID"] = GetParameter("ID");
                SetParameters(config.ToDictionary(k => k.Key, v => v.Value.ToString()));
            }

            _symbol = AddSecurity(SecurityType.Crypto, "BTCUSD", Resolution.Tick, Market.GDAX, false, 1m, false).Symbol;
            SetBrokerageModel(QuantConnect.Brokerages.BrokerageName.GDAX, AccountType.Cash);
            var con = new TickConsolidator(new TimeSpan(1, 0, 0));

            SetBenchmark(_symbol);

            var factory = new SignalFactory(7);

            _entry = factory.Create(this, _symbol, true);
            _exit  = factory.Create(this, _symbol, false);
        }
        public void DoesNotConsolidateDifferentSymbols()
        {
            var consolidator = new TickConsolidator(2);

            var reference = DateTime.Today;

            var tick1 = new Tick
            {
                Symbol   = Symbols.AAPL,
                Time     = reference,
                BidPrice = 1000,
                BidSize  = 20,
                TickType = TickType.Quote,
            };

            var tick2 = new Tick
            {
                Symbol   = Symbols.ZNGA,
                Time     = reference,
                BidPrice = 20,
                BidSize  = 30,
                TickType = TickType.Quote,
            };

            consolidator.Update(tick1);

            Exception ex = Assert.Throws <InvalidOperationException>(() => consolidator.Update(tick2));

            Assert.That(ex.Message, Is.StringContaining("is not the same"));
        }
        public void ProcessesTradeTicksOnly()
        {
            TradeBar consolidated = null;
            var      consolidator = new TickConsolidator(TimeSpan.FromMinutes(1));

            consolidator.DataConsolidated += (sender, bar) =>
            {
                consolidated = bar;
            };

            var reference = new DateTime(2015, 06, 02);
            var tick1     = new Tick
            {
                Symbol = Symbols.SPY,
                Time   = reference.AddSeconds(3),
                Value  = 200m
            };

            consolidator.Update(tick1);
            Assert.IsNull(consolidated);

            var tick2 = new Tick
            {
                Symbol   = Symbols.SPY,
                Time     = reference.AddSeconds(10),
                Value    = 20000m,
                TickType = TickType.OpenInterest
            };

            consolidator.Update(tick2);
            Assert.IsNull(consolidated);

            var tick3 = new Tick
            {
                Symbol   = Symbols.SPY,
                Time     = reference.AddSeconds(10),
                Value    = 10000m,
                TickType = TickType.Quote
            };

            consolidator.Update(tick3);
            Assert.IsNull(consolidated);

            var tick4 = new Tick
            {
                Symbol = Symbols.SPY,
                Time   = reference.AddSeconds(61),
                Value  = 250m
            };

            consolidator.Update(tick4);
            Assert.IsNotNull(consolidated);

            Assert.AreEqual(consolidated.Time, reference);
            Assert.AreEqual(consolidated.Open, tick1.Value);
            Assert.AreEqual(consolidated.Close, tick1.Value);
        }
Example #4
0
        public void AggregatesNewTradeBarsProperly()
        {
            TradeBar newTradeBar = null;
            var consolidator = new TickConsolidator(4);
            consolidator.DataConsolidated += (sender, tradeBar) =>
            {
                newTradeBar = tradeBar;
            };
            var reference = DateTime.Today;
            var bar1 = new Tick
            {
                Symbol = "SPY",
                Time = reference,
                Value = 5,
                Quantity = 10
            };
            consolidator.Update(bar1);
            Assert.IsNull(newTradeBar);

            var bar2 = new Tick
            {
                Symbol = "SPY",
                Time = reference.AddHours(1),
                Value = 10,
                Quantity = 20
            };
            consolidator.Update(bar2);
            Assert.IsNull(newTradeBar);
            var bar3 = new Tick
            {
                Symbol = "SPY",
                Time = reference.AddHours(2),
                Value = 1,
                Quantity = 10
            };
            consolidator.Update(bar3);
            Assert.IsNull(newTradeBar);

            var bar4 = new Tick
            {
                Symbol = "SPY",
                Time = reference.AddHours(3),
                Value = 9,
                Quantity = 20
            };
            consolidator.Update(bar4);
            Assert.IsNotNull(newTradeBar);

            Assert.AreEqual("SPY", newTradeBar.Symbol);
            Assert.AreEqual(bar1.Time, newTradeBar.Time);
            Assert.AreEqual(bar1.Value, newTradeBar.Open);
            Assert.AreEqual(bar2.Value, newTradeBar.High);
            Assert.AreEqual(bar3.Value, newTradeBar.Low);
            Assert.AreEqual(bar4.Value, newTradeBar.Close);
            Assert.AreEqual(bar1.Quantity + bar2.Quantity + bar3.Quantity + bar4.Quantity, newTradeBar.Volume);
        }
Example #5
0
 public TradeTickAggregator(Resolution resolution)
     : base(resolution)
 {
     Consolidated = new List <BaseData>();
     Consolidator = new TickConsolidator(resolution.ToTimeSpan());
     Consolidator.DataConsolidated += (sender, consolidated) =>
     {
         Consolidated.Add(consolidated as TradeBar);
     };
 }
Example #6
0
 public KaikoTradeDataAggregator(Resolution resolution)
 {
     Resolution    = resolution;
     Consolidated  = new List <BaseData>();
     _consolidator = new TickConsolidator(resolution.ToTimeSpan());
     _consolidator.DataConsolidated += (sender, consolidated) =>
     {
         Consolidated.Add(consolidated);
     };
 }
Example #7
0
        /// <summary>
        /// Add new subscription to current <see cref="IDataAggregator"/> instance
        /// </summary>
        /// <param name="dataConfig">defines the parameters to subscribe to a data feed</param>
        /// <param name="newDataAvailableHandler">handler to be fired on new data available</param>
        /// <returns>The new enumerator for this subscription request</returns>
        public IEnumerator <BaseData> Add(SubscriptionDataConfig dataConfig, EventHandler newDataAvailableHandler)
        {
            IDataConsolidator consolidator;
            var period        = dataConfig.Resolution.ToTimeSpan();
            var isPeriodBased = false;

            switch (dataConfig.Type.Name)
            {
            case nameof(QuoteBar):
                isPeriodBased = dataConfig.Resolution != Resolution.Tick;
                consolidator  = new TickQuoteBarConsolidator(period);
                break;

            case nameof(TradeBar):
                isPeriodBased = dataConfig.Resolution != Resolution.Tick;
                consolidator  = new TickConsolidator(period);
                break;

            case nameof(OpenInterest):
                isPeriodBased = dataConfig.Resolution != Resolution.Tick;
                consolidator  = new OpenInterestConsolidator(period);
                break;

            case nameof(Tick):
                consolidator = FilteredIdentityDataConsolidator.ForTickType(dataConfig.TickType);
                break;

            case nameof(Split):
                consolidator = new IdentityDataConsolidator <Split>();
                break;

            case nameof(Dividend):
                consolidator = new IdentityDataConsolidator <Dividend>();
                break;

            default:
                // streaming custom data subscriptions can pass right through
                consolidator = new FilteredIdentityDataConsolidator <BaseData>(data => data.GetType() == dataConfig.Type);
                break;
            }

            var enumerator = new ScannableEnumerator <BaseData>(consolidator, dataConfig.ExchangeTimeZone, TimeProvider, newDataAvailableHandler, isPeriodBased);

            _enumerators.AddOrUpdate(
                dataConfig.Symbol.ID,
                new List <KeyValuePair <SubscriptionDataConfig, ScannableEnumerator <BaseData> > > {
                new KeyValuePair <SubscriptionDataConfig, ScannableEnumerator <BaseData> >(dataConfig, enumerator)
            },
                (k, v) => { return(v.Concat(new[] { new KeyValuePair <SubscriptionDataConfig, ScannableEnumerator <BaseData> >(dataConfig, enumerator) }).ToList()); });

            return(enumerator);
        }
Example #8
0
 public LogReturnSignal(LogReturn logr_Fast,
                        LogReturn logr_Slow,
                        LeastSquaresMovingAverage logrFast,
                        LeastSquaresMovingAverage logrSlow,
                        TickConsolidator ticks,
                        SecurityHolding securityHolding)
 {
     _logr_Fast       = logr_Fast;
     _logr_Slow       = logr_Slow;
     _logrFast        = logrFast;
     _logrSlow        = logrSlow;
     _ticks           = ticks;
     _securityHolding = securityHolding;
 }
Example #9
0
        public void METHOD()
        {
            var tickf = new TickFactory();
            var cons  = new TickConsolidator(3);
            int i     = 0;

            //cons.DataConsolidated += (sender, bar) => {
            //    i++;
            //    Console.WriteLine(bar);
            //};

            //for (int j = 0; j < 9; j++) {
            //    cons.Update(tickf.NewTick);
            //}
        }
        public override void Initialize()
        {
            SetCash(10000);
            SetStartDate(Configuration.GetConfigDateTime("startDate", new DateTime(2017, 11, 21), this));
            SetEndDate(Configuration.GetConfigDateTime("endDate", new DateTime(2017, 11, 21), this));
            Configuration.GetConfiguration(Configuration.configUrl, config);
            if (IsOutOfSampleRun)
            {
                //var startDate = new DateTime(year: 2016, month: 1, day: 1);
                //SetStartDate(startDate);
                //SetEndDate(startDate.AddMonths(oosPeriod));
                RuntimeStatistics["ID"] = GetParameter("ID");
                SetParameters(config.ToDictionary(k => k.Key, v => v.Value.ToString()));
            }

            SetBrokerageModel(QuantConnect.Brokerages.BrokerageName.OandaBrokerage);
            var con = new TickConsolidator(new TimeSpan(1, 0, 0));

            // SetBenchmark(_symbol);

            _symbols = new List <Symbol>();
            _entry   = new List <Rule>();
            _exit    = new List <Rule>();
            foreach (var symbol in TradingSymbols.OandaFXMajors0)
            {
                var security = AddSecurity(SecurityType.Forex, symbol, Configuration._resolution, Market.Oanda, true, Configuration._leverage, false);
                _symbols.Add(security.Symbol);
            }
            foreach (var symbol in TradingSymbols.OandaCFD)
            {
                //    AddSecurity(SecurityType.Cfd, symbol, _resolution, Market.Oanda, true, _leverage, false);
            }
            var factory = new SignalFactory();

            foreach (var symbol in _symbols)
            {
                Securities[symbol].VolatilityModel = new ThreeSigmaVolatilityModel(STD(symbol: symbol, period: 12 * 60, resolution: Configuration._resolution), 20.0m);

                _entry.Add(factory.Create(this, symbol, true, Configuration._resolution));
                _exit.Add(factory.Create(this, symbol, false, Configuration._resolution));
            }

            RiskManager = new FxRiskManagment(Portfolio, Configuration._riskPerTrade, Configuration._maxExposurePerTrade, Configuration._maxExposure, Configuration._lotSize);
        }
        public void AggregatesPeriodInPeriodModeWithDailyDataAndRoundedTime()
        {
            TradeBar consolidated = null;
            var      consolidator = new TickConsolidator(TimeSpan.FromDays(1));

            consolidator.DataConsolidated += (sender, bar) =>
            {
                consolidated = bar;
            };

            var reference = new DateTime(2015, 04, 13);

            consolidator.Update(new Tick {
                Time = reference.AddSeconds(5)
            });
            Assert.IsNull(consolidated);

            consolidator.Update(new Tick {
                Time = reference.AddDays(1).AddSeconds(15)
            });
            Assert.IsNotNull(consolidated);
            Assert.AreEqual(TimeSpan.FromDays(1), consolidated.Period);
            Assert.AreEqual(reference, consolidated.Time);
            consolidated = null;

            consolidator.Update(new Tick {
                Time = reference.AddDays(2).AddMinutes(1)
            });
            Assert.IsNotNull(consolidated);
            Assert.AreEqual(TimeSpan.FromDays(1), consolidated.Period);
            Assert.AreEqual(reference.AddDays(1), consolidated.Time);
            consolidated = null;

            consolidator.Update(new Tick {
                Time = reference.AddDays(3).AddMinutes(5)
            });
            Assert.IsNotNull(consolidated);
            Assert.AreEqual(TimeSpan.FromDays(1), consolidated.Period);
            Assert.AreEqual(reference.AddDays(2), consolidated.Time);
        }
        public void AggregatesPeriodInCountModeWithDailyData()
        {
            TradeBar consolidated = null;
            var      consolidator = new TickConsolidator(2);

            consolidator.DataConsolidated += (sender, bar) =>
            {
                consolidated = bar;
            };

            var reference = new DateTime(2015, 04, 13);

            consolidator.Update(new Tick {
                Time = reference
            });
            Assert.IsNull(consolidated);

            consolidator.Update(new Tick {
                Time = reference.AddMilliseconds(1)
            });
            Assert.IsNotNull(consolidated);

            // sadly the first emit will be off by the data resolution since we 'swallow' a point, so to
            Assert.AreEqual(TimeSpan.FromMilliseconds(1), consolidated.Period);
            consolidated = null;

            consolidator.Update(new Tick {
                Time = reference.AddMilliseconds(2)
            });
            Assert.IsNull(consolidated);

            consolidator.Update(new Tick {
                Time = reference.AddMilliseconds(3)
            });
            Assert.IsNotNull(consolidated);

            Assert.AreEqual(TimeSpan.FromMilliseconds(2), consolidated.Period);
        }
Example #13
0
        public void AggregatesPeriodInCountModeWithDailyData()
        {
            TradeBar consolidated = null;
            var consolidator = new TickConsolidator(2);
            consolidator.DataConsolidated += (sender, bar) =>
            {
                consolidated = bar;
            };

            var reference = new DateTime(2015, 04, 13);
            consolidator.Update(new Tick { Time = reference});
            Assert.IsNull(consolidated);

            consolidator.Update(new Tick { Time = reference.AddMilliseconds(1)});
            Assert.IsNotNull(consolidated);

            // sadly the first emit will be off by the data resolution since we 'swallow' a point, so to
            Assert.AreEqual(TimeSpan.FromMilliseconds(1), consolidated.Period);
            consolidated = null;

            consolidator.Update(new Tick { Time = reference.AddMilliseconds(2)});
            Assert.IsNull(consolidated);

            consolidator.Update(new Tick { Time = reference.AddMilliseconds(3)});
            Assert.IsNotNull(consolidated);

            Assert.AreEqual(TimeSpan.FromMilliseconds(2), consolidated.Period);
        }
Example #14
0
        public void AggregatesNewTicksInPeriodWithRoundedTime()
        {
            TradeBar consolidated = null;
            var consolidator = new TickConsolidator(TimeSpan.FromMinutes(1));
            consolidator.DataConsolidated += (sender, bar) =>
            {
                consolidated = bar;
            };

            var reference = new DateTime(2015, 06, 02);
            var tick1 = new Tick
            {
                Symbol = "EURUSD",
                Time = reference.AddSeconds(3),
                Value = 1.1000m
            };
            consolidator.Update(tick1);
            Assert.IsNull(consolidated);

            var tick2 = new Tick
            {
                Symbol = "EURUSD",
                Time = reference.AddSeconds(10),
                Value = 1.1005m
            };
            consolidator.Update(tick2);
            Assert.IsNull(consolidated);

            var tick3 = new Tick
            {
                Symbol = "EURUSD",
                Time = reference.AddSeconds(61),
                Value = 1.1010m
            };
            consolidator.Update(tick3);
            Assert.IsNotNull(consolidated);

            Assert.AreEqual(consolidated.Time, reference);
            Assert.AreEqual(consolidated.Open, tick1.Value);
            Assert.AreEqual(consolidated.Close, tick2.Value);

            var tick4 = new Tick
            {
                Symbol = "EURUSD",
                Time = reference.AddSeconds(70),
                Value = 1.1015m
            };
            consolidator.Update(tick4);
            Assert.IsNotNull(consolidated);

            var tick5 = new Tick
            {
                Symbol = "EURUSD",
                Time = reference.AddSeconds(118),
                Value = 1.1020m
            };
            consolidator.Update(tick5);
            Assert.IsNotNull(consolidated);

            var tick6 = new Tick
            {
                Symbol = "EURUSD",
                Time = reference.AddSeconds(140),
                Value = 1.1025m
            };
            consolidator.Update(tick6);
            Assert.IsNotNull(consolidated);

            Assert.AreEqual(consolidated.Time, reference.AddSeconds(60));
            Assert.AreEqual(consolidated.Open, tick3.Value);
            Assert.AreEqual(consolidated.Close, tick5.Value);
        }
Example #15
0
        public void AggregatesPeriodInPeriodModeWithDailyData()
        {
            TradeBar consolidated = null;
            var consolidator = new TickConsolidator(TimeSpan.FromDays(1));
            consolidator.DataConsolidated += (sender, bar) =>
            {
                consolidated = bar;
            };

            var reference = new DateTime(2015, 04, 13);
            consolidator.Update(new Tick { Time = reference});
            Assert.IsNull(consolidated);

            consolidator.Update(new Tick { Time = reference.AddDays(1)});
            Assert.IsNotNull(consolidated);
            Assert.AreEqual(TimeSpan.FromDays(1), consolidated.Period);
            consolidated = null;

            consolidator.Update(new Tick { Time = reference.AddDays(2)});
            Assert.IsNotNull(consolidated);
            Assert.AreEqual(TimeSpan.FromDays(1), consolidated.Period);
            consolidated = null;

            consolidator.Update(new Tick { Time = reference.AddDays(3)});
            Assert.IsNotNull(consolidated);
            Assert.AreEqual(TimeSpan.FromDays(1), consolidated.Period);
        }
Example #16
0
        public void AggregatesTickToCalendarTradeBarProperly()
        {
            // Monday
            var reference = new DateTime(2019, 3, 18);
            var ticks     = new List <Tick>
            {
                new Tick(reference.AddDays(1), Symbols.SPY, 9, 11, 8)
                {
                    TickType = TickType.Trade, Quantity = 10
                },
                new Tick(reference.AddDays(3), Symbols.SPY, 10, 12, 8)
                {
                    TickType = TickType.Trade, Quantity = 10
                },
                new Tick(reference.AddDays(5), Symbols.SPY, 11, 13, 9)
                {
                    TickType = TickType.Trade, Quantity = 10
                },
                new Tick(reference.AddDays(7), Symbols.SPY, 11, 13, 9)
                {
                    TickType = TickType.Trade, Quantity = 10
                },
                new Tick(reference.AddDays(14), Symbols.SPY, 11, 13, 9)
                {
                    TickType = TickType.Trade, Quantity = 10
                }
            };

            var weeklyConsolidator = new TickConsolidator(CalendarType.Weekly);

            weeklyConsolidator.DataConsolidated += (s, e) =>
            {
                AssertTickTradeBar(
                    ticks.Take(3),
                    reference,
                    reference.AddDays(7),
                    Symbols.SPY,
                    e);
            };

            var monthlyConsolidator = new TickConsolidator(CalendarType.Monthly);

            monthlyConsolidator.DataConsolidated += (s, e) =>
            {
                AssertTickTradeBar(
                    ticks.Take(4),
                    new DateTime(reference.Year, reference.Month, 1),
                    new DateTime(reference.Year, reference.Month + 1, 1),
                    Symbols.SPY,
                    e);
            };

            foreach (var tick in ticks.Take(4))
            {
                weeklyConsolidator.Update(tick);
            }

            foreach (var tick in ticks)
            {
                monthlyConsolidator.Update(tick);
            }
        }
        public void AggregatesNewTicksInPeriodWithRoundedTime()
        {
            TradeBar consolidated = null;
            var      consolidator = new TickConsolidator(TimeSpan.FromMinutes(1));

            consolidator.DataConsolidated += (sender, bar) =>
            {
                consolidated = bar;
            };

            var reference = new DateTime(2015, 06, 02);
            var tick1     = new Tick
            {
                Symbol = Symbols.EURUSD,
                Time   = reference.AddSeconds(3),
                Value  = 1.1000m
            };

            consolidator.Update(tick1);
            Assert.IsNull(consolidated);

            var tick2 = new Tick
            {
                Symbol = Symbols.EURUSD,
                Time   = reference.AddSeconds(10),
                Value  = 1.1005m
            };

            consolidator.Update(tick2);
            Assert.IsNull(consolidated);

            var tick3 = new Tick
            {
                Symbol = Symbols.EURUSD,
                Time   = reference.AddSeconds(61),
                Value  = 1.1010m
            };

            consolidator.Update(tick3);
            Assert.IsNotNull(consolidated);

            Assert.AreEqual(consolidated.Time, reference);
            Assert.AreEqual(consolidated.Open, tick1.Value);
            Assert.AreEqual(consolidated.Close, tick2.Value);

            var tick4 = new Tick
            {
                Symbol = Symbols.EURUSD,
                Time   = reference.AddSeconds(70),
                Value  = 1.1015m
            };

            consolidator.Update(tick4);
            Assert.IsNotNull(consolidated);

            var tick5 = new Tick
            {
                Symbol = Symbols.EURUSD,
                Time   = reference.AddSeconds(118),
                Value  = 1.1020m
            };

            consolidator.Update(tick5);
            Assert.IsNotNull(consolidated);

            var tick6 = new Tick
            {
                Symbol = Symbols.EURUSD,
                Time   = reference.AddSeconds(140),
                Value  = 1.1025m
            };

            consolidator.Update(tick6);
            Assert.IsNotNull(consolidated);

            Assert.AreEqual(consolidated.Time, reference.AddSeconds(60));
            Assert.AreEqual(consolidated.Open, tick3.Value);
            Assert.AreEqual(consolidated.Close, tick5.Value);
        }
        public void AggregatesNewTradeBarsProperly()
        {
            TradeBar newTradeBar  = null;
            var      consolidator = new TickConsolidator(4);

            consolidator.DataConsolidated += (sender, tradeBar) =>
            {
                newTradeBar = tradeBar;
            };
            var reference = DateTime.Today;
            var bar1      = new Tick
            {
                Symbol   = Symbols.SPY,
                Time     = reference,
                Value    = 5,
                Quantity = 10
            };

            consolidator.Update(bar1);
            Assert.IsNull(newTradeBar);

            var bar2 = new Tick
            {
                Symbol   = Symbols.SPY,
                Time     = reference.AddHours(1),
                Value    = 10,
                Quantity = 20
            };

            consolidator.Update(bar2);
            Assert.IsNull(newTradeBar);
            var bar3 = new Tick
            {
                Symbol   = Symbols.SPY,
                Time     = reference.AddHours(2),
                Value    = 1,
                Quantity = 10
            };

            consolidator.Update(bar3);
            Assert.IsNull(newTradeBar);

            var bar4 = new Tick
            {
                Symbol   = Symbols.SPY,
                Time     = reference.AddHours(3),
                Value    = 9,
                Quantity = 20
            };

            consolidator.Update(bar4);
            Assert.IsNotNull(newTradeBar);

            Assert.AreEqual(Symbols.SPY, newTradeBar.Symbol);
            Assert.AreEqual(bar1.Time, newTradeBar.Time);
            Assert.AreEqual(bar1.Value, newTradeBar.Open);
            Assert.AreEqual(bar2.Value, newTradeBar.High);
            Assert.AreEqual(bar3.Value, newTradeBar.Low);
            Assert.AreEqual(bar4.Value, newTradeBar.Close);
            Assert.AreEqual(bar4.EndTime, newTradeBar.EndTime);
            Assert.AreEqual(bar1.Quantity + bar2.Quantity + bar3.Quantity + bar4.Quantity, newTradeBar.Volume);
        }