Click or drag to resize

Google Finance

For backtesting in the HistoryEmulationConnector class, along with IStorageRegistry data storage using, an alternative mechanism for working with data sources is provided. This mechanism allows to download data directly from servers of the historical information providers, in particular from the Quandl, Google Finance, or to work with one’s own data sources.

Consider the work of this mechanism using the example of the data getting from the Quandl. To work with Google Finance there is special QuandlHistorySource class in S#, which allows to get candles, ticks and information about instruments.

Testing with data downloaded from Quandl

  1. At first we need to get information about the instruments from the Quandl service. To do this, it is necessary to create a storage for the instruments (QuandlSecurityStorage). This is a class that implements the ISecurityStorage interface. The code of this class is in the example Samples/Testing/SampleHistoryTesting.

    1. Creating the instrument.

      var security = new Security
      {
          Id = secid,
          Code = secCode,
          Board = board
      };
    2. Creating the instances of classes of the instrument storage and data loader from Quandl. Note that in this example we pass one instrument to the QuandlSecurityStorage constructor. If it is necessary to work with multiple instruments, you can modify the constructor code to pass an array of instruments.

      var qlSecurityStorage = new QuandlSecurityStorage(security);
      var qlHistorySource = new QuandlHistorySource();
    3. Next, it is necessary to update the instrument storage. The QuandlHistorySourceRefresh(ISecurityStorage, Security, ActionSecurity, FuncBoolean) method is used for it. In fact, this method records to the SecurityExtensionInfo property the instrument and site codes used in Quandl, which will be used to download data by the instrument. Note that the first argument of the method is of the ISecurityStorage type - this interface has been implemented in QuandlSecurityStorage. Also it is necessary to bear in mind that the second argument (Security) performs the template function while updating instruments of the storage.

      qlHistorySource.Refresh(qlSecurityStorage, security, s => {}, () => false);
  2. Now it is necessary to register a new data source in HistoryEmulationConnector. This is done using the HistoryEmulationConnectorRegisterHistorySource(Security, MarketDataTypes, Object, FuncDateTimeOffset, IEnumerableMessage) method. The first argument of the method is the instrument. Next, the source data type is specified. In our case these are usual candles CandleTimeFrame. The third argument is a parameter related to the market data type. For ordinary candles this is the candles timeframe.

    In the last argument it is necessary to specify a function that returns the market data. At that the function must return a value of the IEnumerable<Message> type and take a parameter of the DateTimeOffset type. In our case, the method that requests the candles from Quandl is used - QuandlHistorySourceGetCandles(Security, TimeSpan, DateTime, DateTime).

    The data will be downloaded after the connector’s start, and the values of the StartDate and StopDate properties from HistoryMessageAdapter will be used as the start and end dates of the download interval.

    connector.RegisterHistorySource(security, MarketDataTypes.CandleTimeFrame, TimeSpan.FromMinutes(1), d =>
        qlHistorySource.GetCandles(security, TimeSpan.FromMinutes(1), d.Date, d.Date));