public void getHistoryPrice() { O2GSession session = null; try { LoginParams loginParams = new LoginParams(ConfigurationManager.AppSettings); SampleParams sampleParams = new SampleParams(ConfigurationManager.AppSettings); PrintSampleParams("GetHistPrices", loginParams, sampleParams); session = O2GTransport.createSession(); SessionStatusListener statusListener = new SessionStatusListener(session, loginParams.SessionID, loginParams.Pin); session.subscribeSessionStatus(statusListener); statusListener.Reset(); session.login(loginParams.Login, loginParams.Password, loginParams.URL, loginParams.Connection); if (statusListener.WaitEvents() && statusListener.Connected) { ResponseListener responseListener = new ResponseListener(session); session.subscribeResponse(responseListener); GetHistoryPrices(session, sampleParams.Instrument, sampleParams.Timeframe, sampleParams.DateFrom, sampleParams.DateTo, responseListener); Console.WriteLine("Done!"); updateLogDelegate("Done!"); PersonForm.EnableStandby(); // writeHistoryPriceToFile(sampleParams.Instrument); // updateNames(sampleParams.Instrument); statusListener.Reset(); session.logout(); statusListener.WaitEvents(); session.unsubscribeResponse(responseListener); } session.unsubscribeSessionStatus(statusListener); } catch (Exception e) { Console.WriteLine("Exception: {0}", e.ToString()); // updateLogDelegate(string.Format("Exception: {0}", e.ToString())); } finally { if (session != null) { session.Dispose(); } } }
/// <summary> /// Request historical prices for the specified timeframe of the specified period /// </summary> /// <param name="session"></param> /// <param name="sInstrument"></param> /// <param name="sTimeframe"></param> /// <param name="dtFrom"></param> /// <param name="dtTo"></param> /// <param name="responseListener"></param> public void GetHistoryPrices(O2GSession session, string sInstrument, string sTimeframe, DateTime dtFrom, DateTime dtTo, ResponseListener responseListener) { O2GRequestFactory factory = session.getRequestFactory(); O2GTimeframe timeframe = factory.Timeframes[sTimeframe]; if (timeframe == null) { throw new Exception(string.Format("Timeframe '{0}' is incorrect!", sTimeframe)); } O2GRequest request = factory.createMarketDataSnapshotRequestInstrument(sInstrument, timeframe, 300); DateTime dtFirst = dtTo; do // cause there is limit for returned candles amount { factory.fillMarketDataSnapshotRequestTime(request, dtFrom, dtFirst, false); responseListener.SetRequestID(request.RequestID); session.sendRequest(request); if (!responseListener.WaitEvents()) { throw new Exception("Response waiting timeout expired"); } // shift "to" bound to oldest datetime of returned data O2GResponse response = responseListener.GetResponse(); if (response != null && response.Type == O2GResponseType.MarketDataSnapshot) { O2GResponseReaderFactory readerFactory = session.getResponseReaderFactory(); if (readerFactory != null) { O2GMarketDataSnapshotResponseReader reader = readerFactory.createMarketDataSnapshotReader(response); if (reader.Count > 0) { if (DateTime.Compare(dtFirst, reader.getDate(0)) != 0) { dtFirst = reader.getDate(0); // earliest datetime of returned data } else { break; } } else { // Console.WriteLine("0 rows received"); updateLogDelegate(string.Format("0 rows received")); break; } } // PrintPrices(session, response); storeHistoryPriceToDataTable(session, response, sInstrument); // DateTime.Subtraction(dtTo, dtFirst)/ Subtraction long percent = (dtTo.Ticks - dtFirst.Ticks) * 100 / (dtTo.Ticks - dtFrom.Ticks); updateProcessDelegate((int)percent, this.InstrumentDT.Rows.Count); } else { break; } } while (dtFirst > dtFrom); }