Table of Contents

Working with the API

Preparation

For working with historical data in the examples, a NuGet package with historical data samples is used. It can be installed from the NuGet Gallery. This package provides a set of data that can be used to demonstrate working with the storage.

All codes are available in the StockSharp repository.

Creating a Storage Registry

To work with market data storage in StockSharp, the StorageRegistry class is used. When creating an object of this class, you can set the path to the default storage via the StorageRegistry.DefaultDrive property or specify a specific folder for working with historical data using the LocalMarketDataDrive.

// Creating StorageRegistry with default path
var storageRegistry = new StorageRegistry();
// Creating StorageRegistry with the path to data from the NuGet package
var pathHistory = Paths.HistoryDataPath; // path to data from the NuGet package
var localDrive = new LocalMarketDataDrive(pathHistory);
var storageRegistry = new StorageRegistry()
{
    DefaultDrive = localDrive,
};

Retrieving Data

Through the StorageRegistry, you can access various types of market data for the desired time range. The methods used for this are:

Each of these methods returns the corresponding storage, from which data can be loaded using the Load method, specifying the start and end dates.

// Retrieving candles
var securityId = "SBER@TQBR".ToSecurityId();
var candleStorage = storageRegistry.GetTimeFrameCandleMessageStorage(securityId, TimeSpan.FromMinutes(1), StorageFormats.Binary);
var candles = candleStorage.Load(new DateTime(2020, 4, 1), new DateTime(2020, 4, 2));

foreach (var candle in candles)
{
    Console.WriteLine(candle);
}
// Retrieving ticks
var tradeStorage = storageRegistry.GetTickMessageStorage(securityId, StorageFormats.Binary);
var trades = tradeStorage.Load(new DateTime(2020, 4, 1), new DateTime(2020, 4, 2));

foreach (var trade in trades)
{
    Console.WriteLine(trade);
}
// Retrieving order books
var marketDepthStorage = storageRegistry.GetQuoteMessageStorage(securityId, StorageFormats.Binary);
var marketDepths = marketDepthStorage.Load(new DateTime(2020, 4, 1), new DateTime(2020, 4, 2));

foreach (var marketDepth in marketDepths)
{
    Console.WriteLine(marketDepth);
}

Saving Data

To save new data to the existing storage, use the Save method of the corresponding storage. This allows you to supplement historical data with new values.

// Saving new candles
var newCandles = new List<CandleMessage>
{
    // New CandleMessage objects are created here
};
candleStorage.Save(newCandles);
// Saving new ticks
var newTrades = new List<ExecutionMessage>
{
    // New ExecutionMessage objects for ticks are created here
};
tradeStorage.Save(newTrades);
// Saving new order books
var newMarketDepths = new List<QuoteChangeMessage>
{
    // New QuoteChangeMessage objects for order books are created here
};
marketDepthStorage.Save(newMarketDepths);

Deleting Data

To delete data for a specific period, use the Delete method of the corresponding storage. Be careful when deleting data from the sample package.

// Deleting candles for the specified period
candleStorage.Delete(new DateTime(2020, 4, 1), new DateTime(2020, 4, 2));
// Deleting ticks for the specified period
tradeStorage.Delete(new DateTime(2020, 4, 1), new DateTime(2020, 4, 2));
// Deleting order books for the specified period
marketDepthStorage.Delete(new DateTime(2020, 4, 1), new DateTime(2020, 4, 2));

These operations allow you to effectively manage historical data, whether loaded through Hydra, provided in the NuGet package, or created during the operation of your application.