Course Name: Python For Trading!, Section No: 3, Unit No: 20, Unit type: Exercise
My results are no where compared to what was demostrated in the lecture. The final plot also looks weird.
Here's the below code. Appreciate if someone could help? Thank you in advance.
import numpy as np
import pandas as pd
import datetime as dt
import matplotlib.pyplot as plt
import quandl
start = dt.datetime(2012,1,1)
df = quandl.get("CHRIS/MCX_GC1", start = start)
df = df.dropna()
n=14
delta = df['Close'].diff()
dUp, dDown = delta.copy() , delta.copy()
dUp[dUp < 0] = 0
dDown[dDown > 0] = 0
RolUp = dUp.rolling(window=n).mean()
RolDown = dDown.abs().rolling(window=n).mean()
print(RolUp.tail())
df['RSI'] = np.where(RolDown != 0, RolUp / RolDown, 1)
print(df.tail())
df['RS_slow'] = df['RSI'].rolling(window=5).mean()
df = df.assign(Signal=pd.Series(np.zeros(len(df))).values)
df.loc[df['RSI'] > df['RS_slow'],'Signal']=1
df.loc[df['RSI'] < df['RS_slow'],'Signal']=-1
df['Returns'] = df['Close'].pct_change()
df['s_returns'] = df['Signal'].shift(1)*df['Returns']
df['Market_Return'] = (df['Returns']+1).cumprod()
df['Strategy_Return'] = (df['s_returns']+1).cumprod()
plt.plot(df['Market_Return'],color='r',label='Market Return')
plt.plot(df['Strategy_Return'],color='b',label='Strategy Return')
plt.legend(loc='best')
plt.show()
The discrepancy in the result is due to the data issue from the Quandl.
We are making changes in the video/code and course and hopefully will update within a week.
Till now, feel free to post your queries in case of any doubt.
Thanks!