Files
pandas-ta/examples/PandasTA_Strategy_Examples.ipynb
T
2021-01-24 12:25:42 -08:00

2649 lines
87 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",
" - **NOTE:** The **watchlist** module is independent of Pandas TA. To easily use it, copy it from your local pandas_ta installation directory into your project directory.\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 # Is this failing? If so, copy it locally. See above.\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 = 01/23/2021, 12:36: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 = 01/23/2021, 12:36: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='TA Description', created='01/23/2021, 12:36:24')"
]
},
"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='TA Description', created='01/23/2021, 12:36:24')"
]
},
"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='TA Description', created='01/23/2021, 12:36:24')"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Misspelled indicator, will fail later when ran with Pandas TA\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\"], timed=False)"
]
},
{
"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='Watch: 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.CommonStrategy\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 = [], plot: bool = False, **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: 8 of 8 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: 8 of 8 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)"
]
},
{
"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-09-28 333.2200 334.9600 332.1500 334.1900 64584614.0 331.181 \n",
" 2020-09-29 333.9700 334.7700 331.6209 332.3700 51531594.0 330.401 \n",
" 2020-09-30 333.0900 338.2900 332.8800 334.8900 104081136.0 330.008 \n",
" 2020-10-01 337.6900 338.7400 335.0100 337.0400 88698745.0 330.128 \n",
" 2020-10-02 331.7000 337.0126 331.1900 333.8400 89431112.0 330.447 \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-09-28 336.9395 334.8642 310.30500 86281647.00 \n",
" 2020-09-29 336.0925 335.0252 310.38120 85553267.55 \n",
" 2020-09-30 335.2070 335.2228 310.46905 88007358.10 \n",
" 2020-10-01 334.1740 335.4264 310.55675 88965293.60 \n",
" 2020-10-02 333.5965 335.6440 310.62810 86036292.75 \n",
" \n",
" [5265 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-09-28 148.37 150.44 146.404 150.02 17600904.0 149.672 151.4385 \n",
" 2020-09-29 149.85 150.28 148.000 149.34 18703037.0 149.269 151.1340 \n",
" 2020-09-30 149.90 151.97 148.490 149.79 29073746.0 148.766 150.7630 \n",
" 2020-10-01 150.81 152.20 149.490 152.18 25871962.0 148.615 150.4490 \n",
" 2020-10-02 149.41 153.58 148.990 152.85 29451992.0 148.571 150.4025 \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-09-28 152.3430 144.94380 24197653.10 \n",
" 2020-09-29 152.4106 144.87070 24280229.40 \n",
" 2020-09-30 152.4458 144.80300 24951209.50 \n",
" 2020-10-01 152.5272 144.74450 25406635.15 \n",
" 2020-10-02 152.6190 144.68525 25273355.50 \n",
" \n",
" [5121 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-09-28</th>\n",
" <td>333.2200</td>\n",
" <td>334.9600</td>\n",
" <td>332.1500</td>\n",
" <td>334.1900</td>\n",
" <td>64584614.0</td>\n",
" <td>331.181</td>\n",
" <td>336.9395</td>\n",
" <td>334.8642</td>\n",
" <td>310.30500</td>\n",
" <td>86281647.00</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2020-09-29</th>\n",
" <td>333.9700</td>\n",
" <td>334.7700</td>\n",
" <td>331.6209</td>\n",
" <td>332.3700</td>\n",
" <td>51531594.0</td>\n",
" <td>330.401</td>\n",
" <td>336.0925</td>\n",
" <td>335.0252</td>\n",
" <td>310.38120</td>\n",
" <td>85553267.55</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2020-09-30</th>\n",
" <td>333.0900</td>\n",
" <td>338.2900</td>\n",
" <td>332.8800</td>\n",
" <td>334.8900</td>\n",
" <td>104081136.0</td>\n",
" <td>330.008</td>\n",
" <td>335.2070</td>\n",
" <td>335.2228</td>\n",
" <td>310.46905</td>\n",
" <td>88007358.10</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2020-10-01</th>\n",
" <td>337.6900</td>\n",
" <td>338.7400</td>\n",
" <td>335.0100</td>\n",
" <td>337.0400</td>\n",
" <td>88698745.0</td>\n",
" <td>330.128</td>\n",
" <td>334.1740</td>\n",
" <td>335.4264</td>\n",
" <td>310.55675</td>\n",
" <td>88965293.60</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2020-10-02</th>\n",
" <td>331.7000</td>\n",
" <td>337.0126</td>\n",
" <td>331.1900</td>\n",
" <td>333.8400</td>\n",
" <td>89431112.0</td>\n",
" <td>330.447</td>\n",
" <td>333.5965</td>\n",
" <td>335.6440</td>\n",
" <td>310.62810</td>\n",
" <td>86036292.75</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>5265 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-09-28 333.2200 334.9600 332.1500 334.1900 64584614.0 331.181 \n",
"2020-09-29 333.9700 334.7700 331.6209 332.3700 51531594.0 330.401 \n",
"2020-09-30 333.0900 338.2900 332.8800 334.8900 104081136.0 330.008 \n",
"2020-10-01 337.6900 338.7400 335.0100 337.0400 88698745.0 330.128 \n",
"2020-10-02 331.7000 337.0126 331.1900 333.8400 89431112.0 330.447 \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-09-28 336.9395 334.8642 310.30500 86281647.00 \n",
"2020-09-29 336.0925 335.0252 310.38120 85553267.55 \n",
"2020-09-30 335.2070 335.2228 310.46905 88007358.10 \n",
"2020-10-01 334.1740 335.4264 310.55675 88965293.60 \n",
"2020-10-02 333.5965 335.6440 310.62810 86036292.75 \n",
"\n",
"[5265 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='TA Description', created='01/23/2021, 12:36:24')"
]
},
"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-09-28</th>\n",
" <td>148.37</td>\n",
" <td>150.44</td>\n",
" <td>146.404</td>\n",
" <td>150.02</td>\n",
" <td>17600904.0</td>\n",
" <td>152.3430</td>\n",
" <td>144.94380</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2020-09-29</th>\n",
" <td>149.85</td>\n",
" <td>150.28</td>\n",
" <td>148.000</td>\n",
" <td>149.34</td>\n",
" <td>18703037.0</td>\n",
" <td>152.4106</td>\n",
" <td>144.87070</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2020-09-30</th>\n",
" <td>149.90</td>\n",
" <td>151.97</td>\n",
" <td>148.490</td>\n",
" <td>149.79</td>\n",
" <td>29073746.0</td>\n",
" <td>152.4458</td>\n",
" <td>144.80300</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2020-10-01</th>\n",
" <td>150.81</td>\n",
" <td>152.20</td>\n",
" <td>149.490</td>\n",
" <td>152.18</td>\n",
" <td>25871962.0</td>\n",
" <td>152.5272</td>\n",
" <td>144.74450</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2020-10-02</th>\n",
" <td>149.41</td>\n",
" <td>153.58</td>\n",
" <td>148.990</td>\n",
" <td>152.85</td>\n",
" <td>29451992.0</td>\n",
" <td>152.6190</td>\n",
" <td>144.68525</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>5121 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-09-28 148.37 150.44 146.404 150.02 17600904.0 152.3430 144.94380\n",
"2020-09-29 149.85 150.28 148.000 149.34 18703037.0 152.4106 144.87070\n",
"2020-09-30 149.90 151.97 148.490 149.79 29073746.0 152.4458 144.80300\n",
"2020-10-01 150.81 152.20 149.490 152.18 25871962.0 152.5272 144.74450\n",
"2020-10-02 149.41 153.58 148.990 152.85 29451992.0 152.6190 144.68525\n",
"\n",
"[5121 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='TA Description', created='01/23/2021, 12:36:24')"
]
},
"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>NaN</td>\n",
" <td>NaN</td>\n",
" <td>1</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>-0.000461</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>1</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>0.007120</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>1</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>0.016915</td>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" <td>1</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",
" <td>...</td>\n",
" <td>...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2020-09-28</th>\n",
" <td>333.2200</td>\n",
" <td>334.9600</td>\n",
" <td>332.1500</td>\n",
" <td>334.1900</td>\n",
" <td>64584614.0</td>\n",
" <td>330.408828</td>\n",
" <td>334.010756</td>\n",
" <td>0.902277</td>\n",
" <td>49.833349</td>\n",
" <td>344.119874</td>\n",
" <td>-1</td>\n",
" <td>NaN</td>\n",
" <td>344.119874</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2020-09-29</th>\n",
" <td>333.9700</td>\n",
" <td>334.7700</td>\n",
" <td>331.6209</td>\n",
" <td>332.3700</td>\n",
" <td>51531594.0</td>\n",
" <td>330.844644</td>\n",
" <td>333.861596</td>\n",
" <td>0.896816</td>\n",
" <td>48.051629</td>\n",
" <td>344.119874</td>\n",
" <td>-1</td>\n",
" <td>NaN</td>\n",
" <td>344.119874</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2020-09-30</th>\n",
" <td>333.0900</td>\n",
" <td>338.2900</td>\n",
" <td>332.8800</td>\n",
" <td>334.8900</td>\n",
" <td>104081136.0</td>\n",
" <td>331.743612</td>\n",
" <td>333.955087</td>\n",
" <td>0.904369</td>\n",
" <td>50.680975</td>\n",
" <td>344.119874</td>\n",
" <td>-1</td>\n",
" <td>NaN</td>\n",
" <td>344.119874</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2020-10-01</th>\n",
" <td>337.6900</td>\n",
" <td>338.7400</td>\n",
" <td>335.0100</td>\n",
" <td>337.0400</td>\n",
" <td>88698745.0</td>\n",
" <td>332.920587</td>\n",
" <td>334.235534</td>\n",
" <td>0.910769</td>\n",
" <td>52.872627</td>\n",
" <td>344.119874</td>\n",
" <td>-1</td>\n",
" <td>NaN</td>\n",
" <td>344.119874</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2020-10-02</th>\n",
" <td>331.7000</td>\n",
" <td>337.0126</td>\n",
" <td>331.1900</td>\n",
" <td>333.8400</td>\n",
" <td>89431112.0</td>\n",
" <td>333.124901</td>\n",
" <td>334.199576</td>\n",
" <td>0.901229</td>\n",
" <td>49.357005</td>\n",
" <td>344.119874</td>\n",
" <td>-1</td>\n",
" <td>NaN</td>\n",
" <td>344.119874</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>5265 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-09-28 333.2200 334.9600 332.1500 334.1900 64584614.0 330.408828 \n",
"2020-09-29 333.9700 334.7700 331.6209 332.3700 51531594.0 330.844644 \n",
"2020-09-30 333.0900 338.2900 332.8800 334.8900 104081136.0 331.743612 \n",
"2020-10-01 337.6900 338.7400 335.0100 337.0400 88698745.0 332.920587 \n",
"2020-10-02 331.7000 337.0126 331.1900 333.8400 89431112.0 333.124901 \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 NaN NaN 1 \n",
"1999-11-03 NaN -0.000461 NaN NaN 1 \n",
"1999-11-04 NaN 0.007120 NaN NaN 1 \n",
"1999-11-05 NaN 0.016915 NaN NaN 1 \n",
"... ... ... ... ... ... \n",
"2020-09-28 334.010756 0.902277 49.833349 344.119874 -1 \n",
"2020-09-29 333.861596 0.896816 48.051629 344.119874 -1 \n",
"2020-09-30 333.955087 0.904369 50.680975 344.119874 -1 \n",
"2020-10-01 334.235534 0.910769 52.872627 344.119874 -1 \n",
"2020-10-02 334.199576 0.901229 49.357005 344.119874 -1 \n",
"\n",
" SUPERTl_7_3.0 SUPERTs_7_3.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-09-28 NaN 344.119874 \n",
"2020-09-29 NaN 344.119874 \n",
"2020-09-30 NaN 344.119874 \n",
"2020-10-01 NaN 344.119874 \n",
"2020-10-02 NaN 344.119874 \n",
"\n",
"[5265 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='TA Description', created='01/23/2021, 12:36:24')"
]
},
"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",
"[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='TA Description', created='01/23/2021, 12:36:24')"
]
},
"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_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-09-28</th>\n",
" <td>333.2200</td>\n",
" <td>334.9600</td>\n",
" <td>332.1500</td>\n",
" <td>334.1900</td>\n",
" <td>64584614.0</td>\n",
" <td>7.861837e+07</td>\n",
" <td>86281647.00</td>\n",
" <td>329.781966</td>\n",
" <td>327.921896</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2020-09-29</th>\n",
" <td>333.9700</td>\n",
" <td>334.7700</td>\n",
" <td>331.6209</td>\n",
" <td>332.3700</td>\n",
" <td>51531594.0</td>\n",
" <td>7.369350e+07</td>\n",
" <td>85553267.55</td>\n",
" <td>330.644644</td>\n",
" <td>328.610232</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2020-09-30</th>\n",
" <td>333.0900</td>\n",
" <td>338.2900</td>\n",
" <td>332.8800</td>\n",
" <td>334.8900</td>\n",
" <td>104081136.0</td>\n",
" <td>7.921853e+07</td>\n",
" <td>88007358.10</td>\n",
" <td>332.059763</td>\n",
" <td>329.854718</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2020-10-01</th>\n",
" <td>337.6900</td>\n",
" <td>338.7400</td>\n",
" <td>335.0100</td>\n",
" <td>337.0400</td>\n",
" <td>88698745.0</td>\n",
" <td>8.094220e+07</td>\n",
" <td>88965293.60</td>\n",
" <td>333.719842</td>\n",
" <td>331.449495</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2020-10-02</th>\n",
" <td>331.7000</td>\n",
" <td>337.0126</td>\n",
" <td>331.1900</td>\n",
" <td>333.8400</td>\n",
" <td>89431112.0</td>\n",
" <td>8.248564e+07</td>\n",
" <td>86036292.75</td>\n",
" <td>333.759895</td>\n",
" <td>332.881012</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>5265 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-09-28 333.2200 334.9600 332.1500 334.1900 64584614.0 \n",
"2020-09-29 333.9700 334.7700 331.6209 332.3700 51531594.0 \n",
"2020-09-30 333.0900 338.2900 332.8800 334.8900 104081136.0 \n",
"2020-10-01 337.6900 338.7400 335.0100 337.0400 88698745.0 \n",
"2020-10-02 331.7000 337.0126 331.1900 333.8400 89431112.0 \n",
"\n",
" VOLUME_EMA_10 VOLUME_SMA_20 EMA_5 EMA_5_LR_8 \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 136.012480 NaN \n",
"... ... ... ... ... \n",
"2020-09-28 7.861837e+07 86281647.00 329.781966 327.921896 \n",
"2020-09-29 7.369350e+07 85553267.55 330.644644 328.610232 \n",
"2020-09-30 7.921853e+07 88007358.10 332.059763 329.854718 \n",
"2020-10-01 8.094220e+07 88965293.60 333.719842 331.449495 \n",
"2020-10-02 8.248564e+07 86036292.75 333.759895 332.881012 \n",
"\n",
"[5265 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='01/23/2021, 12:36:24')"
]
},
"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_BBL_20_2.0</th>\n",
" <th>MACD_BBM_20_2.0</th>\n",
" <th>MACD_BBU_20_2.0</th>\n",
" <th>MACD_BBB_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",
" <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",
" </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",
" </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",
" </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",
" </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",
" </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",
" </tr>\n",
" <tr>\n",
" <th>2020-09-28</th>\n",
" <td>333.2200</td>\n",
" <td>334.9600</td>\n",
" <td>332.1500</td>\n",
" <td>334.1900</td>\n",
" <td>64584614.0</td>\n",
" <td>-2.475445</td>\n",
" <td>-1.262367</td>\n",
" <td>-1.213078</td>\n",
" <td>-5.034888</td>\n",
" <td>1.888513</td>\n",
" <td>8.811913</td>\n",
" <td>733.211957</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2020-09-29</th>\n",
" <td>333.9700</td>\n",
" <td>334.7700</td>\n",
" <td>331.6209</td>\n",
" <td>332.3700</td>\n",
" <td>51531594.0</td>\n",
" <td>-2.252083</td>\n",
" <td>-0.831203</td>\n",
" <td>-1.420879</td>\n",
" <td>-5.348731</td>\n",
" <td>1.450066</td>\n",
" <td>8.248862</td>\n",
" <td>937.722469</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2020-09-30</th>\n",
" <td>333.0900</td>\n",
" <td>338.2900</td>\n",
" <td>332.8800</td>\n",
" <td>334.8900</td>\n",
" <td>104081136.0</td>\n",
" <td>-1.850393</td>\n",
" <td>-0.343611</td>\n",
" <td>-1.506782</td>\n",
" <td>-5.445367</td>\n",
" <td>1.019390</td>\n",
" <td>7.484147</td>\n",
" <td>1268.357614</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2020-10-01</th>\n",
" <td>337.6900</td>\n",
" <td>338.7400</td>\n",
" <td>335.0100</td>\n",
" <td>337.0400</td>\n",
" <td>88698745.0</td>\n",
" <td>-1.343082</td>\n",
" <td>0.130960</td>\n",
" <td>-1.474042</td>\n",
" <td>-5.235913</td>\n",
" <td>0.587945</td>\n",
" <td>6.411803</td>\n",
" <td>1981.090096</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2020-10-02</th>\n",
" <td>331.7000</td>\n",
" <td>337.0126</td>\n",
" <td>331.1900</td>\n",
" <td>333.8400</td>\n",
" <td>89431112.0</td>\n",
" <td>-1.185581</td>\n",
" <td>0.230769</td>\n",
" <td>-1.416350</td>\n",
" <td>-4.926333</td>\n",
" <td>0.197149</td>\n",
" <td>5.320631</td>\n",
" <td>5197.569576</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>5265 rows × 12 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-09-28 333.2200 334.9600 332.1500 334.1900 64584614.0 -2.475445 \n",
"2020-09-29 333.9700 334.7700 331.6209 332.3700 51531594.0 -2.252083 \n",
"2020-09-30 333.0900 338.2900 332.8800 334.8900 104081136.0 -1.850393 \n",
"2020-10-01 337.6900 338.7400 335.0100 337.0400 88698745.0 -1.343082 \n",
"2020-10-02 331.7000 337.0126 331.1900 333.8400 89431112.0 -1.185581 \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-09-28 -1.262367 -1.213078 -5.034888 1.888513 \n",
"2020-09-29 -0.831203 -1.420879 -5.348731 1.450066 \n",
"2020-09-30 -0.343611 -1.506782 -5.445367 1.019390 \n",
"2020-10-01 0.130960 -1.474042 -5.235913 0.587945 \n",
"2020-10-02 0.230769 -1.416350 -4.926333 0.197149 \n",
"\n",
" MACD_BBU_20_2.0 MACD_BBB_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-09-28 8.811913 733.211957 \n",
"2020-09-29 8.248862 937.722469 \n",
"2020-09-30 7.484147 1268.357614 \n",
"2020-10-01 6.411803 1981.090096 \n",
"2020-10-02 5.320631 5197.569576 \n",
"\n",
"[5265 rows x 12 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='01/23/2021, 12:36:24')"
]
},
"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"
]
},
{
"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>BBB_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",
" <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>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>NaN</td>\n",
" <td>NaN</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>NaN</td>\n",
" <td>NaN</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>NaN</td>\n",
" <td>NaN</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>NaN</td>\n",
" <td>NaN</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 BBB_20_2.0 \\\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",
" MACD_12_26_9 MACDh_12_26_9 MACDs_12_26_9 RSI_14 CUMLOGRET_1 \\\n",
"date \n",
"1999-11-01 NaN NaN NaN NaN NaN \n",
"1999-11-02 NaN NaN NaN NaN -0.007172 \n",
"1999-11-03 NaN NaN NaN NaN -0.000461 \n",
"1999-11-04 NaN NaN NaN NaN 0.007120 \n",
"1999-11-05 NaN NaN NaN NaN 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\")\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='01/23/2021, 12:36:24')"
]
},
"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"
]
},
{
"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-09-28</th>\n",
" <td>333.22</td>\n",
" <td>334.9600</td>\n",
" <td>332.1500</td>\n",
" <td>334.19</td>\n",
" <td>64584614.0</td>\n",
" <td>331.135274</td>\n",
" <td>-0.761769</td>\n",
" <td>318.226448</td>\n",
" <td>337.517552</td>\n",
" <td>0.021841</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2020-09-29</th>\n",
" <td>333.97</td>\n",
" <td>334.7700</td>\n",
" <td>331.6209</td>\n",
" <td>332.37</td>\n",
" <td>51531594.0</td>\n",
" <td>331.359770</td>\n",
" <td>-0.249828</td>\n",
" <td>317.965316</td>\n",
" <td>338.606684</td>\n",
" <td>0.006247</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2020-09-30</th>\n",
" <td>333.09</td>\n",
" <td>338.2900</td>\n",
" <td>332.8800</td>\n",
" <td>334.89</td>\n",
" <td>104081136.0</td>\n",
" <td>332.001630</td>\n",
" <td>0.311580</td>\n",
" <td>321.342411</td>\n",
" <td>340.129589</td>\n",
" <td>0.037265</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2020-10-01</th>\n",
" <td>337.69</td>\n",
" <td>338.7400</td>\n",
" <td>335.0100</td>\n",
" <td>337.04</td>\n",
" <td>88698745.0</td>\n",
" <td>332.917697</td>\n",
" <td>0.832955</td>\n",
" <td>327.202692</td>\n",
" <td>339.685308</td>\n",
" <td>0.041003</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2020-10-02</th>\n",
" <td>331.70</td>\n",
" <td>337.0126</td>\n",
" <td>331.1900</td>\n",
" <td>333.84</td>\n",
" <td>89431112.0</td>\n",
" <td>333.085389</td>\n",
" <td>0.854917</td>\n",
" <td>331.050371</td>\n",
" <td>337.881629</td>\n",
" <td>0.015425</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" open high low close volume EMA_10 \\\n",
"date \n",
"2020-09-28 333.22 334.9600 332.1500 334.19 64584614.0 331.135274 \n",
"2020-09-29 333.97 334.7700 331.6209 332.37 51531594.0 331.359770 \n",
"2020-09-30 333.09 338.2900 332.8800 334.89 104081136.0 332.001630 \n",
"2020-10-01 337.69 338.7400 335.0100 337.04 88698745.0 332.917697 \n",
"2020-10-02 331.70 337.0126 331.1900 333.84 89431112.0 333.085389 \n",
"\n",
" MACDh_9_19_10 LB UB LOGRET_5 \n",
"date \n",
"2020-09-28 -0.761769 318.226448 337.517552 0.021841 \n",
"2020-09-29 -0.249828 317.965316 338.606684 0.006247 \n",
"2020-09-30 0.311580 321.342411 340.129589 0.037265 \n",
"2020-10-01 0.832955 327.202692 339.685308 0.041003 \n",
"2020-10-02 0.854917 331.050371 337.881629 0.015425 "
]
},
"execution_count": 31,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"spy = watch.load(\"SPY\")\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
}