mirror of
https://github.com/StockSharp/StockSharp.git
synced 2026-12-04 08:32:23 +00:00
74 lines
2.3 KiB
Python
74 lines
2.3 KiB
Python
import clr
|
|
|
|
# Add .NET references
|
|
clr.AddReference("System")
|
|
clr.AddReference("System.Core")
|
|
clr.AddReference("System.Linq")
|
|
clr.AddReference("System.Threading")
|
|
clr.AddReference("System.Threading.Tasks")
|
|
clr.AddReference("StockSharp.Algo")
|
|
clr.AddReference("StockSharp.Algo.Analytics")
|
|
clr.AddReference("StockSharp.BusinessEntities")
|
|
clr.AddReference("StockSharp.Storages")
|
|
clr.AddReference("StockSharp.Logging")
|
|
clr.AddReference("StockSharp.Messages")
|
|
|
|
from System import DateTime, TimeSpan
|
|
from System.Linq import Enumerable
|
|
from System.Threading.Tasks import Task
|
|
from StockSharp.Algo.Analytics import IAnalyticsScript
|
|
|
|
# The analytic script, calculating distribution of the biggest volume by hours.
|
|
class time_volume_script(IAnalyticsScript):
|
|
def Run(
|
|
self,
|
|
logs,
|
|
panel,
|
|
securities,
|
|
from_date,
|
|
to_date,
|
|
storage,
|
|
drive,
|
|
format,
|
|
time_frame,
|
|
cancellation_token
|
|
):
|
|
# Check if there are no instruments
|
|
if not securities:
|
|
logs.AddWarningLog("No instruments.")
|
|
return Task.CompletedTask
|
|
|
|
# Script can process only 1 instrument
|
|
security = Enumerable.First(securities)
|
|
|
|
# Get candle storage
|
|
candle_storage = storage.GetTimeFrameCandleMessageStorage(security, time_frame, drive, format)
|
|
|
|
# Get available dates for the specified period
|
|
dates = list(candle_storage.GetDates(from_date, to_date))
|
|
|
|
if len(dates) == 0:
|
|
logs.AddWarningLog("no data")
|
|
return Task.CompletedTask
|
|
|
|
# Grouping candles by opening time (hourly truncation) and summing their volumes
|
|
candles = list(candle_storage.Load(from_date, to_date))
|
|
rows = {}
|
|
for candle in candles:
|
|
# Truncate TimeOfDay to the nearest hour
|
|
time_of_day = candle.OpenTime.TimeOfDay
|
|
truncated = TimeSpan.FromHours(int(time_of_day.TotalHours))
|
|
# Sum volumes for each truncated hour
|
|
rows[truncated] = rows.get(truncated, 0) + candle.TotalVolume
|
|
|
|
# Put our calculations into grid
|
|
grid = panel.CreateGrid("Time", "Volume")
|
|
|
|
for key, value in rows.items():
|
|
grid.SetRow(key, value)
|
|
|
|
# Sorting by Volume column in descending order
|
|
grid.SetSort("Volume", False)
|
|
|
|
return Task.CompletedTask
|