<jupyter_start><jupyter_text>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 7-day low, buy.    3. If the SPY closes at a 7-day high, sell your long position.<jupyter_code># use future imports for python 3.x forward compatibility
from __future__ import print_function
from __future__ import unicode_literals
from __future__ import division
from __future__ import absolute_import

# other imports
import pandas as pd
import matplotlib.pyplot as plt
import datetime
from talib.abstract import *

# project imports
import pinkfish as pf
import strategy

# format price data
pd.options.display.float_format = '{:0.2f}'.format

%matplotlib inline
# 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)<jupyter_output><empty_output><jupyter_text>Some global data<jupyter_code>#symbol = '^GSPC'
#symbol = 'SPY'
#symbol = 'DIA'
#symbol = 'QQQ'
#symbol = 'IWM'
#symbol = 'TLT'
#symbol = 'GLD'
#symbol = 'AAPL'
#symbol = 'BBRY'
#symbol = 'GDX'
symbol = 'OIH'
capital = 10000
start = datetime.datetime(2000, 1, 1)
end = datetime.datetime.now()<jupyter_output><empty_output><jupyter_text>Define high low trade periods<jupyter_code>period = 7<jupyter_output><empty_output><jupyter_text>Run Strategy<jupyter_code>s = strategy.Strategy(symbol, capital, start, end, period)
s.run()<jupyter_output>2002-03-14 00:00:00 BUY  461 OIH @ 21.67
2002-03-18 00:00:00 SELL 461 OIH @ 22.93
2002-04-05 00:00:00 BUY  482 OIH @ 21.92
2002-04-16 00:00:00 SELL 482 OIH @ 22.52
2002-05-06 00:00:00 BUY  459 OIH @ 23.68
2002-05-23 00:00:00 SELL 459 OIH @ 24.09
2002-06-03 00:00:00 BUY  487 OIH @ 22.71
2002-06-17 00:00:00 SELL 487 OIH @ 22.53
2002-06-21 00:00:00 BUY  505 OIH @ 21.70
2002-07-29 00:00:00 SELL 505 OIH @ 17.76
2003-03-03 00:00:00 BUY  468 OIH @ 19.17
2003-03-18 00:00:00 SELL 468 OIH @ 18.80
2003-04-29 00:00:00 BUY  481 OIH @ 18.30
2003-05-06 00:00:00 SELL 481 OIH @ 18.92
2003-06-16 00:00:00 BUY  439 OIH @ 20.73
2003-07-09 00:00:00 SELL 439 OIH @ 20.06
2003-07-10 00:00:00 BUY  453 OIH @ 19.45
2003-07-18 00:00:00 SELL 453 OIH @ 19.82
2003-08-27 00:00:00 BUY  457 OIH @ 19.64
2003-08-28 00:00:00 SELL 457 OIH @ 20.26
2003-09-09 00:00:00 BUY  469 OIH @ 19.72
2003-09-19 00:00:00 SELL 469 OIH @ 19.42
2003-10-13 00:00:00 BUY  467 OIH @ 19.52
2003-10-31 00:00:00 SELL 467 OIH @ 18.73
2004-01-07 00:00[...]<jupyter_text>Retrieve log DataFrames<jupyter_code>s.tlog, s.dbal = s.get_logs()
s.stats = s.stats()
s.tlog.tail(100)
s.dbal.tail()<jupyter_output><empty_output><jupyter_text>Generate strategy stats - display all available stats<jupyter_code>pf.print_full(s.stats)<jupyter_output>start                                                  2001-12-13
end                                                    2019-12-13
beginning_balance                                           10000
ending_balance                                           17172.78
total_net_profit                                          7172.78
gross_profit                                             46559.71
gross_loss                                              -39386.93
profit_factor                                                1.18
return_on_initial_capital                                   71.73
annual_return_rate                                           3.05
trading_period                           18 years 0 months 0 days
pct_time_in_market                                          24.34
total_num_trades                                              135
num_winning_trades                                             94
num_losing_trades                                              41
num_even_t[...]<jupyter_text>Equity curve Run Benchmark, Retrieve benchmark logs, and Generate benchmark stats<jupyter_code>benchmark = pf.Benchmark(symbol, capital, s._start, s._end)
benchmark.run()
benchmark.tlog, benchmark.dbal = benchmark.get_logs()
benchmark.stats = benchmark.stats()<jupyter_output>2001-12-13 00:00:00 BUY  539 OIH @ 18.55
2019-12-13 00:00:00 SELL 539 OIH @ 12.88<jupyter_text>Plot Equity Curves: Strategy vs Benchmark<jupyter_code>pf.plot_equity_curve(s.dbal, benchmark=benchmark.dbal)<jupyter_output><empty_output><jupyter_text>Plot Trades<jupyter_code>pf.plot_trades(s.dbal, benchmark=benchmark.dbal)<jupyter_output><empty_output><jupyter_text>Bar Graph: Strategy vs Benchmark<jupyter_code>metrics = ('annual_return_rate',
           'max_closed_out_drawdown',
           'drawdown_annualized_return',
           'drawdown_recovery',
           'best_month',
           'worst_month',
           'sharpe_ratio',
           'sortino_ratio',
           'monthly_std')

df = pf.plot_bar_graph(s.stats, benchmark.stats, *metrics)
df
returns = s.dbal['close']
returns.tail()
benchmark_returns = benchmark.dbal['close']
benchmark_returns.tail()
pf.prettier_graphs(returns, benchmark_returns, label1='Strategy', label2='Benchmark', points_to_plot=500)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09952244508118434, 'fraction_numerical': 0.12722063037249284, 'mean_word_length': 2.6462395543175488, 'pattern_counts': {'":': 0, '<': 38, '<?xml version=': 0, '>': 38, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 27, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '582502', 'license': 'MIT', 'n_tokens_mistral': 2296, 'n_tokens_neox': 1763, 'n_words': 446, 'path': 'examples/double-7s/strategy.ipynb', 'repo_name': 'jimmyhzuk/pinkfish', 'size': '5235'}