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 Google Finance server, or to work with one’s own data sources.

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

Testing with data downloaded from Google Finance

  1. At first we need to get information about the instruments from the Google Finance service. To do this, it is necessary to create a storage for the instruments (GoogleSecurityStorage). 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 Google Finance. Note that in this example we pass one instrument to the GoogleSecurityStorage constructor. If it is necessary to work with multiple instruments, you can modify the constructor code to pass an array of instruments.

      var googleSecurityStorage = new GoogleSecurityStorage(security);
      var googleHistorySource = new GoogleHistorySource();
    3. Next, it is necessary to update the instrument storage. The GoogleHistorySource.Refresh(ISecurityStorage, INativeIdStorage, Security, ActionSecurity, FuncBoolean) method is used for it. In fact, this method records to the SecurityExtensionInfo property the instrument and site codes used in Google Finance, 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 GoogleSecurityStorage. Also it is necessary to bear in mind that the second argument (Security) performs the template function while updating instruments of the storage.

      googleHistorySource.Refresh(googleSecurityStorage, 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 Google Finance is used - GoogleHistorySource.GetCandles(Security, INativeIdStorage, 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 =>
        _googleHistorySource.GetCandles(security, TimeSpan.FromMinutes(1), d.Date, d.Date));