/// <summary> /// Get historical data enumerable for a single symbol, type and resolution given this start and end time (in UTC). /// </summary> /// <param name="dataDownloaderGetParameters">model class for passing in parameters for historical data</param> /// <returns>Enumerable of base data for this symbol</returns> public IEnumerable <BaseData> Get(DataDownloaderGetParameters dataDownloaderGetParameters) { var symbol = dataDownloaderGetParameters.Symbol; var resolution = dataDownloaderGetParameters.Resolution; var startUtc = dataDownloaderGetParameters.StartUtc; var endUtc = dataDownloaderGetParameters.EndUtc; var tickType = dataDownloaderGetParameters.TickType; if (tickType != TickType.Quote) { yield break; } if (!_symbolMapper.IsKnownLeanSymbol(symbol)) { throw new ArgumentException("Invalid symbol requested: " + symbol.Value); } if (symbol.ID.SecurityType != SecurityType.Forex && symbol.ID.SecurityType != SecurityType.Cfd) { throw new NotSupportedException("SecurityType not available: " + symbol.ID.SecurityType); } if (endUtc < startUtc) { throw new ArgumentException("The end date must be greater or equal to the start date."); } // set the starting date DateTime date = startUtc; // loop until last date while (date <= endUtc) { // request all ticks for a specific date var ticks = DownloadTicks(symbol, date); switch (resolution) { case Resolution.Tick: foreach (var tick in ticks) { yield return(new Tick(tick.Time, symbol, tick.BidPrice, tick.AskPrice)); } break; case Resolution.Second: case Resolution.Minute: case Resolution.Hour: case Resolution.Daily: foreach (var bar in LeanData.AggregateTicks(ticks, symbol, resolution.ToTimeSpan())) { yield return(bar); } break; } date = date.AddDays(1); } }
/// <summary> /// Primary entry point to the program /// </summary> public static void DukascopyDownloader(IList <string> tickers, string resolution, DateTime startDate, DateTime endDate) { if (resolution.IsNullOrEmpty() || tickers.IsNullOrEmpty()) { Console.WriteLine("DukascopyDownloader ERROR: '--tickers=' or '--resolution=' parameter is missing"); Console.WriteLine("--tickers=eg EURUSD,USDJPY"); Console.WriteLine("--resolution=Tick/Second/Minute/Hour/Daily/All"); Environment.Exit(1); } try { // Load settings from command line var allResolutions = resolution.ToLowerInvariant() == "all"; var castResolution = allResolutions ? Resolution.Tick : (Resolution)Enum.Parse(typeof(Resolution), resolution); // Load settings from config.json var dataDirectory = Config.Get("data-directory", "../../../Data"); // Download the data var downloader = new DukascopyDataDownloader(); foreach (var ticker in tickers) { if (!downloader.HasSymbol(ticker)) { throw new ArgumentException("The ticker " + ticker + " is not available."); } } foreach (var ticker in tickers) { var securityType = downloader.GetSecurityType(ticker); var symbolObject = Symbol.Create(ticker, securityType, Market.Dukascopy); var data = downloader.Get(symbolObject, castResolution, startDate, endDate); if (allResolutions) { var ticks = data.Cast <Tick>().ToList(); // Save the data (tick resolution) var writer = new LeanDataWriter(castResolution, symbolObject, dataDirectory); writer.Write(ticks); // Save the data (other resolutions) foreach (var res in new[] { Resolution.Second, Resolution.Minute, Resolution.Hour, Resolution.Daily }) { var resData = LeanData.AggregateTicks(ticks, symbolObject, res.ToTimeSpan()); writer = new LeanDataWriter(res, symbolObject, dataDirectory); writer.Write(resData); } } else { // Save the data (single resolution) var writer = new LeanDataWriter(castResolution, symbolObject, dataDirectory); writer.Write(data); } } } catch (Exception err) { Log.Error(err); } }