<jupyter_start><jupyter_text>Double 7's (Short Term Trading Strategies that Work)    1. The SPY is above its 200-day moving average or X-day ma    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>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
# 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(1900, 1, 1)
start = datetime.datetime.strptime(pf.SP500_BEGIN, '%Y-%m-%d')
end = datetime.datetime.now()<jupyter_output><empty_output><jupyter_text>Run Strategy<jupyter_code>s = strategy.Strategy(symbol, capital, start, end, stop_loss_pct=85, margin=1.5, period=7, sma=70)
s.run()<jupyter_output><empty_output><jupyter_text>Retrieve log DataFrames<jupyter_code>tlog, dbal = s.get_logs()
stats = s.get_stats()
tlog.tail()
dbal.tail()<jupyter_output><empty_output><jupyter_text>Generate strategy stats - display all available stats<jupyter_code>pf.print_full(stats)<jupyter_output>start                                                   1993-11-11
end                                                     2020-07-23
beginning_balance                                            10000
ending_balance                                           297622.55
total_net_profit                                         287622.55
gross_profit                                             517448.86
gross_loss                                              -229826.31
profit_factor                                                 2.25
return_on_initial_capital                                  2876.23
annual_return_rate                                           13.55
trading_period                           26 years 8 months 12 days
pct_time_in_market                                           31.97
margin                                                        1.50
avg_leverage                                                  1.16
max_leverage                                                  [...]<jupyter_text>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.get_stats()<jupyter_output>1993-11-11 00:00:00 BUY  215 SPY @ 46.38
2020-07-23 00:00:00 SELL 215 SPY @ 322.28<jupyter_text>Plot Equity Curves: Strategy vs Benchmark<jupyter_code>pf.plot_equity_curve(dbal, benchmark=benchmark.dbal)<jupyter_output><empty_output><jupyter_text>Plot Trades<jupyter_code>pf.plot_trades(dbal, benchmark=benchmark.dbal)<jupyter_output><empty_output><jupyter_text>Bar Graph: Strategy vs Benchmark<jupyter_code>df = pf.plot_bar_graph(stats, benchmark.stats)
df
returns = 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=5000)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09228278467350243, 'fraction_numerical': 0.044522396114409064, 'mean_word_length': 2.4742268041237114, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 35, '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': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '389141', 'license': 'MIT', 'n_tokens_mistral': 1261, 'n_tokens_neox': 1106, 'n_words': 262, 'path': 'examples/double-7s/strategy.ipynb', 'repo_name': 'HaidongHe/pinkfish', 'size': '3706'}