def get_forecast_matrix_over_code(
        data_backtest,
        stage_name="combForecast",
        method_name="get_forecast_weights"):
    instrument_codes = data_backtest.system.get_instrument_list()
    trading_rules = data_backtest.system.rules.trading_rules()
    trading_rule_names = list(trading_rules.keys())

    datetime_cutoff = from_marker_to_datetime(data_backtest.timestamp)

    value_dict = {}
    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]
        value_dict[instrument_code] = values_by_rule

    value_df = pd.DataFrame(value_dict, index=trading_rule_names)
    value_df = value_df.transpose()

    return value_df
Example #2
0
def get_list_of_values_by_instrument_for_config(data_backtest,
                                                config_for_method):
    instrument_codes = data_backtest.system.get_instrument_list()
    datetime_cutoff = from_marker_to_datetime(data_backtest.timestamp)

    stage = getattr(data_backtest.system, config_for_method.stage_name)
    method = getattr(stage, config_for_method.method_name)

    if config_for_method.global_bool:
        # Same value regardless of instrument
        value = method().ffill()[:datetime_cutoff].iloc[-1]
        if config_for_method.col_selector is not None:
            value = value[config_for_method.col_selector]
        value_list = [value] * len(instrument_codes)

        return value_list

    if config_for_method.requires_code_bool:
        # call for each code
        if config_for_method.col_selector is not None:
            value_list = [
                method(instrument_code).ffill()[:datetime_cutoff].iloc[-1][
                    config_for_method.col_selector]
                for instrument_code in instrument_codes
            ]
        else:
            value_list = [
                method(instrument_code).ffill()[:datetime_cutoff].iloc[-1]
                for instrument_code in instrument_codes
            ]

        return value_list

    if config_for_method.scalar_dict_bool:
        value = method()[config_for_method.scalar_dict_entry]
        value_list = [value] * len(instrument_codes)

        return value_list

    # get dataframe

    value_row = method().ffill()[:datetime_cutoff].iloc[-1]
    value_list = [
        value_row.get(instrument_code, np.nan)
        for instrument_code in instrument_codes
    ]

    return value_list
Example #3
0
def get_position_for_instrument_code_at_timestamp(data_backtest, data,
                                                  instrument_code):
    diag_positions = diagPositions(data)
    positions_over_time = diag_positions.get_position_df_for_strategy_and_instrument(
        data_backtest.strategy_name, instrument_code)
    if positions_over_time is missing_data:
        return np.nan

    datetime_cutoff = from_marker_to_datetime(data_backtest.timestamp)
    positions_over_time_ffill = positions_over_time.ffill()
    positions_before_cutoff = positions_over_time_ffill[:datetime_cutoff]

    if len(positions_before_cutoff) == 0:
        return np.nan
    final_position = positions_before_cutoff.iloc[-1].position

    return final_position
Example #4
0
def get_forecast_matrix(
    data_backtest, stage_name="combForecast", method_name="get_capped_forecast"
):
    instrument_codes = data_backtest.system.get_instrument_list()
    trading_rules = data_backtest.system.rules.trading_rules()
    trading_rule_names = list(trading_rules.keys())

    datetime_cutoff = from_marker_to_datetime(data_backtest.timestamp)

    value_dict = {}
    for rule_name in trading_rule_names:
        value_dict[rule_name] = []
        for instrument_code in instrument_codes:
            stage = getattr(data_backtest.system, stage_name)
            method = getattr(stage, method_name)
            value = method(instrument_code, rule_name).ffill()[:datetime_cutoff][-1]
            value_dict[rule_name].append(value)

    value_df = pd.DataFrame(value_dict, index=instrument_codes)

    return value_df