Build a simple trading strategy
May 16, 2024
0.1 Build a simple trading strategy
In [1]: import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline
0.1.1 1. Munging the stock data and add two columns - MA10 and MA50
In [4]: #import FB's stock data, add two columns - MA10 and MA50
#use dropna to remove any "Not a Number" data
fb = pd.DataFrame.from_csv('../data/facebook.csv')
fb['MA10'] = fb['Close'].rolling(10).mean()
fb['MA50'] = fb['Close'].rolling(50).mean()
fb = fb.dropna()
fb.head()
Out[4]: Open High Low Close Adj Close Volume \
Date
2015-03-13 22.559999 22.760000 22.250000 22.700001 22.116024 8982200
2015-03-16 22.879999 23.110001 22.730000 22.969999 22.379078 5923900
2015-03-17 22.920000 23.260000 22.760000 23.250000 22.651876 7497500
2015-03-18 23.250000 23.370001 22.660000 22.870001 22.281652 10337600
2015-03-19 22.950001 23.299999 22.780001 23.219999 22.622650 7768900
MA10 MA50
Date
2015-03-13 22.648 21.0174
2015-03-16 22.685 21.0758
2015-03-17 22.792 21.1382
2015-03-18 22.836 21.1998
2015-03-19 22.872 21.2804
0.1.2 2. Add "Shares" column to make decisions base on the strategy
In [6]: #Add a new column "Shares", if MA10>MA50, denote as 1 (long one share of stock), otherwi
fb['Shares'] = [1 if fb.loc[ei, 'MA10']>fb.loc[ei, 'MA50'] else 0 for ei in fb.index]
1
In [7]: #Add a new column "Profit" using List Comprehension, for any rows in fb, if Shares=1, th
#tomorrow - the close price of today. Otherwise the profit is 0.
#Plot a graph to show the Profit/Loss
fb['Close1'] = fb['Close'].shift(-1)
fb['Profit'] = [fb.loc[ei, 'Close1'] - fb.loc[ei, 'Close'] if fb.loc[ei, 'Shares']==1 el
fb['Profit'].plot()
plt.axhline(y=0, color='red')
Out[7]: <matplotlib.lines.Line2D at 0x7f0c99862128>
0.1.3 3. Use .cumsum() to display our model’s performance if we follow the strategy
In [8]: #Use .cumsum() to calculate the accumulated wealth over the period
fb['wealth'] = fb['Profit'].cumsum()
fb.tail()
Out[8]: Open High Low Close Adj Close \
Date
2018-01-30 241.110001 246.419998 238.410004 242.720001 242.720001
2018-01-31 245.770004 249.270004 244.449997 245.800003 245.800003
2018-02-01 238.520004 246.899994 238.059998 240.500000 240.500000
2018-02-02 237.000000 237.970001 231.169998 233.520004 233.520004
2
2018-02-05 227.000000 233.229996 205.000000 213.699997 213.699997
Volume MA10 MA50 Shares Close1 Profit \
Date
2018-01-30 14270800 235.692003 210.030001 1 245.800003 3.080002
2018-01-31 11964400 237.800003 210.713801 1 240.500000 -5.300003
2018-02-01 12980600 239.406003 211.296601 1 233.520004 -6.979996
2018-02-02 17961600 239.747003 211.685401 1 213.699997 -19.820007
2018-02-05 28869000 237.748003 211.638401 1 NaN NaN
wealth
Date
2018-01-30 177.820011
2018-01-31 172.520008
2018-02-01 165.540012
2018-02-02 145.720005
2018-02-05 NaN
In [12]: #plot the wealth to show the growth of profit over the period
fb['wealth'].plot()
plt.title('Total money you win is {}'.format(fb.loc[fb.index[-2], 'wealth']))
Out[12]: <matplotlib.text.Text at 0x7f0c93b9e400>
3
0.2 You can create your own simple trading strategy by copying the codes above and
modify the codes accordingly using the data of Microsoft (microsoft.csv).