How to get historic prices

Brief

The article discusses how to get historic prices in .Net.

Details

To get historic prices in your .Net 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:

    public class MyResponseListener : IO2GResponseListener{...}
  3. Subscribe an object of this class to the session response:

    
    MyResponseListener responseListener = new MyResponseListener();
    mSession.subscribeResponse(responseListener);
    
  4. Log in (see this article).

  5. Use the listener onSessionStatusChanged function to capture the Connected event:

  6. Create O2GRequestFactory:

    O2GRequestFactory factory = mSession.getRequestFactory();
  7. Get O2GTimeframeCollection:

    O2GTimeframeCollection timeframes = factory.Timeframes;
  8. Get the O2GTimeframe you need:

    O2GTimeframe tfo = timeframes["m1"];
  9. Create a market data snapshot request (O2GRequest), using the instrument, time frame, and maximum number of bars as arguments:

    O2GRequest request = factory.createMarketDataSnapshotRequestInstrument("EUR/USD", tfo, 300);
  10. Fill the market data snapshot request, using the request, date and time "from", and date and time "to" as arguments:

    factory.fillMarketDataSnapshotRequestTime(request, from, to);
  11. Send the request:

    mSession.sendRequest(request);
  12. Finally capture O2GResponse in the onRequestCompleted function of a response listener class.
    If the type of response is MarketDataSnapshot, process the response to extract necessary information.

  13. Get O2GResponseReaderFactory:

    O2GResponseReaderFactory readerFactory = mSession.getResponseReaderFactory();
  14. Create O2GMarketDataSnapshotResponseReader:

    O2GMarketDataSnapshotResponseReader mReader = readerFactory.createMarketDataSnapshotReader(response);
  15. Process row by row:

    
    for (int i = 0; i < reader.Count; i++)
    {
        // information like reader.getDate(i), reader.getBidOpen(i), reader.getBidHigh(i), reader.getBidLow(i), reader.getBidClose(i), reader.getVolume(i) is now available
    }
    
  16. Unsubscribe the session from the response listener.

    
    mSession.unsubscribeResponse(responseListener);
    
  17. Log out.

    
    mSession.logout();
    

Get history prices example: [show]

See also the simplified request/response sequence diagram.

back