Double 7's (Short Term Trading Strategies that Work)

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]:
SPY SPY_merged
start 2000-01-03 2000-01-03
end 2020-07-15 2020-07-15
beginning_balance 10000 10000
ending_balance 19695.54 19695.54
total_net_profit 9695.54 9695.54
gross_profit 20756.25 19563.62
gross_loss -11060.71 -9868.08
profit_factor 1.88 1.98
return_on_initial_capital 96.96 96.96
annual_return_rate 3.36 3.36
trading_period 20 years 6 months 12 days 20 years 6 months 12 days
pct_time_in_market 27.66 27.66
margin 1 1
avg_leverage 1.00 1.00
max_leverage 1.00 1.00
min_leverage 1.00 1.00
total_num_trades 411 171
trades_per_year 20.02 8.33
num_winning_trades 323 145
num_losing_trades 88 26
num_even_trades 0 0
pct_profitable_trades 78.59 84.80
avg_profit_per_trade 23.59 56.70
avg_profit_per_winning_trade 64.26 134.92
avg_loss_per_losing_trade -125.69 -379.54
ratio_avg_profit_win_loss 0.51 0.36
largest_profit_winning_trade 287.04 586.33
largest_loss_losing_trade -1228.76 -4314.27
num_winning_points 859.12 805.81
num_losing_points -523.82 -470.50
total_net_points 335.31 335.31
avg_points 0.82 1.96
largest_points_winning_trade 11.96 25.65
largest_points_losing_trade -72.28 -235.25
avg_pct_gain_per_trade 0.70 1.66
largest_pct_winning_trade 6.69 12.80
largest_pct_losing_trade -21.67 -73.63
max_consecutive_winning_trades 32 26
max_consecutive_losing_trades 5 3
avg_bars_winning_trades 7.18 7.70
avg_bars_losing_trades 16.81 18.58
max_closed_out_drawdown -30.83 -30.83
max_closed_out_drawdown_start_date 2018-09-13 2018-09-13
max_closed_out_drawdown_end_date 2020-03-23 2020-03-23
max_closed_out_drawdown_recovery_date Not Recovered Yet Not Recovered Yet
drawdown_recovery -1.53 -1.53
drawdown_annualized_return -9.18 -9.18
max_intra_day_drawdown -32.45 -32.45
avg_yearly_closed_out_drawdown -4.56 -4.56
max_yearly_closed_out_drawdown -29.92 -29.92
avg_monthly_closed_out_drawdown -0.89 -0.89
max_monthly_closed_out_drawdown -29.36 -29.36
avg_weekly_closed_out_drawdown -0.30 -0.30
max_weekly_closed_out_drawdown -17.78 -17.78
avg_yearly_closed_out_runup 7.58 7.58
max_yearly_closed_out_runup 18.48 18.48
avg_monthly_closed_out_runup 1.14 1.14
max_monthly_closed_out_runup 16.91 16.91
avg_weekly_closed_out_runup 0.38 0.38
max_weekly_closed_out_runup 16.91 16.91
pct_profitable_years 73.95 73.95
best_year 17.08 17.08
worst_year -28.26 -28.26
avg_year 4.00 4.00
annual_std 5.59 5.59
pct_profitable_months 50.12 50.12
best_month 16.91 16.91
worst_month -29.36 -29.36
avg_month 0.27 0.27
monthly_std 1.79 1.79
pct_profitable_weeks 27.32 27.32
best_week 16.91 16.91
worst_week -17.78 -17.78
avg_week 0.07 0.07
weekly_std 1.02 1.02
sharpe_ratio 0.43 0.43
sortino_ratio 0.24 0.24