Short Selling in Trading (steps 2-4)

Section 7, Unit 11.



i don't understand these steps and how the code works, steps (2-4)



Step 2: Eliminate all the swings low higher than highs. For comparison, compare the graphs.



high_low.loc[(high_low['swing_high_low'].shift(1) * high_low['swing_high_low'] < 0) &

             (high_low['swing_high_low'].shift(1) < 0) & (np.abs(high_low['swing_high_low'].shift(1)) < high_low['swing_high_low']), 'swing_high_low'] = np.nan




#There will naturally be swing lows that are higher than highs as the price series progresses higher  so why does he eliminate them? 



Simillarly i don't understand the coding in the futher steps:



Step 3: Eliminates all the earlier lower values



#What does he mean eliminating the 'earlier lower values'?




high_low.loc[(high_low['swing_high_low'].shift(1) * high_low['swing_high_low'] > 0) &

             (high_low['swing_high_low'].shift(1) < high_low['swing_high_low']), 'swing_high_low'] = np.nan



Step 4: Eliminates subsequent lower values



high_low.loc[(high_low['swing_high_low'].shift(-1) * high_low['swing_high_low'] > 0) &

             (high_low['swing_high_low'].shift(-1) < high_low['swing_high_low']), 'swing_high_low'] = np.nan



#why the need to eliminate 'subsequent lower values' 



Thanks



 

Step 2: Eliminate all the swings low higher than highs. For comparison, compare the graphs.



high_low.loc[(high_low['swing_high_low'].shift(1) * high_low['swing_high_low'] < 0) &

       (high_low['swing_high_low'].shift(1) < 0) & (np.abs(high_low['swing_high_low'].shift(1)) < high_low['swing_high_low']), 'swing_high_low'] = np.nan



In the second step, we eliminate all the swing low higher than highs. We are doing this because we buy at the swing low and sell at the swing high. If swings low is higher than the swing loss, it results in a loss-making position because you are buying at low and selling at a lower point.

In the code, we are checking this using three conditions. 

  1. previous value of swing high low multiplied by a current value should be less than 0
  2. previous of swing high low less than 0 and
  3. absolute previous value of swing high low should be less than the current value.



    If above these conditions satisfy, will fill the swing high low column with nan value and further in the code will remove those.



    Step 3: Eliminates all the earlier lower values



    high_low.loc[(high_low['swing_high_low'].shift(1) * high_low['swing_high_low'] > 0) &

           (high_low['swing_high_low'].shift(1) < high_low['swing_high_low']), 'swing_high_low'] = np.nan



    Let's say two consecutive new swing high has formed. We will keep only the new swing high and remove the previous one. Let's a first swing high has formed at 17, second formed at 17.81. We will remove the first one and keep the latest one. The same goes for the swing low.



    Step 4: Eliminates subsequent lower values



    high_low.loc[(high_low['swing_high_low'].shift(-1) * high_low['swing_high_low'] > 0) &

           (high_low['swing_high_low'].shift(-1) < high_low['swing_high_low']), 'swing_high_low'] = np.nan



    This step is similar to the third step. Here we compare the future value of swing high or low with the current and eliminate the following values.



    In all the codes, whenever the criterion satisfies, the column is filled with the nan values to identify the conditions and then remove them.



    If you go below in the code in the same notebook, a brief explanation is provided.

Hi, Thanks for your answer.



Your conditional interpretation of the code made the code much simpler to comprehend for all the steps



but i think the Steps 3 and 4 explanation in the Quantra course is termed incorrectly (Section 7, Unit 11)



Step 3 reads  Step 3: Eliminates all the earlier lower values



high_low.loc[(high_low['swing_high_low'].shift(1) * high_low['swing_high_low'] > 0) &

       (high_low['swing_high_low'].shift(1) < high_low['swing_high_low']), 'swing_high_low'] = np.nan





Using your given example of sequece of swing highs:  -17 (previous) and -17.85(current) (swing highs are designated by -ve sign) the above function will do nothing as -17.85 is less than -17 but if the sequence of numbers were -17.85 (previous) then followed by -17 (current),



then the current swing high would get eliminated with reference to the earlier value





Similarly for step 4




Step 4: Eliminates subsequent lower values



high_low.loc[(high_low['swing_high_low'].shift(-1) * high_low['swing_high_low'] > 0) &

             (high_low['swing_high_low'].shift(-1) < high_low['swing_high_low']), 'swing_high_low'] = np.nan



Take for example a sequence of 12 (current value) and then 10 (subsequent value) for swing lows, the swing low value of 12 gets eliminated



This step in fact eliminates the current swing highs and current swing lows with reference to subsequent swing highs and swing lows







Thanks

















 

Thank you for pointing this out.



The explanation that you mentioned is in line with the code. We will change the comment in the notebook. Thanks!