In [17]:
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 [18]:
# 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 [19]:
#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)
end = datetime.datetime.now()
Define high low trade periods
In [20]:
period = 7
Define max number of positions to scale into
In [21]:
max_positions = 1
Define the margin multiple
In [22]:
margin = 1
Run Strategy
In [23]:
s = strategy.Strategy(symbol, capital, start, end, period=period, max_positions=max_positions, stop_loss_pct=85, margin=margin)
s.run()
Retrieve log DataFrames
In [24]:
rlog, tlog, dbal = s.get_logs()
stats = s.get_stats()
In [25]:
s.rlog.tail(10)
Out[25]:
In [26]:
tlog.tail(10)
Out[26]:
In [27]:
dbal.tail()
Out[27]:
Generate strategy stats - display all available stats
In [28]:
pf.print_full(stats)
Equity curve
Run Benchmark, Retrieve benchmark logs, and Generate benchmark stats
In [29]:
benchmark = pf.Benchmark(symbol, capital, s._start, s._end)
benchmark.run()
benchmark.tlog, benchmark.dbal = benchmark.get_logs()
benchmark.stats = benchmark.get_stats()
Plot Equity Curves: Strategy vs Benchmark
In [30]:
pf.plot_equity_curve(dbal, benchmark=benchmark.dbal)
Plot Trades
In [31]:
pf.plot_trades(dbal, benchmark=benchmark.dbal)
Bar Graph: Strategy vs Benchmark
In [32]:
df = pf.plot_bar_graph(stats, benchmark.stats)
df
Out[32]: