Short Selling in Trading: Stop Losses seem to be calculated incorrectly

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











 

Hello Alexander, thanks for pointing that out. We're currently investigating this. Do get back to us if any other assistance is needed meanwhile. 

Hi Alexander,



Thanks for bringing this up.



Just for clarity purpose, on 23-July-2012 the signal is absent as the floor ceiling regime is -1 and short term moving average (6.27) is greater than medium term moving average (6.14). For short signal, fc_regime should be -1 and st moving average < mt moving average.




Coming to the core example of Bank of America on 2-August-2012, the most recent is swing low of 5.57. Since the close of 5.73 is greater than the swing low, the signal is replaced with nan in the stop loss code.



The rationale is that the most recent swing high or low is considered to determine the stop loss. If you ignore the most recent swing low then and going to a swing high as the stop loss you are ignoring the most recent data points. We will update the description to avoid any confusions.



However, it would be interesting to see how the strategy results would have been if only highs are considered as the stop loss for shorts and swing lows are considered for longs.



Thank you!