Click or drag to resize


All processes of the data saving and recovery in the S# run through a special API, located in the StockSharp.Algo.Storages section. There is the IStorageRegistry, interface in this section, which created to describe all the possible actions with the storage and contains such properties as Securities, Positions etc. Through these properties, it is possible to get all the previously saved trading objects, for example, instruments. All processes run as with regular collection using the IStorageEntityListT interface. If you want to save the trading object in the storage (for example, a new order appeared or previously registered order updated), you should use the IStorageEntityListTSave(T) method.

The default implementation of the IStorageRegistry interface is the StorageRegistry class. It interacts with the data through a low-level system interface IStorage. This interface provides a transparent work with databases, hiding details.

The work with market data, such as tick trades or order books, takes place through the certain interface IMarketDataStorageTMessage, which is obtained based on the information about the instrument through IStorageRegistryGetTradeStorage mathod. IStorageRegistryGetMarketDepthStorage for tick trades or order books accordingly.

If the StorageRegistry used, the implementation of methods with market data does not depend on StorageRegistryDefaultDrive, since the data is always stored in the file. This is the internal format of the S#, and it is organized in such a way that the trades or order books takes a minimum of disk space. The path to the directory where the market data will be stored (or read) indicated by the LocalMarketDataDrivePath property of the IStorageRegistryDefaultDrive storage.

The folders with the names equal instruments identifiers (separate folder for each instrument) will be created at this path.

Subfolders, indicating the date of the market data will be created inside each of these folders. For example, if you save the tick trades for a period of 3 days, then three separate folders with dates will be created. The format of the folder name is always fixed as yyyy_MM_dd.

Files with the bin extension are inside each folder with dates. Trades are stored in the trades.bin file, order books in the quotes.bin file. Also the candles_XXX.bin files may be present, which store various types of candles (filename indicates the type and parameter of candles) and the orderLog.bin files where the order log stored.

Example of working with market data storage

  1. The SampleStorage example, located in the S# installation package, shows how to save and load the trades through the StorageRegistryclass. In the beginning an instrument created and its basic properties are initialized - Id (to determine location on disk), StepPrice and Decimals (for decimal value compression in the trades.bin file):

    var security = new Security
        Id = "TestId",
        MinStepSize = 0.1,
        Decimals = 1,
  2. Further, as initial data, a list of the 1,000 undefined trades created (in a real application, it will be those trades that are get from external sources or trade application):

    var trades = new List<Trade>();
    // generating 1000 random ticks
    var tradeGenerator = new RandomWalkTradeGenerator(security, 99)
        IdGenerator = new IdGenerator
            Current = DateTime.Now.Ticks
    // generator initialization
    for (var i = 0; i < 1000; i++)
        var t = tradeGenerator.Generate(DateTime.Today + TimeSpan.FromMinutes(i));
        t.Id = i + 1;
  3. The StorageRegistry itself created on the next step:

    var storage = new StorageRegistry();
  4. The market data storage obtained via the trade objects store. The tick trades storage, which is obtained via the StorageRegistryGetTradeStorage(Security, IMarketDataDrive, StorageFormats) method, used in example:

    var tradeStorage = storage.GetTradeStorage(security);
  5. The saving of trades:

  6. The loading of trades, which were saved in the previous step:

    var loadedTrades = tradeStorage.Load(DateTime.Today, DateTime.Today + TimeSpan.FromMinutes(1000));
    foreach (var trade in loadedTrades)
        Console.WriteLine("Tick № {0}: {1}", trade.Id, trade);
  7. Since the trades are stored in the file, the next time you run the sample, they will be present there, and you will get 2000 trades instead of 1000. For correct work of example, it is necessary to remove the trades at the end of the work:

    tradeStorage.Delete(DateTime.Today, DateTime.Today + TimeSpan.FromMinutes(1000));