How to get historic prices

Brief

The article discusses how to get historic prices in C++.

Details

To get historic prices in your C++ application, you should:

  1. Create a session, a session status listener, subscribe the listener to the session status (see this article).

  2. Implement the IO2GResponseListener interface in a response listener class:

    class ResponseListener : public IO2GResponseListener{...}
  3. Create a response listener object:

    ResponseListener * responseListener = new ResponseListener(...);

    Subscribe the object of this class to the session response:

    pSession->subscribeResponse(responseListener);
  4. Log in (see this article).

  5. The events are coming asynchronously. They should come in another thread. When an event comes, a signal will be sent to the main thread.

  6. Use the function onSessionStatusChanged of the session status listener to capture the Connected event.

  7. Create IO2GRequestFactory:

    IO2GRequestFactory * factory = mSession->getRequestFactory();
  8. Get IO2GTimeframeCollection:

    IO2GTimeframeCollection * timeFrames = factory->getTimeFrameCollection();
  9. Get the IO2GTimeframe you need:

    IO2GTimeframe * timeFrame = timeFrames->get("m1");
  10. Create a market data snapshot request (IO2GRequest) using the instrument, time frame, and maximum number of bars as arguments:

    IO2GRequest * getMarketDataSnapshot = factory->createMarketDataSnapshotRequestInstrument("EUR/USD", timeFrame, 300);
  11. Fill the market data snapshot request using the request, date and time "from", and date and time "to" as arguments:

    factory->fillMarketDataSnapshotRequestTime(getMarketDataSnapshot, dateFrom, dateTo);
  12. Send the request:

    mSession->sendRequest(getMarketDataSnapshot);
  13. Finally capture IO2GResponse in the onRequestCompleted function of a response listener class.
    If the type of response is MarketDataSnapshot, process the response to extract necessary information.

  14. Get IO2GResponseReaderFactory:

    IO2GResponseReaderFactory * factory = mSession->getResponseReaderFactory();
  15. Create IO2GMarketDataSnapshotResponseReader:

    IO2GMarketDataSnapshotResponseReader * marketSnapshotReader = factory->createMarketDataSnapshotReader(response);
  16. Process row by row:

    
    for (int i = 0; i < marketSnapshotReader->size(); i++)
    {
          // Information like
          // marketSnapshotReader->getBidOpen(i)
          // is now available.
    }
    


  17. Unsubscribe the session from the response listener.

  18. Log out and unsubscribe session from session status listener(see this article).

See also the simplified request/response sequence diagram.

back