{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Robustness evaluation\n" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "from lightweight_charts import Panel, chart, PlotAccessor\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 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": 5, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "06ca3871e4c446568100a2be701f5542", "version_major": 2, "version_minor": 0 }, "text/plain": [ "100%|##########| 1/1 [00:10<00:00, 10.97s/it, symbol=BAC]" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "\n", "DatetimeIndex: 288034 entries, 2024-08-01 09:30:00-04:00 to 2024-09-04 15:59:59-04:00\n", "Data columns (total 10 columns):\n", " # Column Non-Null Count Dtype \n", "--- ------ -------------- ----- \n", " 0 open 288034 non-null float64 \n", " 1 high 288034 non-null float64 \n", " 2 low 288034 non-null float64 \n", " 3 close 288034 non-null float64 \n", " 4 volume 288034 non-null float64 \n", " 5 trades 288034 non-null float64 \n", " 6 updated 288034 non-null datetime64[ns, UTC]\n", " 7 vwap 288034 non-null float64 \n", " 8 buyvolume 288034 non-null float64 \n", " 9 sellvolume 288034 non-null float64 \n", "dtypes: datetime64[ns, UTC](1), float64(9)\n", "memory usage: 24.2 MB\n" ] } ], "source": [ "#fetching US-STOCKS ohlcv_1s\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-08-01\", end=\"2024-09-05\", tz_convert='America/New_York')\n", "\n", "basic_data.data[SYMBOL].info()\n", "\n", "#1month 1s data - 15s - 24MB\n" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "basic_data.xloc[\"2024-08-05\":\"2024-08-10\"].data[SYMBOL].close.lw.plot()" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "24" ] }, "execution_count": 7, "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", "#NUMDAYS\n", "basic_data.wrapper.index.normalize().nunique()\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Add resample function to custom columns" ] }, { "cell_type": "code", "execution_count": 8, "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": 21, "metadata": {}, "outputs": [ { "ename": "ValueError", "evalue": "No indices could be matched", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn[21], line 17\u001b[0m\n\u001b[1;32m 14\u001b[0m s1close \u001b[38;5;241m=\u001b[39m s1data\u001b[38;5;241m.\u001b[39mclose\n\u001b[1;32m 15\u001b[0m t1close \u001b[38;5;241m=\u001b[39m t1data\u001b[38;5;241m.\u001b[39mclose\n\u001b[0;32m---> 17\u001b[0m \u001b[43mt1data\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\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m]\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mclose\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mlw\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mplot\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n", "File \u001b[0;32m~/Documents/Development/python/strategy-lab1/.venv/lib/python3.10/site-packages/lightweight_charts/helpers.py:26\u001b[0m, in \u001b[0;36mPlotAccessor.plot\u001b[0;34m(self, **kwargs)\u001b[0m\n\u001b[1;32m 22\u001b[0m kwargs[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124msize\u001b[39m\u001b[38;5;124m\"\u001b[39m] \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mxs\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 23\u001b[0m pane1 \u001b[38;5;241m=\u001b[39m Panel(\n\u001b[1;32m 24\u001b[0m right\u001b[38;5;241m=\u001b[39m[(\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_obj, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mline\u001b[39m\u001b[38;5;124m\"\u001b[39m)],\n\u001b[1;32m 25\u001b[0m )\n\u001b[0;32m---> 26\u001b[0m ch \u001b[38;5;241m=\u001b[39m \u001b[43mchart\u001b[49m\u001b[43m(\u001b[49m\u001b[43m[\u001b[49m\u001b[43mpane1\u001b[49m\u001b[43m]\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n", "File \u001b[0;32m~/Documents/Development/python/strategy-lab1/.venv/lib/python3.10/site-packages/lightweight_charts/helpers.py:300\u001b[0m, in \u001b[0;36mchart\u001b[0;34m(panes, sync, title, size, xloc, session, precision)\u001b[0m\n\u001b[1;32m 298\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m session \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m \u001b[38;5;129;01mand\u001b[39;00m session:\n\u001b[1;32m 299\u001b[0m last_used_series \u001b[38;5;241m=\u001b[39m output_series \u001b[38;5;28;01mif\u001b[39;00m is_vbt_indicator(series) \u001b[38;5;28;01melse\u001b[39;00m series \u001b[38;5;66;03m#pokud byl posledni series vbt, pak pouzijeme jeho outputy\u001b[39;00m\n\u001b[0;32m--> 300\u001b[0m active_chart\u001b[38;5;241m.\u001b[39mvertical_span(start_time\u001b[38;5;241m=\u001b[39m\u001b[43mxloc_me\u001b[49m\u001b[43m(\u001b[49m\u001b[43mlast_used_series\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mxloc\u001b[49m\u001b[43m)\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mvbt\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mxloc\u001b[49m\u001b[43m[\u001b[49m\u001b[43msession\u001b[49m\u001b[43m]\u001b[49m\u001b[38;5;241m.\u001b[39mobj\u001b[38;5;241m.\u001b[39mindex\u001b[38;5;241m.\u001b[39mto_list(), color\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mrgba(252, 255, 187, 0.42)\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m 302\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m main_title_set:\n\u001b[1;32m 303\u001b[0m chartX\u001b[38;5;241m.\u001b[39mtopbar\u001b[38;5;241m.\u001b[39mtextbox(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mtitle\u001b[39m\u001b[38;5;124m\"\u001b[39m,title)\n", "File \u001b[0;32m~/Documents/Development/python/strategy-lab1/.venv/lib/python3.10/site-packages/vectorbtpro/base/indexing.py:128\u001b[0m, in \u001b[0;36mpdLoc.__getitem__\u001b[0;34m(self, key)\u001b[0m\n\u001b[1;32m 127\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21m__getitem__\u001b[39m(\u001b[38;5;28mself\u001b[39m, key: tp\u001b[38;5;241m.\u001b[39mAny) \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m>\u001b[39m tp\u001b[38;5;241m.\u001b[39mAny:\n\u001b[0;32m--> 128\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mindexing_func\u001b[49m\u001b[43m(\u001b[49m\u001b[43mpartial\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mpd_indexing_func\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mkey\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mkey\u001b[49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mindexing_kwargs\u001b[49m\u001b[43m)\u001b[49m\n", "File \u001b[0;32m~/Documents/Development/python/strategy-lab1/.venv/lib/python3.10/site-packages/vectorbtpro/base/accessors.py:749\u001b[0m, in \u001b[0;36mBaseAccessor.indexing_func\u001b[0;34m(self, wrapper_meta, *args, **kwargs)\u001b[0m\n\u001b[1;32m 747\u001b[0m \u001b[38;5;250m\u001b[39m\u001b[38;5;124;03m\"\"\"Perform indexing on `BaseAccessor`.\"\"\"\u001b[39;00m\n\u001b[1;32m 748\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m wrapper_meta \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[0;32m--> 749\u001b[0m wrapper_meta \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mwrapper\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mindexing_func_meta\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43margs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 750\u001b[0m new_obj \u001b[38;5;241m=\u001b[39m ArrayWrapper\u001b[38;5;241m.\u001b[39mselect_from_flex_array(\n\u001b[1;32m 751\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_obj,\n\u001b[1;32m 752\u001b[0m row_idxs\u001b[38;5;241m=\u001b[39mwrapper_meta[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mrow_idxs\u001b[39m\u001b[38;5;124m\"\u001b[39m],\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 755\u001b[0m columns_changed\u001b[38;5;241m=\u001b[39mwrapper_meta[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mcolumns_changed\u001b[39m\u001b[38;5;124m\"\u001b[39m],\n\u001b[1;32m 756\u001b[0m )\n\u001b[1;32m 757\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m checks\u001b[38;5;241m.\u001b[39mis_series(new_obj):\n", "File \u001b[0;32m~/Documents/Development/python/strategy-lab1/.venv/lib/python3.10/site-packages/vectorbtpro/base/wrapping.py:613\u001b[0m, in \u001b[0;36mArrayWrapper.indexing_func_meta\u001b[0;34m(self, pd_indexing_func, index, columns, column_only_select, range_only_select, group_select, return_slices, return_none_slices, return_scalars, group_by, wrapper_kwargs)\u001b[0m\n\u001b[1;32m 611\u001b[0m init_row_mapper_values \u001b[38;5;241m=\u001b[39m reshaping\u001b[38;5;241m.\u001b[39mbroadcast_array_to(np\u001b[38;5;241m.\u001b[39marange(n_rows)[:, \u001b[38;5;28;01mNone\u001b[39;00m], (n_rows, n_cols))\n\u001b[1;32m 612\u001b[0m init_row_mapper \u001b[38;5;241m=\u001b[39m i_wrapper\u001b[38;5;241m.\u001b[39mwrap(init_row_mapper_values, index\u001b[38;5;241m=\u001b[39mindex, columns\u001b[38;5;241m=\u001b[39mcolumns)\n\u001b[0;32m--> 613\u001b[0m row_mapper \u001b[38;5;241m=\u001b[39m \u001b[43mpd_indexing_func\u001b[49m\u001b[43m(\u001b[49m\u001b[43minit_row_mapper\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 614\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m i_wrapper\u001b[38;5;241m.\u001b[39mndim \u001b[38;5;241m==\u001b[39m \u001b[38;5;241m1\u001b[39m:\n\u001b[1;32m 615\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m checks\u001b[38;5;241m.\u001b[39mis_series(row_mapper):\n", "File \u001b[0;32m~/Documents/Development/python/strategy-lab1/.venv/lib/python3.10/site-packages/vectorbtpro/base/indexing.py:260\u001b[0m, in \u001b[0;36mxLoc.pd_indexing_func\u001b[0;34m(cls, obj, key)\u001b[0m\n\u001b[1;32m 258\u001b[0m columns \u001b[38;5;241m=\u001b[39m get_index(obj, \u001b[38;5;241m1\u001b[39m)\n\u001b[1;32m 259\u001b[0m freq \u001b[38;5;241m=\u001b[39m dt\u001b[38;5;241m.\u001b[39minfer_index_freq(index)\n\u001b[0;32m--> 260\u001b[0m row_idxs, col_idxs \u001b[38;5;241m=\u001b[39m \u001b[43mget_idxs\u001b[49m\u001b[43m(\u001b[49m\u001b[43mkey\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mindex\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mindex\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mcolumns\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mcolumns\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mfreq\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mfreq\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 261\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(row_idxs, np\u001b[38;5;241m.\u001b[39mndarray) \u001b[38;5;129;01mand\u001b[39;00m row_idxs\u001b[38;5;241m.\u001b[39mndim \u001b[38;5;241m==\u001b[39m \u001b[38;5;241m2\u001b[39m:\n\u001b[1;32m 262\u001b[0m row_idxs \u001b[38;5;241m=\u001b[39m normalize_idxs(row_idxs, target_len\u001b[38;5;241m=\u001b[39m\u001b[38;5;28mlen\u001b[39m(index))\n", "File \u001b[0;32m~/Documents/Development/python/strategy-lab1/.venv/lib/python3.10/site-packages/vectorbtpro/base/indexing.py:2238\u001b[0m, in \u001b[0;36mget_idxs\u001b[0;34m(idxr, index, columns, freq, template_context, **kwargs)\u001b[0m\n\u001b[1;32m 2236\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(idxr, Idxr):\n\u001b[1;32m 2237\u001b[0m idxr \u001b[38;5;241m=\u001b[39m Idxr(idxr, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs)\n\u001b[0;32m-> 2238\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43midxr\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mget\u001b[49m\u001b[43m(\u001b[49m\u001b[43mindex\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mindex\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mcolumns\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mcolumns\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mfreq\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mfreq\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mtemplate_context\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mtemplate_context\u001b[49m\u001b[43m)\u001b[49m\n", "File \u001b[0;32m~/Documents/Development/python/strategy-lab1/.venv/lib/python3.10/site-packages/vectorbtpro/base/indexing.py:2214\u001b[0m, in \u001b[0;36mIdxr.get\u001b[0;34m(self, index, columns, freq, template_context)\u001b[0m\n\u001b[1;32m 2212\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mTypeError\u001b[39;00m(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mIndexer \u001b[39m\u001b[38;5;132;01m{\u001b[39;00m\u001b[38;5;28mtype\u001b[39m(row_idxr)\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m not supported as a row indexer\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m 2213\u001b[0m row_idxr \u001b[38;5;241m=\u001b[39m RowIdxr(row_idxr, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39m\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39midxr_kwargs)\n\u001b[0;32m-> 2214\u001b[0m row_idxs \u001b[38;5;241m=\u001b[39m \u001b[43mrow_idxr\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mget\u001b[49m\u001b[43m(\u001b[49m\u001b[43mindex\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mindex\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mfreq\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mfreq\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mtemplate_context\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mtemplate_context\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 2215\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(col_idxr, ColIdxr):\n\u001b[1;32m 2216\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(col_idxr, (RowIdxr, Idxr)):\n", "File \u001b[0;32m~/Documents/Development/python/strategy-lab1/.venv/lib/python3.10/site-packages/vectorbtpro/base/indexing.py:2119\u001b[0m, in \u001b[0;36mRowIdxr.get\u001b[0;34m(self, index, freq, template_context)\u001b[0m\n\u001b[1;32m 2117\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mTypeError\u001b[39;00m(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mIndexer of \u001b[39m\u001b[38;5;132;01m{\u001b[39;00m\u001b[38;5;28mtype\u001b[39m(\u001b[38;5;28mself\u001b[39m)\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m must be an instance of UniIdxr\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m 2118\u001b[0m idxr \u001b[38;5;241m=\u001b[39m AutoIdxr(idxr, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39m\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39midxr_kwargs)\n\u001b[0;32m-> 2119\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43midxr\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mget\u001b[49m\u001b[43m(\u001b[49m\u001b[43mindex\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mindex\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mfreq\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mfreq\u001b[49m\u001b[43m)\u001b[49m\n", "File \u001b[0;32m~/Documents/Development/python/strategy-lab1/.venv/lib/python3.10/site-packages/vectorbtpro/base/indexing.py:2087\u001b[0m, in \u001b[0;36mAutoIdxr.get\u001b[0;34m(self, index, freq)\u001b[0m\n\u001b[1;32m 2085\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[1;32m 2086\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mInvalid option kind=\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mkind\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m-> 2087\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43midx\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mget\u001b[49m\u001b[43m(\u001b[49m\u001b[43mindex\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mindex\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mfreq\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mfreq\u001b[49m\u001b[43m)\u001b[49m\n", "File \u001b[0;32m~/Documents/Development/python/strategy-lab1/.venv/lib/python3.10/site-packages/vectorbtpro/base/indexing.py:985\u001b[0m, in \u001b[0;36mDTCIdxr.get\u001b[0;34m(self, index, freq)\u001b[0m\n\u001b[1;32m 983\u001b[0m func \u001b[38;5;241m=\u001b[39m jit_reg\u001b[38;5;241m.\u001b[39mresolve_option(dt_nb\u001b[38;5;241m.\u001b[39mindex_matches_dtc_nb, \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mjitted)\n\u001b[1;32m 984\u001b[0m mask \u001b[38;5;241m=\u001b[39m func(ns_index, dtc)\n\u001b[0;32m--> 985\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mMaskIdxr\u001b[49m\u001b[43m(\u001b[49m\u001b[43mmask\u001b[49m\u001b[43m)\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mget\u001b[49m\u001b[43m(\u001b[49m\u001b[43mindex\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mindex\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mfreq\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mfreq\u001b[49m\u001b[43m)\u001b[49m\n", "File \u001b[0;32m~/Documents/Development/python/strategy-lab1/.venv/lib/python3.10/site-packages/vectorbtpro/base/indexing.py:784\u001b[0m, in \u001b[0;36mMaskIdxr.get\u001b[0;34m(self, index, freq)\u001b[0m\n\u001b[1;32m 782\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mslice\u001b[39m(\u001b[38;5;28;01mNone\u001b[39;00m, \u001b[38;5;28;01mNone\u001b[39;00m, \u001b[38;5;28;01mNone\u001b[39;00m)\n\u001b[1;32m 783\u001b[0m idxs \u001b[38;5;241m=\u001b[39m np\u001b[38;5;241m.\u001b[39mflatnonzero(\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mvalue)\n\u001b[0;32m--> 784\u001b[0m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mcheck_idxs\u001b[49m\u001b[43m(\u001b[49m\u001b[43midxs\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 785\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m idxs\n", "File \u001b[0;32m~/Documents/Development/python/strategy-lab1/.venv/lib/python3.10/site-packages/vectorbtpro/base/indexing.py:581\u001b[0m, in \u001b[0;36mIdxrBase.check_idxs\u001b[0;34m(self, idxs, check_minus_one)\u001b[0m\n\u001b[1;32m 579\u001b[0m \u001b[38;5;28;01melif\u001b[39;00m checks\u001b[38;5;241m.\u001b[39mis_sequence(idxs) \u001b[38;5;129;01mand\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m np\u001b[38;5;241m.\u001b[39misscalar(idxs):\n\u001b[1;32m 580\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mlen\u001b[39m(idxs) \u001b[38;5;241m==\u001b[39m \u001b[38;5;241m0\u001b[39m:\n\u001b[0;32m--> 581\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mNo indices could be matched\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m 582\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(idxs, np\u001b[38;5;241m.\u001b[39mndarray):\n\u001b[1;32m 583\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mIndices must be a NumPy array, not \u001b[39m\u001b[38;5;132;01m{\u001b[39;00m\u001b[38;5;28mtype\u001b[39m(idxs)\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m\"\u001b[39m)\n", "\u001b[0;31mValueError\u001b[0m: No indices could be matched" ] } ], "source": [ "s1data = basic_data[['open', 'high', 'low', 'close', 'volume','vwap','buyvolume','trades','sellvolume']]\n", "\n", "# s5data = s1data.resample(\"12s\")\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", "\n", "t1data.data[\"BAC\"].close.lw.plot()\n" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "from lightweight_charts import JupyterChart, chart, Panel, PlotAccessor\n", "s5data.close.lw.plot()\n", "\n", "# pane1 = Panel(\n", "# ohlcv=(s5data.ohlcv.get(),))\n", "\n", "# # Create the chart with the panel\n", "# ch = chart([pane1], title=\"Chart\", sync=True, session=None, size=\"s\")" ] }, { "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 }