Files
pandas-ta/examples/PandasTA_Strategy_Examples.ipynb
T

2765 lines
94 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
{
"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/25/2020, 07:53:11\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/25/2020, 07:53:11\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/25/2020, 07:53:11')"
]
},
"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/25/2020, 07:53:11')"
]
},
"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/25/2020, 07:53:11')"
]
},
"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",
" | A simple Class to load/download financial market data and automatically\n",
" | apply Technical Analysis indicators with a Pandas TA Strategy.\n",
" | \n",
" | Default Strategy: pandas_ta.AllStrategy.\n",
" | \n",
" | ## Package Support:\n",
" | ### Data Source (Default: AlphaVantage)\n",
" | - AlphaVantage (pip install alphaVantage-api).\n",
" | - Python Binance (pip install python-binance). # Future Support\n",
" | - Yahoo Finance (pip install yfinance). # Almost Supported\n",
" | \n",
" | # Technical Analysis:\n",
" | - Pandas TA (pip install pandas_ta)\n",
" | \n",
" | ## Required Arguments:\n",
" | - tickers: A list of strings containing tickers. Example: ['SPY', 'AAPL']\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",
" 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",
"[5241 rows x 5 columns]\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",
" open high low close volume\n",
"date \n",
"2000-05-26 91.06 91.44 90.630 91.44 37400.0\n",
"2000-05-30 92.75 94.81 92.750 94.81 28800.0\n",
"2000-05-31 95.13 96.38 95.130 95.75 18000.0\n",
"2000-06-01 97.11 97.31 97.110 97.31 3500.0\n",
"2000-06-02 101.70 102.40 101.700 102.40 14700.0\n",
"... ... ... ... ... ...\n",
"2020-08-31 157.19 157.37 155.300 155.43 17051511.0\n",
"2020-09-01 155.22 157.31 154.450 157.21 15654144.0\n",
"2020-09-02 157.96 158.98 156.175 158.46 16763449.0\n",
"2020-09-03 158.12 158.29 152.960 153.78 32117585.0\n",
"2020-09-04 155.71 155.89 149.290 152.80 30618783.0\n",
"\n",
"[5102 rows x 5 columns]\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_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_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_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_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/25/2020, 07:53:11')"
]
},
"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",
" open high low close volume\n",
"date \n",
"2000-05-26 91.06 91.44 90.630 91.44 37400.0\n",
"2000-05-30 92.75 94.81 92.750 94.81 28800.0\n",
"2000-05-31 95.13 96.38 95.130 95.75 18000.0\n",
"2000-06-01 97.11 97.31 97.110 97.31 3500.0\n",
"2000-06-02 101.70 102.40 101.700 102.40 14700.0\n",
"... ... ... ... ... ...\n",
"2020-08-31 157.19 157.37 155.300 155.43 17051511.0\n",
"2020-09-01 155.22 157.31 154.450 157.21 15654144.0\n",
"2020-09-02 157.96 158.98 156.175 158.46 16763449.0\n",
"2020-09-03 158.12 158.29 152.960 153.78 32117585.0\n",
"2020-09-04 155.71 155.89 149.290 152.80 30618783.0\n",
"\n",
"[5102 rows x 5 columns]\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/25/2020, 07:53:11')"
]
},
"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",
" 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",
"[5241 rows x 5 columns]\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/25/2020, 07:53:11')"
]
},
"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",
" open high low close volume\n",
"date \n",
"2000-05-26 91.06 91.44 90.630 91.44 37400.0\n",
"2000-05-30 92.75 94.81 92.750 94.81 28800.0\n",
"2000-05-31 95.13 96.38 95.130 95.75 18000.0\n",
"2000-06-01 97.11 97.31 97.110 97.31 3500.0\n",
"2000-06-02 101.70 102.40 101.700 102.40 14700.0\n",
"... ... ... ... ... ...\n",
"2020-08-31 157.19 157.37 155.300 155.43 17051511.0\n",
"2020-09-01 155.22 157.31 154.450 157.21 15654144.0\n",
"2020-09-02 157.96 158.98 156.175 158.46 16763449.0\n",
"2020-09-03 158.12 158.29 152.960 153.78 32117585.0\n",
"2020-09-04 155.71 155.89 149.290 152.80 30618783.0\n",
"\n",
"[5102 rows x 5 columns]\n",
"[X] Oops! 'AnalysisIndicators' object has no attribute 'percet_return'\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/25/2020, 07:53:11')"
]
},
"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",
" 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",
"[5241 rows x 5 columns]\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_EMA_10</th>\n",
" <th>VOLUME_SMA_20</th>\n",
" <th>EMA_5</th>\n",
" <th>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 VOLUME_EMA_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 4.999906e+07 \n",
"2020-08-25 343.5300 344.2100 342.2700 344.1200 38463381.0 4.790167e+07 \n",
"2020-08-26 344.7600 347.8600 344.1700 347.5700 50790237.0 4.842686e+07 \n",
"2020-08-27 348.5100 349.9000 346.5300 348.3300 58034142.0 5.017364e+07 \n",
"2020-08-28 349.4400 350.7200 348.1500 350.5800 48588940.0 4.988551e+07 \n",
"\n",
" VOLUME_SMA_20 EMA_5 EMA_5_LR_8 \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 136.012480 NaN \n",
"... ... ... ... \n",
"2020-08-24 51792231.45 339.779956 338.589691 \n",
"2020-08-25 50840651.55 341.226637 339.658445 \n",
"2020-08-26 50957455.45 343.341091 341.152455 \n",
"2020-08-27 50766076.85 345.004061 342.852470 \n",
"2020-08-28 48934986.10 346.862707 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/25/2020, 07:53:11')"
]
},
"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",
" 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",
"[5241 rows x 5 columns]\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_BBL_20_2.0</th>\n",
" <th>MACD_BBM_20_2.0</th>\n",
" <th>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_BBL_20_2.0 MACD_BBM_20_2.0 \\\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 0.069881 5.271794 3.678022 4.914752 \n",
"2020-08-25 0.192980 5.320039 3.807707 4.990945 \n",
"2020-08-26 0.431690 5.427961 3.933776 5.083307 \n",
"2020-08-27 0.557694 5.567385 4.100036 5.194943 \n",
"2020-08-28 0.700269 5.742452 4.251657 5.319172 \n",
"\n",
" MACD_BBU_20_2.0 \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 6.151482 \n",
"2020-08-25 6.174183 \n",
"2020-08-26 6.232838 \n",
"2020-08-27 6.289851 \n",
"2020-08-28 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/25/2020, 07:53:11')"
]
},
"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",
" 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",
"[5241 rows x 5 columns]\n",
"[i] Runtime: 927.2966 ms (0.9273 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</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",
" </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",
"\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",
"\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",
"\n",
" SMA_5_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 "
]
},
"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()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Additional Strategy Options"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The ```params``` keyword takes a _tuple_ as a shorthand to the parameter arguments in order.\n",
"* **Note**: If the indicator arguments change, so will results. Breaking Changes will **always** be posted on the README.\n",
"\n",
"The ```col_numbers``` keyword takes a _tuple_ specifying which column to return if the result is a DataFrame."
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"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/25/2020, 07:53:11')"
]
},
"execution_count": 29,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"params_ta = [\n",
" {\"kind\":\"ema\", \"params\": (10,)},\n",
" # params sets MACD's keyword arguments: fast=9, slow=19, signal=10\n",
" # and returning the 2nd column: histogram\n",
" {\"kind\":\"macd\", \"params\": (9, 19, 10), \"col_numbers\": (1,)},\n",
" # Selects the Lower and Upper Bands and renames them LB and UB, ignoring the MB\n",
" {\"kind\":\"bbands\", \"col_numbers\": (0,2), \"col_names\": (\"LB\", \"UB\")},\n",
" {\"kind\":\"log_return\", \"params\": (5, False)},\n",
"]\n",
"params_ta_strategy = ta.Strategy(\n",
" \"EMA, MACD History, Outter BBands, Log Returns\", # name\n",
" params_ta, # ta\n",
" \"EMA, MACD History, BBands(LB, UB), and Log Returns Strategy\" # description\n",
")\n",
"params_ta_strategy"
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'EMA, MACD History, Outter BBands, Log Returns'"
]
},
"execution_count": 30,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Update the Watchlist\n",
"watch.strategy = params_ta_strategy\n",
"watch.strategy.name"
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[i] Loaded['D']: SPY_D.csv\n",
" 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",
"[5241 rows x 5 columns]\n",
"[i] Runtime: 180.5198 ms (0.1805 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>EMA_10</th>\n",
" <th>MACDh_9_19_10</th>\n",
" <th>LB</th>\n",
" <th>UB</th>\n",
" <th>LOGRET_5</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>2020-08-24</th>\n",
" <td>342.12</td>\n",
" <td>343.00</td>\n",
" <td>339.4504</td>\n",
" <td>342.92</td>\n",
" <td>48588662.0</td>\n",
" <td>337.797677</td>\n",
" <td>-0.000276</td>\n",
" <td>334.962909</td>\n",
" <td>343.657091</td>\n",
" <td>0.014718</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2020-08-25</th>\n",
" <td>343.53</td>\n",
" <td>344.21</td>\n",
" <td>342.2700</td>\n",
" <td>344.12</td>\n",
" <td>38463381.0</td>\n",
" <td>338.947190</td>\n",
" <td>0.165373</td>\n",
" <td>334.441244</td>\n",
" <td>346.370756</td>\n",
" <td>0.016053</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2020-08-26</th>\n",
" <td>344.76</td>\n",
" <td>347.86</td>\n",
" <td>344.1700</td>\n",
" <td>347.57</td>\n",
" <td>50790237.0</td>\n",
" <td>340.514974</td>\n",
" <td>0.469555</td>\n",
" <td>335.028792</td>\n",
" <td>349.919208</td>\n",
" <td>0.030201</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2020-08-27</th>\n",
" <td>348.51</td>\n",
" <td>349.90</td>\n",
" <td>346.5300</td>\n",
" <td>348.33</td>\n",
" <td>58034142.0</td>\n",
" <td>341.935888</td>\n",
" <td>0.613083</td>\n",
" <td>337.277495</td>\n",
" <td>351.690505</td>\n",
" <td>0.029276</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2020-08-28</th>\n",
" <td>349.44</td>\n",
" <td>350.72</td>\n",
" <td>348.1500</td>\n",
" <td>350.58</td>\n",
" <td>48588940.0</td>\n",
" <td>343.507545</td>\n",
" <td>0.771996</td>\n",
" <td>340.426029</td>\n",
" <td>352.981971</td>\n",
" <td>0.032174</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" open high low close volume EMA_10 \\\n",
"date \n",
"2020-08-24 342.12 343.00 339.4504 342.92 48588662.0 337.797677 \n",
"2020-08-25 343.53 344.21 342.2700 344.12 38463381.0 338.947190 \n",
"2020-08-26 344.76 347.86 344.1700 347.57 50790237.0 340.514974 \n",
"2020-08-27 348.51 349.90 346.5300 348.33 58034142.0 341.935888 \n",
"2020-08-28 349.44 350.72 348.1500 350.58 48588940.0 343.507545 \n",
"\n",
" MACDh_9_19_10 LB UB LOGRET_5 \n",
"date \n",
"2020-08-24 -0.000276 334.962909 343.657091 0.014718 \n",
"2020-08-25 0.165373 334.441244 346.370756 0.016053 \n",
"2020-08-26 0.469555 335.028792 349.919208 0.030201 \n",
"2020-08-27 0.613083 337.277495 351.690505 0.029276 \n",
"2020-08-28 0.771996 340.426029 352.981971 0.032174 "
]
},
"execution_count": 31,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"spy = watch.load(\"SPY\", timed=True)\n",
"spy.tail()"
]
}
],
"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
}