Click or drag to resize

Getting historical data

S#.API provides an opportunity to receive historical candles, which can be used both for testing and for building Indicators.

Working with historical candles through Connector

  1. To get candles through Connector , you need to create Connector and add the corresponding MessageAdapter to it. How to do this using a special control, see Connection settings window.

    You can also add the corresponding MessageAdapter to the Connector through the code. For example, adapter initialization for Interactive Brokers is described in Adapter initialization Interactive Brokers and looks like this:

    C#
    Connector Connector = new Connector();                
    ...                
    var messageAdapter = new InteractiveBrokersMessageAdapter(Connector.TransactionIdGenerator)
    {
        Address = "<Your Address>".To<EndPoint>(),
    };
    
    Connector.Adapter.InnerAdapters.Add(messageAdapter);
    ...    
    
    Connector.Connect();
    
    ...
  2. In order to get historical candles, you need to call the SubscribeCandles method:

    C#
    ...
    var tf = (TimeSpan)CandlesPeriods.SelectedItem;
    var series = new CandleSeries(typeof(TimeFrameCandle), SelectedSecurity, tf);
    Connector.SubscribeCandles(SelectedSecurity, DateTime.Now.Subtract(TimeSpan.FromTicks(tf.Ticks * 100)), DateTime.Now);
    ...
  3. Historical candles are passed through the CandleSeriesProcessing event:

    C#
    ...
    Connector.CandleSeriesProcessing += ProcessCandle;
    ...
  4. Candles that appear can be rendered through the Chart.

  1. To get candles through the MessageAdapter, you need to create the corresponding MessageAdapter.

    For example, adapter initialization for Interactive Brokers is described in Adapter initialization Interactive Brokers and looks like this:

    C#
    ...         
    var messageAdapter = new InteractiveBrokersMessageAdapter(Connector.TransactionIdGenerator)
    {
        Address = "<Your Address>".To<EndPoint>(),
    };
    ...
  2. We wrap the Interactive Brokers adapter in the SecurityNativeIdMessageAdapter. security system identifier adapter. This is necessary if the trading system works with numeric or any other security identifiers other than the usual string representation.

    C#
    ...
    SecurityNativeIdMessageAdapter _securityAdapter;
    
    if (adapter.IsNativeIdentifiers)
        _securityAdapter = new SecurityNativeIdMessageAdapter(adapter, new InMemoryNativeIdStorage());
    
    var securities = _securityAdapter.GetSecurities(new SecurityLookupMessage
    {
        SecurityId = new SecurityId
        {
            SecurityCode = "EUR"
        }
    });
    
    SecurityMessage eurUsd = null;
    
    foreach (var security in securities)
    {
        if (security.SecurityId.SecurityCode.CompareIgnoreCase("EURUSD"))
            eurUsd = security;
    }
    ...
  3. Now, using the received security identifier, we get candles from the adapter:

    C#
    ...
    var candles = adapter.GetCandles(eurUsd.SecurityId, TimeSpan.FromDays(1), DateTimeOffset.Now.AddDays(-100), DateTimeOffset.Now);
    
    foreach (var candle in candles)
    {
        Console.WriteLine(candle);
    }
    ...
See Also

Other Resources