diff --git a/Algo.Analytics.Python/biggest_candle_script.py b/Algo.Analytics.Python/biggest_candle_script.py index f4f30b0b0..f8eb14eea 100644 --- a/Algo.Analytics.Python/biggest_candle_script.py +++ b/Algo.Analytics.Python/biggest_candle_script.py @@ -34,7 +34,7 @@ class biggest_candle_script(IAnalyticsScript): # get candle storage candle_storage = get_candle_storage(storage, security, time_frame, drive, format) - all_candles = load_tf_candles(candle_storage, from_date, to_date) + all_candles = load_tf_candles(candle_storage, from_date, to_date, cancellation_token) if len(all_candles) > 0: # first orders by volume desc will be our biggest candle diff --git a/Algo.Analytics.Python/chart3d_script.py b/Algo.Analytics.Python/chart3d_script.py index bfe59ad52..075321821 100644 --- a/Algo.Analytics.Python/chart3d_script.py +++ b/Algo.Analytics.Python/chart3d_script.py @@ -58,14 +58,14 @@ class chart3d_script(IAnalyticsScript): 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) + 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 opening time (truncated to the nearest hour) and summing volumes - candles = load_tf_candles(candle_storage, from_date, to_date) + candles = load_tf_candles(candle_storage, from_date, to_date, cancellation_token) by_hours = {} for candle in candles: # Truncate TimeOfDay to the nearest hour diff --git a/Algo.Analytics.Python/chart_draw_script.py b/Algo.Analytics.Python/chart_draw_script.py index 340e422d6..7dffca660 100644 --- a/Algo.Analytics.Python/chart_draw_script.py +++ b/Algo.Analytics.Python/chart_draw_script.py @@ -49,7 +49,7 @@ class chart_draw_script(IAnalyticsScript): # Get candle storage for the current security candle_storage = get_candle_storage(storage, security, time_frame, drive, format) - for candle in load_tf_candles(candle_storage, from_date, to_date): + for candle in load_tf_candles(candle_storage, from_date, to_date, cancellation_token): # Fill series with closing prices and volumes candles_series[candle.OpenTime] = candle.ClosePrice vols_series[candle.OpenTime] = candle.TotalVolume diff --git a/Algo.Analytics.Python/common/storage_extensions.py b/Algo.Analytics.Python/common/storage_extensions.py index ddc81153c..ed98833f5 100644 --- a/Algo.Analytics.Python/common/storage_extensions.py +++ b/Algo.Analytics.Python/common/storage_extensions.py @@ -3,9 +3,11 @@ import clr clr.AddReference("StockSharp.Messages") clr.AddReference("StockSharp.Algo") +from System.Threading import CancellationToken from StockSharp.Algo.Storages import StorageHelper from StockSharp.Messages import Extensions from StockSharp.Messages import TimeFrameCandleMessage +from StockSharp.Messages import CandleMessage def to_string_id(security_id): """ @@ -15,34 +17,69 @@ def to_string_id(security_id): """ return Extensions.ToStringId(security_id) -def get_dates(storage, from_date, to_date): +def get_dates(storage, from_date, to_date, cancellation_token=None): """ - Helper function to mimic the GetDates extension method. + Helper function to get dates from storage using async API. :param storage: The storage object. :param from_date: The start date of the range. :param to_date: The end date of the range. + :param cancellation_token: Optional cancellation token. :return: A list of dates within the specified range. """ - # Call the GetDates extension method from StorageHelper.GetDates - return list(StorageHelper.GetDates(storage, from_date, to_date)) + if cancellation_token is None: + cancellation_token = CancellationToken() + # Call async method and block waiting for result + all_dates = storage.GetDatesAsync(cancellation_token).GetAwaiter().GetResult() + return [d for d in all_dates if d >= from_date and d <= to_date] -def load_tf_candles(storage, from_date, to_date): - return load_range(storage, TimeFrameCandleMessage, from_date, to_date) - -def load_range(storage, message_type, from_date, to_date): +def load_tf_candles(storage, from_date, to_date, cancellation_token=None): """ - Helper function to mimic the Load extension method. + Helper function to load CandleMessage from storage. :param storage: The storage object. - :param message_type: The type of the message (e.g., TimeFrameCandleMessage). :param from_date: The start date of the range. :param to_date: The end date of the range. + :param cancellation_token: Optional cancellation token. :return: A list of candles within the specified date range. """ - # Get the generic method Load from StorageHelper - load_method = StorageHelper.Load[message_type] - - # Call the Load method with the specified type - return list(load_method(storage, from_date, to_date)) + return load_async_enumerable(storage, CandleMessage, from_date, to_date, cancellation_token) + +def load_range(storage, message_type, from_date, to_date, cancellation_token=None): + """ + Helper function to load messages from storage. + :param storage: The storage object. + :param message_type: The type of the message (e.g., CandleMessage). + :param from_date: The start date of the range. + :param to_date: The end date of the range. + :param cancellation_token: Optional cancellation token. + :return: A list of messages within the specified date range. + """ + return load_async_enumerable(storage, message_type, from_date, to_date, cancellation_token) + +def load_async_enumerable(storage, message_type, from_date, to_date, cancellation_token=None): + """ + Helper function to iterate over IAsyncEnumerable from LoadAsync. + :param storage: The storage object. + :param message_type: The .NET type for generic method (e.g., CandleMessage). + :param from_date: The start date of the range. + :param to_date: The end date of the range. + :param cancellation_token: Optional cancellation token. + :return: A list of messages within the specified date range. + """ + if cancellation_token is None: + cancellation_token = CancellationToken() + + # Get IAsyncEnumerable from LoadAsync extension method with explicit type + async_enumerable = StorageHelper.LoadAsync[message_type](storage, from_date, to_date, cancellation_token) + + # Iterate over IAsyncEnumerable by blocking on each MoveNextAsync + enumerator = async_enumerable.GetAsyncEnumerator(cancellation_token) + result = [] + try: + while enumerator.MoveNextAsync().GetAwaiter().GetResult(): + result.append(enumerator.Current) + finally: + enumerator.DisposeAsync().GetAwaiter().GetResult() + return result def get_candle_storage(registry, security, data_type, drive, format): """ diff --git a/Algo.Analytics.Python/indicator_script.py b/Algo.Analytics.Python/indicator_script.py index 8c953a2bc..8ecae909d 100644 --- a/Algo.Analytics.Python/indicator_script.py +++ b/Algo.Analytics.Python/indicator_script.py @@ -41,7 +41,7 @@ class indicator_script(IAnalyticsScript): # get candle storage candle_storage = get_candle_storage(storage, security, time_frame, drive, format) - for candle in load_tf_candles(candle_storage, from_date, to_date): + for candle in load_tf_candles(candle_storage, from_date, to_date, cancellation_token): # fill series candles_series[candle.OpenTime] = candle.ClosePrice indicator_series[candle.OpenTime] = to_decimal(process_candle(roc, candle)) diff --git a/Algo.Analytics.Python/normalize_price_script.py b/Algo.Analytics.Python/normalize_price_script.py index 55c88ef89..ff0d143ad 100644 --- a/Algo.Analytics.Python/normalize_price_script.py +++ b/Algo.Analytics.Python/normalize_price_script.py @@ -35,7 +35,7 @@ class normalize_price_script(IAnalyticsScript): first_close = None - for candle in load_tf_candles(candle_storage, from_date, to_date): + for candle in load_tf_candles(candle_storage, from_date, to_date, cancellation_token): if first_close is None: first_close = candle.ClosePrice diff --git a/Algo.Analytics.Python/pearson_correlation_script.py b/Algo.Analytics.Python/pearson_correlation_script.py index e5078471a..9ff097c19 100644 --- a/Algo.Analytics.Python/pearson_correlation_script.py +++ b/Algo.Analytics.Python/pearson_correlation_script.py @@ -48,7 +48,7 @@ class pearson_correlation_script(IAnalyticsScript): candle_storage = get_candle_storage(storage, security, time_frame, drive, format) # get closing prices - prices = [float(c.ClosePrice) for c in load_tf_candles(candle_storage, from_date, to_date)] + prices = [float(c.ClosePrice) for c in load_tf_candles(candle_storage, from_date, to_date, cancellation_token)] if len(prices) == 0: logs.LogWarning("No data for {0}", security) diff --git a/Algo.Analytics.Python/price_volume_script.py b/Algo.Analytics.Python/price_volume_script.py index 4ae640233..02db5eed3 100644 --- a/Algo.Analytics.Python/price_volume_script.py +++ b/Algo.Analytics.Python/price_volume_script.py @@ -41,14 +41,14 @@ class price_volume_script(IAnalyticsScript): 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) + 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 - candles = load_tf_candles(candle_storage, from_date, to_date) + candles = load_tf_candles(candle_storage, from_date, to_date, cancellation_token) rows_dict = {} for candle in candles: # Calculate middle price of the candle diff --git a/Algo.Analytics.Python/time_volume_script.py b/Algo.Analytics.Python/time_volume_script.py index 654c74aef..91f90d6c7 100644 --- a/Algo.Analytics.Python/time_volume_script.py +++ b/Algo.Analytics.Python/time_volume_script.py @@ -41,14 +41,14 @@ class time_volume_script(IAnalyticsScript): 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) + 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 opening time (hourly truncation) and summing their volumes - candles = load_tf_candles(candle_storage, from_date, to_date) + candles = load_tf_candles(candle_storage, from_date, to_date, cancellation_token) rows = {} for candle in candles: # Truncate TimeOfDay to the nearest hour