{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Intraday vwap mean reverting\n",
"\n",
"Desription: mean reverting intraday strategy with momentum entry\n",
"\n",
"Goal is to recreate strategy in vectorbptro and compare with v2trading engine backtest.\n",
"\n",
"Symbol: BAC\n",
"Timeframe: 12s\n",
"Backtested on: 1s\n",
"Entry window: 0 - 380 (mins. from open)\n",
"EOD Exit: 382-390\n",
"\n",
"Indicators:\n",
"- vwap_cum - Anchored daily cumulative vwap \n",
"- div_vwap_cum - Divergence of vwap_cum and close \n",
"- div_vwap_angle - Linreg angle of div_vwap_cum over period N (hp1)\n",
"\n",
"**Long entries:**\n",
"- div_vwap_angle.AND.go_short_if_fallingc = 3 (hp2)\n",
"- div_vwap_cum.AND.go_short_if_above = 0 (hp3)\n",
"\n",
"**Short entries:**\n",
"- div_vwap_angle.AND.go_long_if_risingc = 3 (hp2)\n",
"- div_vwap_cum.AND.go_long_if_below = 0 (hp3)\n",
"\n",
"**Exits:**\n",
"only SL, TP pct based\n",
"- SL: 0.3 (hp4)\n",
"- TP: 0.4 (hp5)\n",
"\n",
"hp - hyperparameters for tuning"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import vectorbtpro as vbt\n",
"import ttools as tts\n",
"from lightweight_charts import chart, Panel, PlotDFAccessor, PlotSRAccessor\n",
"import talib\n",
"from numba import jit\n",
"import pandas as pd\n",
"import numpy as np\n"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "4a094aebc1d84f74b8a0cd1779acb02d",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"100%|##########| 1/1 [00:09<00:00, 9.19s/it, symbol=BAC]"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
" #fetching from remote db\n",
"from lib.db import Connection\n",
"SYMBOL = \"BAC\"\n",
"SCHEMA = \"ohlcv_1s\" #time based 1s other options ohlcv_vol_200 (volume based ohlcv with resolution of 200), ohlcv_renko_20 (renko with 20 bricks size) ...\n",
"DB = \"market_data\"\n",
"\n",
"con = Connection(db_name=DB, default_schema=SCHEMA, create_db=True)\n",
"basic_data = con.pull(symbols=[SYMBOL], schema=SCHEMA,start=\"2024-10-14\", end=\"2024-10-17\", tz_convert='America/New_York')\n",
"\n",
"#basic_data = basic_data.add_symbol(\"BAC2\", basic_data.data[\"BAC\"])\n",
"\n",
"#basic_data.index.normalize().nunique()"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"basic_data = basic_data[['open',\n",
" 'high',\n",
" 'low',\n",
" 'close',\n",
" 'volume']]"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"#Resample to 12s\n",
"s12_data = basic_data.resample(\"12s\")\n",
"s12_data = s12_data.transform(lambda df: df.between_time('09:30', '16:00').dropna())\n",
"\n",
"s12_data.data[\"BAC\"].lw.plot()"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['BAC']"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#s12_data = s12_data.add_symbol(\"BAC3\", s12_data.data[\"BAC\"])\n",
"s12_data.symbols"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"ename": "KeyError",
"evalue": "'BAC-LONG'",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mKeyError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[0;32mIn[6], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m s12_data \u001b[38;5;241m=\u001b[39m s12_data\u001b[38;5;241m.\u001b[39madd_symbol(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mBAC-SHORT\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[43ms12_data\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mdata\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mBAC-LONG\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m]\u001b[49m)\n",
"\u001b[0;31mKeyError\u001b[0m: 'BAC-LONG'"
]
}
],
"source": [
"s12_data = s12_data.add_symbol(\"BAC-SHORT\", s12_data.data[\"BAC-LONG\"])"
]
},
{
"cell_type": "code",
"execution_count": 46,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['ttools:DIVRELN', 'talib:DIV']"
]
},
"execution_count": 46,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"vbt.IF.list_indicators(\"*DIV*\")"
]
},
{
"cell_type": "code",
"execution_count": 37,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"LINEARREG_ANGLE.run(\n",
" close,\n",
" timeperiod=Default(value=14),\n",
" timeframe=Default(value=None),\n",
" short_name='linearreg_angle',\n",
" hide_params=None,\n",
" hide_default=True,\n",
" **kwargs\n",
"):\n",
" Run `LINEARREG_ANGLE` indicator.\n",
" \n",
" * Inputs: `close`\n",
" * Parameters: `timeperiod`, `timeframe`\n",
" * Outputs: `real`\n",
" \n",
" Pass a list of parameter names as `hide_params` to hide their column levels, or True to hide all.\n",
" Set `hide_default` to False to show the column levels of the parameters with a default value.\n",
" \n",
" Other keyword arguments are passed to `LINEARREG_ANGLE.run_pipeline`.\n"
]
}
],
"source": [
"vbt.phelp(vbt.indicator(\"talib:LINEARREG_ANGLE\").run)"
]
},
{
"cell_type": "code",
"execution_count": 55,
"metadata": {},
"outputs": [],
"source": [
"from numba import jit\n",
"import numpy as np\n",
"\n",
"@jit(nopython=True)\n",
"def isrisingc(pole: np.ndarray, pocet: int) -> np.ndarray:\n",
" # Ensure pocet is valid\n",
" if pocet < 1 or pocet > pole.shape[0]:\n",
" raise ValueError(\"Invalid window size.\")\n",
"\n",
" # Create a result array initialized to False\n",
" result = np.zeros(pole.shape[0], dtype=np.bool_)\n",
"\n",
" # Iterate through the array, starting from the first valid rolling window\n",
" for i in range(pocet - 1, pole.shape[0]):\n",
" is_increasing = True\n",
" # Compare the current value with the previous value within the rolling window\n",
" for j in range(i - pocet + 2, i + 1): # Start comparison from the second element in the window\n",
" if pole[j] <= pole[j - 1]: # Check if current element is not greater than the previous one\n",
" is_increasing = False\n",
" break\n",
" result[i] = is_increasing\n",
"\n",
" return result\n"
]
},
{
"cell_type": "code",
"execution_count": 119,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | symbol | \n",
" BAC | \n",
"
\n",
" \n",
" | time | \n",
" | \n",
"
\n",
" \n",
" \n",
" \n",
" | 2024-10-14 15:50:00-04:00 | \n",
" -0.0000 | \n",
"
\n",
" \n",
" | 2024-10-14 15:50:12-04:00 | \n",
" -0.0000 | \n",
"
\n",
" \n",
" | 2024-10-14 15:50:24-04:00 | \n",
" -0.0000 | \n",
"
\n",
" \n",
" | 2024-10-14 15:50:36-04:00 | \n",
" -0.0000 | \n",
"
\n",
" \n",
" | 2024-10-14 15:50:48-04:00 | \n",
" -0.0000 | \n",
"
\n",
" \n",
" | 2024-10-14 15:51:00-04:00 | \n",
" -0.0000 | \n",
"
\n",
" \n",
" | 2024-10-14 15:51:12-04:00 | \n",
" -0.0002 | \n",
"
\n",
" \n",
" | 2024-10-14 15:51:24-04:00 | \n",
" -0.0002 | \n",
"
\n",
" \n",
" | 2024-10-14 15:51:36-04:00 | \n",
" -0.0002 | \n",
"
\n",
" \n",
" | 2024-10-14 15:51:48-04:00 | \n",
" -0.0002 | \n",
"
\n",
" \n",
" | 2024-10-14 15:52:00-04:00 | \n",
" 0.0000 | \n",
"
\n",
" \n",
" | 2024-10-14 15:52:12-04:00 | \n",
" 0.0000 | \n",
"
\n",
" \n",
" | 2024-10-14 15:52:24-04:00 | \n",
" 0.0002 | \n",
"
\n",
" \n",
" | 2024-10-14 15:52:36-04:00 | \n",
" 0.0001 | \n",
"
\n",
" \n",
" | 2024-10-14 15:52:48-04:00 | \n",
" 0.0001 | \n",
"
\n",
" \n",
" | 2024-10-14 15:53:00-04:00 | \n",
" -0.0001 | \n",
"
\n",
" \n",
" | 2024-10-14 15:53:12-04:00 | \n",
" -0.0000 | \n",
"
\n",
" \n",
" | 2024-10-14 15:53:24-04:00 | \n",
" 0.0000 | \n",
"
\n",
" \n",
" | 2024-10-14 15:53:36-04:00 | \n",
" 0.0001 | \n",
"
\n",
" \n",
" | 2024-10-14 15:53:48-04:00 | \n",
" 0.0002 | \n",
"
\n",
" \n",
" | 2024-10-14 15:54:00-04:00 | \n",
" 0.0004 | \n",
"
\n",
" \n",
" | 2024-10-14 15:54:12-04:00 | \n",
" 0.0004 | \n",
"
\n",
" \n",
" | 2024-10-14 15:54:24-04:00 | \n",
" 0.0005 | \n",
"
\n",
" \n",
" | 2024-10-14 15:54:36-04:00 | \n",
" 0.0004 | \n",
"
\n",
" \n",
" | 2024-10-14 15:54:48-04:00 | \n",
" 0.0004 | \n",
"
\n",
" \n",
" | 2024-10-14 15:55:00-04:00 | \n",
" 0.0005 | \n",
"
\n",
" \n",
" | 2024-10-14 15:55:12-04:00 | \n",
" 0.0005 | \n",
"
\n",
" \n",
" | 2024-10-14 15:55:24-04:00 | \n",
" 0.0007 | \n",
"
\n",
" \n",
" | 2024-10-14 15:55:36-04:00 | \n",
" 0.0008 | \n",
"
\n",
" \n",
" | 2024-10-14 15:55:48-04:00 | \n",
" 0.0005 | \n",
"
\n",
" \n",
" | 2024-10-14 15:56:00-04:00 | \n",
" 0.0006 | \n",
"
\n",
" \n",
" | 2024-10-14 15:56:12-04:00 | \n",
" 0.0004 | \n",
"
\n",
" \n",
" | 2024-10-14 15:56:24-04:00 | \n",
" 0.0003 | \n",
"
\n",
" \n",
" | 2024-10-14 15:56:36-04:00 | \n",
" 0.0003 | \n",
"
\n",
" \n",
" | 2024-10-14 15:56:48-04:00 | \n",
" 0.0005 | \n",
"
\n",
" \n",
" | 2024-10-14 15:57:00-04:00 | \n",
" 0.0005 | \n",
"
\n",
" \n",
" | 2024-10-14 15:57:12-04:00 | \n",
" 0.0004 | \n",
"
\n",
" \n",
" | 2024-10-14 15:57:24-04:00 | \n",
" 0.0002 | \n",
"
\n",
" \n",
" | 2024-10-14 15:57:36-04:00 | \n",
" 0.0003 | \n",
"
\n",
" \n",
" | 2024-10-14 15:57:48-04:00 | \n",
" 0.0004 | \n",
"
\n",
" \n",
" | 2024-10-14 15:58:00-04:00 | \n",
" 0.0004 | \n",
"
\n",
" \n",
" | 2024-10-14 15:58:12-04:00 | \n",
" 0.0004 | \n",
"
\n",
" \n",
" | 2024-10-14 15:58:24-04:00 | \n",
" 0.0003 | \n",
"
\n",
" \n",
" | 2024-10-14 15:58:36-04:00 | \n",
" 0.0003 | \n",
"
\n",
" \n",
" | 2024-10-14 15:58:48-04:00 | \n",
" 0.0001 | \n",
"
\n",
" \n",
" | 2024-10-14 15:59:00-04:00 | \n",
" 0.0002 | \n",
"
\n",
" \n",
" | 2024-10-14 15:59:12-04:00 | \n",
" 0.0003 | \n",
"
\n",
" \n",
" | 2024-10-14 15:59:24-04:00 | \n",
" -0.0003 | \n",
"
\n",
" \n",
" | 2024-10-14 15:59:36-04:00 | \n",
" -0.0002 | \n",
"
\n",
" \n",
" | 2024-10-14 15:59:48-04:00 | \n",
" -0.0001 | \n",
"
\n",
" \n",
" | 2024-10-15 09:30:00-04:00 | \n",
" 0.0101 | \n",
"
\n",
" \n",
" | 2024-10-15 09:30:12-04:00 | \n",
" 0.0090 | \n",
"
\n",
" \n",
" | 2024-10-15 09:30:24-04:00 | \n",
" 0.0095 | \n",
"
\n",
" \n",
" | 2024-10-15 09:30:36-04:00 | \n",
" 0.0095 | \n",
"
\n",
" \n",
" | 2024-10-15 09:30:48-04:00 | \n",
" 0.0077 | \n",
"
\n",
" \n",
" | 2024-10-15 09:31:00-04:00 | \n",
" 0.0075 | \n",
"
\n",
" \n",
" | 2024-10-15 09:31:12-04:00 | \n",
" 0.0063 | \n",
"
\n",
" \n",
" | 2024-10-15 09:31:24-04:00 | \n",
" 0.0067 | \n",
"
\n",
" \n",
" | 2024-10-15 09:31:36-04:00 | \n",
" 0.0060 | \n",
"
\n",
" \n",
" | 2024-10-15 09:31:48-04:00 | \n",
" 0.0047 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
"symbol BAC\n",
"time \n",
"2024-10-14 15:50:00-04:00 -0.0000\n",
"2024-10-14 15:50:12-04:00 -0.0000\n",
"2024-10-14 15:50:24-04:00 -0.0000\n",
"2024-10-14 15:50:36-04:00 -0.0000\n",
"2024-10-14 15:50:48-04:00 -0.0000\n",
"2024-10-14 15:51:00-04:00 -0.0000\n",
"2024-10-14 15:51:12-04:00 -0.0002\n",
"2024-10-14 15:51:24-04:00 -0.0002\n",
"2024-10-14 15:51:36-04:00 -0.0002\n",
"2024-10-14 15:51:48-04:00 -0.0002\n",
"2024-10-14 15:52:00-04:00 0.0000\n",
"2024-10-14 15:52:12-04:00 0.0000\n",
"2024-10-14 15:52:24-04:00 0.0002\n",
"2024-10-14 15:52:36-04:00 0.0001\n",
"2024-10-14 15:52:48-04:00 0.0001\n",
"2024-10-14 15:53:00-04:00 -0.0001\n",
"2024-10-14 15:53:12-04:00 -0.0000\n",
"2024-10-14 15:53:24-04:00 0.0000\n",
"2024-10-14 15:53:36-04:00 0.0001\n",
"2024-10-14 15:53:48-04:00 0.0002\n",
"2024-10-14 15:54:00-04:00 0.0004\n",
"2024-10-14 15:54:12-04:00 0.0004\n",
"2024-10-14 15:54:24-04:00 0.0005\n",
"2024-10-14 15:54:36-04:00 0.0004\n",
"2024-10-14 15:54:48-04:00 0.0004\n",
"2024-10-14 15:55:00-04:00 0.0005\n",
"2024-10-14 15:55:12-04:00 0.0005\n",
"2024-10-14 15:55:24-04:00 0.0007\n",
"2024-10-14 15:55:36-04:00 0.0008\n",
"2024-10-14 15:55:48-04:00 0.0005\n",
"2024-10-14 15:56:00-04:00 0.0006\n",
"2024-10-14 15:56:12-04:00 0.0004\n",
"2024-10-14 15:56:24-04:00 0.0003\n",
"2024-10-14 15:56:36-04:00 0.0003\n",
"2024-10-14 15:56:48-04:00 0.0005\n",
"2024-10-14 15:57:00-04:00 0.0005\n",
"2024-10-14 15:57:12-04:00 0.0004\n",
"2024-10-14 15:57:24-04:00 0.0002\n",
"2024-10-14 15:57:36-04:00 0.0003\n",
"2024-10-14 15:57:48-04:00 0.0004\n",
"2024-10-14 15:58:00-04:00 0.0004\n",
"2024-10-14 15:58:12-04:00 0.0004\n",
"2024-10-14 15:58:24-04:00 0.0003\n",
"2024-10-14 15:58:36-04:00 0.0003\n",
"2024-10-14 15:58:48-04:00 0.0001\n",
"2024-10-14 15:59:00-04:00 0.0002\n",
"2024-10-14 15:59:12-04:00 0.0003\n",
"2024-10-14 15:59:24-04:00 -0.0003\n",
"2024-10-14 15:59:36-04:00 -0.0002\n",
"2024-10-14 15:59:48-04:00 -0.0001\n",
"2024-10-15 09:30:00-04:00 0.0101\n",
"2024-10-15 09:30:12-04:00 0.0090\n",
"2024-10-15 09:30:24-04:00 0.0095\n",
"2024-10-15 09:30:36-04:00 0.0095\n",
"2024-10-15 09:30:48-04:00 0.0077\n",
"2024-10-15 09:31:00-04:00 0.0075\n",
"2024-10-15 09:31:12-04:00 0.0063\n",
"2024-10-15 09:31:24-04:00 0.0067\n",
"2024-10-15 09:31:36-04:00 0.0060\n",
"2024-10-15 09:31:48-04:00 0.0047"
]
},
"execution_count": 119,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#s12_data.ohlcv.data[\"BAC\"].vbt.xloc[slice(\"2024-10-14 15:50:00\", \"2024-10-15 15:50:00\")].obj.head(10)\n",
"\n",
"a = div_vwap_cum.xloc[slice(\"2024-10-14 15:50:00\", \"2024-10-15 15:50:00\")].div.head(60)\n",
"a"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The autoreload extension is already loaded. To reload it, use:\n",
" %reload_ext autoreload\n"
]
},
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"%load_ext autoreload\n",
"%autoreload 2\n",
"from ttools.vbtindicators import register_custom_inds\n",
"register_custom_inds(None, \"override\")\n",
"#chopiness = vbt.indicator(\"technical:CHOPINESS\").run(s12_data.open, s12_data.high, s12_data.low, s12_data.close, s12_data.volume, window = 100)\n",
"#vwap_cum_roll = vbt.indicator(\"technical:ROLLING_VWAP\").run(s12_data.open, s12_data.high, s12_data.low, s12_data.close, s12_data.volume, window = 100, min_periods = 5)\n",
"\n",
"#note that VWAP is calculated from HLCC4 rounded to 3 decimals, can be changed by hlcc4_round parameter\n",
"vwap_cum_d = vbt.indicator(\"ttools:CUVWAP\").run(s12_data.high, s12_data.low, s12_data.close, s12_data.volume, anchor=vbt.Default(value=\"D\"), drag=vbt.Default(value=50), hide_default=True)\n",
"div_vwap_cum = vbt.indicator(\"ttools:DIVERGENCE\").run(s12_data.close, vwap_cum_d.vwap, divtype=vbt.Default(value=\"reln\"), hide_default=True)\n",
"div_vwap_lin_angle = vbt.indicator(\"talib:LINEARREG_ANGLE\").run(div_vwap_cum.div, timeperiod=2)\n",
"dvla = np.round(div_vwap_lin_angle.real,4)\n",
"\n",
"long_entries = tts.isrisingc(dvla,3).vbt & div_vwap_cum.div_below(0)\n",
"short_entries = tts.isfallingc(dvla,3).vbt & div_vwap_cum.div_above(0)\n",
"\n",
"#SIGNAL - make indicator with 1 - long, -1 - short, 0 nothing\n",
"long_series = long_entries.squeeze()\n",
"short_series = short_entries.squeeze()\n",
"signals = pd.Series(0, index=long_series.index) # Initialize with 0\n",
"signals[long_series] = 1 # Set 1 where entries are True\n",
"signals[short_series] = -1 # Set -1 where exits are True\n",
"\n",
"Panel(\n",
" ohlcv=(s12_data.ohlcv.data[\"BAC\"],), #[(long_entries.squeeze())],[(short_entries.squeeze())]),\n",
" right=[(vwap_cum_d.vwap, \"vwap_cum_daily\")], \n",
" middle1=[(div_vwap_lin_angle.real, \"div_vwap_angle\")],\n",
" middle2=[(signals, \"signals\")],\n",
" left=[(div_vwap_cum.div, \"div_vwap_cum\")]).chart(size=\"m\", xloc=slice(\"2024-10-14 15:00:00\", \"2024-10-15 16:00:00\"), precision=4)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#TED backtest, porovnat s v2realbot, delsi obdobi, tuning parametru a zpetne vyzkouseni s realbotem\n",
"#pripadne mrknout na markov variance switching zda nepujde na intraday"
]
},
{
"cell_type": "code",
"execution_count": 40,
"metadata": {},
"outputs": [],
"source": [
"from typing import Any\n",
"from ttools import create_mask_from_window\n",
"\n",
"#single for exits, as it is just EOD exits\n",
"exits = pd.DataFrame.vbt.signals.empty_like(long_entries) \n",
"entry_window_opens = 0 #in minutes from start of the market\n",
"entry_window_closes = 380\n",
"forced_exit_start = 382\n",
"forced_exit_end = 390\n",
"\n",
"#create mask based on main session that day\n",
"entry_window_opened = create_mask_from_window(long_entries, entry_window_opens, entry_window_closes, use_cal=False)\n",
"#limit entries to the window\n",
"long_entries_cln = long_entries.vbt & entry_window_opened\n",
"short_entries_cln = short_entries.vbt & entry_window_opened\n",
"\n",
"#create forced exits mask\n",
"forced_exits_window = create_mask_from_window(exits, forced_exit_start, forced_exit_end, use_cal=False)\n",
"\n",
"#add just forced EOD exits to exits, series is ok\n",
"long_exits = forced_exits_window\n",
"short_exits = forced_exits_window"
]
},
{
"cell_type": "code",
"execution_count": 38,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | linearreg_angle_timeperiod | \n",
" 2 | \n",
"
\n",
" \n",
" | symbol | \n",
" BAC | \n",
"
\n",
" \n",
" | time | \n",
" | \n",
"
\n",
" \n",
" \n",
" \n",
" | 2024-10-03 09:30:00-04:00 | \n",
" False | \n",
"
\n",
" \n",
" | 2024-10-03 09:30:12-04:00 | \n",
" False | \n",
"
\n",
" \n",
" | 2024-10-03 09:30:24-04:00 | \n",
" False | \n",
"
\n",
" \n",
" | 2024-10-03 09:30:36-04:00 | \n",
" False | \n",
"
\n",
" \n",
" | 2024-10-03 09:30:48-04:00 | \n",
" False | \n",
"
\n",
" \n",
" | ... | \n",
" ... | \n",
"
\n",
" \n",
" | 2024-10-16 15:59:00-04:00 | \n",
" False | \n",
"
\n",
" \n",
" | 2024-10-16 15:59:12-04:00 | \n",
" False | \n",
"
\n",
" \n",
" | 2024-10-16 15:59:24-04:00 | \n",
" False | \n",
"
\n",
" \n",
" | 2024-10-16 15:59:36-04:00 | \n",
" False | \n",
"
\n",
" \n",
" | 2024-10-16 15:59:48-04:00 | \n",
" False | \n",
"
\n",
" \n",
"
\n",
"
19393 rows × 1 columns
\n",
"
"
],
"text/plain": [
"linearreg_angle_timeperiod 2\n",
"symbol BAC\n",
"time \n",
"2024-10-03 09:30:00-04:00 False\n",
"2024-10-03 09:30:12-04:00 False\n",
"2024-10-03 09:30:24-04:00 False\n",
"2024-10-03 09:30:36-04:00 False\n",
"2024-10-03 09:30:48-04:00 False\n",
"... ...\n",
"2024-10-16 15:59:00-04:00 False\n",
"2024-10-16 15:59:12-04:00 False\n",
"2024-10-16 15:59:24-04:00 False\n",
"2024-10-16 15:59:36-04:00 False\n",
"2024-10-16 15:59:48-04:00 False\n",
"\n",
"[19393 rows x 1 columns]"
]
},
"execution_count": 38,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"long_exits\n"
]
},
{
"cell_type": "code",
"execution_count": 154,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"19393"
]
},
"execution_count": 154,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"s12_data.wrapper.shape[0]"
]
},
{
"cell_type": "code",
"execution_count": 155,
"metadata": {},
"outputs": [],
"source": [
"group_lens =s12_data.wrapper.get_index_grouper(\"D\").get_group_lens()"
]
},
{
"cell_type": "code",
"execution_count": 157,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([ 0, 1941, 3871, 5799, 7727, 9669, 11601, 13551, 15500,\n",
" 17448])"
]
},
"execution_count": 157,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"group_end_idxs = np.cumsum(group_lens)\n",
"# # group_start_idxs = group_end_idxs - group_lens"
]
},
{
"cell_type": "code",
"execution_count": 144,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"s12_data.ohlcv.data[\"BAC-LONG\"].lw.plot(right=[(vwap_cum, \"vwap\")])"
]
},
{
"cell_type": "code",
"execution_count": 61,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | symbol | \n",
" BAC | \n",
"
\n",
" \n",
" | time | \n",
" | \n",
"
\n",
" \n",
" \n",
" \n",
" | 2024-10-03 09:30:00-04:00 | \n",
" NaN | \n",
"
\n",
" \n",
" | 2024-10-03 09:30:12-04:00 | \n",
" NaN | \n",
"
\n",
" \n",
" | 2024-10-03 09:30:24-04:00 | \n",
" NaN | \n",
"
\n",
" \n",
" | 2024-10-03 09:30:36-04:00 | \n",
" NaN | \n",
"
\n",
" \n",
" | 2024-10-03 09:30:48-04:00 | \n",
" NaN | \n",
"
\n",
" \n",
" | ... | \n",
" ... | \n",
"
\n",
" \n",
" | 2024-10-16 15:59:00-04:00 | \n",
" 42.7735 | \n",
"
\n",
" \n",
" | 2024-10-16 15:59:12-04:00 | \n",
" 42.7720 | \n",
"
\n",
" \n",
" | 2024-10-16 15:59:24-04:00 | \n",
" 42.7715 | \n",
"
\n",
" \n",
" | 2024-10-16 15:59:36-04:00 | \n",
" 42.7730 | \n",
"
\n",
" \n",
" | 2024-10-16 15:59:48-04:00 | \n",
" 42.7770 | \n",
"
\n",
" \n",
"
\n",
"
19393 rows × 1 columns
\n",
"
"
],
"text/plain": [
"symbol BAC\n",
"time \n",
"2024-10-03 09:30:00-04:00 NaN\n",
"2024-10-03 09:30:12-04:00 NaN\n",
"2024-10-03 09:30:24-04:00 NaN\n",
"2024-10-03 09:30:36-04:00 NaN\n",
"2024-10-03 09:30:48-04:00 NaN\n",
"... ...\n",
"2024-10-16 15:59:00-04:00 42.7735\n",
"2024-10-16 15:59:12-04:00 42.7720\n",
"2024-10-16 15:59:24-04:00 42.7715\n",
"2024-10-16 15:59:36-04:00 42.7730\n",
"2024-10-16 15:59:48-04:00 42.7770\n",
"\n",
"[19393 rows x 1 columns]"
]
},
"execution_count": 61,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import talib\n",
"pd.set_option('display.max_rows', 500)\n",
"def apply_func_1d(close, high, timeperiod):\n",
" return talib.SMA(close.astype(np.double), timeperiod)\n",
"\n",
"SMA = vbt.IF(\n",
" input_names=['close', 'high'],\n",
" param_names=['timeperiod'],\n",
" output_names=['sma']\n",
").with_apply_func(\n",
" apply_func_1d,\n",
" takes_1d=True,\n",
" timeperiod=10, #single default\n",
" high=vbt.Ref('close')) #default from another input\n",
"\n",
"ind = SMA.run(s12_data.close)\n",
"ind.sma\n",
"#sma.sma"
]
},
{
"cell_type": "code",
"execution_count": 39,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"ROLLING_VWAP.run(\n",
" open,\n",
" high,\n",
" low,\n",
" close,\n",
" volume,\n",
" window=Default(value=200),\n",
" min_periods=Default(value=None),\n",
" short_name='rolling_vwap',\n",
" hide_params=None,\n",
" hide_default=True,\n",
" **kwargs\n",
"):\n",
" Run `ROLLING_VWAP` indicator.\n",
" \n",
" * Inputs: `open`, `high`, `low`, `close`, `volume`\n",
" * Parameters: `window`, `min_periods`\n",
" * Outputs: `rolling_vwap`\n",
" \n",
" Pass a list of parameter names as `hide_params` to hide their column levels, or True to hide all.\n",
" Set `hide_default` to False to show the column levels of the parameters with a default value.\n",
" \n",
" Other keyword arguments are passed to `ROLLING_VWAP.run_pipeline`.\n"
]
}
],
"source": [
"#vbt.IF.list_indicators(\"*vwap\")\n",
"vbt.phelp(vbt.indicator(\"technical:ROLLING_VWAP\").run)\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": ".venv",
"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.10.11"
}
},
"nbformat": 4,
"nbformat_minor": 2
}