namespace StockSharp.Algo.Analytics;
///
/// The analytic script, calculating distribution of the biggest volume by hours
/// and shows its in 3D chart.
///
public class Chart3DScript : IAnalyticsScript
{
async Task IAnalyticsScript.Run(ILogReceiver logs, IAnalyticsPanel panel, SecurityId[] securities, DateTime from, DateTime to, IStorageRegistry storage, IMarketDataDrive drive, StorageFormats format, DataType dataType, CancellationToken cancellationToken)
{
if (securities.Length == 0)
{
logs.LogWarning("No instruments.");
return;
}
var x = new List();
var y = new List();
// fill Y labels
for (var h = 0; h < 24; h++)
y.Add(h.ToString());
var z = new double[securities.Length, y.Count];
for (var i = 0; i < securities.Length; i++)
{
// stop calculation if user cancel script execution
if (cancellationToken.IsCancellationRequested)
break;
var security = securities[i];
logs.LogInfo("Processing {0} of {1}: {2}...", i + 1, securities.Length, security);
// fill X labels
x.Add(security.ToStringId());
// get candle storage
var candleStorage = storage.GetCandleMessageStorage(security, dataType, drive, format);
// get available dates for the specified period
var dates = await candleStorage.GetDatesAsync(from, to).ToArrayAsync(cancellationToken);
if (dates.Length == 0)
{
logs.LogWarning("no data");
return;
}
// grouping candles by opening time (time part only) with 1 hour truncating
var byHours = new Dictionary();
var prevDate = default(DateOnly);
await foreach (var candle in candleStorage.LoadAsync(from, to).WithCancellation(cancellationToken))
{
var currDate = DateOnly.FromDateTime(candle.OpenTime.Date);
if (currDate != prevDate)
{
prevDate = currDate;
logs.LogInfo(" {0}...", currDate);
}
var hour = candle.OpenTime.TimeOfDay.Truncate(TimeSpan.FromHours(1)).Hours;
if (byHours.TryGetValue(hour, out var volume))
byHours[hour] = volume + candle.TotalVolume;
else
byHours[hour] = candle.TotalVolume;
}
// fill Z values
foreach (var pair in byHours)
z[i, pair.Key] = (double)pair.Value;
}
panel.Draw3D(x, y, z, "Instruments", "Hours", "Volume");
}
}