mirror of
https://github.com/StockSharp/StockSharp.git
synced 2026-12-04 00:21:31 +00:00
73 lines
2.3 KiB
Python
73 lines
2.3 KiB
Python
import clr
|
|
|
|
# Add .NET references
|
|
clr.AddReference("StockSharp.Messages")
|
|
clr.AddReference("StockSharp.Algo.Analytics")
|
|
clr.AddReference("Ecng.Drawing")
|
|
|
|
from Ecng.Drawing import DrawStyles
|
|
from System import TimeSpan
|
|
from System.Threading.Tasks import Task
|
|
from StockSharp.Algo.Analytics import IAnalyticsScript
|
|
from storage_extensions import *
|
|
from candle_extensions import *
|
|
from chart_extensions import *
|
|
from indicator_extensions import *
|
|
|
|
# The analytic script, calculating distribution of the volume by price levels.
|
|
class price_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.LogWarning("No instruments.")
|
|
return Task.CompletedTask
|
|
|
|
# Script can process only 1 instrument
|
|
security = securities[0]
|
|
|
|
logs.LogInfo("Processing {0}...", security)
|
|
|
|
# Get candle storage
|
|
candle_storage = get_candle_storage(storage, security, time_frame, drive, format)
|
|
|
|
# Get available dates for the specified period
|
|
dates = get_dates(candle_storage, from_date, to_date, cancellation_token)
|
|
|
|
if len(dates) == 0:
|
|
logs.LogWarning("no data")
|
|
return Task.CompletedTask
|
|
|
|
# Grouping candles by middle price and summing their volumes
|
|
rows_dict = {}
|
|
prev_date = None
|
|
|
|
for candle in iter_candles(candle_storage, from_date, to_date, cancellation_token):
|
|
# Log date change
|
|
curr_date = candle.OpenTime.Date
|
|
if curr_date != prev_date:
|
|
prev_date = curr_date
|
|
logs.LogInfo(" {0}...", curr_date.ToString("yyyy-MM-dd"))
|
|
|
|
# Calculate middle price of the candle
|
|
key = candle.LowPrice + get_length(candle) / 2
|
|
# Sum volumes for same price level
|
|
rows_dict[key] = rows_dict.get(key, 0) + candle.TotalVolume
|
|
|
|
# Draw on chart
|
|
chart = create_chart(panel, float, float)
|
|
chart.Append(to_string_id(security), list(rows_dict.keys()), list(rows_dict.values()), DrawStyles.Histogram)
|
|
|
|
return Task.CompletedTask
|