In [1]:
import pandas as pd
import matplotlib.pyplot as plt
import datetime
from talib.abstract import *
import pinkfish as pf
import strategy
# format price data
pd.options.display.float_format = '{:0.2f}'.format
%matplotlib inline
In [2]:
# set size of inline plots
'''note: rcParams can't be in same cell as import matplotlib
or %matplotlib inline
%matplotlib notebook: will lead to interactive plots embedded within
the notebook, you can zoom and resize the figure
%matplotlib inline: only draw static images in the notebook
'''
plt.rcParams["figure.figsize"] = (10, 7)
Some global data
In [3]:
symbol = '^GSPC'
#symbol = 'SPY'
#symbol = 'DIA'
#symbol = 'QQQ'
#symbol = 'IWM'
#symbol = 'TLT'
#symbol = 'GLD'
#symbol = 'AAPL'
#symbol = 'BBRY'
#symbol = 'GDX'
capital = 10000
start = datetime.datetime(1900, 1, 1)
#start = datetime.datetime.strptime(pf.SP500_BEGIN, '%Y-%m-%d')
end = datetime.datetime.now()
Include dividends? (If yes, set to True)
In [4]:
use_adj = True
Define high low trade periods
In [5]:
sma_period = 200
percent_band = 3.5
Run Strategy
In [6]:
s = strategy.Strategy(symbol, capital, start, end, use_adj, sma_period, percent_band)
s.run()
Retrieve log DataFrames
In [7]:
rlog, tlog, dbal = s.get_logs()
stats = s.get_stats()
In [8]:
tlog.tail(10)
Out[8]:
In [9]:
dbal.tail()
Out[9]:
Generate strategy stats - display all available stats
In [10]:
pf.print_full(stats)
Run Benchmark, Retrieve benchmark logs, and Generate benchmark stats
In [11]:
benchmark = pf.Benchmark(symbol, capital, s._start, s._end, s._use_adj)
benchmark.run()
benchmark.tlog, benchmark.dbal = benchmark.get_logs()
benchmark.stats = benchmark.get_stats()
Plot Equity Curves: Strategy vs Benchmark
In [12]:
pf.plot_equity_curve(dbal, benchmark=benchmark.dbal)
Plot Trades
In [13]:
pf.plot_trades(dbal, benchmark=benchmark.dbal)
In [14]:
print('trading period: {} to {}'.format(stats['start'], stats['end']))
df = pf.summary(stats, benchmark.stats, metrics=pf.currency_metrics)
df
Out[14]:
Bar Graph: Strategy vs Benchmark
In [15]:
df = pf.plot_bar_graph(stats, benchmark.stats)
df
Out[15]:
Print the raw transaction log
In [16]:
pd.set_option('display.max_rows', len(rlog))
rlog
Out[16]:
In [ ]: