df = pd.read_csv('APPLE.csv',index_col=0)
df = df[['Adj_Open','Adj_High', 'Adj_Low', 'Adj_Close']]
df =df.rename(columns={'Adj_Open': 'Open', 'Adj_High': 'High', 'Adj_Low':'Low', 'Adj_Close':'Close'})
df.index = pd.to_datetime(df.index)
df = df.dropna()
df['SAR']=ta.SAR(df.High.values, df.Low.values, acceleration = 0.02, maximum = 0.2)
df['fastk'], df['fastd'] = ta.STOCHF(df.High.values, df.Low.values, df.Close.values ,fastk_period=5, fastd_period=3)
df['slowk'], df['slowd'] = ta.STOCH(df.High.values, df.Low.values, df.Close.values ,fastk_period=5, slowk_period=3, slowd_period=3)
df['Signal'] = np.nan
df.loc[(df.SAR< df.Close) & (df.fastd>df.slowd) & (df.fastk>df.slowk),'Signal'] =1
df.loc[(df.SAR> df.Close) & (df.fastd<df.slowd) & (df.fastk<df.slowk),'Signal'] =-1
df = df.fillna(method='ffill')
df['Stock_Return'] = df.Close.pct_change()
df['strategy_return'] = (df.Stock_Return * df.Signal.shift(1))
df = df.dropna()
(df.strategy_returns+1).cumprod().plot()
plt.show()
First, we calculated the daily stock returns which are a percentage change of daily closing prices. Next, we calculated the daily strategy returns which are nothing but the daily stock returns multiplied by the previous day's signal (1 or -1). Finally, we calculated the cumulative product of strategy returns.