In [1]:
import pandas as pd
import numpy as np
%matplotlib inline
In [2]:
df = pd.read_csv('C:\\Users\\vaish\\Desktop\\pythoncode\\youtube_new\\python_part_three.csv')

df
Out[2]:
Date TLT SPY QQQ
0 7/30/2002 42.858082 63.208256 21.158520
1 7/31/2002 43.389084 63.361134 20.758162
2 8/1/2002 43.636154 61.706898 19.783344
3 8/2/2002 44.083084 60.323750 19.365582
4 8/5/2002 44.277576 58.224693 18.660582
... ... ... ... ...
4635 12/24/2020 157.289993 369.000000 309.559998
4636 12/28/2020 157.360001 372.170013 312.679993
4637 12/29/2020 157.160004 371.459991 312.959991
4638 12/30/2020 157.500000 371.989990 312.970001
4639 12/31/2020 157.729996 373.880005 313.739990

4640 rows × 4 columns

In [3]:
df['QQQ Change'] = df['QQQ'].pct_change()
df['SPY Change'] = df['SPY'].pct_change()
df['TLT Change'] = df['TLT'].pct_change()

df
Out[3]:
Date TLT SPY QQQ QQQ Change SPY Change TLT Change
0 7/30/2002 42.858082 63.208256 21.158520 NaN NaN NaN
1 7/31/2002 43.389084 63.361134 20.758162 -0.018922 0.002419 0.012390
2 8/1/2002 43.636154 61.706898 19.783344 -0.046961 -0.026108 0.005694
3 8/2/2002 44.083084 60.323750 19.365582 -0.021117 -0.022415 0.010242
4 8/5/2002 44.277576 58.224693 18.660582 -0.036405 -0.034797 0.004412
... ... ... ... ... ... ... ...
4635 12/24/2020 157.289993 369.000000 309.559998 0.004413 0.003890 0.003957
4636 12/28/2020 157.360001 372.170013 312.679993 0.010079 0.008591 0.000445
4637 12/29/2020 157.160004 371.459991 312.959991 0.000895 -0.001908 -0.001271
4638 12/30/2020 157.500000 371.989990 312.970001 0.000032 0.001427 0.002163
4639 12/31/2020 157.729996 373.880005 313.739990 0.002460 0.005081 0.001460

4640 rows × 7 columns

In [4]:
df['QQQ Trend'] = df['QQQ'] > df['QQQ'].rolling(window=50).mean()
df['SPY Trend'] = df['SPY'].rolling(window=20).mean() > df['SPY'].rolling(window=100).mean()




df['QQQ Long'] = (df['QQQ Trend'] == True)
df['SPY Long'] = ((df['QQQ Trend'] == False) & (df['SPY Trend'] == True))
df['TLT Long'] = ((df['QQQ Long'] == False) & (df['SPY Long'] == False))

df
Out[4]:
Date TLT SPY QQQ QQQ Change SPY Change TLT Change QQQ Trend SPY Trend QQQ Long SPY Long TLT Long
0 7/30/2002 42.858082 63.208256 21.158520 NaN NaN NaN False False False False True
1 7/31/2002 43.389084 63.361134 20.758162 -0.018922 0.002419 0.012390 False False False False True
2 8/1/2002 43.636154 61.706898 19.783344 -0.046961 -0.026108 0.005694 False False False False True
3 8/2/2002 44.083084 60.323750 19.365582 -0.021117 -0.022415 0.010242 False False False False True
4 8/5/2002 44.277576 58.224693 18.660582 -0.036405 -0.034797 0.004412 False False False False True
... ... ... ... ... ... ... ... ... ... ... ... ...
4635 12/24/2020 157.289993 369.000000 309.559998 0.004413 0.003890 0.003957 True True True False False
4636 12/28/2020 157.360001 372.170013 312.679993 0.010079 0.008591 0.000445 True True True False False
4637 12/29/2020 157.160004 371.459991 312.959991 0.000895 -0.001908 -0.001271 True True True False False
4638 12/30/2020 157.500000 371.989990 312.970001 0.000032 0.001427 0.002163 True True True False False
4639 12/31/2020 157.729996 373.880005 313.739990 0.002460 0.005081 0.001460 True True True False False

4640 rows × 12 columns

In [5]:
lst = range(0,99)
df = df.drop(lst)

df
Out[5]:
Date TLT SPY QQQ QQQ Change SPY Change TLT Change QQQ Trend SPY Trend QQQ Long SPY Long TLT Long
99 12/18/2002 46.289497 62.695686 22.089811 -0.021211 -0.011557 0.007801 True True True False False
100 12/19/2002 46.701317 62.248859 21.811289 -0.012609 -0.007127 0.008897 False True False True False
101 12/20/2002 46.733421 63.137085 22.037586 0.010375 0.014269 0.000687 False True False True False
102 12/23/2002 46.744106 63.158100 22.342218 0.013823 0.000333 0.000229 True True True False False
103 12/24/2002 47.006168 62.688023 22.063696 -0.012466 -0.007443 0.005606 False True False True False
... ... ... ... ... ... ... ... ... ... ... ... ...
4635 12/24/2020 157.289993 369.000000 309.559998 0.004413 0.003890 0.003957 True True True False False
4636 12/28/2020 157.360001 372.170013 312.679993 0.010079 0.008591 0.000445 True True True False False
4637 12/29/2020 157.160004 371.459991 312.959991 0.000895 -0.001908 -0.001271 True True True False False
4638 12/30/2020 157.500000 371.989990 312.970001 0.000032 0.001427 0.002163 True True True False False
4639 12/31/2020 157.729996 373.880005 313.739990 0.002460 0.005081 0.001460 True True True False False

4541 rows × 12 columns

In [6]:
df['Buy and hold SPY'] = 100*(1+df['SPY Change']).cumprod()

df['Strategy'] = 100*(1+df['QQQ Long'].shift(1)*df['QQQ Change']+df['SPY Long'].shift(1)*df['SPY Change']+df['TLT Long'].shift(1)*df['TLT Change']).cumprod()

df
Out[6]:
Date TLT SPY QQQ QQQ Change SPY Change TLT Change QQQ Trend SPY Trend QQQ Long SPY Long TLT Long Buy and hold SPY Strategy
99 12/18/2002 46.289497 62.695686 22.089811 -0.021211 -0.011557 0.007801 True True True False False 98.844286 NaN
100 12/19/2002 46.701317 62.248859 21.811289 -0.012609 -0.007127 0.008897 False True False True False 98.139831 98.7391
101 12/20/2002 46.733421 63.137085 22.037586 0.010375 0.014269 0.000687 False True False True False 99.540183 100.148
102 12/23/2002 46.744106 63.158100 22.342218 0.013823 0.000333 0.000229 True True True False False 99.573315 100.181
103 12/24/2002 47.006168 62.688023 22.063696 -0.012466 -0.007443 0.005606 False True False True False 98.832204 98.9325
... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
4635 12/24/2020 157.289993 369.000000 309.559998 0.004413 0.003890 0.003957 True True True False False 581.755201 1274.02
4636 12/28/2020 157.360001 372.170013 312.679993 0.010079 0.008591 0.000445 True True True False False 586.752956 1286.86
4637 12/29/2020 157.160004 371.459991 312.959991 0.000895 -0.001908 -0.001271 True True True False False 585.633555 1288.01
4638 12/30/2020 157.500000 371.989990 312.970001 0.000032 0.001427 0.002163 True True True False False 586.469137 1288.05
4639 12/31/2020 157.729996 373.880005 313.739990 0.002460 0.005081 0.001460 True True True False False 589.448882 1291.22

4541 rows × 14 columns

In [7]:
df = df.set_index('Date')

graph = df[['Strategy', 'Buy and hold SPY']].plot(grid=True, kind='line', title ="Testing trading algorithm", logy=True, figsize=(11, 6))
In [10]:
start_portfolio = df['Strategy'].iloc[1]
end_portfolio = df['Strategy'].iloc[-1]

years = 18

average_return = (end_portfolio/start_portfolio)**(1/years)-1

print('Average trading algorithm return is', average_return, 'per year')
Average trading algorithm return is 0.15352860729161444 per year
In [ ]: