public List<BarData> RetrieveData(Symbol symbol, int frequency, DateTime startDate, DateTime endDate, BarConstructionType barConstruction) { ClearError(); if (iqFeed == null) { if (!CreateIQFeed()) { lastError = "Unable to connect to IQFeed"; hadError = true; return null; } } ReturnValue<List<BarData>> ret = iqFeed.GetHistoricalBarData(symbol, frequency, startDate, endDate); if (!ret.Success) { lastError = ret.ReturnCode.Message; hadError = true; return null; } // Filter out bars outside of the time frame we requested. List<BarData> bars = new List<BarData>(ret.Value.Count); foreach (BarData bar in ret.Value) { if (bar.BarStartTime >= startDate && bar.BarStartTime <= endDate) { bars.Add(bar); } } if (dropLastHistBar && bars.Count > 0) { bars.RemoveAt(bars.Count - 1); } return bars; }
public List <BarData> RetrieveData(Symbol symbol, int frequency, DateTime startDate, DateTime endDate, BarConstructionType barConstruction) { if (frequency != (int)BarFrequency.Daily && frequency != (int)BarFrequency.FiveMinute && frequency != (int)BarFrequency.OneMinute) { return(new List <BarData>()); } if (startDate == DateTime.MinValue) { // I think this is about as far back as Yahoo will go startDate = new DateTime(1960, 1, 1); } int startMonth = startDate.Month - 1; int endMonth = endDate.Month - 1; // Alphavantage API Demo // https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol=MSFT&apikey=XXPUTYOURKEYHERE string baseURL = "https://www.alphavantage.co/query?"; string url = baseURL + "function=TIME_SERIES_DAILY&symbol=" + symbol.Name + "&apikey=XXPUTYOURKEYHERE"; if (frequency == (int)BarFrequency.Daily) { // Use Above } if (startDate.Date == DateTime.Today) { // Alphavantage // https://www.alphavantage.co/query?function=GLOBAL_QUOTE&symbol=GSAT&apikey=XXPUTYOURKEYHERE baseURL = "https://www.alphavantage.co/query?"; url = baseURL + "function=GLOBAL_QUOTE&symbol=" + symbol.Name + "&apikey=XXPUTYOURKEYHERE"; } if (frequency == (int)BarFrequency.FiveMinute) { // Alphavantage // https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&symbol=MSFT&interval=5min&apikey=XXPUTYOURKEYHERE baseURL = "https://www.alphavantage.co/query?"; url = baseURL + "function=TIME_SERIES_INTRADAY&symbol=" + symbol.Name + "&interval=5min&outputsize=full&apikey=XXPUTYOURKEYHERE"; } if (frequency == (int)BarFrequency.OneMinute) { // Alphavantage // https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&symbol=MSFT&interval=1min&apikey=XXPUTYOURKEYHERE baseURL = "https://www.alphavantage.co/query?"; url = baseURL + "function=TIME_SERIES_INTRADAY&symbol=" + symbol.Name + "&interval=1min&outputsize=full&apikey=XXPUTYOURKEYHERE"; } WebResponse result = null; WebRequest req = WebRequest.Create(url); result = req.GetResponse(); Stream receiveStream = result.GetResponseStream(); Encoding encode = System.Text.Encoding.GetEncoding("utf-8"); StreamReader sr = new StreamReader(receiveStream, encode); List <BarData> bars = new List <BarData>(); if (frequency == (int)BarFrequency.OneMinute) { PutRawDataInBarCollectionIntra1m(sr, bars); } else if (frequency == (int)BarFrequency.FiveMinute) { PutRawDataInBarCollectionIntra5m(sr, bars); } else if (startDate.Date == DateTime.Today) { PutRawDataInBarCollectionRT(sr, bars); } else { PutRawDataInBarCollectionJSON(sr, bars); } sr.Close(); List <BarData> ret = new List <BarData>(); // Sort bars and remove duplicates foreach (BarData bar in bars.OrderBy(b => b.BarStartTime)) { if (ret.Count > 0 && ret[ret.Count - 1].BarStartTime == bar.BarStartTime) { // Avoid duplicate bars (but take the "later" one) ret[ret.Count - 1] = bar; } else { ret.Add(bar); } } return(ret); //return bars.OrderBy(b => b.BarStartTime).ToList(); }
public List<BarData> RetrieveData(Symbol symbol, int frequency, DateTime startDate, DateTime endDate, BarConstructionType barConstruction) { if (_histRetrieval != null) { lastError = "Historical data retrieval already in progress."; return null; } if (Thread.CurrentThread.Name == null) { Thread.CurrentThread.Name = "RetrieveData thread"; } Trace.WriteLine("RetrieveData: " + startDate.ToString() + "-" + endDate.ToString() + " Thread: " + Thread.CurrentThread.ManagedThreadId.ToString("x")); ClearError(); if (!_connected) { if (!Connect(ServiceConnectOptions.HistoricalData)) { return null; } } EventHandler<HistoricalDataEventArgs> handler = null; try { BarSize barSize; //string TWSFreq; // Legal ones are: 1 secs, 5 secs, 15 secs, 30 secs, 1 min, 2 mins, 3 mins, 5 mins, 15 mins, 30 mins, 1 hour, 1 day, 1 week, 1 month, 3 months, 1 year if (frequency == (int)BarFrequency.OneMinute) { //TWSFreq = "1 min"; barSize = BarSize.OneMinute; } else if (frequency == 2) { //TWSFreq = "2 mins"; barSize = BarSize.TwoMinutes; } else if (frequency == 3) { //TWSFreq = "3 mins"; barSize = BarSize.ThreeMinutes; } else if (frequency == (int)BarFrequency.FiveMinute) { //TWSFreq = "5 mins"; barSize = BarSize.FiveMinutes; } else if (frequency == (int)BarFrequency.FifteenMinute) { //TWSFreq = "15 mins"; barSize = BarSize.FifteenMinutes; } else if (frequency == (int)BarFrequency.ThirtyMinute) { //TWSFreq = "30 mins"; barSize = BarSize.ThirtyMinutes; } else if (frequency == (int)BarFrequency.SixtyMinute) { //TWSFreq = "1 hour"; barSize = BarSize.OneHour; } else if (frequency == (int)BarFrequency.Daily) { //TWSFreq = "1 day"; barSize = BarSize.OneDay; } else if (frequency == (int)BarFrequency.Weekly) { //TWSFreq = "1 week"; barSize = BarSize.OneWeek; } else if (frequency == (int)BarFrequency.Monthly) { //TWSFreq = "1 month"; barSize = BarSize.OneMonth; } else if (frequency == (int)BarFrequency.Yearly) { //TWSFreq = "1 year"; barSize = BarSize.OneYear; } else { lastError = "Frequency not supported for historical data retrieval."; return null; } DateTime accountTime = GetAccountTime("RetrieveData call"); if (endDate > accountTime || endDate == DateTime.MinValue) { endDate = accountTime.AddHours(12); } _histRetrieval = new HistRetrieval(); _histRetrieval.client = client; _histRetrieval.twsPlugin = this; _histRetrieval.symbol = symbol; _histRetrieval.barSize = barSize; _histRetrieval.startDate = startDate; _histRetrieval.endDate = endDate; _histRetrieval.RTHOnly = _useRTH; _histRetrieval.BarConstruction = barConstruction; _histRetrieval.waitEvent = new ManualResetEvent(false); //if (_firstHistRequest) //{ // // The first request for historical data seems to fail. So we will submit a small request first and then wait a bit // _histRetrieval.barSize = BarSize.OneDay; // _histRetrieval.Duration = new TimeSpan(5, 0, 0, 0); // _histRetrieval.SendRequest(); // Trace.WriteLine("TWS Plugin sleeping..."); // Thread.Sleep(2500); // Trace.WriteLine("TWS Plugin done sleeping."); // if (hadError) // { // return null; // } // _firstHistRequest = false; //} _histRetrieval.barSize = barSize; // Requesting a duration which will return more than 2000 bars gives an "invalid step" error message // See http://www.interactivebrokers.com/cgi-bin/discus/board-auth.pl?file=/2/39164.html // So we need to find the largest duration which will return less than 2000 bars // Since the trading day is from 9:30 to 4:00, there are 6.5 hours, or 390 minutes per trading day // This means that 5 days will have a bit less than 2000 bars. if (frequency < (int)BarFrequency.Daily) { _histRetrieval.Duration = new TimeSpan(5, 0, 0, 0); } else { //_histRetrieval.Duration = new TimeSpan(7, 0, 0, 0); // Can only request up to 52 weeks at once // Requesting data that is more than a year old will reject the whole request _histRetrieval.Duration = new TimeSpan(360, 0, 0, 0); } handler = new EventHandler<HistoricalDataEventArgs>( delegate(object sender, HistoricalDataEventArgs args) { _histRetrieval.GotData(args); }); client.HistoricalData += handler; int pacingPause = 1000; while (!_histRetrieval.Done) { _histRetrieval.waitEvent.Reset(); _histRetrieval.SendRequest(); _histRetrieval.waitEvent.WaitOne(); if (!CheckError()) { return null; } if (_histRetrieval.bPaused) { Trace.WriteLine("Waiting " + pacingPause + " ms."); Thread.Sleep(pacingPause); Trace.WriteLine("Attempting to resume data collection."); _histRetrieval.bPaused = false; if (pacingPause < 30000) { pacingPause *= 2; } } else { pacingPause = 1000; } } if (!CheckError()) return null; _histRetrieval.ret.Reverse(); if (dropLastHistBar && _histRetrieval.ret.Count > 0) { _histRetrieval.ret.RemoveAt(_histRetrieval.ret.Count - 1); } return _histRetrieval.ret; } finally { if (handler != null) { client.HistoricalData -= handler; } _histRetrieval = null; } }