mirror of
https://github.com/wassname/pandas-ta.git
synced 2026-08-12 12:20:27 +08:00
86 KiB
86 KiB
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 inlinePopulating 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 = 09/27/2020, 09:24:11 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 = 09/27/2020, 09:24:11
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=None, created='09/27/2020, 09:24:11')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=None, created='09/27/2020, 09:24:11')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_failureOut [6]:
Strategy(name='Runtime Failure', ta=[{'kind': 'percet_return'}], description=None, created='09/27/2020, 09:24:11')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]:
watch = Watchlist(["SPY", "IWM"])In [9]:
watchOut [9]:
Watch(name='Watch: SPY, IWM', 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: 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.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: 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'], **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, 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.dataOut [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-09-21 325.7000 327.1300 321.7300 326.9700 99450829.0 335.186
2020-09-22 328.5700 330.9000 325.8600 330.3000 63612107.0 334.895
2020-09-23 330.9000 331.2000 322.1000 322.6400 93112240.0 333.180
2020-09-24 321.2200 326.7970 319.8000 323.5000 76681332.0 332.141
2020-09-25 322.5800 329.5800 321.6400 328.7300 68610432.0 331.608
SMA_20 SMA_50 SMA_200 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-09-21 341.6475 334.0394 309.95180 80051929.15
2020-09-22 341.0165 334.3486 310.04320 80803101.40
2020-09-23 339.9425 334.4230 310.08205 83535544.35
2020-09-24 338.7390 334.4560 310.13015 84830099.10
2020-09-25 337.7590 334.6148 310.20615 85358913.60
[5260 rows x 10 columns],
'IWM': open high low close volume SMA_10 SMA_20 \
date
2000-05-26 91.06 91.440 90.63 91.44 37400.0 NaN NaN
2000-05-30 92.75 94.810 92.75 94.81 28800.0 NaN NaN
2000-05-31 95.13 96.380 95.13 95.75 18000.0 NaN NaN
2000-06-01 97.11 97.310 97.11 97.31 3500.0 NaN NaN
2000-06-02 101.70 102.400 101.70 102.40 14700.0 NaN NaN
... ... ... ... ... ... ... ...
2020-09-21 149.82 150.255 146.33 147.92 40652521.0 151.746 153.8055
2020-09-22 148.65 149.300 146.56 149.06 19385790.0 151.666 153.4470
2020-09-23 148.41 149.430 143.98 144.07 33403387.0 150.860 152.8305
2020-09-24 144.04 146.530 142.09 144.07 31723749.0 150.252 152.2630
2020-09-25 143.44 146.850 143.35 146.41 20689266.0 149.978 151.7935
SMA_50 SMA_200 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-09-21 152.0930 145.33285 21738963.00
2020-09-22 152.2838 145.27345 22022895.70
2020-09-23 152.3286 145.17970 22879582.20
2020-09-24 152.2694 145.08750 23777133.35
2020-09-25 152.2744 145.00650 23936178.00
[5116 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_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-09-21 | 325.7000 | 327.1300 | 321.7300 | 326.9700 | 99450829.0 | 335.186 | 341.6475 | 334.0394 | 309.95180 | 80051929.15 |
| 2020-09-22 | 328.5700 | 330.9000 | 325.8600 | 330.3000 | 63612107.0 | 334.895 | 341.0165 | 334.3486 | 310.04320 | 80803101.40 |
| 2020-09-23 | 330.9000 | 331.2000 | 322.1000 | 322.6400 | 93112240.0 | 333.180 | 339.9425 | 334.4230 | 310.08205 | 83535544.35 |
| 2020-09-24 | 321.2200 | 326.7970 | 319.8000 | 323.5000 | 76681332.0 | 332.141 | 338.7390 | 334.4560 | 310.13015 | 84830099.10 |
| 2020-09-25 | 322.5800 | 329.5800 | 321.6400 | 328.7300 | 68610432.0 | 331.608 | 337.7590 | 334.6148 | 310.20615 | 85358913.60 |
5260 rows × 10 columns
In [ ]:
In [14]:
# Load custom_a into Watchlist and verify
watch.strategy = custom_a
# watch.debug = True
watch.strategyOut [14]:
Strategy(name='A', ta=[{'kind': 'sma', 'length': 50}, {'kind': 'sma', 'length': 200}], description=None, created='09/27/2020, 09:24:11')In [15]:
watch.load("IWM")Out [15]:
[i] Loaded['D']: IWM_D.csv
| open | high | low | close | volume | SMA_50 | SMA_200 | |
|---|---|---|---|---|---|---|---|
| date | |||||||
| 2000-05-26 | 91.06 | 91.440 | 90.63 | 91.44 | 37400.0 | NaN | NaN |
| 2000-05-30 | 92.75 | 94.810 | 92.75 | 94.81 | 28800.0 | NaN | NaN |
| 2000-05-31 | 95.13 | 96.380 | 95.13 | 95.75 | 18000.0 | NaN | NaN |
| 2000-06-01 | 97.11 | 97.310 | 97.11 | 97.31 | 3500.0 | NaN | NaN |
| 2000-06-02 | 101.70 | 102.400 | 101.70 | 102.40 | 14700.0 | NaN | NaN |
| ... | ... | ... | ... | ... | ... | ... | ... |
| 2020-09-21 | 149.82 | 150.255 | 146.33 | 147.92 | 40652521.0 | 152.0930 | 145.33285 |
| 2020-09-22 | 148.65 | 149.300 | 146.56 | 149.06 | 19385790.0 | 152.2838 | 145.27345 |
| 2020-09-23 | 148.41 | 149.430 | 143.98 | 144.07 | 33403387.0 | 152.3286 | 145.17970 |
| 2020-09-24 | 144.04 | 146.530 | 142.09 | 144.07 | 31723749.0 | 152.2694 | 145.08750 |
| 2020-09-25 | 143.44 | 146.850 | 143.35 | 146.41 | 20689266.0 | 152.2744 | 145.00650 |
5116 rows × 7 columns
In [16]:
# Load custom_b into Watchlist and verify
watch.strategy = custom_b
watch.strategyOut [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/27/2020, 09:24:11')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.96875 | NaN |
| 1999-11-03 | 136.0000 | 136.3750 | 135.1250 | 135.5000 | 7222300.0 | NaN | NaN | -0.000461 | 50.185503 | 131.968750 | 1 | 131.96875 | NaN |
| 1999-11-04 | 136.7500 | 137.3593 | 135.7656 | 136.5312 | 7907500.0 | NaN | NaN | 0.007120 | 69.153995 | 131.968750 | 1 | 131.96875 | NaN |
| 1999-11-05 | 138.6250 | 139.1093 | 136.7812 | 137.8750 | 7431500.0 | NaN | NaN | 0.016915 | 79.896816 | 131.968750 | 1 | 131.96875 | NaN |
| ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
| 2020-09-21 | 325.7000 | 327.1300 | 321.7300 | 326.9700 | 99450829.0 | 334.624165 | 337.581041 | 0.880436 | 38.586649 | 344.903390 | -1 | NaN | 344.903390 |
| 2020-09-22 | 328.5700 | 330.9000 | 325.8600 | 330.3000 | 63612107.0 | 333.663239 | 336.919128 | 0.890569 | 42.849311 | 344.903390 | -1 | NaN | 344.903390 |
| 2020-09-23 | 330.9000 | 331.2000 | 322.1000 | 322.6400 | 93112240.0 | 331.213630 | 335.621026 | 0.867104 | 36.562589 | 344.903390 | -1 | NaN | 344.903390 |
| 2020-09-24 | 321.2200 | 326.7970 | 319.8000 | 323.5000 | 76681332.0 | 329.499490 | 334.519114 | 0.869766 | 37.668306 | 344.119874 | -1 | NaN | 344.119874 |
| 2020-09-25 | 322.5800 | 329.5800 | 321.6400 | 328.7300 | 68610432.0 | 329.328493 | 333.992831 | 0.885804 | 44.054622 | 344.119874 | -1 | NaN | 344.119874 |
5260 rows × 13 columns
In [18]:
# Load custom_run_failure into Watchlist and verify
watch.strategy = custom_run_failure
watch.strategyOut [18]:
Strategy(name='Runtime Failure', ta=[{'kind': 'percet_return'}], description=None, created='09/27/2020, 09:24:11')In [19]:
try:
iwm = watch.load("IWM")
except AttributeError as error:
print(f"[X] Oops! {error}")[i] Loaded['D']: IWM_D.csv [X] Oops! 'AnalysisIndicators' object has no attribute 'percet_return'
In [ ]:
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_taOut [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/27/2020, 09:24:11')In [21]:
# Update the Watchlist
watch.strategy = vp_ma_chain_ta
watch.strategy.nameOut [21]:
'Volume MAs and Price MA chain'
In [22]:
spy = watch.load("SPY")
spyOut [22]:
[i] Loaded['D']: SPY_D.csv
| open | high | low | close | volume | VOLUME_EMA_10 | VOLUME_SMA_20 | EMA_5 | EMA_5_LR_8 | |
|---|---|---|---|---|---|---|---|---|---|
| date | |||||||||
| 1999-11-01 | 136.5000 | 137.0000 | 135.5625 | 135.5625 | 4006500.0 | NaN | NaN | NaN | NaN |
| 1999-11-02 | 135.9687 | 137.2500 | 134.5937 | 134.5937 | 6516900.0 | NaN | NaN | NaN | NaN |
| 1999-11-03 | 136.0000 | 136.3750 | 135.1250 | 135.5000 | 7222300.0 | NaN | NaN | NaN | NaN |
| 1999-11-04 | 136.7500 | 137.3593 | 135.7656 | 136.5312 | 7907500.0 | NaN | NaN | NaN | NaN |
| 1999-11-05 | 138.6250 | 139.1093 | 136.7812 | 137.8750 | 7431500.0 | NaN | NaN | 136.012480 | NaN |
| ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
| 2020-09-21 | 325.7000 | 327.1300 | 321.7300 | 326.9700 | 99450829.0 | 8.868160e+07 | 80051929.15 | 332.563993 | 335.473066 |
| 2020-09-22 | 328.5700 | 330.9000 | 325.8600 | 330.3000 | 63612107.0 | 8.412351e+07 | 80803101.40 | 331.809328 | 333.974346 |
| 2020-09-23 | 330.9000 | 331.2000 | 322.1000 | 322.6400 | 93112240.0 | 8.575783e+07 | 83535544.35 | 328.752886 | 331.650953 |
| 2020-09-24 | 321.2200 | 326.7970 | 319.8000 | 323.5000 | 76681332.0 | 8.410756e+07 | 84830099.10 | 327.001924 | 329.309445 |
| 2020-09-25 | 322.5800 | 329.5800 | 321.6400 | 328.7300 | 68610432.0 | 8.128990e+07 | 85358913.60 | 327.577949 | 327.915225 |
5260 rows × 9 columns
In [ ]:
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_taOut [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/27/2020, 09:24:11')In [24]:
# Update the Watchlist
watch.strategy = macd_bands_ta
watch.strategy.nameOut [24]:
'MACD BBands'
In [25]:
spy = watch.load("SPY")
spyOut [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_BBL_20_2.0 | MACD_BBM_20_2.0 | MACD_BBU_20_2.0 | |
|---|---|---|---|---|---|---|---|---|---|---|---|
| date | |||||||||||
| 1999-11-01 | 136.5000 | 137.0000 | 135.5625 | 135.5625 | 4006500.0 | NaN | NaN | NaN | NaN | NaN | NaN |
| 1999-11-02 | 135.9687 | 137.2500 | 134.5937 | 134.5937 | 6516900.0 | NaN | NaN | NaN | NaN | NaN | NaN |
| 1999-11-03 | 136.0000 | 136.3750 | 135.1250 | 135.5000 | 7222300.0 | NaN | NaN | NaN | NaN | NaN | NaN |
| 1999-11-04 | 136.7500 | 137.3593 | 135.7656 | 136.5312 | 7907500.0 | NaN | NaN | NaN | NaN | NaN | NaN |
| 1999-11-05 | 138.6250 | 139.1093 | 136.7812 | 137.8750 | 7431500.0 | NaN | NaN | NaN | NaN | NaN | NaN |
| ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
| 2020-09-21 | 325.7000 | 327.1300 | 321.7300 | 326.9700 | 99450829.0 | -0.892645 | -2.268501 | 1.375856 | -1.306935 | 3.942561 | 9.192057 |
| 2020-09-22 | 328.5700 | 330.9000 | 325.8600 | 330.3000 | 63612107.0 | -1.311585 | -2.149953 | 0.838368 | -2.090184 | 3.609898 | 9.309980 |
| 2020-09-23 | 330.9000 | 331.2000 | 322.1000 | 322.6400 | 93112240.0 | -2.235922 | -2.459431 | 0.223510 | -2.965507 | 3.222451 | 9.410409 |
| 2020-09-24 | 321.2200 | 326.7970 | 319.8000 | 323.5000 | 76681332.0 | -2.866032 | -2.471634 | -0.394399 | -3.834206 | 2.786167 | 9.406539 |
| 2020-09-25 | 322.5800 | 329.5800 | 321.6400 | 328.7300 | 68610432.0 | -2.909839 | -2.012353 | -0.897487 | -4.554227 | 2.334421 | 9.223069 |
5260 rows × 11 columns
In [ ]:
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_strategyOut [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/27/2020, 09:24:11')In [27]:
# Update the Watchlist
watch.strategy = momo_bands_sma_strategy
watch.strategy.nameOut [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()Out [28]:
[i] Loaded['D']: SPY_D.csv [i] Runtime: 1818.4281 ms (1.8184 s)
| open | high | low | close | volume | SMA_50 | SMA_200 | BBL_20_2.0 | BBM_20_2.0 | BBU_20_2.0 | MACD_12_26_9 | MACDh_12_26_9 | MACDs_12_26_9 | RSI_14 | CUMLOGRET_1 | SMA_5_CUMLOGRET | 0 | 30 | 70 | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| date | |||||||||||||||||||
| 1999-11-01 | 136.5000 | 137.0000 | 135.5625 | 135.5625 | 4006500.0 | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | 0 | 30 | 70 |
| 1999-11-02 | 135.9687 | 137.2500 | 134.5937 | 134.5937 | 6516900.0 | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | 0.000000 | -0.007172 | NaN | 0 | 30 | 70 |
| 1999-11-03 | 136.0000 | 136.3750 | 135.1250 | 135.5000 | 7222300.0 | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | 50.185503 | -0.000461 | NaN | 0 | 30 | 70 |
| 1999-11-04 | 136.7500 | 137.3593 | 135.7656 | 136.5312 | 7907500.0 | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | 69.153995 | 0.007120 | NaN | 0 | 30 | 70 |
| 1999-11-05 | 138.6250 | 139.1093 | 136.7812 | 137.8750 | 7431500.0 | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | 79.896816 | 0.016915 | NaN | 0 | 30 | 70 |
In [ ]:
In [29]:
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 [29]:
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='09/27/2020, 09:24:11')In [30]:
# Update the Watchlist
watch.strategy = params_ta_strategy
watch.strategy.nameOut [30]:
'EMA, MACD History, Outter BBands, Log Returns'
In [31]:
spy = watch.load("SPY", timed=True)
spy.tail()Out [31]:
[i] Loaded['D']: SPY_D.csv [i] Runtime: 334.7583 ms (0.3348 s)
| open | high | low | close | volume | EMA_10 | MACDh_9_19_10 | LB | UB | LOGRET_5 | |
|---|---|---|---|---|---|---|---|---|---|---|
| date | ||||||||||
| 2020-09-21 | 325.70 | 327.130 | 321.73 | 326.97 | 99450829.0 | 335.623943 | -2.280086 | 323.348645 | 345.631355 | -0.034537 |
| 2020-09-22 | 328.57 | 330.900 | 325.86 | 330.30 | 63612107.0 | 334.655954 | -2.066826 | 323.032323 | 341.999677 | -0.029444 |
| 2020-09-23 | 330.90 | 331.200 | 322.10 | 322.64 | 93112240.0 | 332.471235 | -2.419469 | 319.513906 | 339.046094 | -0.048932 |
| 2020-09-24 | 321.22 | 326.797 | 319.80 | 323.50 | 76681332.0 | 330.840101 | -2.375870 | 319.375853 | 334.248147 | -0.037436 |
| 2020-09-25 | 322.58 | 329.580 | 321.64 | 328.73 | 68610432.0 | 330.456446 | -1.735596 | 319.831980 | 333.024020 | -0.005824 |
In [ ]: