diff --git a/requirements.txt b/requirements.txt index 6c82214..417580f 100644 --- a/requirements.txt +++ b/requirements.txt @@ -84,7 +84,7 @@ jupyterlab_server==2.27.1 jupyterlab_widgets==3.0.13 kiwisolver==1.4.5 korean-lunar-calendar==0.3.1 -lightweight_charts @ git+https://github.com/drew2323/lightweight-charts-python.git@35f029714b23c18abe791b90a85447e959c72258 +lightweight_charts @ git+https://github.com/drew2323/lightweight-charts-python.git@7986aa9195d9d3204a998d1a8f5778d95219a08e llvmlite==0.39.1 lxml==5.3.0 markdown-it-py==3.0.0 @@ -121,9 +121,11 @@ platformdirs==4.2.2 plotly==5.24.0 prometheus_client==0.21.0 prompt_toolkit==3.0.47 +protobuf==5.28.2 proxy-tools==0.1.0 psutil==6.0.0 psycopg2==2.9.9 +psycopg2-binary==2.9.9 ptyprocess==0.7.0 pure_eval==0.2.3 pyarrow==11.0.0 @@ -147,6 +149,7 @@ PyWavelets==1.7.0 pywebview==5.2 PyYAML==6.0.2 pyzmq==26.2.0 +ray==2.37.0 referencing==0.35.1 regex==2024.7.24 requests==2.32.3 @@ -166,6 +169,7 @@ soupsieve==2.6 SQLAlchemy==2.0.32 sseclient-py==1.8.0 stack-data==0.6.3 +stratlab_db @ git+https://gitea.stratlab.dev/Stratlab/db.git@0bbe486de7ac410a9375f2ccf7d557a658a662ea stumpy==1.13.0 TA-Lib==0.4.32 tenacity==9.0.0 @@ -193,7 +197,7 @@ webencodings==0.5.1 websocket-client==1.8.0 websockets==11.0.3 Werkzeug==3.0.4 -widgetsnbextension==4.0.9 +widgetsnbextension==4.0.13 yarl==1.13.1 yfinance==0.2.43 zipp==3.20.1 diff --git a/research/get_trades_at_once.ipynb b/research/get_trades_at_once.ipynb index 220dddd..3361393 100644 --- a/research/get_trades_at_once.ipynb +++ b/research/get_trades_at_once.ipynb @@ -13,9 +13,38 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "metadata": {}, - "outputs": [], + "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-2023-01-01T09:30:00-2024-10-02T16:00:00-['4', '7', 'B', 'C', 'F', 'O', 'P', 'U', 'V', 'W', 'Z']-100.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": [ + "['4', '7', 'B', 'C', 'F', 'O', 'P', 'U', 'V', 'W', 'Z']" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "import pandas as pd\n", "import numpy as np\n", @@ -45,10 +74,10 @@ "exclude_conditions = cfh.config_handler.get_val('AGG_EXCLUDED_TRADES') #standard ['C','O','4','B','7','V','P','W','U','Z','F']\n", "minsize = 100\n", "\n", - "symbol = \"SPY\"\n", + "symbol = \"BAC\"\n", "#datetime in zoneNY \n", - "day_start = datetime(2024, 1, 1, 9, 30, 0)\n", - "day_stop = datetime(2024, 1, 14, 16, 00, 0)\n", + "day_start = datetime(2023, 1, 1, 9, 30, 0)\n", + "day_stop = datetime(2024, 10, 2, 16, 00, 0)\n", "day_start = zoneNY.localize(day_start)\n", "day_stop = zoneNY.localize(day_stop)\n", "#filename of trades_df parquet, date are in isoformat but without time zone part\n", @@ -56,13 +85,15 @@ "#parquet interval cache contains exclude conditions and minsize filtering\n", "file_trades = dir + f\"trades_df-{symbol}-{day_start.strftime('%Y-%m-%dT%H:%M:%S')}-{day_stop.strftime('%Y-%m-%dT%H:%M:%S')}-{exclude_conditions}-{minsize}.parquet\"\n", "#file_trades = dir + f\"trades_df-{symbol}-{day_start.strftime('%Y-%m-%dT%H:%M:%S')}-{day_stop.strftime('%Y-%m-%dT%H:%M:%S')}.parquet\"\n", - "file_ohlcv = dir + f\"ohlcv_df-{symbol}-{day_start.strftime('%Y-%m-%dT%H:%M:%S')}-{day_stop.strftime('%Y-%m-%dT%H:%M:%S')}-{exclude_conditions}-{minsize}.parquet\"\n", + "file_ohlcv = dir + f\"ohlcv_df-{symbol}-{day_start.strftime('%Y-%m-%dT%H:%M:%S')}-{day_stop.strftime('%Y-%m-%dT%H:%M:%S')}-{str(exclude_conditions)}-{minsize}.parquet\"\n", "\n", "#PRINT all parquet in directory\n", "import os\n", "files = [f for f in os.listdir(dir) if f.endswith(\".parquet\")]\n", "for f in files:\n", - " print(f)" + " print(f)\n", + "\n", + "exclude_conditions" ] }, { @@ -71,13 +102,26 @@ "metadata": {}, "outputs": [], "source": [ - "trades_df = fetch_daily_stock_trades(symbol, day_start, day_stop, exclude_conditions=exclude_conditions, minsize=minsize, force_remote=False, max_retries=5, backoff_factor=1)\n", - "trades_df" + "#fetch trades in one go\n", + "#trades_df = fetch_daily_stock_trades(symbol, day_start, day_stop, exclude_conditions=exclude_conditions, minsize=minsize, force_remote=False, max_retries=5, backoff_factor=1)\n", + "#fetch trades in parallel - for longer intervals\n", + "#trades_df = fetch_trades_parallel(symbol, day_start, day_stop, exclude_conditions=exclude_conditions, minsize=minsize, force_remote=False, max_workers=None)\n", + " \n", + "##trades_df.info()" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, + "metadata": {}, + "outputs": [], + "source": [ + "#trades_df.to_parquet(file_trades, engine='pyarrow', compression='gzip')" + ] + }, + { + "cell_type": "code", + "execution_count": 3, "metadata": {}, "outputs": [], "source": [ diff --git a/research/indicators/eval_robustness.ipynb b/research/indicators/eval_robustness.ipynb new file mode 100644 index 0000000..0106213 --- /dev/null +++ b/research/indicators/eval_robustness.ipynb @@ -0,0 +1,1662 @@ +{ + "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 +} diff --git a/research/strat_SUPERTREND/SUPERTREND_v1_SINGLE.ipynb b/research/strat_SUPERTREND/SUPERTREND_v1_SINGLE.ipynb index c8f901c..1e9b749 100644 --- a/research/strat_SUPERTREND/SUPERTREND_v1_SINGLE.ipynb +++ b/research/strat_SUPERTREND/SUPERTREND_v1_SINGLE.ipynb @@ -11,7 +11,7 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 2, "metadata": {}, "outputs": [ { @@ -68,7 +68,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 3, "metadata": {}, "outputs": [ { @@ -80,7 +80,9 @@ "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-2023-01-01T09:30:00-2024-10-02T16:00:00-['4', '7', 'B', 'C', 'F', 'O', 'P', 'U', 'V', 'W', 'Z']-100.parquet\n", "trades_df-BAC-2024-05-15T09_30_00-2024-05-25T16_00_00-47BCFOPUVWZ-100.parquet\n", + "ohlcv_df-BAC-2023-01-01T09:30:00-2024-10-02T16:00:00-['4', '7', 'B', 'C', 'F', 'O', 'P', 'U', 'V', 'W', 'Z']-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", @@ -94,7 +96,7 @@ "5" ] }, - "execution_count": 2, + "execution_count": 3, "metadata": {}, "output_type": "execute_result" } @@ -129,36 +131,22 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 1, "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" + "ename": "NameError", + "evalue": "name 'basic_data' is not defined", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[1], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43mbasic_data\u001b[49m\u001b[38;5;241m.\u001b[39mdata[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mBAC\u001b[39m\u001b[38;5;124m\"\u001b[39m]\u001b[38;5;241m.\u001b[39minfo()\n\u001b[1;32m 3\u001b[0m df \u001b[38;5;241m=\u001b[39m basic_data\u001b[38;5;241m.\u001b[39mdata[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mBAC\u001b[39m\u001b[38;5;124m\"\u001b[39m]\n\u001b[1;32m 5\u001b[0m nan_rows \u001b[38;5;241m=\u001b[39m df[df\u001b[38;5;241m.\u001b[39misna()\u001b[38;5;241m.\u001b[39many(axis\u001b[38;5;241m=\u001b[39m\u001b[38;5;241m1\u001b[39m)]\n", + "\u001b[0;31mNameError\u001b[0m: name 'basic_data' is not defined" ] } ], - "source": [ - "basic_data.data[\"BAC\"].info()" - ] + "source": [] }, { "cell_type": "markdown", diff --git a/research/tests/database_saver.ipynb b/research/tests/database_saver.ipynb new file mode 100644 index 0000000..09548ad --- /dev/null +++ b/research/tests/database_saver.ipynb @@ -0,0 +1,1840 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# How to save aggregated data to remote database\n" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "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": 20, + "metadata": {}, + "outputs": [ + { + "data": { + "application/vnd.jupyter.widget-view+json": { + "model_id": "ac5230e6571e4664ad606c118fa0e9f5", + "version_major": 2, + "version_minor": 0 + }, + "text/plain": [ + "100%|##########| 1/1 [00:10<00:00, 10.52s/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", + "data = con.pull(symbols=[SYMBOL], schema=SCHEMA,start=\"2024-08-01\", end=\"2024-09-05\", tz_convert='America/New_York')\n", + "\n", + "data.data[SYMBOL].info()\n", + "\n", + "#1month 1s data - 15s - 24MB\n" + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "data.xloc[\"2024-08-05\":\"2024-08-10\"].data[SYMBOL].close.lw.plot()" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "data": { + "application/vnd.jupyter.widget-view+json": { + "model_id": "55ebcdb7940b4645a61fe3aa2e2604e4", + "version_major": 2, + "version_minor": 0 + }, + "text/plain": [ + "100%|##########| 1/1 [00:10<00:00, 10.14s/it, symbol=BTC/USD]" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "#fetching CRYPTO\n", + "from lib.db import Connection\n", + "SYMBOL = \"BTC/USD\"\n", + "con = Connection(db_name=\"market_data\", default_schema=\"krakenfutures\", create_db=True)\n", + "data = con.pull(symbols=[SYMBOL], start=\"2024-01-01\", end=\"2025-01-01\")" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "ename": "NameError", + "evalue": "name 'data' is not defined", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[2], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43mdata\u001b[49m\u001b[38;5;241m.\u001b[39mdata[SYMBOL]\u001b[38;5;241m.\u001b[39mtail(\u001b[38;5;241m10\u001b[39m)\n", + "\u001b[0;31mNameError\u001b[0m: name 'data' is not defined" + ] + } + ], + "source": [ + "data.data[SYMBOL].tail(10)" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "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-2023-01-01T09:30:00-2024-10-02T16:00:00-['4', '7', 'B', 'C', 'F', 'O', 'P', 'U', 'V', 'W', 'Z']-100.parquet\n", + "trades_df-BAC-2024-05-15T09_30_00-2024-05-25T16_00_00-47BCFOPUVWZ-100.parquet\n", + "ohlcv_df-BAC-2023-01-01T09:30:00-2024-10-02T16:00:00-['4', '7', 'B', 'C', 'F', 'O', 'P', 'U', 'V', 'W', 'Z']-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": [ + "440" + ] + }, + "execution_count": 5, + "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 local PARQUET cache\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", + "file_name = \"ohlcv_df-BAC-2023-01-01T09:30:00-2024-10-02T16:00:00-['4', '7', 'B', 'C', 'F', 'O', 'P', 'U', 'V', 'W', 'Z']-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()\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "DatetimeIndex: 1000000 entries, 2023-01-03 09:30:01-05:00 to 2023-04-17 11:05:36-04:00\n", + "Data columns (total 10 columns):\n", + " # Column Non-Null Count Dtype \n", + "--- ------ -------------- ----- \n", + " 0 open 1000000 non-null float64 \n", + " 1 high 1000000 non-null float64 \n", + " 2 low 1000000 non-null float64 \n", + " 3 close 1000000 non-null float64 \n", + " 4 volume 1000000 non-null float64 \n", + " 5 trades 1000000 non-null float64 \n", + " 6 updated 1000000 non-null datetime64[ns, US/Eastern]\n", + " 7 vwap 1000000 non-null float64 \n", + " 8 buyvolume 1000000 non-null float64 \n", + " 9 sellvolume 1000000 non-null float64 \n", + "dtypes: datetime64[ns, US/Eastern](1), float64(9)\n", + "memory usage: 83.9 MB\n" + ] + } + ], + "source": [ + "#basic_data.data[\"BAC\"][0:10].info()\n", + "#transform basic data to first 100rows\n", + "bd = basic_data.transform(lambda df: df[:1000000])\n", + "\n", + "bd.data[\"BAC\"].info()\n" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Empty DataFrame\n", + "Columns: [open, high, low, close, volume, trades, updated, vwap, buyvolume, sellvolume]\n", + "Index: []\n", + "Empty DataFrame\n", + "Columns: [open, high, low, close, volume, trades, updated, vwap, buyvolume, sellvolume]\n", + "Index: []\n" + ] + } + ], + "source": [ + "df = basic_data.data[\"BAC\"]\n", + "\n", + "nan_rows = df[df.isna().any(axis=1)]\n", + "print(nan_rows)\n", + "\n", + "inf_rows = df[np.isinf(df).any(axis=1)]\n", + "print(inf_rows)" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [], + "source": [ + "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", + "try:\n", + " con = Connection(db_name=DB, default_schema=SCHEMA, create_db=True)\n", + " con.force_rollback_all() # Force cleanup before attempting to save\n", + " con.save(basic_data,1000)\n", + "except Exception as e:\n", + " print(f\"Error occurred: {str(e)}\")\n", + "\n", + "#63sec to insert remotely 5mio records\n" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "DatetimeIndex: 4549772 entries, 2023-01-03 09:30:01-05:00 to 2024-05-24 15:59:59-04:00\n", + "Data columns (total 10 columns):\n", + " # Column Dtype \n", + "--- ------ ----- \n", + " 0 open float64 \n", + " 1 high float64 \n", + " 2 low float64 \n", + " 3 close float64 \n", + " 4 volume float64 \n", + " 5 trades float64 \n", + " 6 updated datetime64[ns, US/Eastern]\n", + " 7 vwap float64 \n", + " 8 buyvolume float64 \n", + " 9 sellvolume float64 \n", + "dtypes: datetime64[ns, US/Eastern](1), float64(9)\n", + "memory usage: 381.8 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(\"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", + "t30close = t30data.close\n", + "t30volume = t30data.volume" + ] + }, + { + "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 +} diff --git a/research/tests/ray_test.ipynb b/research/tests/ray_test.ipynb new file mode 100644 index 0000000..d79e1c8 --- /dev/null +++ b/research/tests/ray_test.ipynb @@ -0,0 +1,126 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "2024-10-03 09:43:41,741\tINFO worker.py:1786 -- Started a local Ray instance.\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "waiting for ray\n", + "ray returned all\n", + "ray finsihed 0.4031927839969285\n", + "worker 0 started\n", + "worker 0 finsihed\n", + "worker 1 started\n", + "worker 1 finsihed\n", + "worker 2 started\n", + "worker 2 finsihed\n", + "worker 3 started\n", + "worker 3 finsihed\n", + "serial function finsihed 0.21200023603159934\n", + "Ray with 4 parts: 0.4031927839969285 seconds\n", + "Serial: 0.21200023603159934 seconds\n", + "Serial computation is faster than Ray with 4 parts\n" + ] + } + ], + "source": [ + "import numpy as np\n", + "import timeit\n", + "import ray\n", + "\n", + "# Define the expensive function\n", + "@ray.remote\n", + "def expensive_function(n):\n", + " # Generate a large random matrix\n", + " A = np.random.rand(1000, 1000)\n", + " B = np.random.rand(1000, 1000)\n", + "\n", + " # Perform the matrix multiplication\n", + " C = np.dot(A, B)\n", + " # Return the result\n", + " return C\n", + "\n", + "def expensive_function_serial(n):\n", + " print(f\"worker {n} started\")\n", + " # Generate a large random matrix\n", + " A = np.random.rand(1000, 1000)\n", + " B = np.random.rand(1000, 1000)\n", + "\n", + " # Perform the matrix multiplication\n", + " C = np.dot(A, B)\n", + "\n", + " # Return the result\n", + " print(f\"worker {n} finsihed\")\n", + " return C\n", + "\n", + "# Initialize Ray\n", + "ray.init()\n", + "\n", + "# Create 4 remote actors to distribute the work\n", + "futures = [expensive_function.remote(_) for _ in range(4)]\n", + "\n", + "# Time the function using Ray with 4 parts\n", + "start_time = timeit.default_timer()\n", + "print(\"waiting for ray\")\n", + "results = ray.get(futures)\n", + "print(\"ray returned all\")\n", + "end_time = timeit.default_timer()\n", + "ray_time = end_time - start_time\n", + "print(\"ray finsihed\", ray_time)\n", + "\n", + "# Time the function serially\n", + "start_time = timeit.default_timer()\n", + "results = [expensive_function_serial(_) for _ in range(4)]\n", + "end_time = timeit.default_timer()\n", + "serial_time = end_time - start_time\n", + "print(\"serial function finsihed\", serial_time)\n", + "\n", + "\n", + "# Print the results\n", + "print(f\"Ray with 4 parts: {ray_time} seconds\")\n", + "print(f\"Serial: {serial_time} seconds\")\n", + "\n", + "# Compare the results\n", + "if ray_time < serial_time:\n", + " print(\"Ray with 4 parts is faster than serial computation\")\n", + "else:\n", + " print(\"Serial computation is faster than Ray with 4 parts\")\n", + "\n", + "# Shutdown Ray\n", + "ray.shutdown()\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 +}