mirror of
https://github.com/wassname/pandas-ta.git
synced 2026-07-17 11:31:03 +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.81b0 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 May 26, 2021, NYSE: 10:57:12, Local: 14:57:12 PDT, Day 146/365 (40.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 May 26, 2021, NYSE: 10:57:12, Local: 14:57:12 PDT, Day 146/365 (40.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 May 26, 2021, NYSE: 10:57:12, Local: 14:57:12 PDT, Day 146/365 (40.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 May 26, 2021, NYSE: 10:57:12, Local: 14:57:12 PDT, Day 146/365 (40.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 May 26, 2021, NYSE: 10:57:12, Local: 14:57:12 PDT, Day 146/365 (40.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 May 26, 2021, NYSE: 10:57:15, Local: 14:57:15 PDT, Day 146/365 (40.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 May 26, 2021, NYSE: 10:57:32, Local: 14:57:32 PDT, Day 146/365 (40.00%)
In [12]:
", ".join([f"{t}: {d.shape}" for t,d in watch.data.items()])Out [12]:
'SPY: (5427, 10), IWM: (5283, 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-05-20 | 411.8000 | 416.6250 | 411.6700 | 415.2800 | 78022218.0 | 414.014 | 415.7705 | 407.6032 | 371.10540 | 78769145.90 |
| 2021-05-21 | 416.8700 | 418.2000 | 414.4500 | 414.9400 | 76578662.0 | 413.296 | 415.6805 | 408.0314 | 371.51955 | 78934269.25 |
| 2021-05-24 | 417.3400 | 420.3200 | 417.0800 | 419.1700 | 51376702.0 | 413.419 | 415.7585 | 408.5336 | 371.94375 | 78893984.75 |
| 2021-05-25 | 420.3300 | 420.7100 | 417.6200 | 418.2400 | 57451396.0 | 413.822 | 415.7945 | 408.9702 | 372.36210 | 79201401.75 |
| 2021-05-26 | 418.8700 | 419.6100 | 417.7600 | 419.0700 | 42955732.0 | 415.188 | 415.8780 | 409.4334 | 372.77960 | 78787245.65 |
5427 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-05-20 | 411.8000 | 416.6250 | 411.6700 | 415.2800 | 78022218.0 | 414.014 | 415.7705 | 407.6032 | 371.10540 | 78769145.90 |
| 2021-05-21 | 416.8700 | 418.2000 | 414.4500 | 414.9400 | 76578662.0 | 413.296 | 415.6805 | 408.0314 | 371.51955 | 78934269.25 |
| 2021-05-24 | 417.3400 | 420.3200 | 417.0800 | 419.1700 | 51376702.0 | 413.419 | 415.7585 | 408.5336 | 371.94375 | 78893984.75 |
| 2021-05-25 | 420.3300 | 420.7100 | 417.6200 | 418.2400 | 57451396.0 | 413.822 | 415.7945 | 408.9702 | 372.36210 | 79201401.75 |
| 2021-05-26 | 418.8700 | 419.6100 | 417.7600 | 419.0700 | 42955732.0 | 415.188 | 415.8780 | 409.4334 | 372.77960 | 78787245.65 |
5427 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 May 26, 2021, NYSE: 10:57:12, Local: 14:57:12 PDT, Day 146/365 (40.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.44 | 90.6300 | 91.44 | 37400.0 | NaN | NaN |
| 2000-05-30 | 92.75 | 94.81 | 92.7500 | 94.81 | 28800.0 | NaN | NaN |
| 2000-05-31 | 95.13 | 96.38 | 95.1300 | 95.75 | 18000.0 | NaN | NaN |
| 2000-06-01 | 97.11 | 97.31 | 97.1100 | 97.31 | 3500.0 | NaN | NaN |
| 2000-06-02 | 101.70 | 102.40 | 101.7000 | 102.40 | 14700.0 | NaN | NaN |
| ... | ... | ... | ... | ... | ... | ... | ... |
| 2021-05-20 | 218.27 | 219.87 | 216.3300 | 219.40 | 24932752.0 | 222.9280 | 192.22100 |
| 2021-05-21 | 221.33 | 222.37 | 219.3497 | 219.97 | 24213723.0 | 222.6830 | 192.55190 |
| 2021-05-24 | 221.18 | 222.45 | 220.0000 | 221.40 | 18324181.0 | 222.4392 | 192.89025 |
| 2021-05-25 | 222.23 | 223.71 | 219.1900 | 219.26 | 20514901.0 | 222.1360 | 193.20570 |
| 2021-05-26 | 220.13 | 223.69 | 220.1200 | 223.35 | 20314326.0 | 221.9930 | 193.53350 |
5283 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 May 26, 2021, NYSE: 10:57:12, Local: 14:57:12 PDT, Day 146/365 (40.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-05-20 | 411.8000 | 416.6250 | 411.6700 | 415.2800 | 78022218.0 | 413.742645 | 413.643188 | 1.119520 | 53.190381 | 422.422132 | -1 | NaN | 422.422132 |
| 2021-05-21 | 416.8700 | 418.2000 | 414.4500 | 414.9400 | 76578662.0 | 414.008724 | 413.761080 | 1.118701 | 52.748699 | 422.422132 | -1 | NaN | 422.422132 |
| 2021-05-24 | 417.3400 | 420.3200 | 417.0800 | 419.1700 | 51376702.0 | 415.155674 | 414.252800 | 1.128844 | 57.479369 | 422.422132 | -1 | NaN | 422.422132 |
| 2021-05-25 | 420.3300 | 420.7100 | 417.6200 | 418.2400 | 57451396.0 | 415.841080 | 414.615272 | 1.126623 | 56.148383 | 422.422132 | -1 | NaN | 422.422132 |
| 2021-05-26 | 418.8700 | 419.6100 | 417.7600 | 419.0700 | 42955732.0 | 416.558618 | 415.020248 | 1.128605 | 57.103084 | 422.422132 | -1 | NaN | 422.422132 |
5427 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 May 26, 2021, NYSE: 10:57:12, Local: 14:57:12 PDT, Day 146/365 (40.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 May 26, 2021, NYSE: 10:57:12, Local: 14:57:12 PDT, Day 146/365 (40.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-05-20 | 411.8000 | 416.6250 | 411.6700 | 415.2800 | 78022218.0 | 8.461273e+07 | 78769145.90 | 413.514631 | 413.070731 |
| 2021-05-21 | 416.8700 | 418.2000 | 414.4500 | 414.9400 | 76578662.0 | 8.315199e+07 | 78934269.25 | 413.989754 | 413.618305 |
| 2021-05-24 | 417.3400 | 420.3200 | 417.0800 | 419.1700 | 51376702.0 | 7.737467e+07 | 78893984.75 | 415.716503 | 414.370775 |
| 2021-05-25 | 420.3300 | 420.7100 | 417.6200 | 418.2400 | 57451396.0 | 7.375225e+07 | 79201401.75 | 416.557668 | 415.115397 |
| 2021-05-26 | 418.8700 | 419.6100 | 417.7600 | 419.0700 | 42955732.0 | 6.815289e+07 | 78787245.65 | 417.395112 | 416.089074 |
5427 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 May 26, 2021, NYSE: 10:57:12, Local: 14:57:12 PDT, Day 146/365 (40.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-05-20 | 411.8000 | 416.6250 | 411.6700 | 415.2800 | 78022218.0 | 1.189878 | -1.043818 | 2.233696 | 7.451354 | 3.971502 | 0.491649 | -175.241148 |
| 2021-05-21 | 416.8700 | 418.2000 | 414.4500 | 414.9400 | 76578662.0 | 1.171810 | -0.849509 | 2.021319 | 7.279997 | 3.729337 | 0.178677 | -190.417786 |
| 2021-05-24 | 417.3400 | 420.3200 | 417.0800 | 419.1700 | 51376702.0 | 1.481736 | -0.431666 | 1.913402 | 7.021896 | 3.503329 | -0.015238 | -200.869906 |
| 2021-05-25 | 420.3300 | 420.7100 | 417.6200 | 418.2400 | 57451396.0 | 1.633482 | -0.223936 | 1.857418 | 6.714411 | 3.289206 | -0.136000 | -208.269455 |
| 2021-05-26 | 418.8700 | 419.6100 | 417.7600 | 419.0700 | 42955732.0 | 1.799967 | -0.045961 | 1.845928 | 6.374729 | 3.090622 | -0.193485 | -212.520806 |
5427 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 May 26, 2021, NYSE: 10:57:12, Local: 14:57:12 PDT, Day 146/365 (40.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-05-20 | 411.80 | 416.625 | 411.67 | 415.28 | 78022218.0 | 407.6032 | 371.10540 | 423.149919 | 415.7705 | 408.391081 | -3.549756 | 1.189878 | -1.043818 | 2.233696 | 53.190381 | 1.119520 | 1.116506 | 0 | 30 | 70 |
| 2021-05-21 | 416.87 | 418.200 | 414.45 | 414.94 | 76578662.0 | 408.0314 | 371.51955 | 423.054331 | 415.6805 | 408.306669 | -3.547836 | 1.171810 | -0.849509 | 2.021319 | 52.748699 | 1.118701 | 1.115717 | 0 | 30 | 70 |
| 2021-05-24 | 417.34 | 420.320 | 417.08 | 419.17 | 51376702.0 | 408.5336 | 371.94375 | 423.244472 | 415.7585 | 408.272528 | -3.601115 | 1.481736 | -0.431666 | 1.913402 | 57.479369 | 1.128844 | 1.117466 | 0 | 30 | 70 |
| 2021-05-25 | 420.33 | 420.710 | 417.62 | 418.24 | 57451396.0 | 408.9702 | 372.36210 | 423.320826 | 415.7945 | 408.268174 | -3.620214 | 1.633482 | -0.223936 | 1.857418 | 56.148383 | 1.126623 | 1.120501 | 0 | 30 | 70 |
| 2021-05-26 | 418.87 | 419.610 | 417.76 | 419.07 | 42955732.0 | 409.4334 | 372.77960 | 423.510034 | 415.8780 | 408.245966 | -3.670324 | 1.799967 | -0.045961 | 1.845928 | 57.103084 | 1.128605 | 1.124459 | 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 May 26, 2021, NYSE: 10:57:12, Local: 14:57:12 PDT, Day 146/365 (40.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-05-20 | 411.80 | 416.625 | 411.67 | 415.28 | 78022218.0 | 413.931044 | -0.745073 | 418.481395 | 409.590605 | 0.012113 |
| 2021-05-21 | 416.87 | 418.200 | 414.45 | 414.94 | 76578662.0 | 414.114490 | -0.506287 | 417.556042 | 409.859958 | -0.003945 |
| 2021-05-24 | 417.34 | 420.320 | 417.08 | 419.17 | 51376702.0 | 415.033674 | -0.003415 | 420.261736 | 408.614264 | 0.008746 |
| 2021-05-25 | 420.33 | 420.710 | 417.62 | 418.24 | 57451396.0 | 415.616642 | 0.202577 | 421.540745 | 409.855255 | 0.015178 |
| 2021-05-26 | 418.87 | 419.610 | 417.76 | 419.07 | 42955732.0 | 416.244526 | 0.362725 | 421.044635 | 413.635365 | 0.019785 |
In [ ]: