mirror of
https://github.com/wassname/pandas-ta.git
synced 2026-07-29 11:24:14 +08:00
208 KiB
208 KiB
In [1]:
%matplotlib inline
import datetime as dt
from tqdm import tqdm
import pandas as pd
import pandas_ta as ta
from alphaVantageAPI.alphavantage import AlphaVantage # pip install alphaVantage-api
from watchlist import Watchlist # Is this failing? If so, copy it locally. See above.
print(f"\nPandas TA v{ta.version}\nTo install the Latest Version:\n$ pip install -U git+https://github.com/twopirllc/pandas-ta\n")
%pylab inlinePandas TA v0.2.74b0 To install the Latest Version: $ pip install -U git+https://github.com/twopirllc/pandas-ta Populating the interactive namespace from numpy and matplotlib
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 = Wednesday April 21, 2021, NYSE: 17:14:16, Local: 21:14:16 PDT, Day 111/365 (30.00%) ta = None
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 = Wednesday April 21, 2021, NYSE: 17:14:16, Local: 21:14:16 PDT, Day 111/365 (30.00%)
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 [ ]:
In [4]:
custom_a = ta.Strategy(name="A", ta=[{"kind": "sma", "length": 50}, {"kind": "sma", "length": 200}])
custom_aOut [4]:
Strategy(name='A', ta=[{'kind': 'sma', 'length': 50}, {'kind': 'sma', 'length': 200}], description='TA Description', created='Wednesday April 21, 2021, NYSE: 17:14:16, Local: 21:14:16 PDT, Day 111/365 (30.00%)')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_bOut [5]:
Strategy(name='B', ta=[{'kind': 'ema', 'length': 8}, {'kind': 'ema', 'length': 21}, {'kind': 'log_return', 'cumulative': True}, {'kind': 'rsi'}, {'kind': 'supertrend'}], description='TA Description', created='Wednesday April 21, 2021, NYSE: 17:14:16, Local: 21:14:16 PDT, Day 111/365 (30.00%)')In [6]:
# Misspelled indicator, will fail later when ran with Pandas TA
custom_run_failure = ta.Strategy(name="Runtime Failure", ta=[{"kind": "percet_return"}])
custom_run_failureOut [6]:
Strategy(name='Runtime Failure', ta=[{'kind': 'percet_return'}], description='TA Description', created='Wednesday April 21, 2021, NYSE: 17:14:16, Local: 21:14:16 PDT, Day 111/365 (30.00%)')In [ ]:
In [7]:
AV = AlphaVantage(
api_key="YOUR API KEY", premium=False,
output_size='full', clean=True,
export_path=".", export=True
)
AVOut [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 = {}
)In [8]:
data_source = "av" # Default
# data_source = "yahoo"
watch = Watchlist(["SPY", "IWM"], ds_name=data_source, timed=False)In [9]:
watchOut [9]:
Watch(name='Watch: SPY, IWM', ds_name='av', tickers[2]='SPY, IWM', tf='D', strategy[5]='Common Price and Volume SMAs')
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_name: str = 'av', **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.CommonStrategy
|
| ## Package Support:
| ### Data Source (Default: AlphaVantage)
| - AlphaVantage (pip install alphaVantage-api).
| - Python Binance (pip install python-binance). # Future Support
| - Yahoo Finance (pip install yfinance). # Almost Supported
|
| # Technical Analysis:
| - Pandas TA (pip install pandas_ta)
|
| ## 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_name: str = 'av', **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 = [], plot: bool = False, **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
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)[!] Loading All: SPY, IWM
[+] Downloading[av]: SPY[D]
[+] Strategy: Common Price and Volume SMAs
[i] Indicator arguments: {'timed': False, 'append': True}
[i] Multiprocessing 5 indicators with 7 chunks and 8/8 cpus.
[i] Total indicators: 5
[i] Columns added: 5
[i] Last Run: Wednesday April 21, 2021, NYSE: 17:14:19, Local: 21:14:19 PDT, Day 111/365 (30.00%)
[+] Downloading[av]: IWM[D]
[+] Strategy: Common Price and Volume SMAs
[i] Indicator arguments: {'timed': False, 'append': True}
[i] Multiprocessing 5 indicators with 7 chunks and 8/8 cpus.
[i] Total indicators: 5
[i] Columns added: 5
[i] Last Run: Wednesday April 21, 2021, NYSE: 17:14:38, Local: 21:14:38 PDT, Day 111/365 (30.00%)
In [12]:
", ".join([f"{t}: {d.shape}" for t,d in watch.data.items()])Out [12]:
'SPY: (5402, 10), IWM: (5258, 10)'
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 |
| ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
| 2021-04-15 | 413.7400 | 416.1600 | 413.6900 | 415.8700 | 60229842.0 | 409.151 | 400.7300 | 393.4496 | 359.19885 | 84100372.00 |
| 2021-04-16 | 417.2500 | 417.9100 | 415.7300 | 417.2600 | 82037278.0 | 410.816 | 402.0190 | 394.1578 | 359.74335 | 82434780.85 |
| 2021-04-19 | 416.2600 | 416.7400 | 413.7900 | 415.2100 | 78498496.0 | 411.701 | 403.3055 | 394.7382 | 360.26680 | 80678481.15 |
| 2021-04-20 | 413.9100 | 415.0859 | 410.5900 | 412.1700 | 81851828.0 | 412.306 | 404.2845 | 395.2274 | 360.76650 | 81082140.25 |
| 2021-04-21 | 411.5100 | 416.2900 | 411.3600 | 416.0700 | 66792983.0 | 413.254 | 405.6130 | 395.7386 | 361.26160 | 79887461.80 |
5402 rows × 10 columns
In [ ]:
In [14]:
watch.load("SPY", plot=True, mas=True)Out [14]:
[i] Loaded SPY[D]: SPY_D.csv
| 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 |
| ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
| 2021-04-15 | 413.7400 | 416.1600 | 413.6900 | 415.8700 | 60229842.0 | 409.151 | 400.7300 | 393.4496 | 359.19885 | 84100372.00 |
| 2021-04-16 | 417.2500 | 417.9100 | 415.7300 | 417.2600 | 82037278.0 | 410.816 | 402.0190 | 394.1578 | 359.74335 | 82434780.85 |
| 2021-04-19 | 416.2600 | 416.7400 | 413.7900 | 415.2100 | 78498496.0 | 411.701 | 403.3055 | 394.7382 | 360.26680 | 80678481.15 |
| 2021-04-20 | 413.9100 | 415.0859 | 410.5900 | 412.1700 | 81851828.0 | 412.306 | 404.2845 | 395.2274 | 360.76650 | 81082140.25 |
| 2021-04-21 | 411.5100 | 416.2900 | 411.3600 | 416.0700 | 66792983.0 | 413.254 | 405.6130 | 395.7386 | 361.26160 | 79887461.80 |
5402 rows × 10 columns
In [ ]:
In [15]:
# Load custom_a into Watchlist and verify
watch.strategy = custom_a
# watch.debug = True
watch.strategyOut [15]:
Strategy(name='A', ta=[{'kind': 'sma', 'length': 50}, {'kind': 'sma', 'length': 200}], description='TA Description', created='Wednesday April 21, 2021, NYSE: 17:14:16, Local: 21:14:16 PDT, Day 111/365 (30.00%)')In [16]:
watch.load("IWM")Out [16]:
[i] Loaded IWM[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 |
| ... | ... | ... | ... | ... | ... | ... | ... |
| 2021-04-15 | 225.09 | 225.1200 | 222.26 | 224.29 | 20587325.0 | 223.0572 | 182.59330 |
| 2021-04-16 | 225.40 | 225.6780 | 223.01 | 224.65 | 23942972.0 | 223.2652 | 183.00065 |
| 2021-04-19 | 223.71 | 224.6571 | 219.94 | 221.73 | 25285933.0 | 223.3274 | 183.40020 |
| 2021-04-20 | 220.77 | 221.6165 | 215.24 | 217.19 | 35570761.0 | 223.2382 | 183.77415 |
| 2021-04-21 | 217.02 | 222.6200 | 215.50 | 222.50 | 31147779.0 | 223.1420 | 184.16950 |
5258 rows × 7 columns
In [17]:
# Load custom_b into Watchlist and verify
watch.strategy = custom_b
watch.strategyOut [17]:
Strategy(name='B', ta=[{'kind': 'ema', 'length': 8}, {'kind': 'ema', 'length': 21}, {'kind': 'log_return', 'cumulative': True}, {'kind': 'rsi'}, {'kind': 'supertrend'}], description='TA Description', created='Wednesday April 21, 2021, NYSE: 17:14:16, Local: 21:14:16 PDT, Day 111/365 (30.00%)')In [18]:
watch.load("SPY")Out [18]:
[i] Loaded SPY[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 | 0.000000 | NaN | 0.000000 | 1 | NaN | NaN |
| 1999-11-02 | 135.9687 | 137.2500 | 134.5937 | 134.5937 | 6516900.0 | NaN | NaN | -0.007172 | NaN | NaN | 1 | NaN | NaN |
| 1999-11-03 | 136.0000 | 136.3750 | 135.1250 | 135.5000 | 7222300.0 | NaN | NaN | -0.000461 | NaN | NaN | 1 | NaN | NaN |
| 1999-11-04 | 136.7500 | 137.3593 | 135.7656 | 136.5312 | 7907500.0 | NaN | NaN | 0.007120 | NaN | NaN | 1 | NaN | NaN |
| 1999-11-05 | 138.6250 | 139.1093 | 136.7812 | 137.8750 | 7431500.0 | NaN | NaN | 0.016915 | NaN | NaN | 1 | NaN | NaN |
| ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
| 2021-04-15 | 413.7400 | 416.1600 | 413.6900 | 415.8700 | 60229842.0 | 410.181984 | 402.967345 | 1.120940 | 73.655303 | 404.511242 | 1 | 404.511242 | NaN |
| 2021-04-16 | 417.2500 | 417.9100 | 415.7300 | 417.2600 | 82037278.0 | 411.754876 | 404.266677 | 1.124277 | 74.749576 | 406.959636 | 1 | 406.959636 | NaN |
| 2021-04-19 | 416.2600 | 416.7400 | 413.7900 | 415.2100 | 78498496.0 | 412.522682 | 405.261524 | 1.119352 | 70.123431 | 406.959636 | 1 | 406.959636 | NaN |
| 2021-04-20 | 413.9100 | 415.0859 | 410.5900 | 412.1700 | 81851828.0 | 412.444308 | 405.889568 | 1.112003 | 63.816107 | 406.959636 | 1 | 406.959636 | NaN |
| 2021-04-21 | 411.5100 | 416.2900 | 411.3600 | 416.0700 | 66792983.0 | 413.250017 | 406.815062 | 1.121421 | 67.815585 | 406.959636 | 1 | 406.959636 | NaN |
5402 rows × 13 columns
In [19]:
# Load custom_run_failure into Watchlist and verify
watch.strategy = custom_run_failure
watch.strategyOut [19]:
Strategy(name='Runtime Failure', ta=[{'kind': 'percet_return'}], description='TA Description', created='Wednesday April 21, 2021, NYSE: 17:14:16, Local: 21:14:16 PDT, Day 111/365 (30.00%)')In [20]:
try:
iwm = watch.load("IWM")
except AttributeError as error:
print(f"[X] Oops! {error}")[i] Loaded IWM[D]: IWM_D.csv [X] Oops! 'AnalysisIndicators' object has no attribute 'percet_return'
In [ ]:
In [21]:
# 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_taOut [21]:
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='TA Description', created='Wednesday April 21, 2021, NYSE: 17:14:16, Local: 21:14:16 PDT, Day 111/365 (30.00%)')In [22]:
# Update the Watchlist
watch.strategy = vp_ma_chain_ta
watch.strategy.nameOut [22]:
'Volume MAs and Price MA chain'
In [23]:
spy = watch.load("SPY")
spyOut [23]:
[i] Loaded SPY[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 |
| ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
| 2021-04-15 | 413.7400 | 416.1600 | 413.6900 | 415.8700 | 60229842.0 | 6.719201e+07 | 84100372.00 | 412.326473 | 411.148064 |
| 2021-04-16 | 417.2500 | 417.9100 | 415.7300 | 417.2600 | 82037278.0 | 6.989115e+07 | 82434780.85 | 413.970982 | 412.535178 |
| 2021-04-19 | 416.2600 | 416.7400 | 413.7900 | 415.2100 | 78498496.0 | 7.145612e+07 | 80678481.15 | 414.383988 | 413.565317 |
| 2021-04-20 | 413.9100 | 415.0859 | 410.5900 | 412.1700 | 81851828.0 | 7.334625e+07 | 81082140.25 | 413.645992 | 413.940568 |
| 2021-04-21 | 411.5100 | 416.2900 | 411.3600 | 416.0700 | 66792983.0 | 7.215475e+07 | 79887461.80 | 414.453995 | 414.381569 |
5402 rows × 9 columns
In [ ]:
In [24]:
# 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, "ddof": 0, "prefix": "MACD"}
]
macd_bands_ta = ta.Strategy("MACD BBands", macd_bands_ta, f"BBANDS_{macd_bands_ta[1]['length']} applied to MACD")
macd_bands_taOut [24]:
Strategy(name='MACD BBands', ta=[{'kind': 'macd'}, {'kind': 'bbands', 'close': 'MACD_12_26_9', 'length': 20, 'ddof': 0, 'prefix': 'MACD'}], description='BBANDS_20 applied to MACD', created='Wednesday April 21, 2021, NYSE: 17:14:16, Local: 21:14:16 PDT, Day 111/365 (30.00%)')In [25]:
# Update the Watchlist
watch.strategy = macd_bands_ta
watch.strategy.nameOut [25]:
'MACD BBands'
In [26]:
spy = watch.load("SPY")
spyOut [26]:
[i] Loaded SPY[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 | MACD_BBB_20_2.0 | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| date | ||||||||||||
| 1999-11-01 | 136.5000 | 137.0000 | 135.5625 | 135.5625 | 4006500.0 | NaN | 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 | NaN |
| 1999-11-03 | 136.0000 | 136.3750 | 135.1250 | 135.5000 | 7222300.0 | NaN | 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 | NaN |
| 1999-11-05 | 138.6250 | 139.1093 | 136.7812 | 137.8750 | 7431500.0 | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
| ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
| 2021-04-15 | 413.7400 | 416.1600 | 413.6900 | 415.8700 | 60229842.0 | 6.478994 | 1.251027 | 5.227967 | 0.107034 | 3.533810 | 6.960587 | 193.942317 |
| 2021-04-16 | 417.2500 | 417.9100 | 415.7300 | 417.2600 | 82037278.0 | 6.775655 | 1.238150 | 5.537504 | 0.033429 | 3.732053 | 7.430676 | 198.208557 |
| 2021-04-19 | 416.2600 | 416.7400 | 413.7900 | 415.2100 | 78498496.0 | 6.767333 | 0.983863 | 5.783470 | 0.063804 | 3.947467 | 7.831129 | 196.767334 |
| 2021-04-20 | 413.9100 | 415.0859 | 410.5900 | 412.1700 | 81851828.0 | 6.441185 | 0.526172 | 5.915013 | 0.184685 | 4.149350 | 8.114015 | 191.098113 |
| 2021-04-21 | 411.5100 | 416.2900 | 411.3600 | 416.0700 | 66792983.0 | 6.423364 | 0.406681 | 6.016683 | 0.402246 | 4.366212 | 8.330177 | 181.574596 |
5402 rows × 12 columns
In [ ]:
In [27]:
momo_bands_sma_ta = [
{"kind":"sma", "length": 50},
{"kind":"sma", "length": 200},
{"kind":"bbands", "length": 20, "ddof": 0},
{"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_strategyOut [27]:
Strategy(name='Momo, Bands and SMAs and Cumulative Log Returns', ta=[{'kind': 'sma', 'length': 50}, {'kind': 'sma', 'length': 200}, {'kind': 'bbands', 'length': 20, 'ddof': 0}, {'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='Wednesday April 21, 2021, NYSE: 17:14:16, Local: 21:14:16 PDT, Day 111/365 (30.00%)')In [28]:
# Update the Watchlist
watch.strategy = momo_bands_sma_strategy
watch.strategy.nameOut [28]:
'Momo, Bands and SMAs and Cumulative Log Returns'
In [29]:
spy = watch.load("SPY")
# Apply constants to the DataFrame for indicators
spy.ta.constants(True, [0, 30, 70])
spy.tail()Out [29]:
[i] Loaded SPY[D]: SPY_D.csv
| open | high | low | close | volume | SMA_50 | SMA_200 | BBL_20_2.0 | BBM_20_2.0 | BBU_20_2.0 | BBB_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 | ||||||||||||||||||||
| 2021-04-15 | 413.74 | 416.1600 | 413.69 | 415.87 | 60229842.0 | 393.4496 | 359.19885 | 381.895791 | 400.7300 | 419.564209 | 9.399949 | 6.478994 | 1.251027 | 5.227967 | 73.655303 | 1.120940 | 1.113188 | 0 | 30 | 70 |
| 2021-04-16 | 417.25 | 417.9100 | 415.73 | 417.26 | 82037278.0 | 394.1578 | 359.74335 | 382.340515 | 402.0190 | 421.697485 | 9.789828 | 6.775655 | 1.238150 | 5.537504 | 74.749576 | 1.124277 | 1.115973 | 0 | 30 | 70 |
| 2021-04-19 | 416.26 | 416.7400 | 413.79 | 415.21 | 78498496.0 | 394.7382 | 360.26680 | 383.714538 | 403.3055 | 422.896462 | 9.715197 | 6.767333 | 0.983863 | 5.783470 | 70.123431 | 1.119352 | 1.117700 | 0 | 30 | 70 |
| 2021-04-20 | 413.91 | 415.0859 | 410.59 | 412.17 | 81851828.0 | 395.2274 | 360.76650 | 384.993564 | 404.2845 | 423.575436 | 9.543248 | 6.441185 | 0.526172 | 5.915013 | 63.816107 | 1.112003 | 1.117365 | 0 | 30 | 70 |
| 2021-04-21 | 411.51 | 416.2900 | 411.36 | 416.07 | 66792983.0 | 395.7386 | 361.26160 | 386.960023 | 405.6130 | 424.265977 | 9.197426 | 6.423364 | 0.406681 | 6.016683 | 67.815585 | 1.121421 | 1.119598 | 0 | 30 | 70 |
In [ ]:
In [30]:
params_ta = [
{"kind":"ema", "params": (10,)},
# params sets MACD's keyword arguments: fast=9, slow=19, signal=10
# and returning the 2nd column: histogram
{"kind":"macd", "params": (9, 19, 10), "col_numbers": (1,)},
# Selects the Lower and Upper Bands and renames them LB and UB, ignoring the MB
{"kind":"bbands", "col_numbers": (0,2), "col_names": ("LB", "UB")},
{"kind":"log_return", "params": (5, False)},
]
params_ta_strategy = ta.Strategy(
"EMA, MACD History, Outter BBands, Log Returns", # name
params_ta, # ta
"EMA, MACD History, BBands(LB, UB), and Log Returns Strategy" # description
)
params_ta_strategyOut [30]:
Strategy(name='EMA, MACD History, Outter BBands, Log Returns', ta=[{'kind': 'ema', 'params': (10,)}, {'kind': 'macd', 'params': (9, 19, 10), 'col_numbers': (1,)}, {'kind': 'bbands', 'col_numbers': (0, 2), 'col_names': ('LB', 'UB')}, {'kind': 'log_return', 'params': (5, False)}], description='EMA, MACD History, BBands(LB, UB), and Log Returns Strategy', created='Wednesday April 21, 2021, NYSE: 17:14:16, Local: 21:14:16 PDT, Day 111/365 (30.00%)')In [31]:
# Update the Watchlist
watch.strategy = params_ta_strategy
watch.strategy.nameOut [31]:
'EMA, MACD History, Outter BBands, Log Returns'
In [32]:
spy = watch.load("SPY")
spy.tail()Out [32]:
[i] Loaded SPY[D]: SPY_D.csv
| open | high | low | close | volume | EMA_10 | MACDh_9_19_10 | LB | UB | LOGRET_5 | |
|---|---|---|---|---|---|---|---|---|---|---|
| date | ||||||||||
| 2021-04-15 | 413.74 | 416.1600 | 413.69 | 415.87 | 60229842.0 | 408.805040 | 1.131842 | 408.891393 | 416.432607 | 0.017832 |
| 2021-04-16 | 417.25 | 417.9100 | 415.73 | 417.26 | 82037278.0 | 410.342305 | 1.100301 | 408.588484 | 419.043516 | 0.013925 |
| 2021-04-19 | 416.26 | 416.7400 | 413.79 | 415.21 | 78498496.0 | 411.227341 | 0.762081 | 409.841056 | 419.218944 | 0.008635 |
| 2021-04-20 | 413.91 | 415.0859 | 410.59 | 412.17 | 81851828.0 | 411.398733 | 0.182182 | 409.424941 | 419.359059 | -0.001673 |
| 2021-04-21 | 411.51 | 416.2900 | 411.36 | 416.07 | 66792983.0 | 412.248055 | 0.066079 | 411.499834 | 419.132166 | 0.011166 |
In [ ]: