Пример как можно читать и писать bin файлы (кеш баров) на c#.
Пример чтения тиков из bin файла:
// нужно добавить TSLab.DataSource.dll, TSLab.DataSourceHelper.dll
using System;
using System.Collections.Generic;
using TSLab.DataSource;
namespace ReadWriteBins
{
public class Program
{
// путь ко всем кешам
private const string Folder = @"C:\Users\andre\AppData\Local\TSLab\TSLab 2.0";
// название папки с кешами
private const string CacheName = "BinanceFutures_TEMPCache";
private static void Main(string[] args)
{
SaveBins();
Console.WriteLine();
ReadBins();
Console.ReadKey();
}
private static void SaveBins()
{
var bars = new List<BidAskBar>
{
new BidAskBar(new DateTime(2020, 1, 1, 0, 1, 0), 10, 10, 10, 10, 10),
new BidAskBar(new DateTime(2020, 1, 1, 0, 2, 0), 20, 20, 20, 20, 20),
new BidAskBar(new DateTime(2020, 1, 1, 0, 3, 0), 30, 30, 30, 30, 30),
};
var cache = new BinaryCache<BidAskBar>(Folder, CacheName);
cache.SaveCached("BTCUSDT", "1M", bars, BaseBar.CacheVersion);
Console.WriteLine("Saved bars.");
}
private static void ReadBins()
{
var cache = new BinaryCache<BidAskBar>(Folder, CacheName);
var bars = cache.LoadCached("BTCUSDT", "1M", BaseBar.CacheVersionForLoad);
Console.WriteLine("Loaded bars:");
foreach (var bar in bars)
Console.WriteLine($"{bar}");
}
}
}
private static void ReadBinsTicks()
{
var cacheName = @"Tinkoff22CacheTrades";
var folder = @"C:\Users\andre\AppData\Local\TSLab\TSLab 2.0";
var file = new FileInfo(@"C:\Users\andre\AppData\Local\TSLab\TSLab 2.0\Tinkoff22CacheTrades\VTBR.09.13.2024.bin.gz");
var cache = new BinaryCache<TradeInfo>(folder, cacheName);
var trades = cache.LoadCached(file, true, HeaderLoader, TradeInfoCache.CurrentVersion, TradeInfoCache.MinVersionWithHeader);
Console.WriteLine($"Loaded {trades.Count} trades");
}
private static void HeaderLoader(BinaryReader reader, int version)
{
if (version >= 9)
{
// skip header info by default
var secInfo = new TradeCacheSecurityInfo();
secInfo.Restore(reader, version);
}
}