Hi,
I am trying to create a simple strategy that buys AAPL when the 50 day Simple Moving Average (SMA) crosses above the 100 day SMA (as much as possible with available cash) and sells AAPL (all positions) when the 50 day SMA crosses below the 100 day SMA. In the code below, the log output shows the correct calculations for the moving averages each day and shows that they barely cross each other, as expected. However, the algorithm is erroneously trading very frequently, every couple of days. I think that there is something wrong with the reference to the moving average calculations, which are calculated using Pipeline.
Looking at the log output of the attached backtest, the algorithm should create the following transactions; these are the only times that the moving averages cross:
Buy 1/5/15
Sell 8/5/15
Buy 12/9/15
Sell 1/15/16
Would anyone be able to help? Thank you! Much appreciated :)
-Tommy