I am not promising you will be proficient at writing python code by the end of the course, but ChatGPT can help.
data.head()
closeadj | |
---|---|
Date | |
2010-06-29 | 1.592667 |
2010-06-30 | 1.588667 |
2010-07-01 | 1.464000 |
2010-07-02 | 1.280000 |
2010-07-06 | 1.074000 |
_ = data[["ten", "fifty"]].loc["2020-01-01":].plot()
data.head()
closeadj | ten | fifty | buy_hold | long | mvg_avg | |
---|---|---|---|---|---|---|
Date | ||||||
2010-09-09 | 1.381 | 1.351 | 1.322 | -0.009 | True | -0.009 |
2010-09-10 | 1.345 | 1.357 | 1.318 | -0.026 | True | -0.026 |
2010-09-13 | 1.381 | 1.360 | 1.313 | 0.027 | True | 0.027 |
2010-09-14 | 1.408 | 1.366 | 1.311 | 0.019 | True | 0.019 |
2010-09-15 | 1.465 | 1.375 | 1.314 | 0.041 | True | 0.041 |
buy_hold = 252*data.buy_hold.mean()
mvg_avg = 252*data.mvg_avg.mean()
print(f"buy and hold mean return is {buy_hold:.2%} annualized")
print(f"moving average mean return is {mvg_avg:.2%} annualized")
buy and hold mean return is 54.43% annualized moving average mean return is 37.46% annualized
_ = (1+data[["buy_hold", "mvg_avg"]]).cumprod().plot()
_ = (1+data[["buy_hold", "mvg_avg"]]).cumprod().plot(
logy=True
)
print(f"Buy and hold Sharpe ratio is {buy_hold_sharpe:.2%} annualized")
print(f"Moving average Sharpe ratio is {mvg_avg_sharpe:.2%} annualized")
Buy and hold Sharpe ratio is 95.92% annualized Moving average Sharpe ratio is 88.76% annualized
data.head(7)
closeadj | ||
---|---|---|
date | ticker | |
2000-01-03 | CVX | 17.508469 |
F | 13.405162 | |
MSFT | 36.205597 | |
PG | 28.428749 | |
WMT | 43.717701 | |
2000-01-04 | CVX | 17.508469 |
F | 12.957256 |
rets.head()
eq_wtd | mvg_avg | |
---|---|---|
date | ||
2000-01-03 | NaN | 0.0 |
2000-01-04 | -0.024771 | 0.0 |
2000-01-05 | -0.001449 | 0.0 |
2000-01-06 | 0.013458 | 0.0 |
2000-01-07 | 0.051980 | 0.0 |
rets.tail()
eq_wtd | mvg_avg | |
---|---|---|
date | ||
2023-10-16 | 0.010296 | 0.004630 |
2023-10-17 | 0.004664 | 0.002299 |
2023-10-18 | 0.000899 | 0.000413 |
2023-10-19 | -0.004877 | 0.000946 |
2023-10-20 | -0.006354 | -0.005492 |
eq_wtd = 252*rets.eq_wtd.mean()
mvg_avg = 252*rets.mvg_avg.mean()
print(f"equally weighted mean return is {eq_wtd:.2%} annualized")
print(f"moving average mean return is {mvg_avg:.2%} annualized")
equally weighted mean return is 10.60% annualized moving average mean return is 5.65% annualized
_ = (1+rets).cumprod().plot()
_ = (1+rets).cumprod().plot(logy=True)
print(f"Equally weighted Sharpe ratio is {eq_wtd_sharpe:.2%} annualized")
print(f"Moving average Sharpe ratio is {mvg_avg_sharpe:.2%} annualized")
Equally weighted Sharpe ratio is 35.35% annualized Moving average Sharpe ratio is 29.48% annualized
Look at different sets of stocks and different moving averages and test strategies.