Files
pandas-ta/examples/PandasTA_Strategy_Examples.ipynb
T

94 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 = 09/09/2020, 22:54:24
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 = 09/09/2020, 22:54:24
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='09/09/2020, 22:54:24', 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='09/09/2020, 22:54:24', 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='09/09/2020, 22:54:24', 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] Multiprocessing: 4 of 4 cores.
[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] Multiprocessing: 4 of 4 cores.
[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-24  342.1200  343.0000  339.4504  342.9200  48588662.0  337.837   
 2020-08-25  343.5300  344.2100  342.2700  344.1200  38463381.0  338.969   
 2020-08-26  344.7600  347.8600  344.1700  347.5700  50790237.0  339.982   
 2020-08-27  348.5100  349.9000  346.5300  348.3300  58034142.0  341.132   
 2020-08-28  349.4400  350.7200  348.1500  350.5800  48588940.0  342.506   
 
               SMA_20    SMA_50    SMA_200  VOL_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-24  333.5285  322.2548  307.05540     51792231.45  
 2020-08-25  334.6760  322.9962  307.23510     50840651.55  
 2020-08-26  335.7985  323.6884  307.42825     50957455.45  
 2020-08-27  337.0170  324.4218  307.62815     50766076.85  
 2020-08-28  338.2200  325.1978  307.83605     48934986.10  
 
 [5241 rows x 10 columns],
 'IWM':               open    high      low   close      volume   SMA_10    SMA_20  \
 date                                                                         
 2000-05-26   91.06   91.44   90.630   91.44     37400.0      NaN       NaN   
 2000-05-30   92.75   94.81   92.750   94.81     28800.0      NaN       NaN   
 2000-05-31   95.13   96.38   95.130   95.75     18000.0      NaN       NaN   
 2000-06-01   97.11   97.31   97.110   97.31      3500.0      NaN       NaN   
 2000-06-02  101.70  102.40  101.700  102.40     14700.0      NaN       NaN   
 ...            ...     ...      ...     ...         ...      ...       ...   
 2020-08-31  157.19  157.37  155.300  155.43  17051511.0  155.956  155.9205   
 2020-09-01  155.22  157.31  154.450  157.21  15654144.0  156.038  156.2415   
 2020-09-02  157.96  158.98  156.175  158.46  16763449.0  156.244  156.4750   
 2020-09-03  158.12  158.29  152.960  153.78  32117585.0  156.046  156.4775   
 2020-09-04  155.71  155.89  149.290  152.80  30618783.0  155.865  156.3090   
 
               SMA_50    SMA_200  VOL_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-31  148.8200  145.82245     17493155.05  
 2020-09-01  149.1162  145.81800     17261612.35  
 2020-09-02  149.4254  145.81570     17173441.45  
 2020-09-03  149.7338  145.79200     17867433.35  
 2020-09-04  149.9808  145.76045     18374614.75  
 
 [5102 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_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-24 342.1200 343.0000 339.4504 342.9200 48588662.0 337.837 333.5285 322.2548 307.05540 51792231.45
2020-08-25 343.5300 344.2100 342.2700 344.1200 38463381.0 338.969 334.6760 322.9962 307.23510 50840651.55
2020-08-26 344.7600 347.8600 344.1700 347.5700 50790237.0 339.982 335.7985 323.6884 307.42825 50957455.45
2020-08-27 348.5100 349.9000 346.5300 348.3300 58034142.0 341.132 337.0170 324.4218 307.62815 50766076.85
2020-08-28 349.4400 350.7200 348.1500 350.5800 48588940.0 342.506 338.2200 325.1978 307.83605 48934986.10

5241 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='09/09/2020, 22:54:24', 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.44 90.630 91.44 37400.0 NaN NaN
2000-05-30 92.75 94.81 92.750 94.81 28800.0 NaN NaN
2000-05-31 95.13 96.38 95.130 95.75 18000.0 NaN NaN
2000-06-01 97.11 97.31 97.110 97.31 3500.0 NaN NaN
2000-06-02 101.70 102.40 101.700 102.40 14700.0 NaN NaN
... ... ... ... ... ... ... ...
2020-08-31 157.19 157.37 155.300 155.43 17051511.0 148.8200 145.82245
2020-09-01 155.22 157.31 154.450 157.21 15654144.0 149.1162 145.81800
2020-09-02 157.96 158.98 156.175 158.46 16763449.0 149.4254 145.81570
2020-09-03 158.12 158.29 152.960 153.78 32117585.0 149.7338 145.79200
2020-09-04 155.71 155.89 149.290 152.80 30618783.0 149.9808 145.76045

5102 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='09/09/2020, 22:54:24', 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 131.968750 1 131.968750 NaN
1999-11-03 136.0000 136.3750 135.1250 135.5000 7222300.0 NaN NaN -0.000461 50.185503 131.968750 1 131.968750 NaN
1999-11-04 136.7500 137.3593 135.7656 136.5312 7907500.0 NaN NaN 0.007120 69.153995 131.968750 1 131.968750 NaN
1999-11-05 138.6250 139.1093 136.7812 137.8750 7431500.0 NaN NaN 0.016915 79.896816 131.968750 1 131.968750 NaN
... ... ... ... ... ... ... ... ... ... ... ... ... ...
2020-08-24 342.1200 343.0000 339.4504 342.9200 48588662.0 338.577882 333.520811 0.928064 72.830131 331.974175 1 331.974175 NaN
2020-08-25 343.5300 344.2100 342.2700 344.1200 38463381.0 339.809464 334.484374 0.931558 74.054445 334.479122 1 334.479122 NaN
2020-08-26 344.7600 347.8600 344.1700 347.5700 50790237.0 341.534027 335.673976 0.941533 77.231098 336.902819 1 336.902819 NaN
2020-08-27 348.5100 349.9000 346.5300 348.3300 58034142.0 343.044243 336.824524 0.943718 77.873776 338.960273 1 338.960273 NaN
2020-08-28 349.4400 350.7200 348.1500 350.5800 48588940.0 344.718856 338.075022 0.950156 79.700567 340.400948 1 340.400948 NaN

5241 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='09/09/2020, 22:54:24', 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
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='09/09/2020, 22:54:24', 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_VOLUME_EMA_10 VOLUME_VOLUME_SMA_20 EMA_5 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-24 342.1200 343.0000 339.4504 342.9200 48588662.0 4.999906e+07 51792231.45 339.779956 338.589691
2020-08-25 343.5300 344.2100 342.2700 344.1200 38463381.0 4.790167e+07 50840651.55 341.226637 339.658445
2020-08-26 344.7600 347.8600 344.1700 347.5700 50790237.0 4.842686e+07 50957455.45 343.341091 341.152455
2020-08-27 348.5100 349.9000 346.5300 348.3300 58034142.0 5.017364e+07 50766076.85 345.004061 342.852470
2020-08-28 349.4400 350.7200 348.1500 350.5800 48588940.0 4.988551e+07 48934986.10 346.862707 344.767837

5241 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='09/09/2020, 22:54:24', 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_MACD_BBL_20_2.0 MACD_MACD_BBM_20_2.0 MACD_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-24 342.1200 343.0000 339.4504 342.9200 48588662.0 5.341674 0.069881 5.271794 3.678022 4.914752 6.151482
2020-08-25 343.5300 344.2100 342.2700 344.1200 38463381.0 5.513019 0.192980 5.320039 3.807707 4.990945 6.174183
2020-08-26 344.7600 347.8600 344.1700 347.5700 50790237.0 5.859651 0.431690 5.427961 3.933776 5.083307 6.232838
2020-08-27 348.5100 349.9000 346.5300 348.3300 58034142.0 6.125079 0.557694 5.567385 4.100036 5.194943 6.289851
2020-08-28 349.4400 350.7200 348.1500 350.5800 48588940.0 6.442721 0.700269 5.742452 4.251657 5.319172 6.386687

5241 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='09/09/2020, 22:54:24', 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: 1208.2109 ms (1.2082 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_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 [ ]: