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]:
    
# S&P All Sectors
symbols = ['SPY', 'XLB', 'XLE', 'XLF', 'XLI', 'XLK', 'XLP', 'XLU', 'XLV', 'XLY']
# Other Sectors
symbols += ['RSP', 'DIA', 'IWM', 'QQQ', 'DAX', 'EEM', 'TLT', 'GLD', 'XHB']
# Elite Stocks
symbols = ['ADP', 'BMY', 'BRK-B', 'BTI', 'BUD', 'CL', 'CLX', 'CMCSA', 'DIS', 'DOV']
symbols += ['GIS', 'HD', 'HRL', 'HSY', 'INTC', 'JNJ', 'K', 'KMB', 'KMI', 'KO']
symbols += ['LLY', 'LMT', 'MCD', 'MO', 'MRK', 'MSFT', 'NUE', 'PG', 'PM', 'RDS-B']
symbols += ['SO', 'T', 'UL', 'V', 'VZ', 'XOM']
    
Run Strategy
In [5]:
    
strategies = pd.Series(dtype=object)
for symbol in symbols:
    print("{0}".format(symbol), end=" ")
    strategies[symbol] = strategy.Strategy(symbol, capital, start, end, use_adj=True,
                                           sma_period=200, percent_band=3.5, regime_filter=True)
    strategies[symbol].run()
    _, strategies[symbol].tlog, strategies[symbol].dbal = strategies[symbol].get_logs()
    strategies[symbol].stats = strategies[symbol].get_stats()
    
    
Summarize results
In [6]:
    
metrics = ('start',
           'annual_return_rate',
           'max_closed_out_drawdown',
           'sharpe_ratio',
           'sortino_ratio',
           'monthly_std',
           'pct_time_in_market',
           'total_num_trades',
           'pct_profitable_trades',
           'avg_points')
df = strategy.summary(strategies, metrics)
pd.set_option('display.max_columns', len(df.columns))
df
    
    Out[6]:
In [7]:
    
pf.plot_equity_curves(strategies)
    
    
In [ ]: