Monday, July 13, 2015

Backtesting with Python

install python data science python vm and install zipline backtester & run the following command
sudo pip install zipline
sudo ipython notebook --profile=dst
You can use following code to import the libraries.
import pandas as pd
import pandas.io.data as web
import datetime
start = datetime.datetime(2005,1,1)
end= datetime.datetime(2010,1,1)
f = web.DataReader('F','yahoo',start,end)
It is possible to save the downloaded date to a csv file by using the following
f.to_csv('xlf_20050101-20100101.csv')
df = pd.read_csv('xlf_20050101-20100101.csv',index_col="Date",parse_dates=True)
df.head()
to calculate returns for prices use
df['c_p'] = 1-df ['Close'].shift(1)/df ['Close'] 
to add day of the week to the price use
df['weekday'] = df.index.dayofweek

returns by day of week.
returnbydayofweek = df.groupby('weekday')['c_p'].mean()
plot returns by day of week
a.plot(kind="bar")