Beispiel #1
0
        public void GetsTickData()
        {
            InteractiveBrokersGatewayRunner.StartFromConfiguration();

            var ib = new InteractiveBrokersBrokerage(new QCAlgorithm(), new OrderProvider(), new SecurityProvider());

            ib.Connect();

            ib.Subscribe(null, new List <Symbol> {
                Symbols.USDJPY, Symbols.EURGBP
            });

            Thread.Sleep(1000);

            var gotUsdData = false;
            var gotEurData = false;

            for (int i = 0; i < 20; i++)
            {
                foreach (var tick in ib.GetNextTicks())
                {
                    Console.WriteLine("{0}: {1} - {2} @ {3}", tick.Time, tick.Symbol, tick.Price, ((Tick)tick).Quantity);
                    gotUsdData |= tick.Symbol == Symbols.USDJPY;
                    gotEurData |= tick.Symbol == Symbols.EURGBP;
                }
            }
            Assert.IsTrue(gotUsdData);
            Assert.IsTrue(gotEurData);
            InteractiveBrokersGatewayRunner.Stop();
        }
        public void GetsTickData()
        {
            InteractiveBrokersGatewayRunner.StartFromConfiguration();

            var ib = new InteractiveBrokersBrokerage(new OrderProvider());

            ib.Connect();

            ib.Subscribe(null, new Dictionary <SecurityType, List <string> >
            {
                { SecurityType.Forex, new List <string> {
                      "USDJPY", "EURGBP"
                  } }
            });

            Thread.Sleep(1000);

            for (int i = 0; i < 10; i++)
            {
                foreach (var tick in ib.GetNextTicks())
                {
                    Console.WriteLine("{0}: {1} - {2} @ {3}", tick.Time, tick.Symbol, tick.Price, ((Tick)tick).Quantity);
                }
            }

            InteractiveBrokersGatewayRunner.Stop();
        }
Beispiel #3
0
        /// <summary>
        /// Primary entry point to the program. This program only supports FOREX for now.
        /// </summary>
        static void Main(string[] args)
        {
            if (args.Length != 5)
            {
                Console.WriteLine("Usage: QuantConnect.ToolBox SYMBOLS RESOLUTION FROMDATE TODATE");
                Console.WriteLine("SYMBOLS = eg EURUSD,USDJPY");
                Console.WriteLine("RESOLUTION = Second/Minute/Hour/Daily/All");
                Console.WriteLine("FROMDATE = yyyymmdd");
                Console.WriteLine("TODATE = yyyymmdd");
                Environment.Exit(1);
            }
            try
            {
                // Load settings from command line
                var tickers        = args[1].Split(',');
                var allResolutions = args[2].ToLower() == "all";
                var resolution     = allResolutions ? Resolution.Second : (Resolution)Enum.Parse(typeof(Resolution), args[2]);
                var startDate      = DateTime.ParseExact(args[3], "yyyyMMdd", CultureInfo.InvariantCulture).ConvertToUtc(TimeZones.NewYork);
                var endDate        = DateTime.ParseExact(args[4], "yyyyMMdd", CultureInfo.InvariantCulture).ConvertToUtc(TimeZones.NewYork);

                // fix end date
                endDate = new DateTime(Math.Min(endDate.Ticks, DateTime.Now.AddDays(-1).Ticks));

                // Max number of histoy days
                int maxDays = 1;
                if (!allResolutions)
                {
                    switch (resolution)
                    {
                    case Resolution.Daily:
                        maxDays = 365;
                        break;

                    case Resolution.Hour:
                        maxDays = 30;
                        break;

                    case Resolution.Minute:
                        maxDays = 10;
                        break;
                    }
                }

                // Load settings from config.json
                var dataDirectory = Config.Get("data-folder", "../../../Data");

                // Create IB Broker Gateway Runner
                InteractiveBrokersGatewayRunner.StartFromConfiguration();

                // Only FOREX for now
                SecurityType securityType = SecurityType.Forex;
                string       market       = Market.FXCM;


                using (var downloader = new IBDataDownloader())
                {
                    foreach (var ticker in tickers)
                    {
                        // Download the data
                        var symbol = Symbol.Create(ticker, securityType, market);

                        var auxEndDate = startDate.AddDays(maxDays);
                        auxEndDate = new DateTime(Math.Min(auxEndDate.Ticks, endDate.Ticks));

                        while (startDate < auxEndDate)
                        {
                            var data = downloader.Get(symbol, resolution, startDate, auxEndDate);
                            var bars = data.Cast <QuoteBar>().ToList();

                            if (allResolutions)
                            {
                                // Save the data (second resolution)
                                var writer = new LeanDataWriter(resolution, symbol, dataDirectory);
                                writer.Write(bars);

                                // Save the data (other resolutions)
                                foreach (var res in new[] { Resolution.Minute, Resolution.Hour, Resolution.Daily })
                                {
                                    var resData = downloader.AggregateBars(symbol, bars, res.ToTimeSpan());

                                    writer = new LeanDataWriter(res, symbol, dataDirectory);
                                    writer.Write(resData);
                                }
                            }
                            else
                            {
                                // Save the data (single resolution)
                                var writer = new LeanDataWriter(resolution, symbol, dataDirectory);
                                writer.Write(data);
                            }

                            startDate  = auxEndDate;
                            auxEndDate = auxEndDate.AddDays(maxDays);
                            auxEndDate = new DateTime(Math.Min(auxEndDate.Ticks, endDate.Ticks));
                        }
                    }
                }
            }
            catch (Exception err)
            {
                Log.Error(err);
            }
            finally
            {
                if (InteractiveBrokersGatewayRunner.IsRunning())
                {
                    InteractiveBrokersGatewayRunner.Stop();
                }
            }
        }
Beispiel #4
0
        /// <summary>
        /// Primary entry point to the program. This program only supports FOREX for now.
        /// </summary>
        public static void IBDownloader(IList <string> tickers, string resolution, DateTime fromDate, DateTime toDate)
        {
            if (resolution.IsNullOrEmpty() || tickers.IsNullOrEmpty())
            {
                Console.WriteLine("IBDownloader ERROR: '--tickers=' or '--resolution=' parameter is missing");
                Console.WriteLine("--tickers=eg EURUSD,USDJPY");
                Console.WriteLine("--resolution=Second/Minute/Hour/Daily/All");
                Environment.Exit(1);
            }
            try
            {
                var allResolutions = resolution.ToLower() == "all";
                var castResolution = allResolutions ? Resolution.Second : (Resolution)Enum.Parse(typeof(Resolution), resolution);
                var startDate      = fromDate.ConvertToUtc(TimeZones.NewYork);
                var endDate        = toDate.ConvertToUtc(TimeZones.NewYork);

                // fix end date
                endDate = new DateTime(Math.Min(endDate.Ticks, DateTime.Now.AddDays(-1).Ticks));

                // Max number of histoy days
                int maxDays = 1;
                if (!allResolutions)
                {
                    switch (castResolution)
                    {
                    case Resolution.Daily:
                        maxDays = 365;
                        break;

                    case Resolution.Hour:
                        maxDays = 30;
                        break;

                    case Resolution.Minute:
                        maxDays = 10;
                        break;
                    }
                }

                // Load settings from config.json
                var dataDirectory = Config.Get("data-folder", "../../../Data");

                // Create IB Broker Gateway Runner
                InteractiveBrokersGatewayRunner.StartFromConfiguration();

                // Only FOREX for now
                SecurityType securityType = SecurityType.Forex;
                string       market       = Market.FXCM;


                using (var downloader = new IBDataDownloader())
                {
                    foreach (var ticker in tickers)
                    {
                        // Download the data
                        var symbol = Symbol.Create(ticker, securityType, market);

                        var auxEndDate = startDate.AddDays(maxDays);
                        auxEndDate = new DateTime(Math.Min(auxEndDate.Ticks, endDate.Ticks));

                        while (startDate < auxEndDate)
                        {
                            var data = downloader.Get(symbol, castResolution, startDate, auxEndDate);
                            var bars = data.Cast <QuoteBar>().ToList();

                            if (allResolutions)
                            {
                                // Save the data (second resolution)
                                var writer = new LeanDataWriter(castResolution, symbol, dataDirectory);
                                writer.Write(bars);

                                // Save the data (other resolutions)
                                foreach (var res in new[] { Resolution.Minute, Resolution.Hour, Resolution.Daily })
                                {
                                    var resData = downloader.AggregateBars(symbol, bars, res.ToTimeSpan());

                                    writer = new LeanDataWriter(res, symbol, dataDirectory);
                                    writer.Write(resData);
                                }
                            }
                            else
                            {
                                // Save the data (single resolution)
                                var writer = new LeanDataWriter(castResolution, symbol, dataDirectory);
                                writer.Write(data);
                            }

                            startDate  = auxEndDate;
                            auxEndDate = auxEndDate.AddDays(maxDays);
                            auxEndDate = new DateTime(Math.Min(auxEndDate.Ticks, endDate.Ticks));
                        }
                    }
                }
            }
            catch (Exception err)
            {
                Log.Error(err);
            }
            finally
            {
                if (InteractiveBrokersGatewayRunner.IsRunning())
                {
                    InteractiveBrokersGatewayRunner.Stop();
                }
            }
        }