Click or drag to resize

Messages storage

Along with the use of trading object storages, you can use message storages. Work with these storages is also carried out through the IMarketDataStorageTData. interface. For example, to work with candles, you can use the storage of the IMarketDataStorage<CandleMessage> type. IStorageRegistry also contains a set of methods for obtaining the required message storages. So, the time candle storage can be obtained, as shown in the following code snippet.

C#
var security = new Security() { Id = "ESM5@NYSE" };
var candleMessageStorage = _storage.GetCandleMessageStorage(typeof(TimeFrameCandleMessage), security, TimeSpan.FromMinutes(1));

Then you can work with the storage using IMarketDataStorageLoad(DateTime) and/or IMarketDataStorageSave(IEnumerable) methods, as shown in the example in the API.

Note that S# allows you to convert the trading object storage types to the corresponding types of message storages and vice versa. For example, IMarketDataStorage<MarketDepth> can be converted to the IMarketDataStorage<QuoteChangeMessage> type and vice versa.

C#
var security = new Security() { Id = "ESM5@NYSE" };

var depthStorage = _storage.GetMarketDepthStorage(_security);
var quoteMessageStorage = depthStorage as IMarketDataStorage<QuoteChangeMessage>;

var quoteMessageStorage1 = _storage.GetQuoteMessageStorage(_security);
var depthStorage1 = quoteMessageStorage1 as IMarketDataStorage<MarketDepth>;

It should pay attention to the relevance of using the message storage to store own orders and trades. The matter is that for these trading objects there are no corresponding storages. In this case, you must use the IMarketDataStorage<ExecutionMessage> message storage. The following example shows how to store own trades in the storage.

Example of saving own trades

  1. 1. First, an instance of the connector is created, as well as a storage. In addition, we specify the identifier of the instrument with which we will work and declare a variable for the transaction storage. The transaction storage itself for the specified instrument will be received in the instrument getting event using the IStorageRegistryGetTransactionStorage method.

    C#
    var connector = new OpenECryTrader();
    
    var securityId = "ESM5@NYSE";
    
    // storage to historical data
    var storageRegistry = new StorageRegistry
    {
        // set historical path
        DefaultDrive = new LocalMarketDataDrive(HistoryPath.Folder)
    };
    
    IMarketDataStorage<ExecutionMessage> tranStorage = null;
    
    connector.NewSecurity += security =>
    {
            if (security.Id == securityId)
                tranStorage = _storage.GetTransactionStorage(security);
    };
  2. Saving own trades will be performed in the IConnectorNewMyTrades event handler using the IMarketDataStorageSave(IEnumerable). method. Before saving, the list of own trades is converted to the IEnumerable<ExecutionMessage> type. The procedure for order registering itself in this example is omitted.

    C#
    connector.NewMyTrades += trades =>
    {
        tranStorage.Save((IEnumerable<ExecutionMessage>)trades);
    };
See Also

Other Resources