<jupyter_start><jupyter_text>SMA Percent Band    1. The SPY closes above its upper band, buy    2. If the SPY closes below its lower band, 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 = 'ES=F'
#symbol = 'DIA'
#symbol = 'QQQ'
#symbol = 'IWM'
#symbol = 'TLT'
#symbol = 'GLD'
#symbol = 'AAPL'
#symbol = 'BBRY'
#symbol = 'GDX'
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>Include dividends? (If yes, set to True)<jupyter_code>use_adj = True<jupyter_output><empty_output><jupyter_text>Define high low trade periods<jupyter_code>sma_period = 200
percent_band = 3.5<jupyter_output><empty_output><jupyter_text>Run Strategy<jupyter_code>s = strategy.Strategy(symbol, capital, start, end, use_adj, sma_period, percent_band)
s.run()<jupyter_output><empty_output><jupyter_text>Retrieve log DataFrames<jupyter_code>rlog, tlog, dbal = s.get_logs()
stats = s.get_stats()
tlog.tail(10)
dbal.tail()<jupyter_output><empty_output><jupyter_text>Generate strategy stats - display all available stats<jupyter_code>pf.print_full(stats)<jupyter_output>start                                                   2001-07-03
end                                                     2020-10-16
beginning_balance                                            10000
ending_balance                                            34698.50
total_net_profit                                          24698.50
gross_profit                                              24698.50
gross_loss                                                    0.00
profit_factor                                                 1000
return_on_initial_capital                                   246.99
annual_return_rate                                            6.66
trading_period                           19 years 3 months 13 days
pct_time_in_market                                           69.88
margin                                                           1
avg_leverage                                                  1.00
max_leverage                                                  [...]<jupyter_text>Run Benchmark, Retrieve benchmark logs, and Generate benchmark stats<jupyter_code>benchmark = pf.Benchmark(symbol, capital, s.start, s.end, s.use_adj)
benchmark.run()<jupyter_output><empty_output><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)
print('trading period:  {} to {}'.format(stats['start'], stats['end']))
df = pf.summary(stats, benchmark.stats, metrics=pf.currency_metrics)
df<jupyter_output>trading period:  2001-07-03 to 2020-10-16<jupyter_text>Bar Graph: Strategy vs Benchmark<jupyter_code>df = pf.plot_bar_graph(stats, benchmark.stats)
df<jupyter_output><empty_output><jupyter_text>Print the raw transaction log<jupyter_code>pd.set_option('display.max_rows', len(rlog))
rlog<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09165346011621764, 'fraction_numerical': 0.030110935023771792, 'mean_word_length': 2.58957345971564, 'pattern_counts': {'":': 0, '<': 47, '<?xml version=': 0, '>': 47, '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': '12', 'dir': 'jupyter-structured-clean-dedup', 'id': '339287', 'license': 'MIT', 'n_tokens_mistral': 1248, 'n_tokens_neox': 1129, 'n_words': 253, 'path': 'examples/sma-percent-band/strategy.ipynb', 'repo_name': 'tombohub/pinkfish', 'size': '3786'}