{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Anchored VWAP\n" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "None\n", "Loaded env variables from file None\n" ] }, { "data": { "text/html": [ "
Activating profile profile1\n",
       "
\n" ], "text/plain": [ "Activating profile profile1\n" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "from dotenv import load_dotenv\n", "\n", "#as V2realbot is client , load env variables here\n", "env_file = \"/Users/davidbrazda/Documents/Development/python/.env\"\n", "# Load the .env file\n", "load_dotenv(env_file)\n", "\n", "from lightweight_charts import Panel, chart\n", "from v2realbot.utils.utils import zoneNY\n", "import pandas as pd\n", "import numpy as np\n", "import vectorbtpro as vbt\n", "# from itables import init_notebook_mode, show\n", "import datetime\n", "from itertools import product\n", "from v2realbot.config import DATA_DIR\n", "from lightweight_charts import JupyterChart, chart, Panel, PlotAccessor\n", "from IPython.display import display\n", "\n", "# init_notebook_mode(all_interactive=True)\n", "\n", "vbt.settings.set_theme(\"dark\")\n", "vbt.settings['plotting']['layout']['width'] = 1280\n", "vbt.settings.plotting.auto_rangebreaks = True\n", "# Set the option to display with pagination\n", "pd.set_option('display.notebook_repr_html', True)\n", "pd.set_option('display.max_rows', 10) # Number of rows per page" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "trades_df-BAC-2024-01-01T09_30_00-2024-05-14T16_00_00-CO4B7VPWUZF-100.parquet\n", "trades_df-BAC-2024-01-11T09:30:00-2024-01-12T16:00:00.parquet\n", "trades_df-SPY-2024-01-01T09:30:00-2024-05-14T16:00:00.parquet\n", "trades_df-BAC-2023-01-01T09_30_00-2024-05-25T16_00_00-47BCFOPUVWZ-100.parquet\n", "ohlcv_df-BAC-2024-01-11T09:30:00-2024-01-12T16:00:00.parquet\n", "trades_df-BAC-2024-05-15T09_30_00-2024-05-25T16_00_00-47BCFOPUVWZ-100.parquet\n", "ohlcv_df-BAC-2024-01-01T09_30_00-2024-05-25T16_00_00-47BCFOPUVWZ-100.parquet\n", "ohlcv_df-SPY-2024-01-01T09:30:00-2024-05-14T16:00:00.parquet\n", "ohlcv_df-BAC-2024-01-01T09_30_00-2024-05-14T16_00_00-CO4B7VPWUZF-100.parquet\n", "ohlcv_df-BAC-2023-01-01T09_30_00-2024-05-25T16_00_00-47BCFOPUVWZ-100.parquet\n", "ohlcv_df-BAC-2023-01-01T09_30_00-2024-05-25T15_30_00-47BCFOPUVWZ-100.parquet\n" ] }, { "data": { "text/plain": [ "5" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Define the market open and close times\n", "market_open = datetime.time(9, 30)\n", "market_close = datetime.time(16, 0)\n", "entry_window_opens = 1\n", "entry_window_closes = 370\n", "forced_exit_start = 380\n", "forced_exit_end = 390\n", "\n", "#LOAD FROM PARQUET\n", "#list all files is dir directory with parquet extension\n", "dir = DATA_DIR + \"/notebooks/\"\n", "import os\n", "files = [f for f in os.listdir(dir) if f.endswith(\".parquet\")]\n", "print('\\n'.join(map(str, files)))\n", "file_name = \"ohlcv_df-BAC-2023-01-01T09_30_00-2024-05-25T15_30_00-47BCFOPUVWZ-100.parquet\"\n", "ohlcv_df = pd.read_parquet(dir+file_name,engine='pyarrow')\n", "#filter ohlcv_df to certain date range (assuming datetime index)\n", "ohlcv_df = ohlcv_df.loc[\"2024-02-12 9:30\":\"2024-02-16 16:00\"]\n", "\n", "#add vwap column to ohlcv_df\n", "#ohlcv_df[\"hlcc4\"] = (ohlcv_df[\"close\"] + ohlcv_df[\"high\"] + ohlcv_df[\"low\"] + ohlcv_df[\"close\"]) / 4\n", "\n", "basic_data = vbt.Data.from_data(vbt.symbol_dict({\"BAC\": ohlcv_df}), tz_convert=zoneNY)\n", "ohlcv_df= None\n", "basic_data.wrapper.index.normalize().nunique()" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "DatetimeIndex: 57966 entries, 2024-02-12 09:30:00-05:00 to 2024-02-16 15:59:59-05:00\n", "Data columns (total 10 columns):\n", " # Column Non-Null Count Dtype \n", "--- ------ -------------- ----- \n", " 0 open 57966 non-null float64 \n", " 1 high 57966 non-null float64 \n", " 2 low 57966 non-null float64 \n", " 3 close 57966 non-null float64 \n", " 4 volume 57966 non-null float64 \n", " 5 trades 57966 non-null float64 \n", " 6 updated 57966 non-null datetime64[ns, US/Eastern]\n", " 7 vwap 57966 non-null float64 \n", " 8 buyvolume 57966 non-null float64 \n", " 9 sellvolume 57966 non-null float64 \n", "dtypes: datetime64[ns, US/Eastern](1), float64(9)\n", "memory usage: 4.9 MB\n" ] } ], "source": [ "basic_data.data[\"BAC\"].info()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Add resample function to custom columns" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "from vectorbtpro.utils.config import merge_dicts, Config, HybridConfig\n", "from vectorbtpro import _typing as tp\n", "from vectorbtpro.generic import nb as generic_nb\n", "\n", "_feature_config: tp.ClassVar[Config] = HybridConfig(\n", " {\n", " \"buyvolume\": dict(\n", " resample_func=lambda self, obj, resampler: obj.vbt.resample_apply(\n", " resampler,\n", " generic_nb.sum_reduce_nb,\n", " )\n", " ),\n", " \"sellvolume\": dict(\n", " resample_func=lambda self, obj, resampler: obj.vbt.resample_apply(\n", " resampler,\n", " generic_nb.sum_reduce_nb,\n", " )\n", " ),\n", " \"trades\": dict(\n", " resample_func=lambda self, obj, resampler: obj.vbt.resample_apply(\n", " resampler,\n", " generic_nb.sum_reduce_nb,\n", " )\n", " )\n", " }\n", ")\n", "\n", "basic_data._feature_config = _feature_config" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "s1data = basic_data[['open', 'high', 'low', 'close', 'volume','vwap','buyvolume','trades','sellvolume']]\n", "\n", "s5data = s1data.resample(\"5s\")\n", "s5data = s5data.transform(lambda df: df.between_time('09:30', '16:00').dropna())\n", "\n", "t1data = basic_data[['open', 'high', 'low', 'close', 'volume','vwap','buyvolume','trades','sellvolume']].resample(\"1T\")\n", "t1data = t1data.transform(lambda df: df.between_time('09:30', '16:00').dropna())\n", "# t1data.data[\"BAC\"].info()\n", "\n", "t30data = basic_data[['open', 'high', 'low', 'close', 'volume','vwap','buyvolume','trades','sellvolume']].resample(\"30T\")\n", "t30data = t30data.transform(lambda df: df.between_time('09:30', '16:00').dropna())\n", "# t30data.data[\"BAC\"].info()\n", "\n", "s1close = s1data.close\n", "t1close = t1data.close\n", "t30close = t30data.close\n", "t30volume = t30data.volume" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
openhighlowclosevolumevwapbuyvolumetradessellvolume
time
2024-02-12 09:30:00-05:0033.00033.0033.0033.000800.033.0000000.02.00.0
2024-02-12 09:30:01-05:0033.02033.0533.0033.010267365.033.020039265765.025.01000.0
2024-02-12 09:30:02-05:0033.00033.0233.0033.0151100.033.009545500.09.0600.0
2024-02-12 09:30:03-05:0033.00533.0832.9933.0807508.033.0278981970.032.03638.0
2024-02-12 09:30:05-05:0033.06033.0633.0633.060500.033.0600000.05.0100.0
\n", "
" ], "text/plain": [ " open high low close volume vwap \\\n", "time \n", "2024-02-12 09:30:00-05:00 33.000 33.00 33.00 33.000 800.0 33.000000 \n", "2024-02-12 09:30:01-05:00 33.020 33.05 33.00 33.010 267365.0 33.020039 \n", "2024-02-12 09:30:02-05:00 33.000 33.02 33.00 33.015 1100.0 33.009545 \n", "2024-02-12 09:30:03-05:00 33.005 33.08 32.99 33.080 7508.0 33.027898 \n", "2024-02-12 09:30:05-05:00 33.060 33.06 33.06 33.060 500.0 33.060000 \n", "\n", " buyvolume trades sellvolume \n", "time \n", "2024-02-12 09:30:00-05:00 0.0 2.0 0.0 \n", "2024-02-12 09:30:01-05:00 265765.0 25.0 1000.0 \n", "2024-02-12 09:30:02-05:00 500.0 9.0 600.0 \n", "2024-02-12 09:30:03-05:00 1970.0 32.0 3638.0 \n", "2024-02-12 09:30:05-05:00 0.0 5.0 100.0 " ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s1data.data[\"BAC\"].head()" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "#resample on specific index \n", "resampler = vbt.Resampler(t30data.index, s1data.index, source_freq=\"30T\", target_freq=\"1s\")\n", "t30close_realigned = t30close.vbt.realign_closing(resampler)\n", "\n", "#resample 1min to s\n", "resampler_s = vbt.Resampler(t1data.index, s1data.index, source_freq=\"1T\", target_freq=\"1s\")\n", "t1close_realigned = t1close.vbt.realign_closing(resampler_s)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "VWAP.run(\n", " high,\n", " low,\n", " close,\n", " volume,\n", " anchor=Default(value='D'),\n", " short_name='vwap',\n", " hide_params=None,\n", " hide_default=True,\n", " **kwargs\n", "):\n", " Run `VWAP` indicator.\n", " \n", " * Inputs: `high`, `low`, `close`, `volume`\n", " * Parameters: `anchor`\n", " * Outputs: `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 `VWAP.run_pipeline`.\n" ] } ], "source": [ "vbt.IF.list_indicators(\"*vwap\")\n", "vbt.phelp(vbt.VWAP.run)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# VWAP" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "\n", "t1vwap_h = vbt.VWAP.run(t1data.high, t1data.low, t1data.close, t1data.volume, anchor=\"H\")\n", "t1vwap_d = vbt.VWAP.run(t1data.high, t1data.low, t1data.close, t1data.volume, anchor=\"D\")\n", "t1vwap_t = vbt.VWAP.run(t1data.high, t1data.low, t1data.close, t1data.volume, anchor=\"T\")\n", "\n", "t1vwap_h_real = t1vwap_h.vwap.vbt.realign_closing(resampler_s)\n", "t1vwap_d_real = t1vwap_d.vwap.vbt.realign_closing(resampler_s)\n", "t1vwap_t_real = t1vwap_t.vwap.vbt.realign_closing(resampler_s)\n", "\n", "#t1vwap_5t.xloc[\"2024-01-3 09:30:00\":\"2024-01-03 16:00:00\"].plot()" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "\n", "#m30data.close.lw.plot()\n", "#quick few liner\n", "pane1 = Panel(\n", " histogram=[\n", " #(s1data.volume, \"volume\",None, 0.8),\n", " #(m30volume, \"m30volume\",None, 1)\n", " ], # [(series, name, \"rgba(53, 94, 59, 0.6)\", opacity)]\n", " right=[\n", " (s1data.close, \"1s close\"),\n", " (t1data.close, \"1min close\"),\n", " (t1vwap_t, \"1mvwap_t\"),\n", " (t1vwap_h, \"1mvwap_h\"),\n", " (t1vwap_d, \"1mvwap_d\"),\n", " (t1vwap_t_real, \"1mvwap_t_real\"),\n", " (t1vwap_h_real, \"1mvwap_h_real\"),\n", " (t1vwap_d_real, \"1mvwap_d_real\")\n", " # (t1close_realigned, \"1min close realigned\"),\n", " # (m30data.close, \"30min-close\"),\n", " # (m30close_realigned, \"30min close realigned\"),\n", " ],\n", ")\n", "ch = chart([pane1], size=\"s\" ) #xloc=slice(\"2024-05-1 09:30:00\",\"2024-05-25 16:00:00\"))" ] } ], "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 }