Files
pandas-ta/examples/PandasTA_Strategy_Examples.ipynb
T
2020-07-25 11:30:00 -07:00

106 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 matplotlib.pyplot as plt
# import mplfinance as mpf

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 = 07/25/2020, 11:24:19
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 = 07/25/2020, 11:24:19
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='07/25/2020, 11:24:19', 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='07/25/2020, 11:24:19', 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='07/25/2020, 11:24:19', 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[0]='All')

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
 |      Pandas TA Strategy Class. Default: pandas_ta.AllStrategy
 |  
 |  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 "All"

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

[+] Downloading['D']: SPY
[+] Strategy: All
[i] Indicators with the following arguments: {'append': True}
[i] Excluded[10]: above, above_value, below, below_value, cross, cross_value, long_run, short_run, trend_return, vp
[i] Total indicators: 101
[i] Columns added: 152

[+] Downloading['D']: IWM
[+] Strategy: All
[i] Indicators with the following arguments: {'append': True}
[i] Excluded[10]: above, above_value, below, below_value, cross, cross_value, long_run, short_run, trend_return, vp
[i] Total indicators: 101
[i] Columns added: 152
In [12]:
watch.data
Out [12]:
{'SPY':                 open      high       low     close      volume  ABER_ZG_5_15  \
 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    136.332267   
 ...              ...       ...       ...       ...         ...           ...   
 2020-07-20  321.4300  325.1300  320.6200  324.3200  56150230.0    320.673333   
 2020-07-21  326.4500  326.9300  323.9400  325.0100  57245315.0    322.353333   
 2020-07-22  324.6200  327.2000  324.5000  326.8600  57792915.0    323.313333   
 2020-07-23  326.4700  327.2300  321.4800  322.9600  75737989.0    324.014000   
 2020-07-24  320.9500  321.9900  319.2460  320.8800  73766597.0    323.886400   
 
             ABER_SG_5_15  ABER_XG_5_15  ABER_ATR_5_15    ACCBL_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-07-20    326.296123    315.050544       5.622790  300.906776  ...   
 2020-07-21    327.800604    316.906063       5.447270  301.895656  ...   
 2020-07-22    328.577452    318.049214       5.264119  302.558926  ...   
 2020-07-23    329.310511    318.717489       5.296511  303.787477  ...   
 2020-07-24    329.077410    318.695390       5.191010  305.041909  ...   
 
                VAR_30   VTXP_14   VTXM_14        VWAP     VWMA_10         WCP  \
 date                                                                            
 1999-11-01        NaN       NaN       NaN  136.041667         NaN  135.921875   
 1999-11-02        NaN       NaN       NaN  135.693303         NaN  135.257775   
 1999-11-03        NaN       NaN       NaN  135.682462         NaN  135.625000   
 1999-11-04        NaN       NaN       NaN  135.950504         NaN  136.546825   
 1999-11-05        NaN       NaN       NaN  136.393305         NaN  137.910125   
 ...               ...       ...       ...         ...         ...         ...   
 2020-07-20  45.091757  1.223611  0.623952  153.261128  318.474591  323.597500   
 2020-07-21  46.469833  1.211493  0.661493  153.278065  319.600245  325.222500   
 2020-07-22  50.919143  1.153997  0.689436  153.295250  320.677611  326.355000   
 2020-07-23  52.999190  1.085706  0.763560  153.317466  321.522738  323.657500   
 2020-07-24  48.773943  1.027629  0.904621  153.338694  321.846761  320.749000   
 
              WILLR_14      WMA_10   ZL_EMA_10      Z_30  
 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-07-20  -3.801032  320.096545  323.215081  1.680556  
 2020-07-21 -10.750280  321.291636  324.115975  1.747819  
 2020-07-22  -2.058111  322.618909  325.718525  1.900614  
 2020-07-23 -25.800604  323.042909  325.442430  1.309102  
 2020-07-24 -38.368580  322.932727  323.987442  0.970050  
 
 [5216 rows x 157 columns],
 'IWM':               open     high     low   close      volume  ABER_ZG_5_15  \
 date                                                                    
 2000-05-26   91.06   91.440   90.63   91.44     37400.0           NaN   
 2000-05-30   92.75   94.810   92.75   94.81     28800.0           NaN   
 2000-05-31   95.13   96.380   95.13   95.75     18000.0           NaN   
 2000-06-01   97.11   97.310   97.11   97.31      3500.0           NaN   
 2000-06-02  101.70  102.400  101.70  102.40     14700.0     96.091333   
 ...            ...      ...     ...     ...         ...           ...   
 2020-07-20  146.12  146.850  145.15  145.96  19581689.0    145.160667   
 2020-07-21  147.47  149.160  147.20  148.03  24467065.0    146.623333   
 2020-07-22  147.09  148.670  147.03  148.11  24424808.0    146.904000   
 2020-07-23  147.98  150.200  146.70  148.26  21704889.0    147.400667   
 2020-07-24  147.29  147.665  145.56  146.08  20015547.0    147.375000   
 
             ABER_SG_5_15  ABER_XG_5_15  ABER_ATR_5_15    ACCBL_20  ...  \
 date                                                               ...   
 2000-05-26           NaN           NaN            NaN         NaN  ...   
 2000-05-30           NaN           NaN            NaN         NaN  ...   
 2000-05-31           NaN           NaN            NaN         NaN  ...   
 2000-06-01           NaN           NaN            NaN         NaN  ...   
 2000-06-02           NaN           NaN            NaN         NaN  ...   
 ...                  ...           ...            ...         ...  ...   
 2020-07-20    149.115307    141.206027       3.954640  133.720919  ...   
 2020-07-21    150.527664    142.719003       3.904331  134.312771  ...   
 2020-07-22    150.657375    143.150625       3.753375  134.568274  ...   
 2020-07-23    151.137150    143.664183       3.736483  135.260831  ...   
 2020-07-24    151.042385    143.707615       3.667385  135.937695  ...   
 
                VAR_30   VTXP_14   VTXM_14       VWAP     VWMA_10        WCP  \
 date                                                                          
 2000-05-26        NaN       NaN       NaN  91.170000         NaN   91.23750   
 2000-05-30        NaN       NaN       NaN  92.454834         NaN   94.29500   
 2000-05-31        NaN       NaN       NaN  93.159976         NaN   95.75250   
 2000-06-01        NaN       NaN       NaN  93.322938         NaN   97.26000   
 2000-06-02        NaN       NaN       NaN  94.592497         NaN  102.22500   
 ...               ...       ...       ...        ...         ...        ...   
 2020-07-20  14.347839  1.085970  0.934117  88.365517  143.089584  145.98000   
 2020-07-21  11.515402  1.030077  0.909147  88.373544  143.813948  148.10500   
 2020-07-22  10.497958  1.013525  0.935595  88.381529  144.438962  147.98000   
 2020-07-23  11.200568  0.993994  0.920849  88.388676  145.342850  148.35500   
 2020-07-24   9.687226  0.945133  0.982616  88.395051  145.807709  146.34625   
 
              WILLR_14      WMA_10   ZL_EMA_10      Z_30  
 date                                                     
 2000-05-26        NaN         NaN         NaN       NaN  
 2000-05-30        NaN         NaN         NaN       NaN  
 2000-05-31        NaN         NaN         NaN       NaN  
 2000-06-01        NaN         NaN         NaN       NaN  
 2000-06-02        NaN         NaN         NaN       NaN  
 ...               ...         ...         ...       ...  
 2020-07-20 -17.267552  144.240000  146.914980  0.906142  
 2020-07-21  -9.479866  145.149091  147.299529  1.671168  
 2020-07-22  -8.808725  145.942909  147.801433  1.797089  
 2020-07-23 -14.969136  146.651818  148.188445  1.763914  
 2020-07-24 -31.790123  146.797273  147.826909  1.077936  
 
 [5072 rows x 157 columns]}

In [13]:
watch.data['SPY']
Out [13]:
open high low close volume ABER_ZG_5_15 ABER_SG_5_15 ABER_XG_5_15 ABER_ATR_5_15 ACCBL_20 ... VAR_30 VTXP_14 VTXM_14 VWAP VWMA_10 WCP WILLR_14 WMA_10 ZL_EMA_10 Z_30
date
1999-11-01 136.5000 137.0000 135.5625 135.5625 4006500.0 NaN NaN NaN NaN NaN ... NaN NaN NaN 136.041667 NaN 135.921875 NaN NaN NaN NaN
1999-11-02 135.9687 137.2500 134.5937 134.5937 6516900.0 NaN NaN NaN NaN NaN ... NaN NaN NaN 135.693303 NaN 135.257775 NaN NaN NaN NaN
1999-11-03 136.0000 136.3750 135.1250 135.5000 7222300.0 NaN NaN NaN NaN NaN ... NaN NaN NaN 135.682462 NaN 135.625000 NaN NaN NaN NaN
1999-11-04 136.7500 137.3593 135.7656 136.5312 7907500.0 NaN NaN NaN NaN NaN ... NaN NaN NaN 135.950504 NaN 136.546825 NaN NaN NaN NaN
1999-11-05 138.6250 139.1093 136.7812 137.8750 7431500.0 136.332267 NaN NaN NaN NaN ... NaN NaN NaN 136.393305 NaN 137.910125 NaN NaN NaN NaN
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
2020-07-20 321.4300 325.1300 320.6200 324.3200 56150230.0 320.673333 326.296123 315.050544 5.622790 300.906776 ... 45.091757 1.223611 0.623952 153.261128 318.474591 323.597500 -3.801032 320.096545 323.215081 1.680556
2020-07-21 326.4500 326.9300 323.9400 325.0100 57245315.0 322.353333 327.800604 316.906063 5.447270 301.895656 ... 46.469833 1.211493 0.661493 153.278065 319.600245 325.222500 -10.750280 321.291636 324.115975 1.747819
2020-07-22 324.6200 327.2000 324.5000 326.8600 57792915.0 323.313333 328.577452 318.049214 5.264119 302.558926 ... 50.919143 1.153997 0.689436 153.295250 320.677611 326.355000 -2.058111 322.618909 325.718525 1.900614
2020-07-23 326.4700 327.2300 321.4800 322.9600 75737989.0 324.014000 329.310511 318.717489 5.296511 303.787477 ... 52.999190 1.085706 0.763560 153.317466 321.522738 323.657500 -25.800604 323.042909 325.442430 1.309102
2020-07-24 320.9500 321.9900 319.2460 320.8800 73766597.0 323.886400 329.077410 318.695390 5.191010 305.041909 ... 48.773943 1.027629 0.904621 153.338694 321.846761 320.749000 -38.368580 322.932727 323.987442 0.970050

5216 rows × 157 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.strategy
Out [14]:
Strategy(name='A', ta=[{'kind': 'sma', 'length': 50}, {'kind': 'sma', 'length': 200}], description=None, created='07/25/2020, 11:24:19', 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.440 90.63 91.44 37400.0 NaN NaN
2000-05-30 92.75 94.810 92.75 94.81 28800.0 NaN NaN
2000-05-31 95.13 96.380 95.13 95.75 18000.0 NaN NaN
2000-06-01 97.11 97.310 97.11 97.31 3500.0 NaN NaN
2000-06-02 101.70 102.400 101.70 102.40 14700.0 NaN NaN
... ... ... ... ... ... ... ...
2020-07-20 146.12 146.850 145.15 145.96 19581689.0 139.7062 145.93605
2020-07-21 147.47 149.160 147.20 148.03 24467065.0 140.0196 145.93750
2020-07-22 147.09 148.670 147.03 148.11 24424808.0 140.3478 145.93235
2020-07-23 147.98 150.200 146.70 148.26 21704889.0 140.7736 145.92925
2020-07-24 147.29 147.665 145.56 146.08 20015547.0 141.2408 145.92735

5072 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='07/25/2020, 11:24:19', 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-07-20 321.4300 325.1300 320.6200 324.3200 56150230.0 319.783320 315.076450 0.872298 63.569724 307.890439 1 307.890439 NaN
2020-07-21 326.4500 326.9300 323.9400 325.0100 57245315.0 320.944805 315.979500 0.874423 64.179426 311.309662 1 311.309662 NaN
2020-07-22 324.6200 327.2000 324.5000 326.8600 57792915.0 322.259293 316.968636 0.880099 65.830627 312.585425 1 312.585425 NaN
2020-07-23 326.4700 327.2300 321.4800 322.9600 75737989.0 322.415005 317.513306 0.868096 59.594031 312.585425 1 312.585425 NaN
2020-07-24 320.9500 321.9900 319.2460 320.8800 73766597.0 322.073893 317.819369 0.861634 56.518677 312.585425 1 312.585425 NaN

5216 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='07/25/2020, 11:24:19', 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='07/25/2020, 11:24:19', 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-07-20 321.4300 325.1300 320.6200 324.3200 56150230.0 7.254859e+07 81019145.80 321.398416 320.058000
2020-07-21 326.4500 326.9300 323.9400 325.0100 57245315.0 6.976618e+07 80181050.95 322.602277 321.307000
2020-07-22 324.6200 327.2000 324.5000 326.8600 57792915.0 6.758922e+07 79667351.70 324.021518 322.687127
2020-07-23 326.4700 327.2300 321.4800 322.9600 75737989.0 6.907082e+07 76850881.55 323.667679 323.406458
2020-07-24 320.9500 321.9900 319.2460 320.8800 73766597.0 6.992459e+07 76090907.45 322.738452 323.487321

5216 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='07/25/2020, 11:24:19', 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
[i] Set 'df.ta.mp = True' to enable multiprocessing. This computer has 4 cores. Default: False
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-07-20 321.4300 325.1300 320.6200 324.3200 56150230.0 4.422827 0.713695 3.709131 1.351168 3.153296 4.955423
2020-07-21 326.4500 326.9300 323.9400 325.0100 57245315.0 4.651974 0.754274 3.897700 1.341550 3.157320 4.973090
2020-07-22 324.6200 327.2000 324.5000 326.8600 57792915.0 4.926070 0.822696 4.103374 1.279092 3.183563 5.088035
2020-07-23 326.4700 327.2300 321.4800 322.9600 75737989.0 4.773569 0.536156 4.237413 1.215602 3.243110 5.270617
2020-07-24 320.9500 321.9900 319.2460 320.8800 73766597.0 4.433763 0.157080 4.276683 1.211360 3.306770 5.402179

5216 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='07/25/2020, 11:24:19', 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, 0, 1) # 0
spy.ta.constants(True, 30, 30, 1) # 30
spy.ta.constants(True, 70, 70, 1) # 70
spy.head(20)
Out [28]:
[i] Loaded['D']: SPY_D.csv
[i] Runtime: 34.4336 ms (0.0344 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 [ ]: