Skip to content

trend scanning labels live market and bug: #551

Description

@ArjunWhabi

Hi, I understand that Trendscanning labels are used for labelling yout financial data (-1,0,1) to create your Y_train dataset. Then you train your ML model on X_train and Y_train. But in the live market, if you have a ML model predicting the outcome (-1,0,1), how do you know when to exit the trade?
The trendscanning method uses forward looking bars which is fine for labelling but in live trading you do not have access to future bars so how do you know when to exit ?
In the triple barrier method its quite simple, when price hits either Profit Target, StopLoss or Vertical barier you exit the trade.

Secondly, the trend scanning labels code has a mistake.
The original code assigns label_endtime_index = subset.index[max_metric_value_index - 1] whether you use look_forward = False or Look_foward=True. The above is correct if look_forward =True but in case of lookforward=False, to get the label_end_time 't1' which had the highest t_value the fix is given below.

`
if t_events is None:
t_events = price_series.index

t1_array = []  # Array of label end times
t_values_array = []  # Array of trend t-values

"""
 Everything that takes place inside the loop "for obs_id, index in enumerate(t_events): is what needs to be calculated in Multicharts"
 Multicharts calculates everything on a bar by bar basis. So the outputs that need to be returned to Multicharts are
 actually t_values_array[-1] and t1_array[-1] and last value in 'labels' .in other words the last values of the output arrays
 I also do not see this loop in the cnoverted code "for forward_window in np.arange(min_sample_length, subset.shape[0], step):"
"""

for obs_id, index in enumerate(t_events):
    print(obs_id)
    
    # Change of subset depending on looking forward or backward
    if look_forward:
        subset = price_series.loc[index:].iloc[:observation_window]  # Take t:t+L window
    else:
        subset = price_series.loc[:index].iloc[max(0, obs_id - observation_window):]  # Take t-L:t window
      
    if subset.shape[0] >= observation_window:
        # Loop over possible look-ahead windows to get the one which yields maximum t values for b_1 regression coef
        max_compare_value = -np.inf  # Maximum abs t-value of b_1 coefficient among l values or min MAE/MSE
        max_metric_value_index = None  # Index with maximum t-value or min MSE/MAE
        max_t_value = None  # Maximum t-value signed
        #raise BreakIt()    
        # Get optimal label end time value based on regression t-statistics
        for forward_window in np.arange(min_sample_length, subset.shape[0], step):
            # subset.loc[index is close[0]
            # Change of y_subset depending on looking forward or backward
            if look_forward:
                y_subset = subset.iloc[:forward_window].values.reshape(-1, 1)  # y{t}:y_{t+l}
                idx      = subset.iloc[:forward_window].index[-1]
            else:
                y_subset = subset.iloc[-forward_window:].values.reshape(-1, 1)  # y{t-l}:y_{t}
                idx      = subset.iloc[-forward_window:].index[0]
            #print(y_subset)
            # Array of [1, 0], [1, 1], [1, 2], ... [1, l] # b_0, b_1 coefficients
            X_subset = np.ones((y_subset.shape[0], 2))
            X_subset[:, 1] = np.arange(y_subset.shape[0])
            
            compare_value, t_beta_1 = _get_regression_quality_metric(X_subset, y_subset, metric)

            if compare_value > max_compare_value:
                max_compare_value = compare_value
                max_t_value = t_beta_1
                max_metric_value_index = forward_window
                
                if look_forward:
                    mylabel   = idx
                if not look_forward:
                    mylabel   = idx
            
            print(idx,"  compare: ",compare_value,"   tbeta:",t_beta_1,"    maxt:",max_t_value,"     ",forward_window)        
        
        # Store label information (t1, return)
         # CODE FIX STARTS HERE
        if look_forward:
            label_endtime_index = subset.index[max_metric_value_index - 1] # Close [-(max_metric_value_index-1)]
        else:
            label_endtime_index = subset.index[- max_metric_value_index] # Close [max_metric_value_index]

          # CODE FIX ENDS HERE
        `

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions