It looks like in Section 10, Unit 9 stop_loss function stop_loss is applied not correctly.
Here is the code snippet:
data['stop_loss'+''+st_mt] = stop_loss(signal=data['s'+''+st_mt], close=data['rebased_close'],
s_low=data['srebased_low'], s_high=data['srebased_high'])
stop_loss() takes s_high and s_low data, merges two columns into one and keeps the first record by the signal as shown here
stoploss = (s_low.add(s_high, fill_value=0)).fillna(method='ffill') # join all swings in 1 column stoploss[~((np.isnan(signal.shift(1))) & (~np.isnan(signal)))] = np.nan # keep 1st sl by signal stoploss = stoploss.fillna(method='ffill') # extend first value with fillna
In the algorithm logic section, it's mentioned that stop loss price for long position is swing low preceding entry and for short is swing high before entry.
However, since data['s'+''+st_mt] signal column is the mix of ceiling/floor regime change and moving averages crossover swing low / high value in stoploss column just before data['s'+''+st_mt] regime change could be not the first before the position entry
For example:
On 21/06/2012 srebased_high = 6.77 r_regime_floorceiling = -1 r_regime_change=6.77 but moving average crossover = +1, hence no signal in data['s'+'_'+st_mt]
On 23/07/2012 srebased_low = 5.57, signal is still absent
On 02/08/2012 MA crossover gets -1 value, hence signal for short position is triggered. But stop loss is set to 5.57 swing low instead of 6.77 swing high.
The example is for mt = 180, st = 30, BAC_Jan_2010_to_Jan_2019.csv file