mirror of
https://github.com/wassname/pandas-ta.git
synced 2026-08-14 12:40:48 +08:00
207 KiB
207 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 = Monday May 3, 2021, NYSE: 12:00:44, Local: 16:00:44 PDT, Day 123/365 (34.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 = Monday May 3, 2021, NYSE: 12:00:44, Local: 16:00:44 PDT, Day 123/365 (34.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='Monday May 3, 2021, NYSE: 12:00:44, Local: 16:00:44 PDT, Day 123/365 (34.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='Monday May 3, 2021, NYSE: 12:00:44, Local: 16:00:44 PDT, Day 123/365 (34.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='Monday May 3, 2021, NYSE: 12:00:44, Local: 16:00:44 PDT, Day 123/365 (34.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: Monday May 3, 2021, NYSE: 12:00:47, Local: 16:00:47 PDT, Day 123/365 (34.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: Monday May 3, 2021, NYSE: 12:01:05, Local: 16:01:05 PDT, Day 123/365 (34.00%)
In [12]:
", ".join([f"{t}: {d.shape}" for t,d in watch.data.items()])Out [12]:
'SPY: (5410, 10), IWM: (5266, 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-27 | 417.9300 | 418.1400 | 416.3000 | 417.5200 | 51303056.0 | 415.217 | 410.3710 | 397.7478 | 363.27265 | 71792983.05 |
| 2021-04-28 | 417.8100 | 419.0100 | 416.9000 | 417.4000 | 51238854.0 | 415.812 | 411.5045 | 398.2498 | 363.78545 | 70541813.30 |
| 2021-04-29 | 420.3200 | 420.7200 | 416.4400 | 420.0600 | 78544329.0 | 416.231 | 412.6910 | 398.8032 | 364.29115 | 68832319.05 |
| 2021-04-30 | 417.6300 | 418.5400 | 416.3400 | 417.3000 | 85527030.0 | 416.235 | 413.5255 | 399.3348 | 364.76840 | 68124526.75 |
| 2021-05-03 | 419.4300 | 419.8400 | 417.6650 | 418.2000 | 67702070.0 | 416.534 | 414.1175 | 399.8982 | 365.25545 | 66925392.05 |
5410 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-27 | 417.9300 | 418.1400 | 416.3000 | 417.5200 | 51303056.0 | 415.217 | 410.3710 | 397.7478 | 363.27265 | 71792983.05 |
| 2021-04-28 | 417.8100 | 419.0100 | 416.9000 | 417.4000 | 51238854.0 | 415.812 | 411.5045 | 398.2498 | 363.78545 | 70541813.30 |
| 2021-04-29 | 420.3200 | 420.7200 | 416.4400 | 420.0600 | 78544329.0 | 416.231 | 412.6910 | 398.8032 | 364.29115 | 68832319.05 |
| 2021-04-30 | 417.6300 | 418.5400 | 416.3400 | 417.3000 | 85527030.0 | 416.235 | 413.5255 | 399.3348 | 364.76840 | 68124526.75 |
| 2021-05-03 | 419.4300 | 419.8400 | 417.6650 | 418.2000 | 67702070.0 | 416.534 | 414.1175 | 399.8982 | 365.25545 | 66925392.05 |
5410 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='Monday May 3, 2021, NYSE: 12:00:44, Local: 16:00:44 PDT, Day 123/365 (34.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.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 |
| ... | ... | ... | ... | ... | ... | ... | ... |
| 2021-04-27 | 228.71 | 229.390 | 227.51 | 228.45 | 19648108.0 | 223.0372 | 185.87295 |
| 2021-04-28 | 228.08 | 229.490 | 227.10 | 228.84 | 16222739.0 | 223.0974 | 186.31955 |
| 2021-04-29 | 230.85 | 230.950 | 225.79 | 227.99 | 25500342.0 | 223.1760 | 186.75035 |
| 2021-04-30 | 225.72 | 227.800 | 224.14 | 224.89 | 27005991.0 | 223.2620 | 187.13965 |
| 2021-05-03 | 227.21 | 227.407 | 224.92 | 225.99 | 18549520.0 | 223.2780 | 187.53880 |
5266 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='Monday May 3, 2021, NYSE: 12:00:44, Local: 16:00:44 PDT, Day 123/365 (34.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-27 | 417.9300 | 418.1400 | 416.3000 | 417.5200 | 51303056.0 | 415.319173 | 409.798636 | 1.124900 | 65.982183 | 405.421805 | 1 | 405.421805 | NaN |
| 2021-04-28 | 417.8100 | 419.0100 | 416.9000 | 417.4000 | 51238854.0 | 415.781579 | 410.489669 | 1.124612 | 65.732538 | 406.547390 | 1 | 406.547390 | NaN |
| 2021-04-29 | 420.3200 | 420.7200 | 416.4400 | 420.0600 | 78544329.0 | 416.732339 | 411.359699 | 1.130965 | 68.571173 | 407.070077 | 1 | 407.070077 | NaN |
| 2021-04-30 | 417.6300 | 418.5400 | 416.3400 | 417.3000 | 85527030.0 | 416.858486 | 411.899727 | 1.124373 | 62.761728 | 407.070077 | 1 | 407.070077 | NaN |
| 2021-05-03 | 419.4300 | 419.8400 | 417.6650 | 418.2000 | 67702070.0 | 417.156600 | 412.472479 | 1.126527 | 63.837618 | 407.543637 | 1 | 407.543637 | NaN |
5410 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='Monday May 3, 2021, NYSE: 12:00:44, Local: 16:00:44 PDT, Day 123/365 (34.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='Monday May 3, 2021, NYSE: 12:00:44, Local: 16:00:44 PDT, Day 123/365 (34.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-27 | 417.9300 | 418.1400 | 416.3000 | 417.5200 | 51303056.0 | 6.806114e+07 | 71792983.05 | 416.300295 | 415.367660 |
| 2021-04-28 | 417.8100 | 419.0100 | 416.9000 | 417.4000 | 51238854.0 | 6.500254e+07 | 70541813.30 | 416.666863 | 415.960662 |
| 2021-04-29 | 420.3200 | 420.7200 | 416.4400 | 420.0600 | 78544329.0 | 6.746469e+07 | 68832319.05 | 417.797909 | 416.829211 |
| 2021-04-30 | 417.6300 | 418.5400 | 416.3400 | 417.3000 | 85527030.0 | 7.074875e+07 | 68124526.75 | 417.631939 | 417.333800 |
| 2021-05-03 | 419.4300 | 419.8400 | 417.6650 | 418.2000 | 67702070.0 | 7.019481e+07 | 66925392.05 | 417.821293 | 417.779676 |
5410 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='Monday May 3, 2021, NYSE: 12:00:44, Local: 16:00:44 PDT, Day 123/365 (34.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-27 | 417.9300 | 418.1400 | 416.3000 | 417.5200 | 51303056.0 | 5.915955 | -0.079691 | 5.995646 | 8.154250 | 5.215001 | 2.275752 | -112.722858 |
| 2021-04-28 | 417.8100 | 419.0100 | 416.9000 | 417.4000 | 51238854.0 | 5.771638 | -0.179207 | 5.950845 | 7.976673 | 5.397597 | 2.818521 | -95.563847 |
| 2021-04-29 | 420.3200 | 420.7200 | 416.4400 | 420.0600 | 78544329.0 | 5.804989 | -0.116685 | 5.921674 | 7.722758 | 5.573795 | 3.424833 | -77.109493 |
| 2021-04-30 | 417.6300 | 418.5400 | 416.3400 | 417.3000 | 85527030.0 | 5.544794 | -0.301504 | 5.846298 | 7.421640 | 5.714893 | 4.008146 | -59.729793 |
| 2021-05-03 | 419.4300 | 419.8400 | 417.6650 | 418.2000 | 67702070.0 | 5.349544 | -0.397403 | 5.746947 | 7.193621 | 5.807538 | 4.421454 | -47.733950 |
5410 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='Monday May 3, 2021, NYSE: 12:00:44, Local: 16:00:44 PDT, Day 123/365 (34.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-27 | 417.93 | 418.14 | 416.300 | 417.52 | 51303056.0 | 397.7478 | 363.27265 | 423.665215 | 410.3710 | 397.076785 | -6.479120 | 5.915955 | -0.079691 | 5.995646 | 65.982183 | 1.124900 | 1.121342 | 0 | 30 | 70 |
| 2021-04-28 | 417.81 | 419.01 | 416.900 | 417.40 | 51238854.0 | 398.2498 | 363.78545 | 423.017539 | 411.5045 | 399.991461 | -5.595583 | 5.771638 | -0.179207 | 5.950845 | 65.732538 | 1.124612 | 1.121980 | 0 | 30 | 70 |
| 2021-04-29 | 420.32 | 420.72 | 416.440 | 420.06 | 78544329.0 | 398.8032 | 364.29115 | 422.463674 | 412.6910 | 402.918326 | -4.736073 | 5.804989 | -0.116685 | 5.921674 | 68.571173 | 1.130965 | 1.125724 | 0 | 30 | 70 |
| 2021-04-30 | 417.63 | 418.54 | 416.340 | 417.30 | 85527030.0 | 399.3348 | 364.76840 | 421.758243 | 413.5255 | 405.292757 | -3.981734 | 5.544794 | -0.301504 | 5.846298 | 62.761728 | 1.124373 | 1.125993 | 0 | 30 | 70 |
| 2021-05-03 | 419.43 | 419.84 | 417.665 | 418.20 | 67702070.0 | 399.8982 | 365.25545 | 421.894229 | 414.1175 | 406.340771 | -3.755808 | 5.349544 | -0.397403 | 5.746947 | 63.837618 | 1.126527 | 1.126275 | 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='Monday May 3, 2021, NYSE: 12:00:44, Local: 16:00:44 PDT, Day 123/365 (34.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-27 | 417.93 | 418.14 | 416.300 | 417.52 | 51303056.0 | 414.553148 | -0.372667 | 419.977386 | 412.106614 | 0.012897 |
| 2021-04-28 | 417.81 | 419.01 | 416.900 | 417.40 | 51238854.0 | 415.070758 | -0.446056 | 420.391986 | 412.224014 | 0.003191 |
| 2021-04-29 | 420.32 | 420.72 | 416.440 | 420.06 | 78544329.0 | 415.977893 | -0.316421 | 420.143528 | 415.588472 | 0.018719 |
| 2021-04-30 | 417.63 | 418.54 | 416.340 | 417.30 | 85527030.0 | 416.218276 | -0.511511 | 420.070593 | 415.885407 | 0.001343 |
| 2021-05-03 | 419.43 | 419.84 | 417.665 | 418.20 | 67702070.0 | 416.578589 | -0.584752 | 420.158604 | 416.033396 | 0.001412 |
In [ ]: