Files
pandas-ta/examples/PandasTA_Strategy_Examples.ipynb
T

93 KiB
Raw Blame History

Pandas TA (pandas_ta) Strategies for Custom Technical Analysis

Topics

  • What is a Pandas TA Strategy?
    • Builtin Strategies: AllStrategy and CommonStrategy
    • Creating Strategies
  • Watchlist Class
    • Strategy Management and Execution
  • Indicator Composition/Chaining for more Complex Strategies
    • Comprehensive Example: MACD and RSI Momo with BBANDS and SMAs 50 & 200 and Cumulative Log Returns
In [1]:
%matplotlib inline
import datetime as dt

import pandas as pd
import pandas_ta as ta
from alphaVantageAPI.alphavantage import AlphaVantage  # pip install alphaVantage-api

from watchlist import Watchlist
%pylab inline
Populating the interactive namespace from numpy and matplotlib

What is a Pandas TA Strategy?

A Strategy is a simple way to name and group your favorite TA indicators. Technically, a Strategy is a simple Data Class to contain list of indicators and their parameters. Note: Strategy is experimental and subject to change. Pandas TA comes with two basic Strategies: AllStrategy and CommonStrategy.

Strategy Requirements:

  • name: Some short memorable string. Note: Case-insensitive "All" is reserved.
  • ta: A list of dicts containing keyword arguments to identify the indicator and the indicator's arguments

Optional Requirements:

  • description: A more detailed description of what the Strategy tries to capture. Default: None
  • created: At datetime string of when it was created. Default: Automatically generated.

Things to note:

  • A Strategy will fail when consumed by Pandas TA if there is no {"kind": "indicator name"} attribute.

Builtin Examples

All

In [2]:
AllStrategy = ta.AllStrategy
print("name =", AllStrategy.name)
print("description =", AllStrategy.description)
print("created =", AllStrategy.created)
print("ta =", AllStrategy.ta)
name = All
description = All the indicators with their default settings. Pandas TA default.
created = 08/21/2020, 11:11:36
ta = None

Common

In [3]:
CommonStrategy = ta.CommonStrategy
print("name =", CommonStrategy.name)
print("description =", CommonStrategy.description)
print("created =", CommonStrategy.created)
print("ta =", CommonStrategy.ta)
name = Common Price and Volume SMAs
description = Common Price SMAs: 10, 20, 50, 200 and Volume SMA: 20.
created = 08/21/2020, 11:11:36
ta = [{'kind': 'sma', 'length': 10}, {'kind': 'sma', 'length': 20}, {'kind': 'sma', 'length': 50}, {'kind': 'sma', 'length': 200}, {'kind': 'sma', 'close': 'volume', 'length': 20, 'prefix': 'VOL'}]
In [ ]:

Creating Strategies

Simple Strategy A

In [4]:
custom_a = ta.Strategy(name="A", ta=[{"kind": "sma", "length": 50}, {"kind": "sma", "length": 200}])
custom_a
Out [4]:
Strategy(name='A', ta=[{'kind': 'sma', 'length': 50}, {'kind': 'sma', 'length': 200}], description=None, created='08/21/2020, 11:11:36', last_run=None, run_time=None)

Simple Strategy B

In [5]:
custom_b = ta.Strategy(name="B", ta=[{"kind": "ema", "length": 8}, {"kind": "ema", "length": 21}, {"kind": "log_return", "cumulative": True}, {"kind": "rsi"}, {"kind": "supertrend"}])
custom_b
Out [5]:
Strategy(name='B', ta=[{'kind': 'ema', 'length': 8}, {'kind': 'ema', 'length': 21}, {'kind': 'log_return', 'cumulative': True}, {'kind': 'rsi'}, {'kind': 'supertrend'}], description=None, created='08/21/2020, 11:11:36', last_run=None, run_time=None)

Bad Strategy. (Misspelled Indicator)

In [6]:
# Misspelled indicator, will fail later when ran with Pandas
custom_run_failure = ta.Strategy(name="Runtime Failure", ta=[{"kind": "percet_return"}])
custom_run_failure
Out [6]:
Strategy(name='Runtime Failure', ta=[{'kind': 'percet_return'}], description=None, created='08/21/2020, 11:11:36', last_run=None, run_time=None)
In [ ]:

Strategy Management and Execution with Watchlist

Initialize AlphaVantage Data Source

In [7]:
AV = AlphaVantage(
    api_key="YOUR API KEY", premium=False,
    output_size='full', clean=True,
    export_path=".", export=True
)
AV
Out [7]:
AlphaVantage(
  end_point:str = https://www.alphavantage.co/query,
  api_key:str = YOUR API KEY,
  export:bool = True,
  export_path:str = .,
  output_size:str = full,
  output:str = csv,
  datatype:str = json,
  clean:bool = True,
  proxy:dict = {}
)

Create Watchlist and set it's 'ds' to AlphaVantage

In [8]:
watch = Watchlist(["SPY", "IWM"], ds=AV)

Info about the Watchlist. Note, the default Strategy is "All"

In [9]:
watch
Out [9]:
Watch(name='Watchlist: SPY, IWM', tickers[2]='SPY, IWM', tf='D', strategy[5]='Common Price and Volume SMAs')

Help about Watchlist

In [10]:
help(Watchlist)
Help on class Watchlist in module watchlist:

class Watchlist(builtins.object)
 |  Watchlist(tickers: list, tf: str = None, name: str = None, strategy: pandas_ta.core.Strategy = None, ds: object = None, **kwargs)
 |  
 |  Watchlist Class (** This is subject to change! **)
 |  ============================================================================
 |  A simple Class to load/download financial market data and automatically
 |  apply Technical Analysis indicators with a Pandas TA Strategy. Default
 |  Strategy: pandas_ta.AllStrategy.
 |  
 |  Requirements:
 |  - Pandas TA (pip install pandas_ta)
 |  - AlphaVantage (pip install alphaVantage-api) for the Default Data Source.
 |      To use another Data Source, update the load() method after AV.
 |  
 |  Required Arguments:
 |  - tickers: A list of strings containing tickers. Example: ['SPY', 'AAPL']
 |  ============================================================================
 |  
 |  Methods defined here:
 |  
 |  __init__(self, tickers: list, tf: str = None, name: str = None, strategy: pandas_ta.core.Strategy = None, ds: object = None, **kwargs)
 |      Initialize self.  See help(type(self)) for accurate signature.
 |  
 |  __repr__(self) -> str
 |      Return repr(self).
 |  
 |  indicators(self, *args, **kwargs) -> <built-in function any>
 |      Returns the list of indicators that are available with Pandas Ta.
 |  
 |  load(self, ticker: str = None, tf: str = None, index: str = 'date', drop: list = ['dividend', 'split_coefficient'], file_path: str = '.', **kwargs) -> pandas.core.frame.DataFrame
 |      Loads or Downloads (if a local csv does not exist) the data from the
 |      Data Source. When successful, it returns a Data Frame for the requested
 |      ticker. If no tickers are given, it loads all the tickers.
 |  
 |  ----------------------------------------------------------------------
 |  Data descriptors defined here:
 |  
 |  __dict__
 |      dictionary for instance variables (if defined)
 |  
 |  __weakref__
 |      list of weak references to the object (if defined)
 |  
 |  data
 |      When not None, it contains a dictionary of DataFrames keyed by ticker. data = {"SPY": pd.DataFrame, ...}
 |  
 |  name
 |      The name of the Watchlist. Default: "Watchlist: {Watchlist.tickers}".
 |  
 |  strategy
 |      Sets a valid Strategy. Default: pandas_ta.CommonStrategy
 |  
 |  tf
 |      Alias for timeframe. Default: 'D'
 |  
 |  tickers
 |      tickers
 |      
 |      If a string, it it converted to a list. Example: "AAPL" -> ["AAPL"]
 |          * Does not accept, comma seperated strings.
 |      If a list, checks if it is a list of strings.
 |  
 |  verbose
 |      Toggle the verbose property. Default: False

Default Strategy is "Common"

In [11]:
# No arguments loads all the tickers and applies the Strategy to each ticker.
# The result can be accessed with Watchlist's 'data' property which returns a 
# dictionary keyed by ticker and DataFrames as values 
watch.load(verbose=True, timed=False)
[!] Loading All: SPY, IWM
[i] Loaded['D']: SPY_D.csv
[+] Strategy: Common Price and Volume SMAs
[i] Indicator arguments: {'timed': False, 'append': True}
[i] Total indicators: 5
[i] Columns added: 5
[i] Loaded['D']: IWM_D.csv
[+] Strategy: Common Price and Volume SMAs
[i] Indicator arguments: {'timed': False, 'append': True}
[i] Total indicators: 5
[i] Columns added: 5
In [12]:
watch.data
Out [12]:
{'SPY':                 open      high       low     close      volume   SMA_10  \
 date                                                                      
 1999-11-01  136.5000  137.0000  135.5625  135.5625   4006500.0      NaN   
 1999-11-02  135.9687  137.2500  134.5937  134.5937   6516900.0      NaN   
 1999-11-03  136.0000  136.3750  135.1250  135.5000   7222300.0      NaN   
 1999-11-04  136.7500  137.3593  135.7656  136.5312   7907500.0      NaN   
 1999-11-05  138.6250  139.1093  136.7812  137.8750   7431500.0      NaN   
 ...              ...       ...       ...       ...         ...      ...   
 2020-08-10  335.0600  335.7700  332.9550  335.5700  44282089.0  329.220   
 2020-08-11  336.8500  337.5400  332.0100  332.8000  69601087.0  330.383   
 2020-08-12  335.4400  338.2800  332.8377  337.4400  53826128.0  331.615   
 2020-08-13  336.6100  338.2514  335.8300  336.8300  41816146.0  332.902   
 2020-08-14  336.4100  337.4200  335.6200  336.8400  47260390.0  333.934   
 
               SMA_20    SMA_50    SMA_200   VOL_SMA_20  
 date                                                    
 1999-11-01       NaN       NaN        NaN          NaN  
 1999-11-02       NaN       NaN        NaN          NaN  
 1999-11-03       NaN       NaN        NaN          NaN  
 1999-11-04       NaN       NaN        NaN          NaN  
 1999-11-05       NaN       NaN        NaN          NaN  
 ...              ...       ...        ...          ...  
 2020-08-10  325.9365  317.1738  305.38150  60185410.65  
 2020-08-11  326.6305  317.7188  305.54365  59025873.05  
 2020-08-12  327.4100  318.3060  305.72285  57371102.75  
 2020-08-13  328.2120  318.7990  305.89050  56740239.35  
 2020-08-14  328.9680  319.3086  306.05865  55882168.75  
 
 [5231 rows x 10 columns],
 'IWM':               open      high     low   close      volume   SMA_10    SMA_20  \
 date                                                                          
 2000-05-26   91.06   91.4400   90.63   91.44     37400.0      NaN       NaN   
 2000-05-30   92.75   94.8100   92.75   94.81     28800.0      NaN       NaN   
 2000-05-31   95.13   96.3800   95.13   95.75     18000.0      NaN       NaN   
 2000-06-01   97.11   97.3100   97.11   97.31      3500.0      NaN       NaN   
 2000-06-02  101.70  102.4000  101.70  102.40     14700.0      NaN       NaN   
 ...            ...       ...     ...     ...         ...      ...       ...   
 2020-08-14  156.28  157.7585  155.87  157.09  13360915.0  155.071  151.3175   
 2020-08-17  157.50  158.0300  156.74  157.90   9651142.0  155.885  151.9145   
 2020-08-18  157.85  157.8500  155.71  156.39  14647527.0  156.445  152.3325   
 2020-08-19  156.93  158.0400  156.19  156.40  14337271.0  156.706  152.7470   
 2020-08-20  154.80  156.4850  154.54  155.76  15413013.0  156.909  153.1220   
 
               SMA_50    SMA_200   VOL_SMA_20  
 date                                          
 2000-05-26       NaN        NaN          NaN  
 2000-05-30       NaN        NaN          NaN  
 2000-05-31       NaN        NaN          NaN  
 2000-06-01       NaN        NaN          NaN  
 2000-06-02       NaN        NaN          NaN  
 ...              ...        ...          ...  
 2020-08-14  146.1280  145.93365  20773889.15  
 2020-08-17  146.2824  145.94150  20277361.80  
 2020-08-18  146.3484  145.94620  19786384.90  
 2020-08-19  146.4712  145.93770  19282008.05  
 2020-08-20  146.6628  145.92175  18967414.25  
 
 [5091 rows x 10 columns]}

In [13]:
watch.data['SPY']
Out [13]:
open high low close volume SMA_10 SMA_20 SMA_50 SMA_200 VOL_SMA_20
date
1999-11-01 136.5000 137.0000 135.5625 135.5625 4006500.0 NaN NaN NaN NaN NaN
1999-11-02 135.9687 137.2500 134.5937 134.5937 6516900.0 NaN NaN NaN NaN NaN
1999-11-03 136.0000 136.3750 135.1250 135.5000 7222300.0 NaN NaN NaN NaN NaN
1999-11-04 136.7500 137.3593 135.7656 136.5312 7907500.0 NaN NaN NaN NaN NaN
1999-11-05 138.6250 139.1093 136.7812 137.8750 7431500.0 NaN NaN NaN NaN NaN
... ... ... ... ... ... ... ... ... ... ...
2020-08-10 335.0600 335.7700 332.9550 335.5700 44282089.0 329.220 325.9365 317.1738 305.38150 60185410.65
2020-08-11 336.8500 337.5400 332.0100 332.8000 69601087.0 330.383 326.6305 317.7188 305.54365 59025873.05
2020-08-12 335.4400 338.2800 332.8377 337.4400 53826128.0 331.615 327.4100 318.3060 305.72285 57371102.75
2020-08-13 336.6100 338.2514 335.8300 336.8300 41816146.0 332.902 328.2120 318.7990 305.89050 56740239.35
2020-08-14 336.4100 337.4200 335.6200 336.8400 47260390.0 333.934 328.9680 319.3086 306.05865 55882168.75

5231 rows × 10 columns

In [ ]:

Easy to swap Strategies and run them

Running Simple Strategy A

In [14]:
# Load custom_a into Watchlist and verify
watch.strategy = custom_a
# watch.debug = True
watch.strategy
Out [14]:
Strategy(name='A', ta=[{'kind': 'sma', 'length': 50}, {'kind': 'sma', 'length': 200}], description=None, created='08/21/2020, 11:11:36', last_run=None, run_time=None)
In [15]:
watch.load('IWM')
Out [15]:
[i] Loaded['D']: IWM_D.csv
open high low close volume SMA_50 SMA_200
date
2000-05-26 91.06 91.4400 90.63 91.44 37400.0 NaN NaN
2000-05-30 92.75 94.8100 92.75 94.81 28800.0 NaN NaN
2000-05-31 95.13 96.3800 95.13 95.75 18000.0 NaN NaN
2000-06-01 97.11 97.3100 97.11 97.31 3500.0 NaN NaN
2000-06-02 101.70 102.4000 101.70 102.40 14700.0 NaN NaN
... ... ... ... ... ... ... ...
2020-08-14 156.28 157.7585 155.87 157.09 13360915.0 146.1280 145.93365
2020-08-17 157.50 158.0300 156.74 157.90 9651142.0 146.2824 145.94150
2020-08-18 157.85 157.8500 155.71 156.39 14647527.0 146.3484 145.94620
2020-08-19 156.93 158.0400 156.19 156.40 14337271.0 146.4712 145.93770
2020-08-20 154.80 156.4850 154.54 155.76 15413013.0 146.6628 145.92175

5091 rows × 7 columns

Running Simple Strategy B

In [16]:
# Load custom_b into Watchlist and verify
watch.strategy = custom_b
watch.strategy
Out [16]:
Strategy(name='B', ta=[{'kind': 'ema', 'length': 8}, {'kind': 'ema', 'length': 21}, {'kind': 'log_return', 'cumulative': True}, {'kind': 'rsi'}, {'kind': 'supertrend'}], description=None, created='08/21/2020, 11:11:36', last_run=None, run_time=None)
In [17]:
watch.load('SPY')
Out [17]:
[i] Loaded['D']: SPY_D.csv
open high low close volume EMA_8 EMA_21 CUMLOGRET_1 RSI_14 SUPERT_7_3.0 SUPERTd_7_3.0 SUPERTl_7_3.0 SUPERTs_7_3.0
date
1999-11-01 136.5000 137.0000 135.5625 135.5625 4006500.0 NaN NaN NaN NaN 0.000000 1 NaN NaN
1999-11-02 135.9687 137.2500 134.5937 134.5937 6516900.0 NaN NaN -0.007172 0.000000 NaN 1 NaN NaN
1999-11-03 136.0000 136.3750 135.1250 135.5000 7222300.0 NaN NaN -0.000461 50.185503 NaN 1 NaN NaN
1999-11-04 136.7500 137.3593 135.7656 136.5312 7907500.0 NaN NaN 0.007120 69.153995 NaN 1 NaN NaN
1999-11-05 138.6250 139.1093 136.7812 137.8750 7431500.0 NaN NaN 0.016915 79.896816 NaN 1 NaN NaN
... ... ... ... ... ... ... ... ... ... ... ... ... ...
2020-08-10 335.0600 335.7700 332.9550 335.5700 44282089.0 331.385297 325.774293 0.906398 70.138852 324.130845 1 324.130845 NaN
2020-08-11 336.8500 337.5400 332.0100 332.8000 69601087.0 331.699676 326.412994 0.898109 64.125274 324.130845 1 324.130845 NaN
2020-08-12 335.4400 338.2800 332.8377 337.4400 53826128.0 332.975303 327.415449 0.911955 68.930669 324.130845 1 324.130845 NaN
2020-08-13 336.6100 338.2514 335.8300 336.8300 41816146.0 333.831903 328.271317 0.910146 67.647776 325.805414 1 325.805414 NaN
2020-08-14 336.4100 337.4200 335.6200 336.8400 47260390.0 334.500369 329.050288 0.910175 67.658402 326.118326 1 326.118326 NaN

5231 rows × 13 columns

Running Bad Strategy. (Misspelled indicator)

In [18]:
# Load custom_run_failure into Watchlist and verify
watch.strategy = custom_run_failure
watch.strategy
Out [18]:
Strategy(name='Runtime Failure', ta=[{'kind': 'percet_return'}], description=None, created='08/21/2020, 11:11:36', last_run=None, run_time=None)
In [19]:
try:
    iwm = watch.load('IWM')
except AttributeError as error:
    print(f"[X] Oops! {error}")
[i] Loaded['D']: IWM_D.csv
[X] Oops! 'AnalysisIndicators' object has no attribute 'percet_return'
In [ ]:

Indicator Composition/Chaining

  • When you need an indicator to depend on the value of a prior indicator
  • Utilitze prefix or suffix to help identify unique columns or avoid column name clashes.

Volume MAs and MA chains

In [20]:
# Set EMA's and SMA's 'close' to 'volume' to create Volume MAs, prefix 'volume' MAs with 'VOLUME' so easy to identify the column
# Take a price EMA and apply LINREG from EMA's output
volmas_price_ma_chain = [
    {"kind":"ema", "close": "volume", "length": 10, "prefix": "VOLUME"},
    {"kind":"sma", "close": "volume", "length": 20, "prefix": "VOLUME"},
    {"kind":"ema", "length": 5},
    {"kind":"linreg", "close": "EMA_5", "length": 8, "prefix": "EMA_5"},
]
vp_ma_chain_ta = ta.Strategy("Volume MAs and Price MA chain", volmas_price_ma_chain)
vp_ma_chain_ta
Out [20]:
Strategy(name='Volume MAs and Price MA chain', ta=[{'kind': 'ema', 'close': 'volume', 'length': 10, 'prefix': 'VOLUME'}, {'kind': 'sma', 'close': 'volume', 'length': 20, 'prefix': 'VOLUME'}, {'kind': 'ema', 'length': 5}, {'kind': 'linreg', 'close': 'EMA_5', 'length': 8, 'prefix': 'EMA_5'}], description=None, created='08/21/2020, 11:11:36', last_run=None, run_time=None)
In [21]:
# Update the Watchlist
watch.strategy = vp_ma_chain_ta
watch.strategy.name
Out [21]:
'Volume MAs and Price MA chain'
In [22]:
spy = watch.load('SPY')
spy
Out [22]:
[i] Loaded['D']: SPY_D.csv
open high low close volume VOLUME_EMA_10 VOLUME_SMA_20 EMA_5 EMA_5_LR_8
date
1999-11-01 136.5000 137.0000 135.5625 135.5625 4006500.0 NaN NaN NaN NaN
1999-11-02 135.9687 137.2500 134.5937 134.5937 6516900.0 NaN NaN NaN NaN
1999-11-03 136.0000 136.3750 135.1250 135.5000 7222300.0 NaN NaN NaN NaN
1999-11-04 136.7500 137.3593 135.7656 136.5312 7907500.0 NaN NaN NaN NaN
1999-11-05 138.6250 139.1093 136.7812 137.8750 7431500.0 NaN NaN 136.012480 NaN
... ... ... ... ... ... ... ... ... ...
2020-08-10 335.0600 335.7700 332.9550 335.5700 44282089.0 5.288160e+07 60185410.65 333.190086 331.866824
2020-08-11 336.8500 337.5400 332.0100 332.8000 69601087.0 5.592150e+07 59025873.05 333.060057 332.787803
2020-08-12 335.4400 338.2800 332.8377 337.4400 53826128.0 5.554053e+07 57371102.75 334.520038 333.770083
2020-08-13 336.6100 338.2514 335.8300 336.8300 41816146.0 5.304518e+07 56740239.35 335.290026 334.616881
2020-08-14 336.4100 337.4200 335.6200 336.8400 47260390.0 5.199340e+07 55882168.75 335.806684 335.275420

5231 rows × 9 columns

In [ ]:

MACD BBANDS

In [23]:
# MACD is the initial indicator that BBANDS depends on.
# Set BBANDS's 'close' to MACD's main signal, in this case 'MACD_12_26_9' and add a prefix (or suffix) so it's easier to identify
macd_bands_ta = [
    {"kind":"macd"},
    {"kind":"bbands", "close": "MACD_12_26_9", "length": 20, "prefix": "MACD"}
]
macd_bands_ta = ta.Strategy("MACD BBands", macd_bands_ta, f"BBANDS_{macd_bands_ta[1]['length']} applied to MACD")
macd_bands_ta
Out [23]:
Strategy(name='MACD BBands', ta=[{'kind': 'macd'}, {'kind': 'bbands', 'close': 'MACD_12_26_9', 'length': 20, 'prefix': 'MACD'}], description='BBANDS_20 applied to MACD', created='08/21/2020, 11:11:36', last_run=None, run_time=None)
In [24]:
# Update the Watchlist
watch.strategy = macd_bands_ta
watch.strategy.name
Out [24]:
'MACD BBands'
In [25]:
spy = watch.load('SPY')
spy
Out [25]:
[i] Loaded['D']: SPY_D.csv
open high low close volume MACD_12_26_9 MACDh_12_26_9 MACDs_12_26_9 MACD_BBL_20_2.0 MACD_BBM_20_2.0 MACD_BBU_20_2.0
date
1999-11-01 136.5000 137.0000 135.5625 135.5625 4006500.0 NaN NaN NaN NaN NaN NaN
1999-11-02 135.9687 137.2500 134.5937 134.5937 6516900.0 NaN NaN NaN NaN NaN NaN
1999-11-03 136.0000 136.3750 135.1250 135.5000 7222300.0 NaN NaN NaN NaN NaN NaN
1999-11-04 136.7500 137.3593 135.7656 136.5312 7907500.0 NaN NaN NaN NaN NaN NaN
1999-11-05 138.6250 139.1093 136.7812 137.8750 7431500.0 NaN NaN NaN NaN NaN NaN
... ... ... ... ... ... ... ... ... ... ... ...
2020-08-10 335.0600 335.7700 332.9550 335.5700 44282089.0 5.380544 0.668073 4.712471 3.363096 4.366870 5.370643
2020-08-11 336.8500 337.5400 332.0100 332.8000 69601087.0 5.253724 0.433003 4.820721 3.482362 4.458185 5.434008
2020-08-12 335.4400 338.2800 332.8377 337.4400 53826128.0 5.464634 0.515130 4.949504 3.517851 4.541105 5.564359
2020-08-13 336.6100 338.2514 335.8300 336.8300 41816146.0 5.518942 0.455550 5.063392 3.543211 4.618297 5.693384
2020-08-14 336.4100 337.4200 335.6200 336.8400 47260390.0 5.499394 0.348802 5.150592 3.568073 4.686453 5.804834

5231 rows × 11 columns

In [ ]:

Comprehensive Strategy

MACD and RSI Momentum with BBANDS and SMAs and Cumulative Log Returns

In [26]:
momo_bands_sma_ta = [
    {"kind":"sma", "length": 50},
    {"kind":"sma", "length": 200},
    {"kind":"bbands", "length": 20},
    {"kind":"macd"},
    {"kind":"rsi"},
    {"kind":"log_return", "cumulative": True},
    {"kind":"sma", "close": "CUMLOGRET_1", "length": 5, "suffix": "CUMLOGRET"},
]
momo_bands_sma_strategy = ta.Strategy(
    "Momo, Bands and SMAs and Cumulative Log Returns", # name
    momo_bands_sma_ta, # ta
    "MACD and RSI Momo with BBANDS and SMAs 50 & 200 and Cumulative Log Returns" # description
)
momo_bands_sma_strategy
Out [26]:
Strategy(name='Momo, Bands and SMAs and Cumulative Log Returns', ta=[{'kind': 'sma', 'length': 50}, {'kind': 'sma', 'length': 200}, {'kind': 'bbands', 'length': 20}, {'kind': 'macd'}, {'kind': 'rsi'}, {'kind': 'log_return', 'cumulative': True}, {'kind': 'sma', 'close': 'CUMLOGRET_1', 'length': 5, 'suffix': 'CUMLOGRET'}], description='MACD and RSI Momo with BBANDS and SMAs 50 & 200 and Cumulative Log Returns', created='08/21/2020, 11:11:36', last_run=None, run_time=None)
In [27]:
# Update the Watchlist
watch.strategy = momo_bands_sma_strategy
watch.strategy.name
Out [27]:
'Momo, Bands and SMAs and Cumulative Log Returns'
In [28]:
spy = watch.load('SPY', timed=True)
# Apply constants to the DataFrame for indicators
spy.ta.constants(True, [0, 30, 70])
spy.head(20)
Out [28]:
[i] Loaded['D']: SPY_D.csv
[i] Runtime: 43.0055 ms (0.0430 s)
open high low close volume SMA_50 SMA_200 BBL_20_2.0 BBM_20_2.0 BBU_20_2.0 MACD_12_26_9 MACDh_12_26_9 MACDs_12_26_9 RSI_14 CUMLOGRET_1 SMA_5_CUMLOGRET 0 30 70
date
1999-11-01 136.5000 137.0000 135.5625 135.5625 4006500.0 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 0 30 70
1999-11-02 135.9687 137.2500 134.5937 134.5937 6516900.0 NaN NaN NaN NaN NaN NaN NaN NaN 0.000000 -0.007172 NaN 0 30 70
1999-11-03 136.0000 136.3750 135.1250 135.5000 7222300.0 NaN NaN NaN NaN NaN NaN NaN NaN 50.185503 -0.000461 NaN 0 30 70
1999-11-04 136.7500 137.3593 135.7656 136.5312 7907500.0 NaN NaN NaN NaN NaN NaN NaN NaN 69.153995 0.007120 NaN 0 30 70
1999-11-05 138.6250 139.1093 136.7812 137.8750 7431500.0 NaN NaN NaN NaN NaN NaN NaN NaN 79.896816 0.016915 NaN 0 30 70
1999-11-08 137.0000 138.3750 136.7500 138.0000 4649200.0 NaN NaN NaN NaN NaN NaN NaN NaN 80.574537 0.017821 0.006845 0 30 70
1999-11-09 138.5000 138.6875 136.2812 136.7031 4533700.0 NaN NaN NaN NaN NaN NaN NaN NaN 58.528352 0.008379 0.009955 0 30 70
1999-11-10 136.2500 138.3906 136.0781 137.7187 6405600.0 NaN NaN NaN NaN NaN NaN NaN NaN 66.303684 0.015780 0.013203 0 30 70
1999-11-11 138.1875 138.5000 137.4687 138.5000 4794100.0 NaN NaN NaN NaN NaN NaN NaN 0.0 70.833962 0.021438 0.016066 0 30 70
1999-11-12 139.2500 139.9843 137.1250 139.7500 11802900.0 NaN NaN NaN NaN NaN NaN NaN 0.0 76.319408 0.030422 0.018768 0 30 70
1999-11-15 139.8437 140.2500 139.4062 140.0781 2187500.0 NaN NaN NaN NaN NaN NaN NaN 0.0 77.514804 0.032767 0.021757 0 30 70
1999-11-16 140.5625 143.0000 140.0937 141.2500 7544800.0 NaN NaN NaN NaN NaN NaN NaN 0.0 81.170904 0.041099 0.028301 0 30 70
1999-11-17 142.2500 142.9375 141.3125 141.6250 9459000.0 NaN NaN NaN NaN NaN NaN NaN 0.0 82.169980 0.043750 0.033895 0 30 70
1999-11-18 142.4375 143.0000 141.6250 142.6250 4491000.0 NaN NaN NaN NaN NaN NaN NaN 0.0 84.527630 0.050786 0.039765 0 30 70
1999-11-19 142.4062 142.9687 142.0000 142.5000 4832100.0 NaN NaN NaN NaN NaN NaN NaN 0.0 83.049344 0.049909 0.043662 0 30 70
1999-11-22 142.4375 143.0000 141.5000 142.4687 4155400.0 NaN NaN NaN NaN NaN NaN NaN 0.0 82.659517 0.049690 0.047047 0 30 70
1999-11-23 142.8437 142.8437 140.3750 141.2187 5918000.0 NaN NaN NaN NaN NaN NaN NaN 0.0 68.775385 0.040877 0.047002 0 30 70
1999-11-24 140.7500 142.4375 140.0000 141.9687 4459700.0 NaN NaN NaN NaN NaN NaN NaN 0.0 71.832489 0.046174 0.047487 0 30 70
1999-11-26 142.4687 142.8750 141.2500 141.4375 1693900.0 NaN NaN NaN NaN NaN NaN NaN 0.0 66.840920 0.042425 0.045815 0 30 70
1999-11-29 140.8750 141.9218 140.4375 140.9375 7348600.0 NaN NaN 134.086598 139.34217 144.597742 NaN NaN 0.0 62.442534 0.038884 0.043610 0 30 70
In [ ]: