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:
Create a session, a session status listener, subscribe the listener to the session status
(see this article).
Implement the IO2GResponseListener interface
in a response listener class:
class ResponseListener : public IO2GResponseListener{...}Create a response listener object:
ResponseListener * responseListener = new ResponseListener(...);
Subscribe the object of this class to the session response:
pSession->subscribeResponse(responseListener);Log in (see this article).
The events are coming asynchronously. They should come in another thread. When an event comes, a signal will be sent to the main thread.
Use the function onSessionStatusChanged
of the session status listener to capture the Connected event.
Create IO2GRequestFactory:
IO2GRequestFactory * factory = mSession->getRequestFactory();IO2GTimeframeCollection * timeFrames = factory->getTimeFrameCollection();Get the IO2GTimeframe you need:
IO2GTimeframe * timeFrame = timeFrames->get("m1");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);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);mSession->sendRequest(getMarketDataSnapshot);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.
Get IO2GResponseReaderFactory:
IO2GResponseReaderFactory * factory = mSession->getResponseReaderFactory();Create IO2GMarketDataSnapshotResponseReader:
IO2GMarketDataSnapshotResponseReader * marketSnapshotReader = factory->createMarketDataSnapshotReader(response);Process row by row:
for (int i = 0; i < marketSnapshotReader->size(); i++)
{
// Information like
// marketSnapshotReader->getBidOpen(i)
// is now available.
}
Log out and unsubscribe session from session status listener(see this article).
See also the simplified request/response sequence diagram.