In [1]:
import pandas as pd
import datetime
import pinkfish as pf
# format price data
pd.options.display.float_format = '{:0.2f}'.format
Some global data
In [2]:
symbol = 'SPY'
capital = 10000
start = datetime.datetime(1900, 1, 1)
end = datetime.datetime.now()
Timeseries
In [3]:
# fetch timeseries, select, finalize
ts = pf.fetch_timeseries(symbol)
ts = pf.select_tradeperiod(ts, start, end, use_adj=True)
ts, start = pf.finalize_timeseries(ts, start)
# create tradelog and daily balance objects
tlog = pf.TradeLog(symbol)
dbal = pf.DailyBal()
In [4]:
ts
Out[4]:
Algorithm
In [5]:
pf.TradeLog.cash = capital
# loop through timeseries
for i, row in enumerate(ts.itertuples()):
date = row.Index.to_pydatetime()
end_flag = pf.is_last_row(ts, i)
# buy
if tlog.shares == 0:
tlog.buy(date, row.close)
# sell
elif end_flag:
tlog.sell(date, row.close)
# record daily balance
dbal.append(date, row.high, row.low, row.close)
Retrieve logs
In [6]:
tlog = tlog.get_log()
dbal = dbal.get_log(tlog)
In [7]:
tlog.tail()
Out[7]:
Get stats
In [8]:
stats = pf.stats(ts, tlog, dbal, capital)
Summary
In [9]:
pf.summary(stats)
Out[9]:
In [ ]: