Skip to content

Commit

Permalink
Merge pull request #1468 from vishalg/bug_1467_forecast_weight_auto_g…
Browse files Browse the repository at this point in the history
…rouping

Fix for - #1467 forecast weight auto grouping
  • Loading branch information
bug-or-feature authored Dec 18, 2024
2 parents 58f54c1 + 0acedb8 commit 0484cd7
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
3 changes: 3 additions & 0 deletions syscore/pandas/frequency.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,9 @@ def interpolate_for_a_single_day(


def reindex_last_monthly_include_first_date(df: pd.DataFrame) -> pd.DataFrame:
if df.empty:
return df

df_monthly_index = list(df.resample("1M").last().index) ## last day in month
df_first_date_in_index = df.index[0]
df_monthly_index = [df_first_date_in_index] + df_monthly_index
Expand Down
14 changes: 10 additions & 4 deletions sysproduction/strategy_code/report_system_classic.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,10 +212,16 @@ def get_forecast_matrix_over_code(
for instrument_code in instrument_codes:
stage = getattr(data_backtest.system, stage_name)
method = getattr(stage, method_name)
value_row = method(instrument_code).ffill()[:datetime_cutoff].iloc[-1]
values_by_rule = [
value_row.get(rule_name, np.nan) for rule_name in trading_rule_names
]
values = method(instrument_code).ffill()[:datetime_cutoff]

if not values.empty:
value_row = values.iloc[-1]
values_by_rule = [
value_row.get(rule_name, np.nan) for rule_name in trading_rule_names
]
else:
values_by_rule = [np.nan] * len(trading_rule_names)

value_dict[instrument_code] = values_by_rule

value_df = pd.DataFrame(value_dict, index=trading_rule_names)
Expand Down
8 changes: 7 additions & 1 deletion systems/forecast_combine.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,9 @@ def get_unsmoothed_forecast_weights(self, instrument_code: str):
monthly_forecast_weights=monthly_forecast_weights,
)

if forecast_weights_fixed_to_forecasts.empty:
return forecast_weights_fixed_to_forecasts

# Remap to business day frequency so the smoothing makes sense also space saver
daily_forecast_weights_fixed_to_forecasts_unsmoothed = (
forecast_weights_fixed_to_forecasts.resample("1B").mean()
Expand Down Expand Up @@ -432,7 +435,7 @@ def get_trading_rule_list_for_estimated_weights(self, instrument_code: str) -> l
@diagnostic()
def get_forecasts_given_rule_list(
self, instrument_code: str, rule_variation_list: list
) -> pd.Series:
) -> pd.DataFrame:
"""
Convenience function to get a list of forecasts
Expand All @@ -445,6 +448,9 @@ def get_forecasts_given_rule_list(
for rule_variation_name in rule_variation_list
]

if not forecasts:
return pd.DataFrame(index=pd.DatetimeIndex([]))

forecasts = pd.concat(forecasts, axis=1)

forecasts.columns = rule_variation_list
Expand Down

0 comments on commit 0484cd7

Please sign in to comment.