1. The SPY is above its 200-day moving average
2. The SPY closes at a X-day low, buy some shares.
If it falls further, buy some more, etc...
3. If the SPY closes at a X-day high, sell your entire long position.
(Scaling in; compare regular trade log vs merged trade log)
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]:
capital = 10000
start = datetime.datetime(2000, 1, 1)
end = datetime.datetime.now()
Define symbols
In [4]:
symbols = ['SPY', 'SPY_merged']
Define high low trade periods
In [5]:
period = 8
Define max number of positions to scale into
In [6]:
max_positions = 4
Run Strategy
In [7]:
merge_trades = False
strategies = pd.Series(dtype=object)
for symbol in symbols:
strategies[symbol] = strategy.Strategy(symbols[0], capital, start, end,
period=period,max_positions=max_positions)
strategies[symbol].run()
_, strategies[symbol].tlog, strategies[symbol].dbal = strategies[symbol].get_logs(merge_trades)
strategies[symbol].stats = strategies[symbol].get_stats()
merge_trades = True
Summarize results
In [8]:
metrics = strategies[symbol].stats.index
df = strategy.summary(strategies, metrics)
pd.set_option('display.max_columns', len(df.columns))
pd.set_option('display.max_rows', len(df))
df
Out[8]: