-
Notifications
You must be signed in to change notification settings - Fork 134
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #133 from amor71/anchor-vwap
Anchor vwap
- Loading branch information
Showing
10 changed files
with
291 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,232 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## Select stock symbol and date range" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"symbol = 'AAPL'\n", | ||
"start_date = \"2020-11-12\"\n", | ||
"end_date = \"2020-11-12\"\n", | ||
"anchored_vwaps_start = ['2020-11-12 11:00:00', '2020-11-12 14:00:00']" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"#### Imports" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# silence warnings\n", | ||
"import warnings\n", | ||
"import liualgotrader.analytics.analysis as ana\n", | ||
"import matplotlib.pyplot as plt\n", | ||
"import nest_asyncio\n", | ||
"import numpy as np\n", | ||
"import pandas as pd\n", | ||
"import pytz\n", | ||
"from empyrical import roll_max_drawdown\n", | ||
"from scipy.stats import kurtosis, skew\n", | ||
"import plotly.graph_objects as go\n", | ||
"from plotly.subplots import make_subplots\n", | ||
"import chart_studio.plotly as py\n", | ||
"from datetime import datetime\n", | ||
"import alpaca_trade_api as tradeapi\n", | ||
"from liualgotrader.common.market_data import get_symbol_data\n", | ||
"from liualgotrader.fincalcs.vwap import anchored_vwap\n", | ||
"%matplotlib inline\n", | ||
"warnings.filterwarnings(\"ignore\")\n", | ||
"\n", | ||
"\n", | ||
"nest_asyncio.apply()\n", | ||
"est = pytz.timezone(\"US/Eastern\")" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"#### Load symbol data" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"start_date = datetime.strptime(start_date, \"%Y-%m-%d\")\n", | ||
"end_date = datetime.strptime(end_date, \"%Y-%m-%d\")" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"api = tradeapi.REST(base_url=\"https://api.alpaca.markets\")\n", | ||
"ohlc_data = get_symbol_data(api, symbol, start_date, end_date)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"if ohlc_data is None or ohlc_data.empty:\n", | ||
" assert False, \"No data loaded\"" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"#### Calculate indicators" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"anchored_vwap_starts = [\n", | ||
" datetime.strptime(anchored_vwap_start, \"%Y-%m-%d %H:%M:%S\").replace(tzinfo=est)\n", | ||
" for anchored_vwap_start in anchored_vwaps_start\n", | ||
"]\n", | ||
"anchored_vwap_indicators = [\n", | ||
" anchored_vwap(ohlc_data, anchored_vwap_start)\n", | ||
" for anchored_vwap_start in anchored_vwap_starts\n", | ||
"]" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## Visuals" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"trace1 = {\n", | ||
" \"x\": ohlc_data.index,\n", | ||
" \"open\": ohlc_data.open,\n", | ||
" \"high\": ohlc_data.high,\n", | ||
" \"low\": ohlc_data.low,\n", | ||
" \"close\": ohlc_data.close,\n", | ||
" \"type\": \"candlestick\",\n", | ||
" \"name\": symbol,\n", | ||
" \"yaxis\": \"y2\",\n", | ||
" \"showlegend\": True,\n", | ||
"}\n", | ||
"trace2 = [{\n", | ||
" \"x\": anchored_vwap_indicator.index,\n", | ||
" \"y\": anchored_vwap_indicator,\n", | ||
" \"type\": \"scatter\",\n", | ||
" \"mode\": \"lines\",\n", | ||
" \"line\": {\"width\": 2, \"color\": \"black\"},\n", | ||
" \"yaxis\": \"y2\",\n", | ||
" \"name\": f\"VWAP-{indx+1}\",\n", | ||
" \"showlegend\": True,\n", | ||
"} for indx, anchored_vwap_indicator in enumerate(anchored_vwap_indicators)]\n", | ||
"fig = dict ( data = [trace1], layout = dict())\n", | ||
"\n", | ||
"fig['layout']['plot_bgcolor'] = 'rgb(200, 200, 200)'\n", | ||
"fig['layout']['xaxis'] = dict( rangeselector = dict( visible = True ) )\n", | ||
"fig['layout']['yaxis'] = dict( domain = [0, 0.2], showticklabels = False )\n", | ||
"fig['layout']['yaxis2'] = dict( domain = [0.2, 0.8] )\n", | ||
"fig['layout']['legend'] = dict( orientation = 'h', y=0.9, x=0.3, yanchor='bottom' )\n", | ||
"fig['layout']['margin'] = dict( t=40, b=40, r=40, l=40 )\n", | ||
"\n", | ||
"rangeselector=dict(\n", | ||
" #visibe = True,\n", | ||
" x = 0, y = 0.9,\n", | ||
" bgcolor = 'rgba(150, 200, 250, 0.4)',\n", | ||
" font = dict( size = 13 ),\n", | ||
" buttons=list([\n", | ||
" dict(count=1,\n", | ||
" label='1 yr',\n", | ||
" step='year'),\n", | ||
" dict(count=3,\n", | ||
" label='3 mo',\n", | ||
" step='month',\n", | ||
" stepmode='backward'),\n", | ||
" dict(count=1,\n", | ||
" label='1 mo',\n", | ||
" step='month',\n", | ||
" stepmode='backward'),\n", | ||
" dict(count=7,\n", | ||
" label='1 wk',\n", | ||
" step='day',\n", | ||
" stepmode='backward'),\n", | ||
" dict(step='all')\n", | ||
" ]))\n", | ||
" \n", | ||
"fig['layout']['xaxis']['rangeselector'] = rangeselector\n", | ||
"\n", | ||
"fig['data'] += trace2\n", | ||
"\n", | ||
"colors = []\n", | ||
"\n", | ||
"for i in range(len(ohlc_data.close)):\n", | ||
" if i != 0:\n", | ||
" if ohlc_data.close[i] > ohlc_data.close[i-1]:\n", | ||
" colors.append(\"green\")\n", | ||
" else:\n", | ||
" colors.append(\"red\")\n", | ||
" else:\n", | ||
" colors.append(\"red\")\n", | ||
"\n", | ||
"fig['data'].append( dict( x=ohlc_data.index, y=ohlc_data.volume, \n", | ||
" marker=dict( color=colors ),\n", | ||
" type='bar', yaxis='y', name='Volume' ) )\n", | ||
"\n", | ||
"f = go.Figure(data=fig['data'], layout=fig['layout'])\n", | ||
"f.show()" | ||
] | ||
} | ||
], | ||
"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.0" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 4 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
__version__ = "0.0.75" | ||
__version__ = "0.0.76" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters