In section 3 unit 12 introduction to python for trading why are we calculating sma2 and lma2 and using them in the buy or sell signals.The strategy can also work without them.
Hello Abhilash,
We calculate sma2 and lma2 ( the shifted sma and lma ) along with sma1 and lma1 because we want to find the first point in the time series where sma goes past the lma. So at such a point we add an additional condition to check if for the last sma and lma ( i.e sma2 and lma2 ) sma2 is less than lma2.
Do get back if further clarification is needed.
Thank you for the response
But can you please elaborate on this and in the strategy x['SMA']>x['LMA'] and x['SMA2']<x['LMA2] why sma2 should be lesser than lma2 while in practicaal we trade when the sma goes above the lma.
Hello Abhilash,
The reason for that is we want to find all points in the time series where the short term moving average is higher than the long term moving average. So, to do that we will have to check two things:
- x['SMA']>x['LMA'] - to check if at a GIVEN point in the timeseries the short term average is higher than the long term average.
- x['SMA2']<x['LMA2] - we will also have to check if in the PREVIOUS point in the timeseries the short term average is lower than the long term average. To get the previous points' moving average we shifted by 1.
Both the conditions are necessary. Do get back if more explanation is needed.