Demonstrate integration of pinkfish with the quantopian empyrical library
https://github.com/quantopian/empyrical
1. The SPY closes above its upper band, buy
2. If the SPY closes below its lower band, sell your long position.
In [1]:
import pandas as pd
import matplotlib.pyplot as plt
import datetime
from talib.abstract import *
import empyrical as em
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 = 'DJA'
#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]:
Run Benchmark, Retrieve benchmark logs
In [10]:
benchmark = pf.Benchmark(symbol, capital, s._start, s._end, s._use_adj)
benchmark.run()
benchmark.tlog, benchmark.dbal = benchmark.get_logs()
Empyrical API
In [11]:
import warnings
warnings.simplefilter(action='ignore', category=FutureWarning)
returns = dbal['close'].pct_change()
benchmark_rets = benchmark.dbal['close'].pct_change()
returns.index = returns.index.tz_localize('UTC')
benchmark_rets.index = benchmark_rets.index.tz_localize('UTC')
aggregate_returns = em.aggregate_returns(returns, 'yearly')
alpha = em.alpha(returns, benchmark_rets)
alpha_aligned = em.alpha_aligned(returns, benchmark_rets)
alpha_beta = em.alpha_beta(returns, benchmark_rets)
alpha_beta_aligned = em.alpha_beta_aligned(returns, benchmark_rets)
annual_return = em.annual_return(returns)
annual_volatility = em.annual_volatility(returns)
beta = em.beta(returns, benchmark_rets)
beta_aligned = em.beta_aligned(returns, benchmark_rets)
cagr = em.cagr(returns)
calmar_ratio = em.calmar_ratio(returns)
capture = em.capture(returns, benchmark_rets)
conditional_value_at_risk = em.conditional_value_at_risk(returns)
cum_returns = em.cum_returns(returns)
cum_returns_final = em.cum_returns_final(returns)
down_alpha_beta = em.down_alpha_beta(returns, benchmark_rets)
down_capture = em.down_capture(returns, benchmark_rets)
downside_risk = em.downside_risk(returns)
excess_sharpe = em.excess_sharpe(returns, benchmark_rets)
max_drawdown = em.max_drawdown(returns)
omega_ratio = em.omega_ratio(returns)
sharpe_ratio = em.sharpe_ratio(returns)
simple_returns = em.simple_returns(dbal['close'])
sortino_ratio = em.sortino_ratio(returns)
stability_of_timeseries = em.stability_of_timeseries(returns)
tail_ratio = em.tail_ratio(returns)
up_alpha_beta = em.up_alpha_beta(returns, benchmark_rets)
up_capture = em.up_capture(returns, benchmark_rets)
up_down_capture = em.up_down_capture(returns, benchmark_rets)
value_at_risk = em.value_at_risk(returns)
In [12]:
s = pd.Series(dtype=object)
s['aggregate_returns'] = aggregate_returns
s['alpha'] = alpha
s['alpha_aligned'] = alpha_aligned
s['alpha_beta'] = alpha_beta
s['alpha_beta_aligned'] = alpha_beta_aligned
s['annual_return'] = annual_return
s['annual_volatility'] = annual_volatility
s['beta'] = beta
s['beta_aligned'] = beta_aligned
s['cagr'] = cagr
s['calmar_ratio'] = calmar_ratio
s['capture'] = capture
s['conditional_value_at_risk'] = conditional_value_at_risk
s['cum_returns'] = cum_returns
s['cum_returns_final'] = cum_returns_final
s['down_alpha_beta'] = down_alpha_beta
s['down_capture'] = down_capture
s['downside_risk'] = downside_risk
s['excess_sharpe'] = excess_sharpe
s['max_drawdown'] = max_drawdown
s['omega_ratio'] = omega_ratio
s['sharpe_ratio'] = sharpe_ratio
s['simple_returns'] = simple_returns
s['sortino_ratio'] = sortino_ratio
s['stability_of_timeseries'] = stability_of_timeseries
s['tail_ratio'] = tail_ratio
s['up_alpha_beta'] = up_alpha_beta
s['up_capture'] = up_capture
s['up_down_capture'] = up_down_capture
s['value_at_risk'] = value_at_risk
In [13]:
df = pd.DataFrame(s, columns=['value'])
df
Out[13]: