Homework Question need help to solve: strategy: buy whenever the price goes above the 50-day moving average, and then sell after 3 trading sessions. How much profit (in %) would we make on average? On trading day x, we say that the price "goes above" the 50-day moving average if (1) the price was below the moving average on trading day x-1 and (2) the price is above the moving average on trading day x.
How do I incorporate condition [1] and [2] into my current code with the 50-day moving average:
rol=stock.rolling(50).mean()
profitMade=((stock.shift(-3)-stock)/stock)
stock>rol
profitMade[stock>rol].mean()
Sample Data Set: Stocks and corresponding date:
Date
2002-05-23 1.196429
2002-05-24 1.210000
2002-05-28 1.157143
2002-05-29 1.103571
2002-05-30 1.071429
2002-05-31 1.076429
2002-06-03 1.128571
2002-06-04 1.117857
2002-06-05 1.147143
2002-06-06 1.182143
2002-06-07 1.118571
2002-06-10 1.156429
2002-06-11 1.153571
2002-06-12 1.092857
2002-06-13 1.082857
2002-06-14 0.986429
2002-06-17 0.922143
2002-06-18 0.910714
2002-06-19 0.951429