1. The SPY is above its 200-day moving average
2. The SPY closes at a X-day low, buy with full capital.
3. If the SPY closes at a X-day high, sell some.
If it sets further highs, sell some more, etc...
4. If you have free cash, use it all when fresh lows are set.
(Scaling out)
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)
end = datetime.datetime.now()
Define high low trade periods
In [4]:
period = 7
Define max number of positions to scale into
In [5]:
max_positions = 2
Define the margin multiple
In [6]:
margin = 1
Run Strategy
In [7]:
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 [8]:
tlog, dbal = s.get_logs()
stats = s.get_stats()
In [9]:
tlog.tail(10)
Out[9]:
In [10]:
dbal.tail()
Out[10]:
Generate strategy stats - display all available stats
In [11]:
pf.print_full(stats)
Equity curve
Run Benchmark, Retrieve benchmark logs, and Generate benchmark stats
In [12]:
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 [13]:
pf.plot_equity_curve(dbal, benchmark=benchmark.dbal)
Plot Trades
In [14]:
pf.plot_trades(dbal, benchmark=benchmark.dbal)
Bar Graph: Strategy vs Benchmark
In [15]:
df = pf.plot_bar_graph(stats, benchmark.stats)
df
Out[15]:
In [ ]: