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 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]:
entry_date entry_price exit_date exit_price pl_points pl_cash qty cumul_total direction symbol
506 2019-09-20 298.28 2019-10-11 296.28 -2.00 -210.00 105 52558.34 LONG SPY
507 2019-09-20 298.28 2019-10-15 298.88 0.60 63.00 105 52621.34 LONG SPY
508 2019-12-03 309.55 2019-12-12 317.13 7.58 765.58 101 53386.92 LONG SPY
509 2019-12-03 309.55 2019-12-13 317.32 7.77 784.77 101 54171.70 LONG SPY
510 2020-01-27 323.50 2020-02-04 329.06 5.56 550.44 99 54722.13 LONG SPY
511 2020-01-27 323.50 2020-02-05 332.86 9.36 926.64 99 55648.77 LONG SPY
512 2020-02-21 333.48 2020-03-04 312.86 -20.62 -2020.76 98 53628.01 LONG SPY
513 2020-02-21 333.48 2020-03-26 261.20 -72.28 -7083.44 98 46544.57 LONG SPY
514 2020-06-24 304.09 2020-07-02 312.23 8.14 748.88 92 47293.45 LONG SPY
515 2020-06-24 304.09 2020-07-06 317.05 12.96 1205.28 93 48498.73 LONG SPY

In [10]:
dbal.tail()


Out[10]:
high low close shares cash leverage state
date
2020-07-09 58498.73 58498.73 58498.73 0 58498.73 1.00 -
2020-07-10 58498.73 58498.73 58498.73 0 58498.73 1.00 -
2020-07-13 58498.73 58498.73 58498.73 0 58498.73 1.00 -
2020-07-14 58498.73 58498.73 58498.73 0 58498.73 1.00 -
2020-07-15 58498.73 58498.73 58498.73 0 58498.73 1.00 -

Generate strategy stats - display all available stats


In [11]:
pf.print_full(stats)


start                                                  1993-11-11
end                                                    2020-07-15
beginning_balance                                           10000
ending_balance                                           58498.73
total_net_profit                                         48498.73
gross_profit                                             96251.45
gross_loss                                              -47752.72
profit_factor                                                2.02
return_on_initial_capital                                  484.99
annual_return_rate                                           6.85
trading_period                           26 years 8 months 4 days
pct_time_in_market                                          38.01
margin                                                          1
avg_leverage                                                 1.00
max_leverage                                                 1.00
min_leverage                                                 1.00
total_num_trades                                              516
trades_per_year                                             19.34
num_winning_trades                                            394
num_losing_trades                                             120
num_even_trades                                                 2
pct_profitable_trades                                       76.36
avg_profit_per_trade                                        93.99
avg_profit_per_winning_trade                               244.29
avg_loss_per_losing_trade                                 -397.94
ratio_avg_profit_win_loss                                    0.61
largest_profit_winning_trade                              1262.21
largest_loss_losing_trade                                -7083.44
num_winning_points                                         932.01
num_losing_points                                         -447.08
total_net_points                                           484.92
avg_points                                                   0.94
largest_points_winning_trade                                12.96
largest_points_losing_trade                                -72.28
avg_pct_gain_per_trade                                       0.79
largest_pct_winning_trade                                    7.35
largest_pct_losing_trade                                   -21.67
max_consecutive_winning_trades                                 33
max_consecutive_losing_trades                                   6
avg_bars_winning_trades                                      7.97
avg_bars_losing_trades                                      18.21
max_closed_out_drawdown                                    -19.58
max_closed_out_drawdown_start_date                     2020-02-05
max_closed_out_drawdown_end_date                       2020-03-23
max_closed_out_drawdown_recovery_date           Not Recovered Yet
drawdown_recovery                                           -0.13
drawdown_annualized_return                                  -2.86
max_intra_day_drawdown                                     -20.83
avg_yearly_closed_out_drawdown                              -6.95
max_yearly_closed_out_drawdown                             -19.58
avg_monthly_closed_out_drawdown                             -1.62
max_monthly_closed_out_drawdown                            -18.70
avg_weekly_closed_out_drawdown                              -0.56
max_weekly_closed_out_drawdown                             -12.82
avg_yearly_closed_out_runup                                 12.85
max_yearly_closed_out_runup                                 39.05
avg_monthly_closed_out_runup                                 2.05
max_monthly_closed_out_runup                                13.66
avg_weekly_closed_out_runup                                  0.70
max_weekly_closed_out_runup                                  7.78
pct_profitable_years                                        75.25
best_year                                                   38.35
worst_year                                                 -13.61
avg_year                                                     7.57
annual_std                                                   8.88
pct_profitable_months                                       52.00
best_month                                                  13.66
worst_month                                                -18.70
avg_month                                                    0.55
monthly_std                                                  2.25
pct_profitable_weeks                                        33.04
best_week                                                    7.78
worst_week                                                 -12.82
avg_week                                                     0.14
weekly_std                                                   1.28
sharpe_ratio                                                 0.69
sortino_ratio                                                0.55
dtype: object

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()


1993-11-11 00:00:00 BUY  215 SPY @ 46.38
2020-07-15 00:00:00 SELL 215 SPY @ 321.85

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]:
strategy benchmark
annual_return_rate 6.85 7.52
max_closed_out_drawdown -19.58 -56.42
drawdown_annualized_return -2.86 -7.50
drawdown_recovery -0.13 -1.42
best_month 13.66 23.06
worst_month -18.70 -31.38
sharpe_ratio 0.69 0.48
sortino_ratio 0.55 0.60
monthly_std 2.25 4.54

In [ ]: