Moving Average Crossover Strategy

Hello Team,



I could not understand the logic behind the calculation of Moving Average Crossover Strategy. 

  1. Why the list was reversed?
  2. Why the SMA2 and LMA2 need to calculated shifting 1 period down? What is the use of SMA2 and LMA2?
  3. Did not understand the logic for buy and sell signal involving use of SMA2 and LMA2
  4. Also, while plotting SMA2 and LMA2 along with SMA and LMA, could not differentiate between SMA & SMA2 and LMA & LMA2?



    Can you share any study notes or logic behind calculation of this, as I did search for this calculation but could not find the same? Also, if you could suggest study material or good TA book which involves maths/logic to calculate TA strategies.



    Thanks,

    Mukesh Kumar Gupta

Thanks for your query Mukesh,



Here are the answers to your questions: 



1. Why the list was reversed? - Can you point to exactly in which section the list was reversed? 





2. Why the SMA2 and LMA2 need to calculated shifting 1 period down? What is the use of SMA2 and LMA2? - So, by shifting 1 we are getting on a date the price of the previous date. Now, as you would have seen the condition for buying was ( SMA>LMA and SMA2<LMA2 ). What this basically means is, we are looking for the date to generate a buying signal where the short term moving average was greater than long term moving average and ALSO the short term moving average based on the price of the previous day is less than the long term moving average based on the price of the previous day. Why we have this additional condition is because we want to want to find the FIRST SUCH DAY ( to take a position ) where the short term moving average was greater than the long term moving average. Because say once SMA > LMA it might continue to do so for a few days, right? We can't take a position each day SMA>LMA. We want to do so only on a particular day. When it first happened. 



3. Did not understand the logic for buy and sell signal involving use of SMA2 and LMA2 - the logic is explained above. I did it for the buying. Can you extrapolate it for selling? 



4. Also, while plotting SMA2 and LMA2 along with SMA and LMA, could not differentiate between SMA & SMA2 and LMA & LMA2? - In the video, they aren't plotted together. Even if they are SMA2/LMA2 will just be lagged versions of SMA/LMA. 



Here are some blogs you can read: 



https://blog.quantinsti.com/moving-average-crossover-trade-nifty-options/  



https://blog.quantinsti.com/moving-average-trading-strategies/



Thanks. Do get back if you want more explantation. 

Hi Akshay,

Thanks for your reply. Please find my answers:

  1. The list was reversed with this line of code:
            Data = Data.iloc[::-1]

        What I've observed is that if this line of code is used, the "moving averages" calculations are done         from  starting till end - n (where n is number of periods). Whereas if we comment this line of code,            Calculations are done from start + n till end. Therefore, I believe this line of code is not required.

     
      2 and 3. Ok, I understand. For selling the same is opposite. For selling, we are looking for the day where LMA is greater than SMA and shifting the period to 1 day before for SMA2 and LMA2 ensures that the condition is satisfied only when the crossover happens, otherwise it will generate signals whenever LMA > SMA. Right?

4. I think they are plotted together. See below lines of code from video (I've added colors attribute to differentiate lines):
plt.plot(Data['SMA'], color='r')
plt.plot(Data['LMA'], color='b')
plt.plot(Data['SMA2'], color='c')

However, I understand the essence of SMA2 and LMA2 so this is understood.

And, thanks for sharing the resources, I will go through them and get back if there is some clarification needed.

Thanks,
Mukesh Kumar Gupta
plt.plot(Data['LMA2'], color='y')

Thanks for getting back Mukesh. 


  1. That's right. There is no need to reverse the list. We'll remove that particular line. Thanks for pointing out.



    2&3) That is spot on. Basically, to get a signal at a particular day instead of a range of values spanning multiple days.


  2. Right. So the code in the video shows all of the 4 moving averages. The plot in the video, however, has only SMA and LMA. 



    Yes, please do reach out if further assistance is needed.