mirror of
https://github.com/wassname/pandas-ta.git
synced 2026-08-13 12:30:58 +08:00
2800 lines
94 KiB
Plaintext
2800 lines
94 KiB
Plaintext
{
|
||
"cells": [
|
||
{
|
||
"cell_type": "markdown",
|
||
"metadata": {},
|
||
"source": [
|
||
"# Pandas TA ([pandas_ta](https://github.com/twopirllc/pandas-ta)) Strategies for Custom Technical Analysis\n",
|
||
"\n",
|
||
"## Topics\n",
|
||
"- What is a Pandas TA Strategy?\n",
|
||
" - Builtin Strategies: __AllStrategy__ and __CommonStrategy__\n",
|
||
" - Creating Strategies\n",
|
||
"- Watchlist Class\n",
|
||
" - Strategy Management and Execution\n",
|
||
"- Indicator Composition/Chaining for more Complex Strategies\n",
|
||
" - Comprehensive Example: _MACD and RSI Momo with BBANDS and SMAs 50 & 200 and Cumulative Log Returns_"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 1,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"name": "stdout",
|
||
"output_type": "stream",
|
||
"text": [
|
||
"Populating the interactive namespace from numpy and matplotlib\n"
|
||
]
|
||
}
|
||
],
|
||
"source": [
|
||
"%matplotlib inline\n",
|
||
"import datetime as dt\n",
|
||
"\n",
|
||
"import pandas as pd\n",
|
||
"import pandas_ta as ta\n",
|
||
"from alphaVantageAPI.alphavantage import AlphaVantage # pip install alphaVantage-api\n",
|
||
"\n",
|
||
"from watchlist import Watchlist\n",
|
||
"%pylab inline"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"metadata": {},
|
||
"source": [
|
||
"# What is a Pandas TA Strategy?\n",
|
||
"A _Strategy_ is a simple way to name and group your favorite TA indicators. Technically, a _Strategy_ is a simple Data Class to contain list of indicators and their parameters. __Note__: _Strategy_ is experimental and subject to change. Pandas TA comes with two basic Strategies: __AllStrategy__ and __CommonStrategy__.\n",
|
||
"\n",
|
||
"## Strategy Requirements:\n",
|
||
"- _name_: Some short memorable string. _Note_: Case-insensitive \"All\" is reserved.\n",
|
||
"- _ta_: A list of dicts containing keyword arguments to identify the indicator and the indicator's arguments\n",
|
||
"\n",
|
||
"## Optional Requirements:\n",
|
||
"- _description_: A more detailed description of what the Strategy tries to capture. Default: None\n",
|
||
"- _created_: At datetime string of when it was created. Default: Automatically generated.\n",
|
||
"\n",
|
||
"### Things to note:\n",
|
||
"- A Strategy will __fail__ when consumed by Pandas TA if there is no {\"kind\": \"indicator name\"} attribute."
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"metadata": {},
|
||
"source": [
|
||
"# Builtin Examples"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"metadata": {},
|
||
"source": [
|
||
"### All"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 2,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"name": "stdout",
|
||
"output_type": "stream",
|
||
"text": [
|
||
"name = All\n",
|
||
"description = All the indicators with their default settings. Pandas TA default.\n",
|
||
"created = 09/09/2020, 22:54:24\n",
|
||
"ta = None\n"
|
||
]
|
||
}
|
||
],
|
||
"source": [
|
||
"AllStrategy = ta.AllStrategy\n",
|
||
"print(\"name =\", AllStrategy.name)\n",
|
||
"print(\"description =\", AllStrategy.description)\n",
|
||
"print(\"created =\", AllStrategy.created)\n",
|
||
"print(\"ta =\", AllStrategy.ta)"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"metadata": {},
|
||
"source": [
|
||
"### Common"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 3,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"name": "stdout",
|
||
"output_type": "stream",
|
||
"text": [
|
||
"name = Common Price and Volume SMAs\n",
|
||
"description = Common Price SMAs: 10, 20, 50, 200 and Volume SMA: 20.\n",
|
||
"created = 09/09/2020, 22:54:24\n",
|
||
"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'}]\n"
|
||
]
|
||
}
|
||
],
|
||
"source": [
|
||
"CommonStrategy = ta.CommonStrategy\n",
|
||
"print(\"name =\", CommonStrategy.name)\n",
|
||
"print(\"description =\", CommonStrategy.description)\n",
|
||
"print(\"created =\", CommonStrategy.created)\n",
|
||
"print(\"ta =\", CommonStrategy.ta)"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": []
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"metadata": {},
|
||
"source": [
|
||
"# Creating Strategies"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"metadata": {},
|
||
"source": [
|
||
"### Simple Strategy A"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 4,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"data": {
|
||
"text/plain": [
|
||
"Strategy(name='A', ta=[{'kind': 'sma', 'length': 50}, {'kind': 'sma', 'length': 200}], description=None, created='09/09/2020, 22:54:24', last_run=None, run_time=None)"
|
||
]
|
||
},
|
||
"execution_count": 4,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"custom_a = ta.Strategy(name=\"A\", ta=[{\"kind\": \"sma\", \"length\": 50}, {\"kind\": \"sma\", \"length\": 200}])\n",
|
||
"custom_a"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"metadata": {},
|
||
"source": [
|
||
"### Simple Strategy B"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 5,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"data": {
|
||
"text/plain": [
|
||
"Strategy(name='B', ta=[{'kind': 'ema', 'length': 8}, {'kind': 'ema', 'length': 21}, {'kind': 'log_return', 'cumulative': True}, {'kind': 'rsi'}, {'kind': 'supertrend'}], description=None, created='09/09/2020, 22:54:24', last_run=None, run_time=None)"
|
||
]
|
||
},
|
||
"execution_count": 5,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"custom_b = ta.Strategy(name=\"B\", ta=[{\"kind\": \"ema\", \"length\": 8}, {\"kind\": \"ema\", \"length\": 21}, {\"kind\": \"log_return\", \"cumulative\": True}, {\"kind\": \"rsi\"}, {\"kind\": \"supertrend\"}])\n",
|
||
"custom_b"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"metadata": {},
|
||
"source": [
|
||
"### Bad Strategy. (Misspelled Indicator)"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 6,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"data": {
|
||
"text/plain": [
|
||
"Strategy(name='Runtime Failure', ta=[{'kind': 'percet_return'}], description=None, created='09/09/2020, 22:54:24', last_run=None, run_time=None)"
|
||
]
|
||
},
|
||
"execution_count": 6,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"# Misspelled indicator, will fail later when ran with Pandas\n",
|
||
"custom_run_failure = ta.Strategy(name=\"Runtime Failure\", ta=[{\"kind\": \"percet_return\"}])\n",
|
||
"custom_run_failure"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": []
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"metadata": {},
|
||
"source": [
|
||
"# Strategy Management and Execution with _Watchlist_"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"metadata": {},
|
||
"source": [
|
||
"### Initialize AlphaVantage Data Source"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 7,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"data": {
|
||
"text/plain": [
|
||
"AlphaVantage(\n",
|
||
" end_point:str = https://www.alphavantage.co/query,\n",
|
||
" api_key:str = YOUR API KEY,\n",
|
||
" export:bool = True,\n",
|
||
" export_path:str = .,\n",
|
||
" output_size:str = full,\n",
|
||
" output:str = csv,\n",
|
||
" datatype:str = json,\n",
|
||
" clean:bool = True,\n",
|
||
" proxy:dict = {}\n",
|
||
")"
|
||
]
|
||
},
|
||
"execution_count": 7,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"AV = AlphaVantage(\n",
|
||
" api_key=\"YOUR API KEY\", premium=False,\n",
|
||
" output_size='full', clean=True,\n",
|
||
" export_path=\".\", export=True\n",
|
||
")\n",
|
||
"AV"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"metadata": {},
|
||
"source": [
|
||
"### Create Watchlist and set it's 'ds' to AlphaVantage"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 8,
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"watch = Watchlist([\"SPY\", \"IWM\"], ds=AV)"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"metadata": {},
|
||
"source": [
|
||
"#### Info about the Watchlist. Note, the default Strategy is \"All\""
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 9,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"data": {
|
||
"text/plain": [
|
||
"Watch(name='Watchlist: SPY, IWM', tickers[2]='SPY, IWM', tf='D', strategy[5]='Common Price and Volume SMAs')"
|
||
]
|
||
},
|
||
"execution_count": 9,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"watch"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"metadata": {},
|
||
"source": [
|
||
"### Help about Watchlist"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 10,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"name": "stdout",
|
||
"output_type": "stream",
|
||
"text": [
|
||
"Help on class Watchlist in module watchlist:\n",
|
||
"\n",
|
||
"class Watchlist(builtins.object)\n",
|
||
" | Watchlist(tickers: list, tf: str = None, name: str = None, strategy: pandas_ta.core.Strategy = None, ds: object = None, **kwargs)\n",
|
||
" | \n",
|
||
" | Watchlist Class (** This is subject to change! **)\n",
|
||
" | ============================================================================\n",
|
||
" | A simple Class to load/download financial market data and automatically\n",
|
||
" | apply Technical Analysis indicators with a Pandas TA Strategy. Default\n",
|
||
" | Strategy: pandas_ta.AllStrategy.\n",
|
||
" | \n",
|
||
" | Requirements:\n",
|
||
" | - Pandas TA (pip install pandas_ta)\n",
|
||
" | - AlphaVantage (pip install alphaVantage-api) for the Default Data Source.\n",
|
||
" | To use another Data Source, update the load() method after AV.\n",
|
||
" | \n",
|
||
" | Required Arguments:\n",
|
||
" | - tickers: A list of strings containing tickers. Example: ['SPY', 'AAPL']\n",
|
||
" | ============================================================================\n",
|
||
" | \n",
|
||
" | Methods defined here:\n",
|
||
" | \n",
|
||
" | __init__(self, tickers: list, tf: str = None, name: str = None, strategy: pandas_ta.core.Strategy = None, ds: object = None, **kwargs)\n",
|
||
" | Initialize self. See help(type(self)) for accurate signature.\n",
|
||
" | \n",
|
||
" | __repr__(self) -> str\n",
|
||
" | Return repr(self).\n",
|
||
" | \n",
|
||
" | indicators(self, *args, **kwargs) -> <built-in function any>\n",
|
||
" | Returns the list of indicators that are available with Pandas Ta.\n",
|
||
" | \n",
|
||
" | load(self, ticker: str = None, tf: str = None, index: str = 'date', drop: list = ['dividend', 'split_coefficient'], file_path: str = '.', **kwargs) -> pandas.core.frame.DataFrame\n",
|
||
" | Loads or Downloads (if a local csv does not exist) the data from the\n",
|
||
" | Data Source. When successful, it returns a Data Frame for the requested\n",
|
||
" | ticker. If no tickers are given, it loads all the tickers.\n",
|
||
" | \n",
|
||
" | ----------------------------------------------------------------------\n",
|
||
" | Data descriptors defined here:\n",
|
||
" | \n",
|
||
" | __dict__\n",
|
||
" | dictionary for instance variables (if defined)\n",
|
||
" | \n",
|
||
" | __weakref__\n",
|
||
" | list of weak references to the object (if defined)\n",
|
||
" | \n",
|
||
" | data\n",
|
||
" | When not None, it contains a dictionary of DataFrames keyed by ticker. data = {\"SPY\": pd.DataFrame, ...}\n",
|
||
" | \n",
|
||
" | name\n",
|
||
" | The name of the Watchlist. Default: \"Watchlist: {Watchlist.tickers}\".\n",
|
||
" | \n",
|
||
" | strategy\n",
|
||
" | Sets a valid Strategy. Default: pandas_ta.CommonStrategy\n",
|
||
" | \n",
|
||
" | tf\n",
|
||
" | Alias for timeframe. Default: 'D'\n",
|
||
" | \n",
|
||
" | tickers\n",
|
||
" | tickers\n",
|
||
" | \n",
|
||
" | If a string, it it converted to a list. Example: \"AAPL\" -> [\"AAPL\"]\n",
|
||
" | * Does not accept, comma seperated strings.\n",
|
||
" | If a list, checks if it is a list of strings.\n",
|
||
" | \n",
|
||
" | verbose\n",
|
||
" | Toggle the verbose property. Default: False\n",
|
||
"\n"
|
||
]
|
||
}
|
||
],
|
||
"source": [
|
||
"help(Watchlist)"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"metadata": {},
|
||
"source": [
|
||
"### Default Strategy is \"Common\""
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 11,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"name": "stdout",
|
||
"output_type": "stream",
|
||
"text": [
|
||
"[!] Loading All: SPY, IWM\n",
|
||
"[i] Loaded['D']: SPY_D.csv\n",
|
||
"[+] Strategy: Common Price and Volume SMAs\n",
|
||
"[i] Indicator arguments: {'timed': False, 'append': True}\n",
|
||
"[i] Multiprocessing: 4 of 4 cores.\n",
|
||
"[i] Total indicators: 5\n",
|
||
"[i] Columns added: 5\n",
|
||
"[i] Loaded['D']: IWM_D.csv\n",
|
||
"[+] Strategy: Common Price and Volume SMAs\n",
|
||
"[i] Indicator arguments: {'timed': False, 'append': True}\n",
|
||
"[i] Multiprocessing: 4 of 4 cores.\n",
|
||
"[i] Total indicators: 5\n",
|
||
"[i] Columns added: 5\n"
|
||
]
|
||
}
|
||
],
|
||
"source": [
|
||
"# No arguments loads all the tickers and applies the Strategy to each ticker.\n",
|
||
"# The result can be accessed with Watchlist's 'data' property which returns a \n",
|
||
"# dictionary keyed by ticker and DataFrames as values \n",
|
||
"watch.load(verbose=True, timed=False)"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 12,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"data": {
|
||
"text/plain": [
|
||
"{'SPY': open high low close volume SMA_10 \\\n",
|
||
" date \n",
|
||
" 1999-11-01 136.5000 137.0000 135.5625 135.5625 4006500.0 NaN \n",
|
||
" 1999-11-02 135.9687 137.2500 134.5937 134.5937 6516900.0 NaN \n",
|
||
" 1999-11-03 136.0000 136.3750 135.1250 135.5000 7222300.0 NaN \n",
|
||
" 1999-11-04 136.7500 137.3593 135.7656 136.5312 7907500.0 NaN \n",
|
||
" 1999-11-05 138.6250 139.1093 136.7812 137.8750 7431500.0 NaN \n",
|
||
" ... ... ... ... ... ... ... \n",
|
||
" 2020-08-24 342.1200 343.0000 339.4504 342.9200 48588662.0 337.837 \n",
|
||
" 2020-08-25 343.5300 344.2100 342.2700 344.1200 38463381.0 338.969 \n",
|
||
" 2020-08-26 344.7600 347.8600 344.1700 347.5700 50790237.0 339.982 \n",
|
||
" 2020-08-27 348.5100 349.9000 346.5300 348.3300 58034142.0 341.132 \n",
|
||
" 2020-08-28 349.4400 350.7200 348.1500 350.5800 48588940.0 342.506 \n",
|
||
" \n",
|
||
" SMA_20 SMA_50 SMA_200 VOL_VOL_SMA_20 \n",
|
||
" date \n",
|
||
" 1999-11-01 NaN NaN NaN NaN \n",
|
||
" 1999-11-02 NaN NaN NaN NaN \n",
|
||
" 1999-11-03 NaN NaN NaN NaN \n",
|
||
" 1999-11-04 NaN NaN NaN NaN \n",
|
||
" 1999-11-05 NaN NaN NaN NaN \n",
|
||
" ... ... ... ... ... \n",
|
||
" 2020-08-24 333.5285 322.2548 307.05540 51792231.45 \n",
|
||
" 2020-08-25 334.6760 322.9962 307.23510 50840651.55 \n",
|
||
" 2020-08-26 335.7985 323.6884 307.42825 50957455.45 \n",
|
||
" 2020-08-27 337.0170 324.4218 307.62815 50766076.85 \n",
|
||
" 2020-08-28 338.2200 325.1978 307.83605 48934986.10 \n",
|
||
" \n",
|
||
" [5241 rows x 10 columns],\n",
|
||
" 'IWM': open high low close volume SMA_10 SMA_20 \\\n",
|
||
" date \n",
|
||
" 2000-05-26 91.06 91.44 90.630 91.44 37400.0 NaN NaN \n",
|
||
" 2000-05-30 92.75 94.81 92.750 94.81 28800.0 NaN NaN \n",
|
||
" 2000-05-31 95.13 96.38 95.130 95.75 18000.0 NaN NaN \n",
|
||
" 2000-06-01 97.11 97.31 97.110 97.31 3500.0 NaN NaN \n",
|
||
" 2000-06-02 101.70 102.40 101.700 102.40 14700.0 NaN NaN \n",
|
||
" ... ... ... ... ... ... ... ... \n",
|
||
" 2020-08-31 157.19 157.37 155.300 155.43 17051511.0 155.956 155.9205 \n",
|
||
" 2020-09-01 155.22 157.31 154.450 157.21 15654144.0 156.038 156.2415 \n",
|
||
" 2020-09-02 157.96 158.98 156.175 158.46 16763449.0 156.244 156.4750 \n",
|
||
" 2020-09-03 158.12 158.29 152.960 153.78 32117585.0 156.046 156.4775 \n",
|
||
" 2020-09-04 155.71 155.89 149.290 152.80 30618783.0 155.865 156.3090 \n",
|
||
" \n",
|
||
" SMA_50 SMA_200 VOL_VOL_SMA_20 \n",
|
||
" date \n",
|
||
" 2000-05-26 NaN NaN NaN \n",
|
||
" 2000-05-30 NaN NaN NaN \n",
|
||
" 2000-05-31 NaN NaN NaN \n",
|
||
" 2000-06-01 NaN NaN NaN \n",
|
||
" 2000-06-02 NaN NaN NaN \n",
|
||
" ... ... ... ... \n",
|
||
" 2020-08-31 148.8200 145.82245 17493155.05 \n",
|
||
" 2020-09-01 149.1162 145.81800 17261612.35 \n",
|
||
" 2020-09-02 149.4254 145.81570 17173441.45 \n",
|
||
" 2020-09-03 149.7338 145.79200 17867433.35 \n",
|
||
" 2020-09-04 149.9808 145.76045 18374614.75 \n",
|
||
" \n",
|
||
" [5102 rows x 10 columns]}"
|
||
]
|
||
},
|
||
"execution_count": 12,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"watch.data"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"metadata": {},
|
||
"source": [
|
||
"### "
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 13,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"data": {
|
||
"text/html": [
|
||
"<div>\n",
|
||
"<style scoped>\n",
|
||
" .dataframe tbody tr th:only-of-type {\n",
|
||
" vertical-align: middle;\n",
|
||
" }\n",
|
||
"\n",
|
||
" .dataframe tbody tr th {\n",
|
||
" vertical-align: top;\n",
|
||
" }\n",
|
||
"\n",
|
||
" .dataframe thead th {\n",
|
||
" text-align: right;\n",
|
||
" }\n",
|
||
"</style>\n",
|
||
"<table border=\"1\" class=\"dataframe\">\n",
|
||
" <thead>\n",
|
||
" <tr style=\"text-align: right;\">\n",
|
||
" <th></th>\n",
|
||
" <th>open</th>\n",
|
||
" <th>high</th>\n",
|
||
" <th>low</th>\n",
|
||
" <th>close</th>\n",
|
||
" <th>volume</th>\n",
|
||
" <th>SMA_10</th>\n",
|
||
" <th>SMA_20</th>\n",
|
||
" <th>SMA_50</th>\n",
|
||
" <th>SMA_200</th>\n",
|
||
" <th>VOL_VOL_SMA_20</th>\n",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>date</th>\n",
|
||
" <th></th>\n",
|
||
" <th></th>\n",
|
||
" <th></th>\n",
|
||
" <th></th>\n",
|
||
" <th></th>\n",
|
||
" <th></th>\n",
|
||
" <th></th>\n",
|
||
" <th></th>\n",
|
||
" <th></th>\n",
|
||
" <th></th>\n",
|
||
" </tr>\n",
|
||
" </thead>\n",
|
||
" <tbody>\n",
|
||
" <tr>\n",
|
||
" <th>1999-11-01</th>\n",
|
||
" <td>136.5000</td>\n",
|
||
" <td>137.0000</td>\n",
|
||
" <td>135.5625</td>\n",
|
||
" <td>135.5625</td>\n",
|
||
" <td>4006500.0</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>1999-11-02</th>\n",
|
||
" <td>135.9687</td>\n",
|
||
" <td>137.2500</td>\n",
|
||
" <td>134.5937</td>\n",
|
||
" <td>134.5937</td>\n",
|
||
" <td>6516900.0</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>1999-11-03</th>\n",
|
||
" <td>136.0000</td>\n",
|
||
" <td>136.3750</td>\n",
|
||
" <td>135.1250</td>\n",
|
||
" <td>135.5000</td>\n",
|
||
" <td>7222300.0</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>1999-11-04</th>\n",
|
||
" <td>136.7500</td>\n",
|
||
" <td>137.3593</td>\n",
|
||
" <td>135.7656</td>\n",
|
||
" <td>136.5312</td>\n",
|
||
" <td>7907500.0</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>1999-11-05</th>\n",
|
||
" <td>138.6250</td>\n",
|
||
" <td>139.1093</td>\n",
|
||
" <td>136.7812</td>\n",
|
||
" <td>137.8750</td>\n",
|
||
" <td>7431500.0</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>...</th>\n",
|
||
" <td>...</td>\n",
|
||
" <td>...</td>\n",
|
||
" <td>...</td>\n",
|
||
" <td>...</td>\n",
|
||
" <td>...</td>\n",
|
||
" <td>...</td>\n",
|
||
" <td>...</td>\n",
|
||
" <td>...</td>\n",
|
||
" <td>...</td>\n",
|
||
" <td>...</td>\n",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>2020-08-24</th>\n",
|
||
" <td>342.1200</td>\n",
|
||
" <td>343.0000</td>\n",
|
||
" <td>339.4504</td>\n",
|
||
" <td>342.9200</td>\n",
|
||
" <td>48588662.0</td>\n",
|
||
" <td>337.837</td>\n",
|
||
" <td>333.5285</td>\n",
|
||
" <td>322.2548</td>\n",
|
||
" <td>307.05540</td>\n",
|
||
" <td>51792231.45</td>\n",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>2020-08-25</th>\n",
|
||
" <td>343.5300</td>\n",
|
||
" <td>344.2100</td>\n",
|
||
" <td>342.2700</td>\n",
|
||
" <td>344.1200</td>\n",
|
||
" <td>38463381.0</td>\n",
|
||
" <td>338.969</td>\n",
|
||
" <td>334.6760</td>\n",
|
||
" <td>322.9962</td>\n",
|
||
" <td>307.23510</td>\n",
|
||
" <td>50840651.55</td>\n",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>2020-08-26</th>\n",
|
||
" <td>344.7600</td>\n",
|
||
" <td>347.8600</td>\n",
|
||
" <td>344.1700</td>\n",
|
||
" <td>347.5700</td>\n",
|
||
" <td>50790237.0</td>\n",
|
||
" <td>339.982</td>\n",
|
||
" <td>335.7985</td>\n",
|
||
" <td>323.6884</td>\n",
|
||
" <td>307.42825</td>\n",
|
||
" <td>50957455.45</td>\n",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>2020-08-27</th>\n",
|
||
" <td>348.5100</td>\n",
|
||
" <td>349.9000</td>\n",
|
||
" <td>346.5300</td>\n",
|
||
" <td>348.3300</td>\n",
|
||
" <td>58034142.0</td>\n",
|
||
" <td>341.132</td>\n",
|
||
" <td>337.0170</td>\n",
|
||
" <td>324.4218</td>\n",
|
||
" <td>307.62815</td>\n",
|
||
" <td>50766076.85</td>\n",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>2020-08-28</th>\n",
|
||
" <td>349.4400</td>\n",
|
||
" <td>350.7200</td>\n",
|
||
" <td>348.1500</td>\n",
|
||
" <td>350.5800</td>\n",
|
||
" <td>48588940.0</td>\n",
|
||
" <td>342.506</td>\n",
|
||
" <td>338.2200</td>\n",
|
||
" <td>325.1978</td>\n",
|
||
" <td>307.83605</td>\n",
|
||
" <td>48934986.10</td>\n",
|
||
" </tr>\n",
|
||
" </tbody>\n",
|
||
"</table>\n",
|
||
"<p>5241 rows × 10 columns</p>\n",
|
||
"</div>"
|
||
],
|
||
"text/plain": [
|
||
" open high low close volume SMA_10 \\\n",
|
||
"date \n",
|
||
"1999-11-01 136.5000 137.0000 135.5625 135.5625 4006500.0 NaN \n",
|
||
"1999-11-02 135.9687 137.2500 134.5937 134.5937 6516900.0 NaN \n",
|
||
"1999-11-03 136.0000 136.3750 135.1250 135.5000 7222300.0 NaN \n",
|
||
"1999-11-04 136.7500 137.3593 135.7656 136.5312 7907500.0 NaN \n",
|
||
"1999-11-05 138.6250 139.1093 136.7812 137.8750 7431500.0 NaN \n",
|
||
"... ... ... ... ... ... ... \n",
|
||
"2020-08-24 342.1200 343.0000 339.4504 342.9200 48588662.0 337.837 \n",
|
||
"2020-08-25 343.5300 344.2100 342.2700 344.1200 38463381.0 338.969 \n",
|
||
"2020-08-26 344.7600 347.8600 344.1700 347.5700 50790237.0 339.982 \n",
|
||
"2020-08-27 348.5100 349.9000 346.5300 348.3300 58034142.0 341.132 \n",
|
||
"2020-08-28 349.4400 350.7200 348.1500 350.5800 48588940.0 342.506 \n",
|
||
"\n",
|
||
" SMA_20 SMA_50 SMA_200 VOL_VOL_SMA_20 \n",
|
||
"date \n",
|
||
"1999-11-01 NaN NaN NaN NaN \n",
|
||
"1999-11-02 NaN NaN NaN NaN \n",
|
||
"1999-11-03 NaN NaN NaN NaN \n",
|
||
"1999-11-04 NaN NaN NaN NaN \n",
|
||
"1999-11-05 NaN NaN NaN NaN \n",
|
||
"... ... ... ... ... \n",
|
||
"2020-08-24 333.5285 322.2548 307.05540 51792231.45 \n",
|
||
"2020-08-25 334.6760 322.9962 307.23510 50840651.55 \n",
|
||
"2020-08-26 335.7985 323.6884 307.42825 50957455.45 \n",
|
||
"2020-08-27 337.0170 324.4218 307.62815 50766076.85 \n",
|
||
"2020-08-28 338.2200 325.1978 307.83605 48934986.10 \n",
|
||
"\n",
|
||
"[5241 rows x 10 columns]"
|
||
]
|
||
},
|
||
"execution_count": 13,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"watch.data[\"SPY\"]"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": []
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"metadata": {},
|
||
"source": [
|
||
"## Easy to swap Strategies and run them"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"metadata": {},
|
||
"source": [
|
||
"### Running Simple Strategy A"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 14,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"data": {
|
||
"text/plain": [
|
||
"Strategy(name='A', ta=[{'kind': 'sma', 'length': 50}, {'kind': 'sma', 'length': 200}], description=None, created='09/09/2020, 22:54:24', last_run=None, run_time=None)"
|
||
]
|
||
},
|
||
"execution_count": 14,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"# Load custom_a into Watchlist and verify\n",
|
||
"watch.strategy = custom_a\n",
|
||
"# watch.debug = True\n",
|
||
"watch.strategy"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 15,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"name": "stdout",
|
||
"output_type": "stream",
|
||
"text": [
|
||
"[i] Loaded['D']: IWM_D.csv\n"
|
||
]
|
||
},
|
||
{
|
||
"data": {
|
||
"text/html": [
|
||
"<div>\n",
|
||
"<style scoped>\n",
|
||
" .dataframe tbody tr th:only-of-type {\n",
|
||
" vertical-align: middle;\n",
|
||
" }\n",
|
||
"\n",
|
||
" .dataframe tbody tr th {\n",
|
||
" vertical-align: top;\n",
|
||
" }\n",
|
||
"\n",
|
||
" .dataframe thead th {\n",
|
||
" text-align: right;\n",
|
||
" }\n",
|
||
"</style>\n",
|
||
"<table border=\"1\" class=\"dataframe\">\n",
|
||
" <thead>\n",
|
||
" <tr style=\"text-align: right;\">\n",
|
||
" <th></th>\n",
|
||
" <th>open</th>\n",
|
||
" <th>high</th>\n",
|
||
" <th>low</th>\n",
|
||
" <th>close</th>\n",
|
||
" <th>volume</th>\n",
|
||
" <th>SMA_50</th>\n",
|
||
" <th>SMA_200</th>\n",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>date</th>\n",
|
||
" <th></th>\n",
|
||
" <th></th>\n",
|
||
" <th></th>\n",
|
||
" <th></th>\n",
|
||
" <th></th>\n",
|
||
" <th></th>\n",
|
||
" <th></th>\n",
|
||
" </tr>\n",
|
||
" </thead>\n",
|
||
" <tbody>\n",
|
||
" <tr>\n",
|
||
" <th>2000-05-26</th>\n",
|
||
" <td>91.06</td>\n",
|
||
" <td>91.44</td>\n",
|
||
" <td>90.630</td>\n",
|
||
" <td>91.44</td>\n",
|
||
" <td>37400.0</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>2000-05-30</th>\n",
|
||
" <td>92.75</td>\n",
|
||
" <td>94.81</td>\n",
|
||
" <td>92.750</td>\n",
|
||
" <td>94.81</td>\n",
|
||
" <td>28800.0</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>2000-05-31</th>\n",
|
||
" <td>95.13</td>\n",
|
||
" <td>96.38</td>\n",
|
||
" <td>95.130</td>\n",
|
||
" <td>95.75</td>\n",
|
||
" <td>18000.0</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>2000-06-01</th>\n",
|
||
" <td>97.11</td>\n",
|
||
" <td>97.31</td>\n",
|
||
" <td>97.110</td>\n",
|
||
" <td>97.31</td>\n",
|
||
" <td>3500.0</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>2000-06-02</th>\n",
|
||
" <td>101.70</td>\n",
|
||
" <td>102.40</td>\n",
|
||
" <td>101.700</td>\n",
|
||
" <td>102.40</td>\n",
|
||
" <td>14700.0</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>...</th>\n",
|
||
" <td>...</td>\n",
|
||
" <td>...</td>\n",
|
||
" <td>...</td>\n",
|
||
" <td>...</td>\n",
|
||
" <td>...</td>\n",
|
||
" <td>...</td>\n",
|
||
" <td>...</td>\n",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>2020-08-31</th>\n",
|
||
" <td>157.19</td>\n",
|
||
" <td>157.37</td>\n",
|
||
" <td>155.300</td>\n",
|
||
" <td>155.43</td>\n",
|
||
" <td>17051511.0</td>\n",
|
||
" <td>148.8200</td>\n",
|
||
" <td>145.82245</td>\n",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>2020-09-01</th>\n",
|
||
" <td>155.22</td>\n",
|
||
" <td>157.31</td>\n",
|
||
" <td>154.450</td>\n",
|
||
" <td>157.21</td>\n",
|
||
" <td>15654144.0</td>\n",
|
||
" <td>149.1162</td>\n",
|
||
" <td>145.81800</td>\n",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>2020-09-02</th>\n",
|
||
" <td>157.96</td>\n",
|
||
" <td>158.98</td>\n",
|
||
" <td>156.175</td>\n",
|
||
" <td>158.46</td>\n",
|
||
" <td>16763449.0</td>\n",
|
||
" <td>149.4254</td>\n",
|
||
" <td>145.81570</td>\n",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>2020-09-03</th>\n",
|
||
" <td>158.12</td>\n",
|
||
" <td>158.29</td>\n",
|
||
" <td>152.960</td>\n",
|
||
" <td>153.78</td>\n",
|
||
" <td>32117585.0</td>\n",
|
||
" <td>149.7338</td>\n",
|
||
" <td>145.79200</td>\n",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>2020-09-04</th>\n",
|
||
" <td>155.71</td>\n",
|
||
" <td>155.89</td>\n",
|
||
" <td>149.290</td>\n",
|
||
" <td>152.80</td>\n",
|
||
" <td>30618783.0</td>\n",
|
||
" <td>149.9808</td>\n",
|
||
" <td>145.76045</td>\n",
|
||
" </tr>\n",
|
||
" </tbody>\n",
|
||
"</table>\n",
|
||
"<p>5102 rows × 7 columns</p>\n",
|
||
"</div>"
|
||
],
|
||
"text/plain": [
|
||
" open high low close volume SMA_50 SMA_200\n",
|
||
"date \n",
|
||
"2000-05-26 91.06 91.44 90.630 91.44 37400.0 NaN NaN\n",
|
||
"2000-05-30 92.75 94.81 92.750 94.81 28800.0 NaN NaN\n",
|
||
"2000-05-31 95.13 96.38 95.130 95.75 18000.0 NaN NaN\n",
|
||
"2000-06-01 97.11 97.31 97.110 97.31 3500.0 NaN NaN\n",
|
||
"2000-06-02 101.70 102.40 101.700 102.40 14700.0 NaN NaN\n",
|
||
"... ... ... ... ... ... ... ...\n",
|
||
"2020-08-31 157.19 157.37 155.300 155.43 17051511.0 148.8200 145.82245\n",
|
||
"2020-09-01 155.22 157.31 154.450 157.21 15654144.0 149.1162 145.81800\n",
|
||
"2020-09-02 157.96 158.98 156.175 158.46 16763449.0 149.4254 145.81570\n",
|
||
"2020-09-03 158.12 158.29 152.960 153.78 32117585.0 149.7338 145.79200\n",
|
||
"2020-09-04 155.71 155.89 149.290 152.80 30618783.0 149.9808 145.76045\n",
|
||
"\n",
|
||
"[5102 rows x 7 columns]"
|
||
]
|
||
},
|
||
"execution_count": 15,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"watch.load(\"IWM\")"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"metadata": {},
|
||
"source": [
|
||
"### Running Simple Strategy B"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 16,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"data": {
|
||
"text/plain": [
|
||
"Strategy(name='B', ta=[{'kind': 'ema', 'length': 8}, {'kind': 'ema', 'length': 21}, {'kind': 'log_return', 'cumulative': True}, {'kind': 'rsi'}, {'kind': 'supertrend'}], description=None, created='09/09/2020, 22:54:24', last_run=None, run_time=None)"
|
||
]
|
||
},
|
||
"execution_count": 16,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"# Load custom_b into Watchlist and verify\n",
|
||
"watch.strategy = custom_b\n",
|
||
"watch.strategy"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 17,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"name": "stdout",
|
||
"output_type": "stream",
|
||
"text": [
|
||
"[i] Loaded['D']: SPY_D.csv\n"
|
||
]
|
||
},
|
||
{
|
||
"data": {
|
||
"text/html": [
|
||
"<div>\n",
|
||
"<style scoped>\n",
|
||
" .dataframe tbody tr th:only-of-type {\n",
|
||
" vertical-align: middle;\n",
|
||
" }\n",
|
||
"\n",
|
||
" .dataframe tbody tr th {\n",
|
||
" vertical-align: top;\n",
|
||
" }\n",
|
||
"\n",
|
||
" .dataframe thead th {\n",
|
||
" text-align: right;\n",
|
||
" }\n",
|
||
"</style>\n",
|
||
"<table border=\"1\" class=\"dataframe\">\n",
|
||
" <thead>\n",
|
||
" <tr style=\"text-align: right;\">\n",
|
||
" <th></th>\n",
|
||
" <th>open</th>\n",
|
||
" <th>high</th>\n",
|
||
" <th>low</th>\n",
|
||
" <th>close</th>\n",
|
||
" <th>volume</th>\n",
|
||
" <th>EMA_8</th>\n",
|
||
" <th>EMA_21</th>\n",
|
||
" <th>CUMLOGRET_1</th>\n",
|
||
" <th>RSI_14</th>\n",
|
||
" <th>SUPERT_7_3.0</th>\n",
|
||
" <th>SUPERTd_7_3.0</th>\n",
|
||
" <th>SUPERTl_7_3.0</th>\n",
|
||
" <th>SUPERTs_7_3.0</th>\n",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>date</th>\n",
|
||
" <th></th>\n",
|
||
" <th></th>\n",
|
||
" <th></th>\n",
|
||
" <th></th>\n",
|
||
" <th></th>\n",
|
||
" <th></th>\n",
|
||
" <th></th>\n",
|
||
" <th></th>\n",
|
||
" <th></th>\n",
|
||
" <th></th>\n",
|
||
" <th></th>\n",
|
||
" <th></th>\n",
|
||
" <th></th>\n",
|
||
" </tr>\n",
|
||
" </thead>\n",
|
||
" <tbody>\n",
|
||
" <tr>\n",
|
||
" <th>1999-11-01</th>\n",
|
||
" <td>136.5000</td>\n",
|
||
" <td>137.0000</td>\n",
|
||
" <td>135.5625</td>\n",
|
||
" <td>135.5625</td>\n",
|
||
" <td>4006500.0</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>0.000000</td>\n",
|
||
" <td>1</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>1999-11-02</th>\n",
|
||
" <td>135.9687</td>\n",
|
||
" <td>137.2500</td>\n",
|
||
" <td>134.5937</td>\n",
|
||
" <td>134.5937</td>\n",
|
||
" <td>6516900.0</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>-0.007172</td>\n",
|
||
" <td>0.000000</td>\n",
|
||
" <td>131.968750</td>\n",
|
||
" <td>1</td>\n",
|
||
" <td>131.968750</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>1999-11-03</th>\n",
|
||
" <td>136.0000</td>\n",
|
||
" <td>136.3750</td>\n",
|
||
" <td>135.1250</td>\n",
|
||
" <td>135.5000</td>\n",
|
||
" <td>7222300.0</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>-0.000461</td>\n",
|
||
" <td>50.185503</td>\n",
|
||
" <td>131.968750</td>\n",
|
||
" <td>1</td>\n",
|
||
" <td>131.968750</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>1999-11-04</th>\n",
|
||
" <td>136.7500</td>\n",
|
||
" <td>137.3593</td>\n",
|
||
" <td>135.7656</td>\n",
|
||
" <td>136.5312</td>\n",
|
||
" <td>7907500.0</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>0.007120</td>\n",
|
||
" <td>69.153995</td>\n",
|
||
" <td>131.968750</td>\n",
|
||
" <td>1</td>\n",
|
||
" <td>131.968750</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>1999-11-05</th>\n",
|
||
" <td>138.6250</td>\n",
|
||
" <td>139.1093</td>\n",
|
||
" <td>136.7812</td>\n",
|
||
" <td>137.8750</td>\n",
|
||
" <td>7431500.0</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>0.016915</td>\n",
|
||
" <td>79.896816</td>\n",
|
||
" <td>131.968750</td>\n",
|
||
" <td>1</td>\n",
|
||
" <td>131.968750</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>...</th>\n",
|
||
" <td>...</td>\n",
|
||
" <td>...</td>\n",
|
||
" <td>...</td>\n",
|
||
" <td>...</td>\n",
|
||
" <td>...</td>\n",
|
||
" <td>...</td>\n",
|
||
" <td>...</td>\n",
|
||
" <td>...</td>\n",
|
||
" <td>...</td>\n",
|
||
" <td>...</td>\n",
|
||
" <td>...</td>\n",
|
||
" <td>...</td>\n",
|
||
" <td>...</td>\n",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>2020-08-24</th>\n",
|
||
" <td>342.1200</td>\n",
|
||
" <td>343.0000</td>\n",
|
||
" <td>339.4504</td>\n",
|
||
" <td>342.9200</td>\n",
|
||
" <td>48588662.0</td>\n",
|
||
" <td>338.577882</td>\n",
|
||
" <td>333.520811</td>\n",
|
||
" <td>0.928064</td>\n",
|
||
" <td>72.830131</td>\n",
|
||
" <td>331.974175</td>\n",
|
||
" <td>1</td>\n",
|
||
" <td>331.974175</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>2020-08-25</th>\n",
|
||
" <td>343.5300</td>\n",
|
||
" <td>344.2100</td>\n",
|
||
" <td>342.2700</td>\n",
|
||
" <td>344.1200</td>\n",
|
||
" <td>38463381.0</td>\n",
|
||
" <td>339.809464</td>\n",
|
||
" <td>334.484374</td>\n",
|
||
" <td>0.931558</td>\n",
|
||
" <td>74.054445</td>\n",
|
||
" <td>334.479122</td>\n",
|
||
" <td>1</td>\n",
|
||
" <td>334.479122</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>2020-08-26</th>\n",
|
||
" <td>344.7600</td>\n",
|
||
" <td>347.8600</td>\n",
|
||
" <td>344.1700</td>\n",
|
||
" <td>347.5700</td>\n",
|
||
" <td>50790237.0</td>\n",
|
||
" <td>341.534027</td>\n",
|
||
" <td>335.673976</td>\n",
|
||
" <td>0.941533</td>\n",
|
||
" <td>77.231098</td>\n",
|
||
" <td>336.902819</td>\n",
|
||
" <td>1</td>\n",
|
||
" <td>336.902819</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>2020-08-27</th>\n",
|
||
" <td>348.5100</td>\n",
|
||
" <td>349.9000</td>\n",
|
||
" <td>346.5300</td>\n",
|
||
" <td>348.3300</td>\n",
|
||
" <td>58034142.0</td>\n",
|
||
" <td>343.044243</td>\n",
|
||
" <td>336.824524</td>\n",
|
||
" <td>0.943718</td>\n",
|
||
" <td>77.873776</td>\n",
|
||
" <td>338.960273</td>\n",
|
||
" <td>1</td>\n",
|
||
" <td>338.960273</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>2020-08-28</th>\n",
|
||
" <td>349.4400</td>\n",
|
||
" <td>350.7200</td>\n",
|
||
" <td>348.1500</td>\n",
|
||
" <td>350.5800</td>\n",
|
||
" <td>48588940.0</td>\n",
|
||
" <td>344.718856</td>\n",
|
||
" <td>338.075022</td>\n",
|
||
" <td>0.950156</td>\n",
|
||
" <td>79.700567</td>\n",
|
||
" <td>340.400948</td>\n",
|
||
" <td>1</td>\n",
|
||
" <td>340.400948</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" </tr>\n",
|
||
" </tbody>\n",
|
||
"</table>\n",
|
||
"<p>5241 rows × 13 columns</p>\n",
|
||
"</div>"
|
||
],
|
||
"text/plain": [
|
||
" open high low close volume EMA_8 \\\n",
|
||
"date \n",
|
||
"1999-11-01 136.5000 137.0000 135.5625 135.5625 4006500.0 NaN \n",
|
||
"1999-11-02 135.9687 137.2500 134.5937 134.5937 6516900.0 NaN \n",
|
||
"1999-11-03 136.0000 136.3750 135.1250 135.5000 7222300.0 NaN \n",
|
||
"1999-11-04 136.7500 137.3593 135.7656 136.5312 7907500.0 NaN \n",
|
||
"1999-11-05 138.6250 139.1093 136.7812 137.8750 7431500.0 NaN \n",
|
||
"... ... ... ... ... ... ... \n",
|
||
"2020-08-24 342.1200 343.0000 339.4504 342.9200 48588662.0 338.577882 \n",
|
||
"2020-08-25 343.5300 344.2100 342.2700 344.1200 38463381.0 339.809464 \n",
|
||
"2020-08-26 344.7600 347.8600 344.1700 347.5700 50790237.0 341.534027 \n",
|
||
"2020-08-27 348.5100 349.9000 346.5300 348.3300 58034142.0 343.044243 \n",
|
||
"2020-08-28 349.4400 350.7200 348.1500 350.5800 48588940.0 344.718856 \n",
|
||
"\n",
|
||
" EMA_21 CUMLOGRET_1 RSI_14 SUPERT_7_3.0 SUPERTd_7_3.0 \\\n",
|
||
"date \n",
|
||
"1999-11-01 NaN NaN NaN 0.000000 1 \n",
|
||
"1999-11-02 NaN -0.007172 0.000000 131.968750 1 \n",
|
||
"1999-11-03 NaN -0.000461 50.185503 131.968750 1 \n",
|
||
"1999-11-04 NaN 0.007120 69.153995 131.968750 1 \n",
|
||
"1999-11-05 NaN 0.016915 79.896816 131.968750 1 \n",
|
||
"... ... ... ... ... ... \n",
|
||
"2020-08-24 333.520811 0.928064 72.830131 331.974175 1 \n",
|
||
"2020-08-25 334.484374 0.931558 74.054445 334.479122 1 \n",
|
||
"2020-08-26 335.673976 0.941533 77.231098 336.902819 1 \n",
|
||
"2020-08-27 336.824524 0.943718 77.873776 338.960273 1 \n",
|
||
"2020-08-28 338.075022 0.950156 79.700567 340.400948 1 \n",
|
||
"\n",
|
||
" SUPERTl_7_3.0 SUPERTs_7_3.0 \n",
|
||
"date \n",
|
||
"1999-11-01 NaN NaN \n",
|
||
"1999-11-02 131.968750 NaN \n",
|
||
"1999-11-03 131.968750 NaN \n",
|
||
"1999-11-04 131.968750 NaN \n",
|
||
"1999-11-05 131.968750 NaN \n",
|
||
"... ... ... \n",
|
||
"2020-08-24 331.974175 NaN \n",
|
||
"2020-08-25 334.479122 NaN \n",
|
||
"2020-08-26 336.902819 NaN \n",
|
||
"2020-08-27 338.960273 NaN \n",
|
||
"2020-08-28 340.400948 NaN \n",
|
||
"\n",
|
||
"[5241 rows x 13 columns]"
|
||
]
|
||
},
|
||
"execution_count": 17,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"watch.load(\"SPY\")"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"metadata": {},
|
||
"source": [
|
||
"### Running Bad Strategy. (Misspelled indicator)"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 18,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"data": {
|
||
"text/plain": [
|
||
"Strategy(name='Runtime Failure', ta=[{'kind': 'percet_return'}], description=None, created='09/09/2020, 22:54:24', last_run=None, run_time=None)"
|
||
]
|
||
},
|
||
"execution_count": 18,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"# Load custom_run_failure into Watchlist and verify\n",
|
||
"watch.strategy = custom_run_failure\n",
|
||
"watch.strategy"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 19,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"name": "stdout",
|
||
"output_type": "stream",
|
||
"text": [
|
||
"[i] Loaded['D']: IWM_D.csv\n"
|
||
]
|
||
}
|
||
],
|
||
"source": [
|
||
"try:\n",
|
||
" iwm = watch.load(\"IWM\")\n",
|
||
"except AttributeError as error:\n",
|
||
" print(f\"[X] Oops! {error}\")"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": []
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"metadata": {},
|
||
"source": [
|
||
"# Indicator Composition/Chaining\n",
|
||
"- When you need an indicator to depend on the value of a prior indicator\n",
|
||
"- Utilitze _prefix_ or _suffix_ to help identify unique columns or avoid column name clashes."
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"metadata": {},
|
||
"source": [
|
||
"### Volume MAs and MA chains"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 20,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"data": {
|
||
"text/plain": [
|
||
"Strategy(name='Volume MAs and Price MA chain', ta=[{'kind': 'ema', 'close': 'volume', 'length': 10, 'prefix': 'VOLUME'}, {'kind': 'sma', 'close': 'volume', 'length': 20, 'prefix': 'VOLUME'}, {'kind': 'ema', 'length': 5}, {'kind': 'linreg', 'close': 'EMA_5', 'length': 8, 'prefix': 'EMA_5'}], description=None, created='09/09/2020, 22:54:24', last_run=None, run_time=None)"
|
||
]
|
||
},
|
||
"execution_count": 20,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"# Set EMA's and SMA's 'close' to 'volume' to create Volume MAs, prefix 'volume' MAs with 'VOLUME' so easy to identify the column\n",
|
||
"# Take a price EMA and apply LINREG from EMA's output\n",
|
||
"volmas_price_ma_chain = [\n",
|
||
" {\"kind\":\"ema\", \"close\": \"volume\", \"length\": 10, \"prefix\": \"VOLUME\"},\n",
|
||
" {\"kind\":\"sma\", \"close\": \"volume\", \"length\": 20, \"prefix\": \"VOLUME\"},\n",
|
||
" {\"kind\":\"ema\", \"length\": 5},\n",
|
||
" {\"kind\":\"linreg\", \"close\": \"EMA_5\", \"length\": 8, \"prefix\": \"EMA_5\"},\n",
|
||
"]\n",
|
||
"vp_ma_chain_ta = ta.Strategy(\"Volume MAs and Price MA chain\", volmas_price_ma_chain)\n",
|
||
"vp_ma_chain_ta"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 21,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"data": {
|
||
"text/plain": [
|
||
"'Volume MAs and Price MA chain'"
|
||
]
|
||
},
|
||
"execution_count": 21,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"# Update the Watchlist\n",
|
||
"watch.strategy = vp_ma_chain_ta\n",
|
||
"watch.strategy.name"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 22,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"name": "stdout",
|
||
"output_type": "stream",
|
||
"text": [
|
||
"[i] Loaded['D']: SPY_D.csv\n"
|
||
]
|
||
},
|
||
{
|
||
"data": {
|
||
"text/html": [
|
||
"<div>\n",
|
||
"<style scoped>\n",
|
||
" .dataframe tbody tr th:only-of-type {\n",
|
||
" vertical-align: middle;\n",
|
||
" }\n",
|
||
"\n",
|
||
" .dataframe tbody tr th {\n",
|
||
" vertical-align: top;\n",
|
||
" }\n",
|
||
"\n",
|
||
" .dataframe thead th {\n",
|
||
" text-align: right;\n",
|
||
" }\n",
|
||
"</style>\n",
|
||
"<table border=\"1\" class=\"dataframe\">\n",
|
||
" <thead>\n",
|
||
" <tr style=\"text-align: right;\">\n",
|
||
" <th></th>\n",
|
||
" <th>open</th>\n",
|
||
" <th>high</th>\n",
|
||
" <th>low</th>\n",
|
||
" <th>close</th>\n",
|
||
" <th>volume</th>\n",
|
||
" <th>VOLUME_VOLUME_EMA_10</th>\n",
|
||
" <th>VOLUME_VOLUME_SMA_20</th>\n",
|
||
" <th>EMA_5</th>\n",
|
||
" <th>EMA_5_EMA_5_LR_8</th>\n",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>date</th>\n",
|
||
" <th></th>\n",
|
||
" <th></th>\n",
|
||
" <th></th>\n",
|
||
" <th></th>\n",
|
||
" <th></th>\n",
|
||
" <th></th>\n",
|
||
" <th></th>\n",
|
||
" <th></th>\n",
|
||
" <th></th>\n",
|
||
" </tr>\n",
|
||
" </thead>\n",
|
||
" <tbody>\n",
|
||
" <tr>\n",
|
||
" <th>1999-11-01</th>\n",
|
||
" <td>136.5000</td>\n",
|
||
" <td>137.0000</td>\n",
|
||
" <td>135.5625</td>\n",
|
||
" <td>135.5625</td>\n",
|
||
" <td>4006500.0</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>1999-11-02</th>\n",
|
||
" <td>135.9687</td>\n",
|
||
" <td>137.2500</td>\n",
|
||
" <td>134.5937</td>\n",
|
||
" <td>134.5937</td>\n",
|
||
" <td>6516900.0</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>1999-11-03</th>\n",
|
||
" <td>136.0000</td>\n",
|
||
" <td>136.3750</td>\n",
|
||
" <td>135.1250</td>\n",
|
||
" <td>135.5000</td>\n",
|
||
" <td>7222300.0</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>1999-11-04</th>\n",
|
||
" <td>136.7500</td>\n",
|
||
" <td>137.3593</td>\n",
|
||
" <td>135.7656</td>\n",
|
||
" <td>136.5312</td>\n",
|
||
" <td>7907500.0</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>1999-11-05</th>\n",
|
||
" <td>138.6250</td>\n",
|
||
" <td>139.1093</td>\n",
|
||
" <td>136.7812</td>\n",
|
||
" <td>137.8750</td>\n",
|
||
" <td>7431500.0</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>136.012480</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>...</th>\n",
|
||
" <td>...</td>\n",
|
||
" <td>...</td>\n",
|
||
" <td>...</td>\n",
|
||
" <td>...</td>\n",
|
||
" <td>...</td>\n",
|
||
" <td>...</td>\n",
|
||
" <td>...</td>\n",
|
||
" <td>...</td>\n",
|
||
" <td>...</td>\n",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>2020-08-24</th>\n",
|
||
" <td>342.1200</td>\n",
|
||
" <td>343.0000</td>\n",
|
||
" <td>339.4504</td>\n",
|
||
" <td>342.9200</td>\n",
|
||
" <td>48588662.0</td>\n",
|
||
" <td>4.999906e+07</td>\n",
|
||
" <td>51792231.45</td>\n",
|
||
" <td>339.779956</td>\n",
|
||
" <td>338.589691</td>\n",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>2020-08-25</th>\n",
|
||
" <td>343.5300</td>\n",
|
||
" <td>344.2100</td>\n",
|
||
" <td>342.2700</td>\n",
|
||
" <td>344.1200</td>\n",
|
||
" <td>38463381.0</td>\n",
|
||
" <td>4.790167e+07</td>\n",
|
||
" <td>50840651.55</td>\n",
|
||
" <td>341.226637</td>\n",
|
||
" <td>339.658445</td>\n",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>2020-08-26</th>\n",
|
||
" <td>344.7600</td>\n",
|
||
" <td>347.8600</td>\n",
|
||
" <td>344.1700</td>\n",
|
||
" <td>347.5700</td>\n",
|
||
" <td>50790237.0</td>\n",
|
||
" <td>4.842686e+07</td>\n",
|
||
" <td>50957455.45</td>\n",
|
||
" <td>343.341091</td>\n",
|
||
" <td>341.152455</td>\n",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>2020-08-27</th>\n",
|
||
" <td>348.5100</td>\n",
|
||
" <td>349.9000</td>\n",
|
||
" <td>346.5300</td>\n",
|
||
" <td>348.3300</td>\n",
|
||
" <td>58034142.0</td>\n",
|
||
" <td>5.017364e+07</td>\n",
|
||
" <td>50766076.85</td>\n",
|
||
" <td>345.004061</td>\n",
|
||
" <td>342.852470</td>\n",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>2020-08-28</th>\n",
|
||
" <td>349.4400</td>\n",
|
||
" <td>350.7200</td>\n",
|
||
" <td>348.1500</td>\n",
|
||
" <td>350.5800</td>\n",
|
||
" <td>48588940.0</td>\n",
|
||
" <td>4.988551e+07</td>\n",
|
||
" <td>48934986.10</td>\n",
|
||
" <td>346.862707</td>\n",
|
||
" <td>344.767837</td>\n",
|
||
" </tr>\n",
|
||
" </tbody>\n",
|
||
"</table>\n",
|
||
"<p>5241 rows × 9 columns</p>\n",
|
||
"</div>"
|
||
],
|
||
"text/plain": [
|
||
" open high low close volume \\\n",
|
||
"date \n",
|
||
"1999-11-01 136.5000 137.0000 135.5625 135.5625 4006500.0 \n",
|
||
"1999-11-02 135.9687 137.2500 134.5937 134.5937 6516900.0 \n",
|
||
"1999-11-03 136.0000 136.3750 135.1250 135.5000 7222300.0 \n",
|
||
"1999-11-04 136.7500 137.3593 135.7656 136.5312 7907500.0 \n",
|
||
"1999-11-05 138.6250 139.1093 136.7812 137.8750 7431500.0 \n",
|
||
"... ... ... ... ... ... \n",
|
||
"2020-08-24 342.1200 343.0000 339.4504 342.9200 48588662.0 \n",
|
||
"2020-08-25 343.5300 344.2100 342.2700 344.1200 38463381.0 \n",
|
||
"2020-08-26 344.7600 347.8600 344.1700 347.5700 50790237.0 \n",
|
||
"2020-08-27 348.5100 349.9000 346.5300 348.3300 58034142.0 \n",
|
||
"2020-08-28 349.4400 350.7200 348.1500 350.5800 48588940.0 \n",
|
||
"\n",
|
||
" VOLUME_VOLUME_EMA_10 VOLUME_VOLUME_SMA_20 EMA_5 \\\n",
|
||
"date \n",
|
||
"1999-11-01 NaN NaN NaN \n",
|
||
"1999-11-02 NaN NaN NaN \n",
|
||
"1999-11-03 NaN NaN NaN \n",
|
||
"1999-11-04 NaN NaN NaN \n",
|
||
"1999-11-05 NaN NaN 136.012480 \n",
|
||
"... ... ... ... \n",
|
||
"2020-08-24 4.999906e+07 51792231.45 339.779956 \n",
|
||
"2020-08-25 4.790167e+07 50840651.55 341.226637 \n",
|
||
"2020-08-26 4.842686e+07 50957455.45 343.341091 \n",
|
||
"2020-08-27 5.017364e+07 50766076.85 345.004061 \n",
|
||
"2020-08-28 4.988551e+07 48934986.10 346.862707 \n",
|
||
"\n",
|
||
" EMA_5_EMA_5_LR_8 \n",
|
||
"date \n",
|
||
"1999-11-01 NaN \n",
|
||
"1999-11-02 NaN \n",
|
||
"1999-11-03 NaN \n",
|
||
"1999-11-04 NaN \n",
|
||
"1999-11-05 NaN \n",
|
||
"... ... \n",
|
||
"2020-08-24 338.589691 \n",
|
||
"2020-08-25 339.658445 \n",
|
||
"2020-08-26 341.152455 \n",
|
||
"2020-08-27 342.852470 \n",
|
||
"2020-08-28 344.767837 \n",
|
||
"\n",
|
||
"[5241 rows x 9 columns]"
|
||
]
|
||
},
|
||
"execution_count": 22,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"spy = watch.load(\"SPY\")\n",
|
||
"spy"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": []
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"metadata": {},
|
||
"source": [
|
||
"### MACD BBANDS"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 23,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"data": {
|
||
"text/plain": [
|
||
"Strategy(name='MACD BBands', ta=[{'kind': 'macd'}, {'kind': 'bbands', 'close': 'MACD_12_26_9', 'length': 20, 'prefix': 'MACD'}], description='BBANDS_20 applied to MACD', created='09/09/2020, 22:54:24', last_run=None, run_time=None)"
|
||
]
|
||
},
|
||
"execution_count": 23,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"# MACD is the initial indicator that BBANDS depends on.\n",
|
||
"# 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\n",
|
||
"macd_bands_ta = [\n",
|
||
" {\"kind\":\"macd\"},\n",
|
||
" {\"kind\":\"bbands\", \"close\": \"MACD_12_26_9\", \"length\": 20, \"prefix\": \"MACD\"}\n",
|
||
"]\n",
|
||
"macd_bands_ta = ta.Strategy(\"MACD BBands\", macd_bands_ta, f\"BBANDS_{macd_bands_ta[1]['length']} applied to MACD\")\n",
|
||
"macd_bands_ta"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 24,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"data": {
|
||
"text/plain": [
|
||
"'MACD BBands'"
|
||
]
|
||
},
|
||
"execution_count": 24,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"# Update the Watchlist\n",
|
||
"watch.strategy = macd_bands_ta\n",
|
||
"watch.strategy.name"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 25,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"name": "stdout",
|
||
"output_type": "stream",
|
||
"text": [
|
||
"[i] Loaded['D']: SPY_D.csv\n"
|
||
]
|
||
},
|
||
{
|
||
"data": {
|
||
"text/html": [
|
||
"<div>\n",
|
||
"<style scoped>\n",
|
||
" .dataframe tbody tr th:only-of-type {\n",
|
||
" vertical-align: middle;\n",
|
||
" }\n",
|
||
"\n",
|
||
" .dataframe tbody tr th {\n",
|
||
" vertical-align: top;\n",
|
||
" }\n",
|
||
"\n",
|
||
" .dataframe thead th {\n",
|
||
" text-align: right;\n",
|
||
" }\n",
|
||
"</style>\n",
|
||
"<table border=\"1\" class=\"dataframe\">\n",
|
||
" <thead>\n",
|
||
" <tr style=\"text-align: right;\">\n",
|
||
" <th></th>\n",
|
||
" <th>open</th>\n",
|
||
" <th>high</th>\n",
|
||
" <th>low</th>\n",
|
||
" <th>close</th>\n",
|
||
" <th>volume</th>\n",
|
||
" <th>MACD_12_26_9</th>\n",
|
||
" <th>MACDh_12_26_9</th>\n",
|
||
" <th>MACDs_12_26_9</th>\n",
|
||
" <th>MACD_MACD_BBL_20_2.0</th>\n",
|
||
" <th>MACD_MACD_BBM_20_2.0</th>\n",
|
||
" <th>MACD_MACD_BBU_20_2.0</th>\n",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>date</th>\n",
|
||
" <th></th>\n",
|
||
" <th></th>\n",
|
||
" <th></th>\n",
|
||
" <th></th>\n",
|
||
" <th></th>\n",
|
||
" <th></th>\n",
|
||
" <th></th>\n",
|
||
" <th></th>\n",
|
||
" <th></th>\n",
|
||
" <th></th>\n",
|
||
" <th></th>\n",
|
||
" </tr>\n",
|
||
" </thead>\n",
|
||
" <tbody>\n",
|
||
" <tr>\n",
|
||
" <th>1999-11-01</th>\n",
|
||
" <td>136.5000</td>\n",
|
||
" <td>137.0000</td>\n",
|
||
" <td>135.5625</td>\n",
|
||
" <td>135.5625</td>\n",
|
||
" <td>4006500.0</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>1999-11-02</th>\n",
|
||
" <td>135.9687</td>\n",
|
||
" <td>137.2500</td>\n",
|
||
" <td>134.5937</td>\n",
|
||
" <td>134.5937</td>\n",
|
||
" <td>6516900.0</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>1999-11-03</th>\n",
|
||
" <td>136.0000</td>\n",
|
||
" <td>136.3750</td>\n",
|
||
" <td>135.1250</td>\n",
|
||
" <td>135.5000</td>\n",
|
||
" <td>7222300.0</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>1999-11-04</th>\n",
|
||
" <td>136.7500</td>\n",
|
||
" <td>137.3593</td>\n",
|
||
" <td>135.7656</td>\n",
|
||
" <td>136.5312</td>\n",
|
||
" <td>7907500.0</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>1999-11-05</th>\n",
|
||
" <td>138.6250</td>\n",
|
||
" <td>139.1093</td>\n",
|
||
" <td>136.7812</td>\n",
|
||
" <td>137.8750</td>\n",
|
||
" <td>7431500.0</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>...</th>\n",
|
||
" <td>...</td>\n",
|
||
" <td>...</td>\n",
|
||
" <td>...</td>\n",
|
||
" <td>...</td>\n",
|
||
" <td>...</td>\n",
|
||
" <td>...</td>\n",
|
||
" <td>...</td>\n",
|
||
" <td>...</td>\n",
|
||
" <td>...</td>\n",
|
||
" <td>...</td>\n",
|
||
" <td>...</td>\n",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>2020-08-24</th>\n",
|
||
" <td>342.1200</td>\n",
|
||
" <td>343.0000</td>\n",
|
||
" <td>339.4504</td>\n",
|
||
" <td>342.9200</td>\n",
|
||
" <td>48588662.0</td>\n",
|
||
" <td>5.341674</td>\n",
|
||
" <td>0.069881</td>\n",
|
||
" <td>5.271794</td>\n",
|
||
" <td>3.678022</td>\n",
|
||
" <td>4.914752</td>\n",
|
||
" <td>6.151482</td>\n",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>2020-08-25</th>\n",
|
||
" <td>343.5300</td>\n",
|
||
" <td>344.2100</td>\n",
|
||
" <td>342.2700</td>\n",
|
||
" <td>344.1200</td>\n",
|
||
" <td>38463381.0</td>\n",
|
||
" <td>5.513019</td>\n",
|
||
" <td>0.192980</td>\n",
|
||
" <td>5.320039</td>\n",
|
||
" <td>3.807707</td>\n",
|
||
" <td>4.990945</td>\n",
|
||
" <td>6.174183</td>\n",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>2020-08-26</th>\n",
|
||
" <td>344.7600</td>\n",
|
||
" <td>347.8600</td>\n",
|
||
" <td>344.1700</td>\n",
|
||
" <td>347.5700</td>\n",
|
||
" <td>50790237.0</td>\n",
|
||
" <td>5.859651</td>\n",
|
||
" <td>0.431690</td>\n",
|
||
" <td>5.427961</td>\n",
|
||
" <td>3.933776</td>\n",
|
||
" <td>5.083307</td>\n",
|
||
" <td>6.232838</td>\n",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>2020-08-27</th>\n",
|
||
" <td>348.5100</td>\n",
|
||
" <td>349.9000</td>\n",
|
||
" <td>346.5300</td>\n",
|
||
" <td>348.3300</td>\n",
|
||
" <td>58034142.0</td>\n",
|
||
" <td>6.125079</td>\n",
|
||
" <td>0.557694</td>\n",
|
||
" <td>5.567385</td>\n",
|
||
" <td>4.100036</td>\n",
|
||
" <td>5.194943</td>\n",
|
||
" <td>6.289851</td>\n",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>2020-08-28</th>\n",
|
||
" <td>349.4400</td>\n",
|
||
" <td>350.7200</td>\n",
|
||
" <td>348.1500</td>\n",
|
||
" <td>350.5800</td>\n",
|
||
" <td>48588940.0</td>\n",
|
||
" <td>6.442721</td>\n",
|
||
" <td>0.700269</td>\n",
|
||
" <td>5.742452</td>\n",
|
||
" <td>4.251657</td>\n",
|
||
" <td>5.319172</td>\n",
|
||
" <td>6.386687</td>\n",
|
||
" </tr>\n",
|
||
" </tbody>\n",
|
||
"</table>\n",
|
||
"<p>5241 rows × 11 columns</p>\n",
|
||
"</div>"
|
||
],
|
||
"text/plain": [
|
||
" open high low close volume MACD_12_26_9 \\\n",
|
||
"date \n",
|
||
"1999-11-01 136.5000 137.0000 135.5625 135.5625 4006500.0 NaN \n",
|
||
"1999-11-02 135.9687 137.2500 134.5937 134.5937 6516900.0 NaN \n",
|
||
"1999-11-03 136.0000 136.3750 135.1250 135.5000 7222300.0 NaN \n",
|
||
"1999-11-04 136.7500 137.3593 135.7656 136.5312 7907500.0 NaN \n",
|
||
"1999-11-05 138.6250 139.1093 136.7812 137.8750 7431500.0 NaN \n",
|
||
"... ... ... ... ... ... ... \n",
|
||
"2020-08-24 342.1200 343.0000 339.4504 342.9200 48588662.0 5.341674 \n",
|
||
"2020-08-25 343.5300 344.2100 342.2700 344.1200 38463381.0 5.513019 \n",
|
||
"2020-08-26 344.7600 347.8600 344.1700 347.5700 50790237.0 5.859651 \n",
|
||
"2020-08-27 348.5100 349.9000 346.5300 348.3300 58034142.0 6.125079 \n",
|
||
"2020-08-28 349.4400 350.7200 348.1500 350.5800 48588940.0 6.442721 \n",
|
||
"\n",
|
||
" MACDh_12_26_9 MACDs_12_26_9 MACD_MACD_BBL_20_2.0 \\\n",
|
||
"date \n",
|
||
"1999-11-01 NaN NaN NaN \n",
|
||
"1999-11-02 NaN NaN NaN \n",
|
||
"1999-11-03 NaN NaN NaN \n",
|
||
"1999-11-04 NaN NaN NaN \n",
|
||
"1999-11-05 NaN NaN NaN \n",
|
||
"... ... ... ... \n",
|
||
"2020-08-24 0.069881 5.271794 3.678022 \n",
|
||
"2020-08-25 0.192980 5.320039 3.807707 \n",
|
||
"2020-08-26 0.431690 5.427961 3.933776 \n",
|
||
"2020-08-27 0.557694 5.567385 4.100036 \n",
|
||
"2020-08-28 0.700269 5.742452 4.251657 \n",
|
||
"\n",
|
||
" MACD_MACD_BBM_20_2.0 MACD_MACD_BBU_20_2.0 \n",
|
||
"date \n",
|
||
"1999-11-01 NaN NaN \n",
|
||
"1999-11-02 NaN NaN \n",
|
||
"1999-11-03 NaN NaN \n",
|
||
"1999-11-04 NaN NaN \n",
|
||
"1999-11-05 NaN NaN \n",
|
||
"... ... ... \n",
|
||
"2020-08-24 4.914752 6.151482 \n",
|
||
"2020-08-25 4.990945 6.174183 \n",
|
||
"2020-08-26 5.083307 6.232838 \n",
|
||
"2020-08-27 5.194943 6.289851 \n",
|
||
"2020-08-28 5.319172 6.386687 \n",
|
||
"\n",
|
||
"[5241 rows x 11 columns]"
|
||
]
|
||
},
|
||
"execution_count": 25,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"spy = watch.load(\"SPY\")\n",
|
||
"spy"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": []
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"metadata": {},
|
||
"source": [
|
||
"# Comprehensive Strategy"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"metadata": {},
|
||
"source": [
|
||
"### MACD and RSI Momentum with BBANDS and SMAs and Cumulative Log Returns"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 26,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"data": {
|
||
"text/plain": [
|
||
"Strategy(name='Momo, Bands and SMAs and Cumulative Log Returns', ta=[{'kind': 'sma', 'length': 50}, {'kind': 'sma', 'length': 200}, {'kind': 'bbands', 'length': 20}, {'kind': 'macd'}, {'kind': 'rsi'}, {'kind': 'log_return', 'cumulative': True}, {'kind': 'sma', 'close': 'CUMLOGRET_1', 'length': 5, 'suffix': 'CUMLOGRET'}], description='MACD and RSI Momo with BBANDS and SMAs 50 & 200 and Cumulative Log Returns', created='09/09/2020, 22:54:24', last_run=None, run_time=None)"
|
||
]
|
||
},
|
||
"execution_count": 26,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"momo_bands_sma_ta = [\n",
|
||
" {\"kind\":\"sma\", \"length\": 50},\n",
|
||
" {\"kind\":\"sma\", \"length\": 200},\n",
|
||
" {\"kind\":\"bbands\", \"length\": 20},\n",
|
||
" {\"kind\":\"macd\"},\n",
|
||
" {\"kind\":\"rsi\"},\n",
|
||
" {\"kind\":\"log_return\", \"cumulative\": True},\n",
|
||
" {\"kind\":\"sma\", \"close\": \"CUMLOGRET_1\", \"length\": 5, \"suffix\": \"CUMLOGRET\"},\n",
|
||
"]\n",
|
||
"momo_bands_sma_strategy = ta.Strategy(\n",
|
||
" \"Momo, Bands and SMAs and Cumulative Log Returns\", # name\n",
|
||
" momo_bands_sma_ta, # ta\n",
|
||
" \"MACD and RSI Momo with BBANDS and SMAs 50 & 200 and Cumulative Log Returns\" # description\n",
|
||
")\n",
|
||
"momo_bands_sma_strategy"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 27,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"data": {
|
||
"text/plain": [
|
||
"'Momo, Bands and SMAs and Cumulative Log Returns'"
|
||
]
|
||
},
|
||
"execution_count": 27,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"# Update the Watchlist\n",
|
||
"watch.strategy = momo_bands_sma_strategy\n",
|
||
"watch.strategy.name"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 28,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"name": "stdout",
|
||
"output_type": "stream",
|
||
"text": [
|
||
"[i] Loaded['D']: SPY_D.csv\n",
|
||
"[i] Runtime: 1208.2109 ms (1.2082 s)\n"
|
||
]
|
||
},
|
||
{
|
||
"data": {
|
||
"text/html": [
|
||
"<div>\n",
|
||
"<style scoped>\n",
|
||
" .dataframe tbody tr th:only-of-type {\n",
|
||
" vertical-align: middle;\n",
|
||
" }\n",
|
||
"\n",
|
||
" .dataframe tbody tr th {\n",
|
||
" vertical-align: top;\n",
|
||
" }\n",
|
||
"\n",
|
||
" .dataframe thead th {\n",
|
||
" text-align: right;\n",
|
||
" }\n",
|
||
"</style>\n",
|
||
"<table border=\"1\" class=\"dataframe\">\n",
|
||
" <thead>\n",
|
||
" <tr style=\"text-align: right;\">\n",
|
||
" <th></th>\n",
|
||
" <th>open</th>\n",
|
||
" <th>high</th>\n",
|
||
" <th>low</th>\n",
|
||
" <th>close</th>\n",
|
||
" <th>volume</th>\n",
|
||
" <th>SMA_50</th>\n",
|
||
" <th>SMA_200</th>\n",
|
||
" <th>BBL_20_2.0</th>\n",
|
||
" <th>BBM_20_2.0</th>\n",
|
||
" <th>BBU_20_2.0</th>\n",
|
||
" <th>MACD_12_26_9</th>\n",
|
||
" <th>MACDh_12_26_9</th>\n",
|
||
" <th>MACDs_12_26_9</th>\n",
|
||
" <th>RSI_14</th>\n",
|
||
" <th>CUMLOGRET_1</th>\n",
|
||
" <th>SMA_5_CUMLOGRET_CUMLOGRET</th>\n",
|
||
" <th>0</th>\n",
|
||
" <th>30</th>\n",
|
||
" <th>70</th>\n",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>date</th>\n",
|
||
" <th></th>\n",
|
||
" <th></th>\n",
|
||
" <th></th>\n",
|
||
" <th></th>\n",
|
||
" <th></th>\n",
|
||
" <th></th>\n",
|
||
" <th></th>\n",
|
||
" <th></th>\n",
|
||
" <th></th>\n",
|
||
" <th></th>\n",
|
||
" <th></th>\n",
|
||
" <th></th>\n",
|
||
" <th></th>\n",
|
||
" <th></th>\n",
|
||
" <th></th>\n",
|
||
" <th></th>\n",
|
||
" <th></th>\n",
|
||
" <th></th>\n",
|
||
" <th></th>\n",
|
||
" </tr>\n",
|
||
" </thead>\n",
|
||
" <tbody>\n",
|
||
" <tr>\n",
|
||
" <th>1999-11-01</th>\n",
|
||
" <td>136.5000</td>\n",
|
||
" <td>137.0000</td>\n",
|
||
" <td>135.5625</td>\n",
|
||
" <td>135.5625</td>\n",
|
||
" <td>4006500.0</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>30</td>\n",
|
||
" <td>70</td>\n",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>1999-11-02</th>\n",
|
||
" <td>135.9687</td>\n",
|
||
" <td>137.2500</td>\n",
|
||
" <td>134.5937</td>\n",
|
||
" <td>134.5937</td>\n",
|
||
" <td>6516900.0</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>0.000000</td>\n",
|
||
" <td>-0.007172</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>30</td>\n",
|
||
" <td>70</td>\n",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>1999-11-03</th>\n",
|
||
" <td>136.0000</td>\n",
|
||
" <td>136.3750</td>\n",
|
||
" <td>135.1250</td>\n",
|
||
" <td>135.5000</td>\n",
|
||
" <td>7222300.0</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>50.185503</td>\n",
|
||
" <td>-0.000461</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>30</td>\n",
|
||
" <td>70</td>\n",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>1999-11-04</th>\n",
|
||
" <td>136.7500</td>\n",
|
||
" <td>137.3593</td>\n",
|
||
" <td>135.7656</td>\n",
|
||
" <td>136.5312</td>\n",
|
||
" <td>7907500.0</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>69.153995</td>\n",
|
||
" <td>0.007120</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>30</td>\n",
|
||
" <td>70</td>\n",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>1999-11-05</th>\n",
|
||
" <td>138.6250</td>\n",
|
||
" <td>139.1093</td>\n",
|
||
" <td>136.7812</td>\n",
|
||
" <td>137.8750</td>\n",
|
||
" <td>7431500.0</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>79.896816</td>\n",
|
||
" <td>0.016915</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>30</td>\n",
|
||
" <td>70</td>\n",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>1999-11-08</th>\n",
|
||
" <td>137.0000</td>\n",
|
||
" <td>138.3750</td>\n",
|
||
" <td>136.7500</td>\n",
|
||
" <td>138.0000</td>\n",
|
||
" <td>4649200.0</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>80.574537</td>\n",
|
||
" <td>0.017821</td>\n",
|
||
" <td>0.006845</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>30</td>\n",
|
||
" <td>70</td>\n",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>1999-11-09</th>\n",
|
||
" <td>138.5000</td>\n",
|
||
" <td>138.6875</td>\n",
|
||
" <td>136.2812</td>\n",
|
||
" <td>136.7031</td>\n",
|
||
" <td>4533700.0</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>58.528352</td>\n",
|
||
" <td>0.008379</td>\n",
|
||
" <td>0.009955</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>30</td>\n",
|
||
" <td>70</td>\n",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>1999-11-10</th>\n",
|
||
" <td>136.2500</td>\n",
|
||
" <td>138.3906</td>\n",
|
||
" <td>136.0781</td>\n",
|
||
" <td>137.7187</td>\n",
|
||
" <td>6405600.0</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>66.303684</td>\n",
|
||
" <td>0.015780</td>\n",
|
||
" <td>0.013203</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>30</td>\n",
|
||
" <td>70</td>\n",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>1999-11-11</th>\n",
|
||
" <td>138.1875</td>\n",
|
||
" <td>138.5000</td>\n",
|
||
" <td>137.4687</td>\n",
|
||
" <td>138.5000</td>\n",
|
||
" <td>4794100.0</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>0.0</td>\n",
|
||
" <td>70.833962</td>\n",
|
||
" <td>0.021438</td>\n",
|
||
" <td>0.016066</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>30</td>\n",
|
||
" <td>70</td>\n",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>1999-11-12</th>\n",
|
||
" <td>139.2500</td>\n",
|
||
" <td>139.9843</td>\n",
|
||
" <td>137.1250</td>\n",
|
||
" <td>139.7500</td>\n",
|
||
" <td>11802900.0</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>0.0</td>\n",
|
||
" <td>76.319408</td>\n",
|
||
" <td>0.030422</td>\n",
|
||
" <td>0.018768</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>30</td>\n",
|
||
" <td>70</td>\n",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>1999-11-15</th>\n",
|
||
" <td>139.8437</td>\n",
|
||
" <td>140.2500</td>\n",
|
||
" <td>139.4062</td>\n",
|
||
" <td>140.0781</td>\n",
|
||
" <td>2187500.0</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>0.0</td>\n",
|
||
" <td>77.514804</td>\n",
|
||
" <td>0.032767</td>\n",
|
||
" <td>0.021757</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>30</td>\n",
|
||
" <td>70</td>\n",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>1999-11-16</th>\n",
|
||
" <td>140.5625</td>\n",
|
||
" <td>143.0000</td>\n",
|
||
" <td>140.0937</td>\n",
|
||
" <td>141.2500</td>\n",
|
||
" <td>7544800.0</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>0.0</td>\n",
|
||
" <td>81.170904</td>\n",
|
||
" <td>0.041099</td>\n",
|
||
" <td>0.028301</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>30</td>\n",
|
||
" <td>70</td>\n",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>1999-11-17</th>\n",
|
||
" <td>142.2500</td>\n",
|
||
" <td>142.9375</td>\n",
|
||
" <td>141.3125</td>\n",
|
||
" <td>141.6250</td>\n",
|
||
" <td>9459000.0</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>0.0</td>\n",
|
||
" <td>82.169980</td>\n",
|
||
" <td>0.043750</td>\n",
|
||
" <td>0.033895</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>30</td>\n",
|
||
" <td>70</td>\n",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>1999-11-18</th>\n",
|
||
" <td>142.4375</td>\n",
|
||
" <td>143.0000</td>\n",
|
||
" <td>141.6250</td>\n",
|
||
" <td>142.6250</td>\n",
|
||
" <td>4491000.0</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>0.0</td>\n",
|
||
" <td>84.527630</td>\n",
|
||
" <td>0.050786</td>\n",
|
||
" <td>0.039765</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>30</td>\n",
|
||
" <td>70</td>\n",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>1999-11-19</th>\n",
|
||
" <td>142.4062</td>\n",
|
||
" <td>142.9687</td>\n",
|
||
" <td>142.0000</td>\n",
|
||
" <td>142.5000</td>\n",
|
||
" <td>4832100.0</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>0.0</td>\n",
|
||
" <td>83.049344</td>\n",
|
||
" <td>0.049909</td>\n",
|
||
" <td>0.043662</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>30</td>\n",
|
||
" <td>70</td>\n",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>1999-11-22</th>\n",
|
||
" <td>142.4375</td>\n",
|
||
" <td>143.0000</td>\n",
|
||
" <td>141.5000</td>\n",
|
||
" <td>142.4687</td>\n",
|
||
" <td>4155400.0</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>0.0</td>\n",
|
||
" <td>82.659517</td>\n",
|
||
" <td>0.049690</td>\n",
|
||
" <td>0.047047</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>30</td>\n",
|
||
" <td>70</td>\n",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>1999-11-23</th>\n",
|
||
" <td>142.8437</td>\n",
|
||
" <td>142.8437</td>\n",
|
||
" <td>140.3750</td>\n",
|
||
" <td>141.2187</td>\n",
|
||
" <td>5918000.0</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>0.0</td>\n",
|
||
" <td>68.775385</td>\n",
|
||
" <td>0.040877</td>\n",
|
||
" <td>0.047002</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>30</td>\n",
|
||
" <td>70</td>\n",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>1999-11-24</th>\n",
|
||
" <td>140.7500</td>\n",
|
||
" <td>142.4375</td>\n",
|
||
" <td>140.0000</td>\n",
|
||
" <td>141.9687</td>\n",
|
||
" <td>4459700.0</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>0.0</td>\n",
|
||
" <td>71.832489</td>\n",
|
||
" <td>0.046174</td>\n",
|
||
" <td>0.047487</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>30</td>\n",
|
||
" <td>70</td>\n",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>1999-11-26</th>\n",
|
||
" <td>142.4687</td>\n",
|
||
" <td>142.8750</td>\n",
|
||
" <td>141.2500</td>\n",
|
||
" <td>141.4375</td>\n",
|
||
" <td>1693900.0</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>0.0</td>\n",
|
||
" <td>66.840920</td>\n",
|
||
" <td>0.042425</td>\n",
|
||
" <td>0.045815</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>30</td>\n",
|
||
" <td>70</td>\n",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>1999-11-29</th>\n",
|
||
" <td>140.8750</td>\n",
|
||
" <td>141.9218</td>\n",
|
||
" <td>140.4375</td>\n",
|
||
" <td>140.9375</td>\n",
|
||
" <td>7348600.0</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>134.086598</td>\n",
|
||
" <td>139.34217</td>\n",
|
||
" <td>144.597742</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>0.0</td>\n",
|
||
" <td>62.442534</td>\n",
|
||
" <td>0.038884</td>\n",
|
||
" <td>0.043610</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>30</td>\n",
|
||
" <td>70</td>\n",
|
||
" </tr>\n",
|
||
" </tbody>\n",
|
||
"</table>\n",
|
||
"</div>"
|
||
],
|
||
"text/plain": [
|
||
" open high low close volume SMA_50 \\\n",
|
||
"date \n",
|
||
"1999-11-01 136.5000 137.0000 135.5625 135.5625 4006500.0 NaN \n",
|
||
"1999-11-02 135.9687 137.2500 134.5937 134.5937 6516900.0 NaN \n",
|
||
"1999-11-03 136.0000 136.3750 135.1250 135.5000 7222300.0 NaN \n",
|
||
"1999-11-04 136.7500 137.3593 135.7656 136.5312 7907500.0 NaN \n",
|
||
"1999-11-05 138.6250 139.1093 136.7812 137.8750 7431500.0 NaN \n",
|
||
"1999-11-08 137.0000 138.3750 136.7500 138.0000 4649200.0 NaN \n",
|
||
"1999-11-09 138.5000 138.6875 136.2812 136.7031 4533700.0 NaN \n",
|
||
"1999-11-10 136.2500 138.3906 136.0781 137.7187 6405600.0 NaN \n",
|
||
"1999-11-11 138.1875 138.5000 137.4687 138.5000 4794100.0 NaN \n",
|
||
"1999-11-12 139.2500 139.9843 137.1250 139.7500 11802900.0 NaN \n",
|
||
"1999-11-15 139.8437 140.2500 139.4062 140.0781 2187500.0 NaN \n",
|
||
"1999-11-16 140.5625 143.0000 140.0937 141.2500 7544800.0 NaN \n",
|
||
"1999-11-17 142.2500 142.9375 141.3125 141.6250 9459000.0 NaN \n",
|
||
"1999-11-18 142.4375 143.0000 141.6250 142.6250 4491000.0 NaN \n",
|
||
"1999-11-19 142.4062 142.9687 142.0000 142.5000 4832100.0 NaN \n",
|
||
"1999-11-22 142.4375 143.0000 141.5000 142.4687 4155400.0 NaN \n",
|
||
"1999-11-23 142.8437 142.8437 140.3750 141.2187 5918000.0 NaN \n",
|
||
"1999-11-24 140.7500 142.4375 140.0000 141.9687 4459700.0 NaN \n",
|
||
"1999-11-26 142.4687 142.8750 141.2500 141.4375 1693900.0 NaN \n",
|
||
"1999-11-29 140.8750 141.9218 140.4375 140.9375 7348600.0 NaN \n",
|
||
"\n",
|
||
" SMA_200 BBL_20_2.0 BBM_20_2.0 BBU_20_2.0 MACD_12_26_9 \\\n",
|
||
"date \n",
|
||
"1999-11-01 NaN NaN NaN NaN NaN \n",
|
||
"1999-11-02 NaN NaN NaN NaN NaN \n",
|
||
"1999-11-03 NaN NaN NaN NaN NaN \n",
|
||
"1999-11-04 NaN NaN NaN NaN NaN \n",
|
||
"1999-11-05 NaN NaN NaN NaN NaN \n",
|
||
"1999-11-08 NaN NaN NaN NaN NaN \n",
|
||
"1999-11-09 NaN NaN NaN NaN NaN \n",
|
||
"1999-11-10 NaN NaN NaN NaN NaN \n",
|
||
"1999-11-11 NaN NaN NaN NaN NaN \n",
|
||
"1999-11-12 NaN NaN NaN NaN NaN \n",
|
||
"1999-11-15 NaN NaN NaN NaN NaN \n",
|
||
"1999-11-16 NaN NaN NaN NaN NaN \n",
|
||
"1999-11-17 NaN NaN NaN NaN NaN \n",
|
||
"1999-11-18 NaN NaN NaN NaN NaN \n",
|
||
"1999-11-19 NaN NaN NaN NaN NaN \n",
|
||
"1999-11-22 NaN NaN NaN NaN NaN \n",
|
||
"1999-11-23 NaN NaN NaN NaN NaN \n",
|
||
"1999-11-24 NaN NaN NaN NaN NaN \n",
|
||
"1999-11-26 NaN NaN NaN NaN NaN \n",
|
||
"1999-11-29 NaN 134.086598 139.34217 144.597742 NaN \n",
|
||
"\n",
|
||
" MACDh_12_26_9 MACDs_12_26_9 RSI_14 CUMLOGRET_1 \\\n",
|
||
"date \n",
|
||
"1999-11-01 NaN NaN NaN NaN \n",
|
||
"1999-11-02 NaN NaN 0.000000 -0.007172 \n",
|
||
"1999-11-03 NaN NaN 50.185503 -0.000461 \n",
|
||
"1999-11-04 NaN NaN 69.153995 0.007120 \n",
|
||
"1999-11-05 NaN NaN 79.896816 0.016915 \n",
|
||
"1999-11-08 NaN NaN 80.574537 0.017821 \n",
|
||
"1999-11-09 NaN NaN 58.528352 0.008379 \n",
|
||
"1999-11-10 NaN NaN 66.303684 0.015780 \n",
|
||
"1999-11-11 NaN 0.0 70.833962 0.021438 \n",
|
||
"1999-11-12 NaN 0.0 76.319408 0.030422 \n",
|
||
"1999-11-15 NaN 0.0 77.514804 0.032767 \n",
|
||
"1999-11-16 NaN 0.0 81.170904 0.041099 \n",
|
||
"1999-11-17 NaN 0.0 82.169980 0.043750 \n",
|
||
"1999-11-18 NaN 0.0 84.527630 0.050786 \n",
|
||
"1999-11-19 NaN 0.0 83.049344 0.049909 \n",
|
||
"1999-11-22 NaN 0.0 82.659517 0.049690 \n",
|
||
"1999-11-23 NaN 0.0 68.775385 0.040877 \n",
|
||
"1999-11-24 NaN 0.0 71.832489 0.046174 \n",
|
||
"1999-11-26 NaN 0.0 66.840920 0.042425 \n",
|
||
"1999-11-29 NaN 0.0 62.442534 0.038884 \n",
|
||
"\n",
|
||
" SMA_5_CUMLOGRET_CUMLOGRET 0 30 70 \n",
|
||
"date \n",
|
||
"1999-11-01 NaN 0 30 70 \n",
|
||
"1999-11-02 NaN 0 30 70 \n",
|
||
"1999-11-03 NaN 0 30 70 \n",
|
||
"1999-11-04 NaN 0 30 70 \n",
|
||
"1999-11-05 NaN 0 30 70 \n",
|
||
"1999-11-08 0.006845 0 30 70 \n",
|
||
"1999-11-09 0.009955 0 30 70 \n",
|
||
"1999-11-10 0.013203 0 30 70 \n",
|
||
"1999-11-11 0.016066 0 30 70 \n",
|
||
"1999-11-12 0.018768 0 30 70 \n",
|
||
"1999-11-15 0.021757 0 30 70 \n",
|
||
"1999-11-16 0.028301 0 30 70 \n",
|
||
"1999-11-17 0.033895 0 30 70 \n",
|
||
"1999-11-18 0.039765 0 30 70 \n",
|
||
"1999-11-19 0.043662 0 30 70 \n",
|
||
"1999-11-22 0.047047 0 30 70 \n",
|
||
"1999-11-23 0.047002 0 30 70 \n",
|
||
"1999-11-24 0.047487 0 30 70 \n",
|
||
"1999-11-26 0.045815 0 30 70 \n",
|
||
"1999-11-29 0.043610 0 30 70 "
|
||
]
|
||
},
|
||
"execution_count": 28,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"spy = watch.load(\"SPY\", timed=True)\n",
|
||
"# Apply constants to the DataFrame for indicators\n",
|
||
"spy.ta.constants(True, [0, 30, 70])\n",
|
||
"spy.head(20)"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": []
|
||
}
|
||
],
|
||
"metadata": {
|
||
"kernelspec": {
|
||
"display_name": "Python 3",
|
||
"language": "python",
|
||
"name": "python3"
|
||
},
|
||
"language_info": {
|
||
"codemirror_mode": {
|
||
"name": "ipython",
|
||
"version": 3
|
||
},
|
||
"file_extension": ".py",
|
||
"mimetype": "text/x-python",
|
||
"name": "python",
|
||
"nbconvert_exporter": "python",
|
||
"pygments_lexer": "ipython3",
|
||
"version": "3.8.2"
|
||
}
|
||
},
|
||
"nbformat": 4,
|
||
"nbformat_minor": 4
|
||
}
|