diff --git a/Algo.Analytics.Python/biggest_candle_script.py b/Algo.Analytics.Python/biggest_candle_script.py index e39d71189..49a672f91 100644 --- a/Algo.Analytics.Python/biggest_candle_script.py +++ b/Algo.Analytics.Python/biggest_candle_script.py @@ -5,11 +5,9 @@ clr.AddReference("StockSharp.Messages") clr.AddReference("StockSharp.Algo.Analytics") from System import TimeSpan -from System import Array -from System import String from System.Threading.Tasks import Task from StockSharp.Algo.Analytics import IAnalyticsScript -from StockSharp.Messages import TimeFrameCandleMessage +from storage_extensions import * # The analytic script, shows biggest candle (by volume and by length) for specified securities. class biggest_candle_script(IAnalyticsScript): @@ -30,8 +28,8 @@ class biggest_candle_script(IAnalyticsScript): break # get candle storage - candle_storage = storage.GetTimeFrameCandleMessageStorage(security, time_frame, drive, format) - all_candles = list(candle_storage.Load(from_date, to_date)) + candle_storage = get_tf_candle_storage(storage, security, time_frame, drive, format) + all_candles = load_tf_candles(candle_storage, from_date, to_date) 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 c298988e0..6fa92d60d 100644 --- a/Algo.Analytics.Python/chart3d_script.py +++ b/Algo.Analytics.Python/chart3d_script.py @@ -5,11 +5,9 @@ clr.AddReference("StockSharp.Messages") clr.AddReference("StockSharp.Algo.Analytics") from System import TimeSpan -from System import Array -from System import String from System.Threading.Tasks import Task from StockSharp.Algo.Analytics import IAnalyticsScript -from StockSharp.Messages import TimeFrameCandleMessage +from storage_extensions import * # The analytic script, calculating distribution of the biggest volume by hours and shows its in 3D chart. class chart3d_script(IAnalyticsScript): @@ -39,7 +37,7 @@ class chart3d_script(IAnalyticsScript): y.append(str(h)) # Create a 2D array for Z values with dimensions: (number of securities) x (number of hours) - z = Array.CreateInstance(Double, securities.Length, len(y)) + z = [[0.0 for _ in range(len(y))] for _ in range(len(securities))] for i in range(securities.Length): # Stop calculation if user cancels script execution @@ -52,17 +50,17 @@ class chart3d_script(IAnalyticsScript): x.append(security.ToStringId()) # Get candle storage for current security - candle_storage = storage.GetTimeFrameCandleMessageStorage(security, time_frame, drive, format) + candle_storage = get_tf_candle_storage(storage, security, time_frame, drive, format) # Get available dates for the specified period - dates = list(candle_storage.GetDates(from_date, to_date)) + dates = get_dates(candle_storage, from_date, to_date) 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 = list(candle_storage.Load(from_date, to_date)) + candles = load_tf_candles(candle_storage, from_date, to_date) 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 8e29d236a..36bdd33f3 100644 --- a/Algo.Analytics.Python/chart_draw_script.py +++ b/Algo.Analytics.Python/chart_draw_script.py @@ -5,11 +5,9 @@ clr.AddReference("StockSharp.Messages") clr.AddReference("StockSharp.Algo.Analytics") from System import TimeSpan -from System import Array -from System import String from System.Threading.Tasks import Task from StockSharp.Algo.Analytics import IAnalyticsScript -from StockSharp.Messages import TimeFrameCandleMessage +from storage_extensions import * # The analytic script, shows chart drawing possibilities. class chart_draw_script(IAnalyticsScript): @@ -44,9 +42,9 @@ class chart_draw_script(IAnalyticsScript): vols_series = {} # Get candle storage for the current security - candle_storage = storage.GetTimeFrameCandleMessageStorage(security, time_frame, drive, format) + candle_storage = get_tf_candle_storage(storage, security, time_frame, drive, format) - for candle in candle_storage.Load(from_date, to_date): + for candle in load_tf_candles(candle_storage, from_date, to_date): # 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 new file mode 100644 index 000000000..626d5a174 --- /dev/null +++ b/Algo.Analytics.Python/common/storage_extensions.py @@ -0,0 +1,121 @@ +import clr + +# Add reference to StockSharp.Algo +clr.AddReference("StockSharp.Algo") + +from StockSharp.Algo.Storages import StorageHelper +from StockSharp.Messages import TimeFrameCandleMessage + +def get_dates(storage, from_date, to_date): + """ + Helper function to mimic the GetDates extension method. + :param storage: The storage object. + :param from_date: The start date of the range. + :param to_date: The end date of the range. + :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)) + +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): + """ + Helper function to mimic the Load extension method. + :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. + :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)) + +def get_tf_candle_storage(registry, security, time_frame, drive, format): + """ + Helper function to get TimeFrameCandleMessage storage. + :param registry: The storage registry. + :param security: The security object. + :param time_frame: The time frame for candles. + :param drive: The market data drive. + :param format: The storage format. + :return: The storage for TimeFrameCandleMessage. + """ + return registry.GetCandleMessageStorage(TimeFrameCandleMessage, security, time_frame, drive, format) + +def get_tick_candle_storage(registry, security, tick_count, drive, format): + """ + Helper function to get TickCandleMessage storage. + :param registry: The storage registry. + :param security: The security object. + :param tick_count: The number of ticks per candle. + :param drive: The market data drive. + :param format: The storage format. + :return: The storage for TickCandleMessage. + """ + return registry.GetCandleMessageStorage(TickCandleMessage, security, tick_count, drive, format) + +def get_volume_candle_storage(registry, security, volume, drive, format): + """ + Helper function to get VolumeCandleMessage storage. + :param registry: The storage registry. + :param security: The security object. + :param volume: The volume per candle. + :param drive: The market data drive. + :param format: The storage format. + :return: The storage for VolumeCandleMessage. + """ + return registry.GetCandleMessageStorage(VolumeCandleMessage, security, volume, drive, format) + +def get_range_candle_storage(registry, security, range, drive, format): + """ + Helper function to get RangeCandleMessage storage. + :param registry: The storage registry. + :param security: The security object. + :param range: The range per candle. + :param drive: The market data drive. + :param format: The storage format. + :return: The storage for RangeCandleMessage. + """ + return registry.GetCandleMessageStorage(RangeCandleMessage, security, range, drive, format) + +def get_pnf_candle_storage(registry, security, box_size, reversal_amount, drive, format): + """ + Helper function to get PnFCandleMessage storage. + :param registry: The storage registry. + :param security: The security object. + :param box_size: The box size for Point and Figure candles. + :param reversal_amount: The reversal amount for Point and Figure candles. + :param drive: The market data drive. + :param format: The storage format. + :return: The storage for PnFCandleMessage. + """ + return registry.GetCandleMessageStorage(PnFCandleMessage, security, (box_size, reversal_amount), drive, format) + +def get_renko_candle_storage(registry, security, brick_size, drive, format): + """ + Helper function to get RenkoCandleMessage storage. + :param registry: The storage registry. + :param security: The security object. + :param brick_size: The brick size for Renko candles. + :param drive: The market data drive. + :param format: The storage format. + :return: The storage for RenkoCandleMessage. + """ + return registry.GetCandleMessageStorage(RenkoCandleMessage, security, brick_size, drive, format) + +def get_heikin_ashi_candle_storage(registry, security, time_frame, drive, format): + """ + Helper function to get HeikinAshiCandleMessage storage. + :param registry: The storage registry. + :param security: The security object. + :param time_frame: The time frame for candles. + :param drive: The market data drive. + :param format: The storage format. + :return: The storage for HeikinAshiCandleMessage. + """ + return registry.GetCandleMessageStorage(HeikinAshiCandleMessage, security, time_frame, drive, format) \ No newline at end of file diff --git a/Algo.Analytics.Python/empty_analytics_script.py b/Algo.Analytics.Python/empty_analytics_script.py index f431eba73..1b13c08e8 100644 --- a/Algo.Analytics.Python/empty_analytics_script.py +++ b/Algo.Analytics.Python/empty_analytics_script.py @@ -5,11 +5,9 @@ clr.AddReference("StockSharp.Messages") clr.AddReference("StockSharp.Algo.Analytics") from System import TimeSpan -from System import Array -from System import String from System.Threading.Tasks import Task from StockSharp.Algo.Analytics import IAnalyticsScript -from StockSharp.Messages import TimeFrameCandleMessage +from storage_extensions import * # The empty analytic strategy. class empty_analytics_script(IAnalyticsScript): diff --git a/Algo.Analytics.Python/indicator_script.py b/Algo.Analytics.Python/indicator_script.py index fe0c0511b..247ddecfd 100644 --- a/Algo.Analytics.Python/indicator_script.py +++ b/Algo.Analytics.Python/indicator_script.py @@ -5,11 +5,9 @@ clr.AddReference("StockSharp.Messages") clr.AddReference("StockSharp.Algo.Analytics") from System import TimeSpan -from System import Array -from System import String from System.Threading.Tasks import Task from StockSharp.Algo.Analytics import IAnalyticsScript -from StockSharp.Messages import TimeFrameCandleMessage +from storage_extensions import * # The analytic script, using indicator ROC. class indicator_script(IAnalyticsScript): @@ -34,9 +32,9 @@ class indicator_script(IAnalyticsScript): roc = RateOfChange() # get candle storage - candle_storage = storage.GetTimeFrameCandleMessageStorage(security, time_frame, drive, format) + candle_storage = get_tf_candle_storage(storage, security, time_frame, drive, format) - for candle in candle_storage.Load(from_date, to_date): + for candle in load_tf_candles(candle_storage, from_date, to_date): # fill series candles_series[candle.OpenTime] = candle.ClosePrice indicator_series[candle.OpenTime] = roc.Process(candle).ToDecimal() diff --git a/Algo.Analytics.Python/normalize_price_script.py b/Algo.Analytics.Python/normalize_price_script.py index fc26a10fc..f4ceab854 100644 --- a/Algo.Analytics.Python/normalize_price_script.py +++ b/Algo.Analytics.Python/normalize_price_script.py @@ -5,11 +5,9 @@ clr.AddReference("StockSharp.Messages") clr.AddReference("StockSharp.Algo.Analytics") from System import TimeSpan -from System import Array -from System import String from System.Threading.Tasks import Task from StockSharp.Algo.Analytics import IAnalyticsScript -from StockSharp.Messages import TimeFrameCandleMessage +from storage_extensions import * # The analytic script, normalize securities close prices and shows on same chart. class normalize_price_script(IAnalyticsScript): @@ -28,11 +26,11 @@ class normalize_price_script(IAnalyticsScript): series = {} # get candle storage - candle_storage = storage.GetTimeFrameCandleMessageStorage(security, time_frame, drive, format) + candle_storage = get_tf_candle_storage(storage, security, time_frame, drive, format) first_close = None - for candle in candle_storage.Load(from_date, to_date): + for candle in load_tf_candles(candle_storage, from_date, to_date): 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 d52e55ac9..8e8bb727a 100644 --- a/Algo.Analytics.Python/pearson_correlation_script.py +++ b/Algo.Analytics.Python/pearson_correlation_script.py @@ -5,11 +5,9 @@ clr.AddReference("StockSharp.Messages") clr.AddReference("StockSharp.Algo.Analytics") from System import TimeSpan -from System import Array -from System import String from System.Threading.Tasks import Task from StockSharp.Algo.Analytics import IAnalyticsScript -from StockSharp.Messages import TimeFrameCandleMessage +from storage_extensions import * # The analytic script, calculating Pearson correlation by specified securities. class pearson_correlation_script(IAnalyticsScript): @@ -38,10 +36,10 @@ class pearson_correlation_script(IAnalyticsScript): break # get candle storage - candle_storage = storage.GetTimeFrameCandleMessageStorage(security, time_frame, drive, format) + candle_storage = get_tf_candle_storage(storage, security, time_frame, drive, format) # get closing prices - prices = [float(c.ClosePrice) for c in candle_storage.Load(from_date, to_date)] + prices = [float(c.ClosePrice) for c in load_tf_candles(candle_storage, from_date, to_date)] 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 1db31d73e..3f49c09c6 100644 --- a/Algo.Analytics.Python/price_volume_script.py +++ b/Algo.Analytics.Python/price_volume_script.py @@ -5,11 +5,9 @@ clr.AddReference("StockSharp.Messages") clr.AddReference("StockSharp.Algo.Analytics") from System import TimeSpan -from System import Array -from System import String from System.Threading.Tasks import Task from StockSharp.Algo.Analytics import IAnalyticsScript -from StockSharp.Messages import TimeFrameCandleMessage +from storage_extensions import * # The analytic script, calculating distribution of the volume by price levels. class price_volume_script(IAnalyticsScript): @@ -35,17 +33,17 @@ class price_volume_script(IAnalyticsScript): security = securities[0] # Get candle storage - candle_storage = storage.GetTimeFrameCandleMessageStorage(security, time_frame, drive, format) + candle_storage = get_tf_candle_storage(storage, security, time_frame, drive, format) # Get available dates for the specified period - dates = list(candle_storage.GetDates(from_date, to_date)) + dates = get_dates(candle_storage, from_date, to_date) if len(dates) == 0: logs.LogWarning("no data") return Task.CompletedTask # Grouping candles by middle price and summing their volumes - candles = list(candle_storage.Load(from_date, to_date)) + candles = load_tf_candles(candle_storage, from_date, to_date) 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 342b3842d..e16599162 100644 --- a/Algo.Analytics.Python/time_volume_script.py +++ b/Algo.Analytics.Python/time_volume_script.py @@ -5,11 +5,9 @@ clr.AddReference("StockSharp.Algo.Analytics") clr.AddReference("StockSharp.Messages") from System import TimeSpan -from System import Array -from System import String from System.Threading.Tasks import Task from StockSharp.Algo.Analytics import IAnalyticsScript -from StockSharp.Messages import TimeFrameCandleMessage +from storage_extensions import * # The analytic script, calculating distribution of the biggest volume by hours. class time_volume_script(IAnalyticsScript): @@ -35,17 +33,17 @@ class time_volume_script(IAnalyticsScript): security = securities[0] # Get candle storage - candle_storage = storage.GetTimeFrameCandleMessageStorage(security, time_frame, drive, format) + candle_storage = get_tf_candle_storage(storage, security, time_frame, drive, format) # Get available dates for the specified period - dates = list(candle_storage.GetDates(from_date, to_date)) + dates = get_dates(candle_storage, from_date, to_date) if len(dates) == 0: logs.LogWarning("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)) + candles = load_tf_candles(candle_storage, from_date, to_date) rows = {} for candle in candles: # Truncate TimeOfDay to the nearest hour diff --git a/Configuration/Paths.cs b/Configuration/Paths.cs index 2a39324f1..9f7dadeb4 100644 --- a/Configuration/Paths.cs +++ b/Configuration/Paths.cs @@ -32,6 +32,7 @@ public static class Paths CompilerCacheDir = Path.Combine(AppDataPath, "compiler_cache"); LogsDir = Path.Combine(AppDataPath, "Logs"); ReportLogsPath = Path.Combine(AppDataPath, "BugReports"); + PythonUtilsPath = Path.Combine(CompanyPath, "Python"); InstallerDir = Path.Combine(CompanyPath, "Installer"); InstallerInstallationsConfigPath = Path.Combine(InstallerDir, $"installer_apps_installed{DefaultSettingsExt}"); @@ -124,6 +125,11 @@ public static class Paths /// public static readonly string AppDataPath; + /// + /// The path to the Python utils directory. + /// + public static readonly string PythonUtilsPath; + /// /// The path to the settings directory. ///