{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Strategy Analysis with **Pandas TA** and AI/ML\n", "* This is a **Work in Progress** and subject to change!\n", "* Contributions are welcome and accepted!\n", "* Examples below are for **educational purposes only**" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Required Packages\n", "##### Uncomment the packages you need to install or are missing" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "#!pip install numpy\n", "#!pip install pandas\n", "#!pip install mplfinance\n", "#!pip install pandas-datareader\n", "#!pip install requests_cache\n", "#!pip install alphaVantage-api" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Populating the interactive namespace from numpy and matplotlib\n", "Numpy v1.18.3\n", "Pandas v1.1.0\n", "mplfinance v0.12.6a3\n", "Pandas TA v0.1.95b\n", "Pandas Data Reader v0.9.0\n", "yfinance v0.1.54\n" ] } ], "source": [ "%pylab inline\n", "import datetime as dt\n", "import random as rnd\n", "from sys import float_info as sflt\n", "\n", "import numpy as np # pip install numpy\n", "import pandas as pd # pip install pandas\n", "pd.set_option('max_rows', 100)\n", "pd.set_option('max_columns', 20)\n", "\n", "import mplfinance as mpf # pip mplfinance\n", "import pandas_datareader as pdr # pip install pandas-datareader\n", "import requests_cache # pip install requests_cache\n", "\n", "from alphaVantageAPI.alphavantage import AlphaVantage # pip install alphaVantage-api\n", "import pandas_ta as ta\n", "\n", "# from pandas_datareader.famafrench import get_available_datasets\n", "import yfinance as yf\n", "yf.pdr_override() # <== that's all it takes :-)\n", "\n", "# from watchlist import famafrench, Fred, Watchlist\n", "from watchlist import Watchlist\n", "# from watchlist import Fred, Watchlist\n", "\n", "print(f\"Numpy v{np.__version__}\")\n", "print(f\"Pandas v{pd.__version__}\")\n", "print(f\"mplfinance v{mpf.__version__}\")\n", "print(f\"Pandas TA v{ta.version}\")\n", "print(f\"Pandas Data Reader v{pdr.__version__}\")\n", "print(f\"yfinance v{yf.__version__}\")\n", "%matplotlib inline" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## MISC Functions" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "def cscheme(colors: str):\n", " aliases = {\n", " \"BkBu\": [\"black\", \"blue\"],\n", " \"gr\": [\"green\", \"red\"],\n", " \"grays\": [\"silver\", \"gray\"],\n", " \"mas\": [\"black\", \"green\", \"orange\", \"red\"],\n", " \"bbands\": [\"blue\", \"navy\", \"blue\"],\n", " \"kc\": [\"purple\", \"fuchsia\", \"purple\"],\n", " }\n", " aliases[\"default\"] = aliases[\"gr\"]\n", " return aliases[colors]\n", "\n", "def recent_bars(df, tf: str = \"1y\"):\n", " # All Data: 0, Last Four Years: 0.25, Last Two Years: 0.5, This Year: 1, Last Half Year: 2, Last Quarter: 3\n", " yearly_divisor = {\"all\": 0, \"10y\": 0.1, \"5y\": 0.2, \"4y\": 0.25, \"3y\": 1./3, \"2y\": 0.5, \"1y\": 1, \"6mo\": 2, \"3mo\": 3}\n", " yd = yearly_divisor[tf] if tf in yearly_divisor.keys() else 0\n", " return int(ta.TRADING_DAYS_PER_YEAR / yd) if yd > 0 else df.shape[0]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Collect some Data" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[!] Loading All: SPY, QQQ\n", "[i] Loaded['D']: SPY_D.csv\n", "[+] Strategy: Common Price and Volume SMAs\n", "[i] Indicator arguments: {'timed': True, 'append': True}\n", "[i] Total indicators: 5\n", "[i] Columns added: 5\n", "[i] Runtime: 46.8974 ms (0.0469 s)\n", "[+] Downloading['D']: QQQ\n", "[+] Strategy: Common Price and Volume SMAs\n", "[i] Indicator arguments: {'timed': True, 'append': True}\n", "[i] Total indicators: 5\n", "[i] Columns added: 5\n", "[i] Runtime: 17.1018 ms (0.0171 s)\n" ] } ], "source": [ "tickers = [\"SPY\", \"QQQ\"]\n", "watch = Watchlist(tickers)\n", "watch.load(tickers, timed=True, verbose=True)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Index(['open', 'high', 'low', 'close', 'volume', 'SMA_10', 'SMA_20', 'SMA_50',\n", " 'SMA_200', 'VOL_SMA_20', '0'],\n", " dtype='object')" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ticker = tickers[1]\n", "watch.data[ticker].ta.constants(True, [0, 0, 0])\n", "watch.data[ticker].columns" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "recent = recent_bars(watch.data[ticker], \"2y\")\n", "asset = watch.data[ticker].copy().tail(recent)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Build some Trends" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
| \n", " | CLTR | \n", "TR_LOGRET | \n", "CLTR_Trends | \n", "CLTR_Trades | \n", "
|---|---|---|---|---|
| date | \n", "\n", " | \n", " | \n", " | \n", " |
| 2020-08-17 | \n", "0.392134 | \n", "0.011544 | \n", "1 | \n", "0.0 | \n", "
| 2020-08-18 | \n", "0.401713 | \n", "0.009579 | \n", "1 | \n", "0.0 | \n", "
| 2020-08-19 | \n", "0.394963 | \n", "-0.006750 | \n", "1 | \n", "0.0 | \n", "
| 2020-08-20 | \n", "0.408739 | \n", "0.013776 | \n", "1 | \n", "0.0 | \n", "
| 2020-08-21 | \n", "0.415646 | \n", "0.006906 | \n", "1 | \n", "0.0 | \n", "