diff --git a/requirements.txt b/requirements.txt index 83f7e7a..6c82214 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,8 @@ -alpaca-py==0.30.1 +aiodns==3.2.0 +aiohappyeyeballs==2.4.3 +aiohttp==3.10.8 +aiosignal==1.3.1 +alpaca-py==0.18.1 annotated-types==0.7.0 anyio==4.6.0 appdirs==1.4.4 @@ -8,18 +12,21 @@ argon2-cffi-bindings==21.2.0 arrow==1.3.0 asttokens==2.4.1 async-lru==2.0.4 +async-timeout==4.0.3 attrs==24.2.0 babel==2.16.0 beautifulsoup4==4.12.3 bleach==6.1.0 blinker==1.8.2 bottle==0.12.25 +ccxt==4.4.10 certifi==2024.8.30 cffi==1.17.1 charset-normalizer==3.3.2 click==8.1.7 comm==0.2.2 contourpy==1.3.0 +cryptography==43.0.1 cycler==0.12.1 dash==2.17.1 dash-bootstrap-components==1.6.0 @@ -40,6 +47,7 @@ Flask==3.0.3 fonttools==4.53.1 fqdn==1.5.1 frozendict==2.4.4 +frozenlist==1.4.1 greenlet==3.0.3 h11==0.14.0 html5lib==1.1 @@ -53,8 +61,10 @@ ipykernel==6.29.5 ipython==8.27.0 ipywidgets==8.1.5 isoduration==20.11.0 -itables==2.2.1 +itables==2.0.1 itsdangerous==2.2.0 +jax==0.4.23 +jaxlib==0.4.23 jedi==0.19.1 Jinja2==3.1.4 joblib==1.4.2 @@ -64,18 +74,18 @@ jsonschema==4.23.0 jsonschema-specifications==2023.12.1 jupyter-events==0.10.0 jupyter-lsp==2.2.5 -jupyter_client==8.6.2 +jupyter_client==8.6.1 jupyter_core==5.7.2 -jupyter_server==2.14.2 +jupyter_server==2.14.0 jupyter_server_terminals==0.5.3 -jupyterlab==4.2.5 +jupyterlab==4.1.8 jupyterlab_pygments==0.3.0 -jupyterlab_server==2.27.3 +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@2b9f238a4242d958bc863b6209bf6444786477c5 -llvmlite==0.43.0 +lightweight_charts @ git+https://github.com/drew2323/lightweight-charts-python.git@35f029714b23c18abe791b90a85447e959c72258 +llvmlite==0.39.1 lxml==5.3.0 markdown-it-py==3.0.0 MarkupSafe==2.1.5 @@ -83,7 +93,9 @@ matplotlib==3.9.2 matplotlib-inline==0.1.7 mdurl==0.1.2 mistune==3.0.2 +ml_dtypes==0.5.0 msgpack==1.0.8 +multidict==6.1.0 multitasking==0.0.11 mypy-extensions==1.0.0 nbclient==0.10.0 @@ -92,12 +104,13 @@ nbformat==5.10.4 nest-asyncio==1.6.0 notebook==7.2.2 notebook_shim==0.2.4 -numba==0.60.0 +numba==0.56.4 numpy==1.23.5 +opt_einsum==3.4.0 orjson==3.10.7 overrides==7.7.0 packaging==24.1 -pandas==2.2.2 +pandas==2.2.1 pandas_market_calendars==4.4.1 pandocfilters==1.5.1 parso==0.8.4 @@ -110,9 +123,11 @@ prometheus_client==0.21.0 prompt_toolkit==3.0.47 proxy-tools==0.1.0 psutil==6.0.0 +psycopg2==2.9.9 ptyprocess==0.7.0 pure_eval==0.2.3 -pyarrow==17.0.0 +pyarrow==11.0.0 +pycares==4.4.0 pycparser==2.22 pydantic==2.8.2 pydantic_core==2.20.1 @@ -142,7 +157,7 @@ rich==13.8.0 rpds-py==0.20.0 schedule==1.2.2 scikit-learn==1.5.1 -scipy==1.14.1 +scipy==1.11.2 seaborn==0.13.2 Send2Trash==1.8.3 six==1.16.0 @@ -164,20 +179,21 @@ tornado==6.4.1 tqdm==4.66.5 traitlets==5.14.3 tulipy==0.4.0 -types-python-dateutil==2.9.0.20240906 -typing_extensions==4.12.2 +types-python-dateutil==2.9.0.20240316 +typing_extensions==4.9.0 tzdata==2024.1 tzlocal==5.2 uri-template==1.3.0 urllib3==2.2.2 v2realbot @ git+https://github.com/drew2323/v2trading.git@700461033f2b816eeb1ee3a14b930f181cf57308 -vectorbtpro @ file:///Users/davidbrazda/Desktop/vectorbtpro-2024.2.22-py3-none-any.whl#sha256=d85431edc7c9df69886ad0b6ee2b32d3aaa40cdc0b456f1c8fa6aa26f01d2be3 +vectorbtpro @ file:///Users/davidbrazda/Downloads/vectorbt.pro-develop wcwidth==0.2.13 webcolors==24.8.0 webencodings==0.5.1 websocket-client==1.8.0 -websockets==13.0.1 +websockets==11.0.3 Werkzeug==3.0.4 -widgetsnbextension==4.0.13 +widgetsnbextension==4.0.9 +yarl==1.13.1 yfinance==0.2.43 zipp==3.20.1 diff --git a/research/dbinsertdata.py b/research/dbinsertdata.py new file mode 100644 index 0000000..0ea9073 --- /dev/null +++ b/research/dbinsertdata.py @@ -0,0 +1,74 @@ +"""Saves data from Kraken Futures, combining BTC and USD settled markets""" + +import logging +from logging.config import fileConfig + +from vectorbtpro import pd, vbt + +from ext_lib.db import db_connect +from ext_lib.util import find_earliest_date + +EXCHANGE = "1s_OHLCV" +SYMBOLS = ("BTC/USD:BTC", "BTC/USD:USD") +RESOLUTION = "1s" +DB_ENGINE = db_connect("ohlcv_1m") +DB_SYMBOL = "BTC/USD" + +#fileConfig("logging.ini", disable_existing_loggers=False) + +logging.basicConfig( + level=logging.INFO, + format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", + handlers=[logging.StreamHandler()] +) + +log = logging.getLogger("main") + +vbt.CCXTData.set_custom_settings(exchange=EXCHANGE, timeframe=RESOLUTION, limit=6000000) +vbt.SQLData.set_engine_settings(engine_name="pg", engine=DB_ENGINE, schema=EXCHANGE, populate_=True, chunksize=1000) +vbt.SQLData.set_custom_settings(engine_name="pg", schema=EXCHANGE) + + +def main(): + if vbt.SQLData.has_table(DB_SYMBOL, schema=EXCHANGE): + data = vbt.SQLData.has_table(DB_SYMBOL, schema=EXCHANGE) + + + + vbt.SQLData.create_schema(EXCHANGE) + db_last_tstamp = None + # TODO: figure out if it's possible to avoid using tables directly, but rather symbols + if vbt.SQLData.has_table(DB_SYMBOL, schema=EXCHANGE): + db_last_tstamp = vbt.SQLData.get_last_row_number(DB_SYMBOL, row_number_column="Open time") + + dfs = [] + for symbol in SYMBOLS: + if db_last_tstamp is None: + start = find_earliest_date(symbol, EXCHANGE) + else: + start = db_last_tstamp + pd.Timedelta(RESOLUTION) + log.info("Start date for %s is %s", symbol, start) + + # Get data + df = vbt.CCXTData.pull(symbol, exchange=EXCHANGE, timeframe=RESOLUTION, start=start).get() + # If symbol is BTC/USD:BTC, convert volume from USD to BTC using the close price + if symbol == "BTC/USD:BTC": + df["Volume"] = df["Volume"].div(df["Close"]) + + dfs.append(df) + + # Combine data from two symbols (last row is incomplete so removed) + concatenated_data = pd.concat(dfs, axis=0) + final_data = ( + concatenated_data.groupby(concatenated_data.index) + .agg({"Open": "mean", "High": "mean", "Low": "mean", "Close": "mean", "Volume": "sum"}) + .iloc[:-1] + ) + data = vbt.SQLData.from_data({DB_SYMBOL: final_data}) + # TODO: use custom method to prevent duplicate timestamps + log.info("Saving to DB") + vbt.SQLDataSaver(data).save_data(method="multi") + + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/research/ext_lib/__init__.py b/research/ext_lib/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/research/ext_lib/db.py b/research/ext_lib/db.py new file mode 100644 index 0000000..a75b28e --- /dev/null +++ b/research/ext_lib/db.py @@ -0,0 +1,39 @@ +"""Database-related stuff""" + +import sqlalchemy +from sqlalchemy import create_engine, text + +DB_USER = "postgres" +DB_PW = "KzsZ0wz5tp2wUIPM" +DB_HOST = "db.stratlab.dev" +DB_PORT = 30543 +DB_URL = f"postgresql://{DB_USER}:{DB_PW}@{DB_HOST}:{DB_PORT}" +print(DB_URL) +#DB_ARGS = {"sslmode": "verify-full", "sslrootcert": "system"} +DB_ARGS = {} + +def db_connect(db_name: str) -> sqlalchemy.engine.Engine: + """Connect to DB. Create it if it doesn't exist + Args: + db_name: name of the database to create + """ + try: + engine = create_engine(f"{DB_URL}/{db_name}", connect_args=DB_ARGS) + engine.connect() + return engine + except sqlalchemy.exc.OperationalError: + # Database doesn't exist, create it + conn = create_engine(f"{DB_URL}/postgres", isolation_level="AUTOCOMMIT", connect_args=DB_ARGS).connect() + # TODO: figure out how to get rid of SQL injection. Standard parameterization adds quotes that breaks syntax + conn.execute(text(f"CREATE DATABASE {db_name}")) + + return create_engine(f"{DB_URL}/{db_name}", connect_args=DB_ARGS) + + +# list exchanges +# list symbols +# first date +# last date + +# get data +# save data diff --git a/research/ext_lib/util.py b/research/ext_lib/util.py new file mode 100644 index 0000000..12cda40 --- /dev/null +++ b/research/ext_lib/util.py @@ -0,0 +1,49 @@ +import logging +from datetime import datetime, timedelta + +from ccxt.base.errors import ExchangeNotAvailable +from vectorbtpro import pd, tp, vbt + +log = logging.getLogger(__name__) + + +def find_earliest_date(symbol: str, exchange: str, **kwargs) -> tp.Optional[pd.Timestamp]: + """Wrapper around CCXTData.find_earliest_date to handle ExchangeNotAvailable error with binary search + Args: + symbol: The trading symbol to query + exchange: The exchange to query + **kwargs: Additional arguments to pass to the find_earliest_date method + + Returns: + tp.Optional[pd.Timestamp]: The earliest available date if found, otherwise None + """ + log.info("Searching for earliest date for %s", symbol) + start_date = pd.Timestamp(kwargs.pop("start", datetime(2010, 1, 1))).floor("D") + end_date = pd.Timestamp(kwargs.pop("end", datetime.now())).floor("D") + + while start_date < end_date: + log.info("Trying %s to %s range", start_date, end_date) + mid_date = (start_date + (end_date - start_date) // 2).floor("D") + try: + found_date = vbt.CCXTData.find_earliest_date( + symbol, exchange=exchange, start=mid_date, end=end_date, limit=10, **kwargs + ) + if found_date: + # Move the end date to mid_date to search the earlier half + end_date = mid_date + else: + # Move the start date to mid_date + 1 to search the later half + start_date = mid_date + timedelta(days=1) + except ExchangeNotAvailable: + # Move the start date to mid_date + 1 to search the later half + start_date = mid_date + timedelta(days=1) + + # After the loop, start_date should be the earliest date with data + try: + found_date = vbt.CCXTData.find_earliest_date( + symbol, exchange=exchange, start=start_date, end=end_date, **kwargs + ) + return found_date + except ExchangeNotAvailable as e: + log.error("ExchangeNotAvailable error encountered at final step... Error: %s", e) + return None diff --git a/research/indicators/anchored_vwap.ipynb b/research/indicators/anchored_vwap.ipynb new file mode 100644 index 0000000..9e981d8 --- /dev/null +++ b/research/indicators/anchored_vwap.ipynb @@ -0,0 +1,958 @@ +{ + "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 +} diff --git a/research/strat_CANDLEGAPS/CANDLEGAPS_v1_SINGLE.ipynb b/research/strat_CANDLEGAPS/CANDLEGAPS_v1_SINGLE.ipynb index c4f1b53..ddfa0d8 100644 --- a/research/strat_CANDLEGAPS/CANDLEGAPS_v1_SINGLE.ipynb +++ b/research/strat_CANDLEGAPS/CANDLEGAPS_v1_SINGLE.ipynb @@ -16,7 +16,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 19, "metadata": {}, "outputs": [], "source": [ @@ -50,9 +50,37 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 20, "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-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": 20, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Define the market open and close times\n", "market_open = datetime.time(9, 30)\n", @@ -71,7 +99,7 @@ "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-14 16:00\"]\n", + "ohlcv_df = ohlcv_df.loc[\"2024-02-12 9:30\":\"2024-02-19 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", @@ -83,9 +111,54 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 21, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "5" + ] + }, + "execution_count": 21, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "\n", + "basic_data.wrapper.index.normalize().nunique()" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "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[us, 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[us, US/Eastern](1), float64(9)\n", + "memory usage: 4.9 MB\n" + ] + } + ], "source": [ "basic_data.data[\"BAC\"].info()" ] @@ -99,7 +172,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 17, "metadata": {}, "outputs": [], "source": [ @@ -135,9 +208,25 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 18, "metadata": {}, - "outputs": [], + "outputs": [ + { + "ename": "TypingError", + "evalue": "Failed in nopython mode pipeline (step: nopython frontend)\nNo implementation of function Function() found for signature:\n \n >>> _realign_nb(array(float64, 2d, C), array(datetime64[us], 1d, C), array(datetime64[us], 1d, C), timedelta64[ns], timedelta64[ns], bool, bool, float64, bool)\n \nThere are 2 candidate implementations:\n - Of which 1 did not match due to:\n Overload in function '_realign_nb': File: vectorbtpro/generic/nb/base.py: Line 1673.\n With argument(s): '(array(float64, 2d, C), array(datetime64[us], 1d, C), array(datetime64[us], 1d, C), timedelta64[ns], timedelta64[ns], bool, bool, float64, bool)':\n Rejected as the implementation raised a specific error:\n TypingError: Failed in nopython mode pipeline (step: nopython frontend)\n Failed in nopython mode pipeline (step: nopython frontend)\n No implementation of function Function() found for signature:\n \n >>> _realign_1d_nb(array(float64, 1d, A), array(datetime64[us], 1d, C), array(datetime64[us], 1d, C), timedelta64[ns], timedelta64[ns], bool, bool, float64, bool)\n \n There are 2 candidate implementations:\n - Of which 2 did not match due to:\n Overload in function '_realign_1d_nb': File: vectorbtpro/generic/nb/base.py: Line 1509.\n With argument(s): '(array(float64, 1d, A), array(datetime64[us], 1d, C), array(datetime64[us], 1d, C), timedelta64[ns], timedelta64[ns], bool, bool, float64, bool)':\n Rejected as the implementation raised a specific error:\n TypingError: Failed in nopython mode pipeline (step: nopython frontend)\n Cannot unify datetime64[ns] and datetime64[us] for 'source_val.4', defined at /Users/davidbrazda/Documents/Development/python/strategy-lab1/.venv/lib/python3.10/site-packages/vectorbtpro/generic/nb/base.py (1577)\n \n File \"../../.venv/lib/python3.10/site-packages/vectorbtpro/generic/nb/base.py\", line 1577:\n def impl(\n \n target_val = target_index[i] + target_freq\n if source_val > target_val:\n ^\n \n During: typing of assignment at /Users/davidbrazda/Documents/Development/python/strategy-lab1/.venv/lib/python3.10/site-packages/vectorbtpro/generic/nb/base.py (1577)\n \n File \"../../.venv/lib/python3.10/site-packages/vectorbtpro/generic/nb/base.py\", line 1577:\n def impl(\n \n target_val = target_index[i] + target_freq\n if source_val > target_val:\n ^\n \n raised from /Users/davidbrazda/Documents/Development/python/strategy-lab1/.venv/lib/python3.10/site-packages/numba/core/typeinfer.py:1091\n \n During: resolving callee type: Function()\n During: typing of call at /Users/davidbrazda/Documents/Development/python/strategy-lab1/.venv/lib/python3.10/site-packages/vectorbtpro/generic/nb/base.py (1660)\n \n \n File \"../../.venv/lib/python3.10/site-packages/vectorbtpro/generic/nb/base.py\", line 1660:\n def realign_1d_nb(\n \n If `arr` contains bar data, both indexes must represent the opening time.\"\"\"\n return _realign_1d_nb(\n ^\n \n During: resolving callee type: type(CPUDispatcher())\n During: typing of call at /Users/davidbrazda/Documents/Development/python/strategy-lab1/.venv/lib/python3.10/site-packages/vectorbtpro/generic/nb/base.py (1706)\n \n During: resolving callee type: type(CPUDispatcher())\n During: typing of call at /Users/davidbrazda/Documents/Development/python/strategy-lab1/.venv/lib/python3.10/site-packages/vectorbtpro/generic/nb/base.py (1706)\n \n During: resolving callee type: type(CPUDispatcher())\n During: typing of call at /Users/davidbrazda/Documents/Development/python/strategy-lab1/.venv/lib/python3.10/site-packages/vectorbtpro/generic/nb/base.py (1706)\n \n During: resolving callee type: type(CPUDispatcher())\n During: typing of call at /Users/davidbrazda/Documents/Development/python/strategy-lab1/.venv/lib/python3.10/site-packages/vectorbtpro/generic/nb/base.py (1706)\n \n During: resolving callee type: type(CPUDispatcher())\n During: typing of call at /Users/davidbrazda/Documents/Development/python/strategy-lab1/.venv/lib/python3.10/site-packages/vectorbtpro/generic/nb/base.py (1706)\n \n During: resolving callee type: type(CPUDispatcher())\n During: typing of call at /Users/davidbrazda/Documents/Development/python/strategy-lab1/.venv/lib/python3.10/site-packages/vectorbtpro/generic/nb/base.py (1706)\n \n \n File \"../../.venv/lib/python3.10/site-packages/vectorbtpro/generic/nb/base.py\", line 1706:\n def impl(\n \n for col in prange(arr.shape[1]):\n out[:, col] = realign_1d_nb(\n ^\n\n raised from /Users/davidbrazda/Documents/Development/python/strategy-lab1/.venv/lib/python3.10/site-packages/numba/core/typeinfer.py:1091\n - Of which 1 did not match due to:\n Overload in function '_realign_nb': File: vectorbtpro/generic/nb/base.py: Line 1673.\n With argument(s): '(array(float64, 2d, C), array(datetime64[us], 1d, C), array(datetime64[us], 1d, C), timedelta64[ns], timedelta64[ns], bool, bool, float64, bool)':\n Rejected as the implementation raised a specific error:\n TypingError: Failed in nopython mode pipeline (step: nopython frontend)\n Failed in nopython mode pipeline (step: nopython frontend)\n No implementation of function Function() found for signature:\n \n >>> _realign_1d_nb(array(float64, 1d, A), array(datetime64[us], 1d, C), array(datetime64[us], 1d, C), timedelta64[ns], timedelta64[ns], bool, bool, float64, bool)\n \n There are 2 candidate implementations:\n - Of which 2 did not match due to:\n Overload in function '_realign_1d_nb': File: vectorbtpro/generic/nb/base.py: Line 1509.\n With argument(s): '(array(float64, 1d, A), array(datetime64[us], 1d, C), array(datetime64[us], 1d, C), timedelta64[ns], timedelta64[ns], bool, bool, float64, bool)':\n Rejected as the implementation raised a specific error:\n TypingError: Failed in nopython mode pipeline (step: nopython frontend)\n Cannot unify datetime64[ns] and datetime64[us] for 'source_val.4', defined at /Users/davidbrazda/Documents/Development/python/strategy-lab1/.venv/lib/python3.10/site-packages/vectorbtpro/generic/nb/base.py (1577)\n \n File \"../../.venv/lib/python3.10/site-packages/vectorbtpro/generic/nb/base.py\", line 1577:\n def impl(\n \n target_val = target_index[i] + target_freq\n if source_val > target_val:\n ^\n \n During: typing of assignment at /Users/davidbrazda/Documents/Development/python/strategy-lab1/.venv/lib/python3.10/site-packages/vectorbtpro/generic/nb/base.py (1577)\n \n File \"../../.venv/lib/python3.10/site-packages/vectorbtpro/generic/nb/base.py\", line 1577:\n def impl(\n \n target_val = target_index[i] + target_freq\n if source_val > target_val:\n ^\n \n raised from /Users/davidbrazda/Documents/Development/python/strategy-lab1/.venv/lib/python3.10/site-packages/numba/core/typeinfer.py:1091\n \n During: resolving callee type: Function()\n During: typing of call at /Users/davidbrazda/Documents/Development/python/strategy-lab1/.venv/lib/python3.10/site-packages/vectorbtpro/generic/nb/base.py (1660)\n \n \n File \"../../.venv/lib/python3.10/site-packages/vectorbtpro/generic/nb/base.py\", line 1660:\n def realign_1d_nb(\n \n If `arr` contains bar data, both indexes must represent the opening time.\"\"\"\n return _realign_1d_nb(\n ^\n \n During: resolving callee type: type(CPUDispatcher())\n During: typing of call at /Users/davidbrazda/Documents/Development/python/strategy-lab1/.venv/lib/python3.10/site-packages/vectorbtpro/generic/nb/base.py (1706)\n \n During: resolving callee type: type(CPUDispatcher())\n During: typing of call at /Users/davidbrazda/Documents/Development/python/strategy-lab1/.venv/lib/python3.10/site-packages/vectorbtpro/generic/nb/base.py (1706)\n \n During: resolving callee type: type(CPUDispatcher())\n During: typing of call at /Users/davidbrazda/Documents/Development/python/strategy-lab1/.venv/lib/python3.10/site-packages/vectorbtpro/generic/nb/base.py (1706)\n \n During: resolving callee type: type(CPUDispatcher())\n During: typing of call at /Users/davidbrazda/Documents/Development/python/strategy-lab1/.venv/lib/python3.10/site-packages/vectorbtpro/generic/nb/base.py (1706)\n \n During: resolving callee type: type(CPUDispatcher())\n During: typing of call at /Users/davidbrazda/Documents/Development/python/strategy-lab1/.venv/lib/python3.10/site-packages/vectorbtpro/generic/nb/base.py (1706)\n \n During: resolving callee type: type(CPUDispatcher())\n During: typing of call at /Users/davidbrazda/Documents/Development/python/strategy-lab1/.venv/lib/python3.10/site-packages/vectorbtpro/generic/nb/base.py (1706)\n \n During: resolving callee type: type(CPUDispatcher())\n During: typing of call at /Users/davidbrazda/Documents/Development/python/strategy-lab1/.venv/lib/python3.10/site-packages/vectorbtpro/generic/nb/base.py (1706)\n \n During: resolving callee type: type(CPUDispatcher())\n During: typing of call at /Users/davidbrazda/Documents/Development/python/strategy-lab1/.venv/lib/python3.10/site-packages/vectorbtpro/generic/nb/base.py (1706)\n \n \n File \"../../.venv/lib/python3.10/site-packages/vectorbtpro/generic/nb/base.py\", line 1706:\n def impl(\n \n for col in prange(arr.shape[1]):\n out[:, col] = realign_1d_nb(\n ^\n\n raised from /Users/davidbrazda/Documents/Development/python/strategy-lab1/.venv/lib/python3.10/site-packages/numba/core/typeinfer.py:1091\n\nDuring: resolving callee type: Function()\nDuring: typing of call at /Users/davidbrazda/Documents/Development/python/strategy-lab1/.venv/lib/python3.10/site-packages/vectorbtpro/generic/nb/base.py (1766)\n\n\nFile \"../../.venv/lib/python3.10/site-packages/vectorbtpro/generic/nb/base.py\", line 1766:\ndef realign_nb(\n \n \"\"\"2-dim version of `realign_1d_nb`.\"\"\"\n return _realign_nb(\n ^\n", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypingError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[18], line 25\u001b[0m\n\u001b[1;32m 23\u001b[0m \u001b[38;5;66;03m#resample on specific index \u001b[39;00m\n\u001b[1;32m 24\u001b[0m resampler \u001b[38;5;241m=\u001b[39m vbt\u001b[38;5;241m.\u001b[39mResampler(t30data\u001b[38;5;241m.\u001b[39mindex, s1data\u001b[38;5;241m.\u001b[39mindex, source_freq\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m30T\u001b[39m\u001b[38;5;124m\"\u001b[39m, target_freq\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m1s\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m---> 25\u001b[0m t30close_realigned \u001b[38;5;241m=\u001b[39m \u001b[43mt30close\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mvbt\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mrealign_closing\u001b[49m\u001b[43m(\u001b[49m\u001b[43mresampler\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 27\u001b[0m \u001b[38;5;66;03m#resample 1min to s\u001b[39;00m\n\u001b[1;32m 28\u001b[0m resampler_s \u001b[38;5;241m=\u001b[39m vbt\u001b[38;5;241m.\u001b[39mResampler(t1data\u001b[38;5;241m.\u001b[39mindex, s1data\u001b[38;5;241m.\u001b[39mindex, source_freq\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m1T\u001b[39m\u001b[38;5;124m\"\u001b[39m, target_freq\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m1s\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n", + "File \u001b[0;32m~/Documents/Development/python/strategy-lab1/.venv/lib/python3.10/site-packages/vectorbtpro/generic/accessors.py:2471\u001b[0m, in \u001b[0;36mGenericAccessor.realign_closing\u001b[0;34m(self, *args, **kwargs)\u001b[0m\n\u001b[1;32m 2465\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mrealign_closing\u001b[39m(\u001b[38;5;28mself\u001b[39m, \u001b[38;5;241m*\u001b[39margs, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs) \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m>\u001b[39m tp\u001b[38;5;241m.\u001b[39mMaybeSeriesFrame:\n\u001b[1;32m 2466\u001b[0m \u001b[38;5;250m \u001b[39m\u001b[38;5;124;03m\"\"\"`GenericAccessor.realign` but creating a resampler and using the right bound\u001b[39;00m\n\u001b[1;32m 2467\u001b[0m \u001b[38;5;124;03m of the source and target index.\u001b[39;00m\n\u001b[1;32m 2468\u001b[0m \n\u001b[1;32m 2469\u001b[0m \u001b[38;5;124;03m !!! note\u001b[39;00m\n\u001b[1;32m 2470\u001b[0m \u001b[38;5;124;03m The timestamps in the source and target index should denote the open time.\"\"\"\u001b[39;00m\n\u001b[0;32m-> 2471\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[43mrealign\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[43msource_rbound\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43;01mTrue\u001b[39;49;00m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mtarget_rbound\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43;01mTrue\u001b[39;49;00m\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/vectorbtpro/generic/accessors.py:2443\u001b[0m, in \u001b[0;36mGenericAccessor.realign\u001b[0;34m(self, index, freq, nan_value, ffill, source_rbound, target_rbound, jitted, chunked, wrap_kwargs, silence_warnings)\u001b[0m\n\u001b[1;32m 2441\u001b[0m func \u001b[38;5;241m=\u001b[39m jit_reg\u001b[38;5;241m.\u001b[39mresolve_option(nb\u001b[38;5;241m.\u001b[39mrealign_nb, jitted)\n\u001b[1;32m 2442\u001b[0m func \u001b[38;5;241m=\u001b[39m ch_reg\u001b[38;5;241m.\u001b[39mresolve_option(func, chunked)\n\u001b[0;32m-> 2443\u001b[0m out \u001b[38;5;241m=\u001b[39m \u001b[43mfunc\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 2444\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mto_2d_array\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 2445\u001b[0m \u001b[43m \u001b[49m\u001b[43mresampler\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43msource_index\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mvalues\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 2446\u001b[0m \u001b[43m \u001b[49m\u001b[43mresampler\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mtarget_index\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mvalues\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 2447\u001b[0m \u001b[43m \u001b[49m\u001b[43msource_freq\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43msource_freq\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 2448\u001b[0m \u001b[43m \u001b[49m\u001b[43mtarget_freq\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mtarget_freq\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 2449\u001b[0m \u001b[43m \u001b[49m\u001b[43msource_rbound\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43muse_source_rbound\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 2450\u001b[0m \u001b[43m \u001b[49m\u001b[43mtarget_rbound\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43muse_target_rbound\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 2451\u001b[0m \u001b[43m \u001b[49m\u001b[43mnan_value\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mnan_value\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 2452\u001b[0m \u001b[43m \u001b[49m\u001b[43mffill\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mffill\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 2453\u001b[0m \u001b[43m\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 2454\u001b[0m wrap_kwargs \u001b[38;5;241m=\u001b[39m merge_dicts(\u001b[38;5;28mdict\u001b[39m(index\u001b[38;5;241m=\u001b[39mindex), wrap_kwargs)\n\u001b[1;32m 2455\u001b[0m out \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mwrapper\u001b[38;5;241m.\u001b[39mwrap(out, group_by\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mFalse\u001b[39;00m, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mwrap_kwargs)\n", + "File \u001b[0;32m~/Documents/Development/python/strategy-lab1/.venv/lib/python3.10/site-packages/numba/core/dispatcher.py:423\u001b[0m, in \u001b[0;36m_DispatcherBase._compile_for_args\u001b[0;34m(self, *args, **kws)\u001b[0m\n\u001b[1;32m 419\u001b[0m msg \u001b[38;5;241m=\u001b[39m (\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;132;01m{\u001b[39;00m\u001b[38;5;28mstr\u001b[39m(e)\u001b[38;5;241m.\u001b[39mrstrip()\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m \u001b[39m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[38;5;124mThis error may have been caused \u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 420\u001b[0m \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mby the following argument(s):\u001b[39m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[38;5;132;01m{\u001b[39;00margs_str\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m 421\u001b[0m e\u001b[38;5;241m.\u001b[39mpatch_message(msg)\n\u001b[0;32m--> 423\u001b[0m \u001b[43merror_rewrite\u001b[49m\u001b[43m(\u001b[49m\u001b[43me\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43mtyping\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m)\u001b[49m\n\u001b[1;32m 424\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m errors\u001b[38;5;241m.\u001b[39mUnsupportedError \u001b[38;5;28;01mas\u001b[39;00m e:\n\u001b[1;32m 425\u001b[0m \u001b[38;5;66;03m# Something unsupported is present in the user code, add help info\u001b[39;00m\n\u001b[1;32m 426\u001b[0m error_rewrite(e, \u001b[38;5;124m'\u001b[39m\u001b[38;5;124munsupported_error\u001b[39m\u001b[38;5;124m'\u001b[39m)\n", + "File \u001b[0;32m~/Documents/Development/python/strategy-lab1/.venv/lib/python3.10/site-packages/numba/core/dispatcher.py:364\u001b[0m, in \u001b[0;36m_DispatcherBase._compile_for_args..error_rewrite\u001b[0;34m(e, issue_type)\u001b[0m\n\u001b[1;32m 362\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m e\n\u001b[1;32m 363\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m--> 364\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m e\u001b[38;5;241m.\u001b[39mwith_traceback(\u001b[38;5;28;01mNone\u001b[39;00m)\n", + "\u001b[0;31mTypingError\u001b[0m: Failed in nopython mode pipeline (step: nopython frontend)\nNo implementation of function Function() found for signature:\n \n >>> _realign_nb(array(float64, 2d, C), array(datetime64[us], 1d, C), array(datetime64[us], 1d, C), timedelta64[ns], timedelta64[ns], bool, bool, float64, bool)\n \nThere are 2 candidate implementations:\n - Of which 1 did not match due to:\n Overload in function '_realign_nb': File: vectorbtpro/generic/nb/base.py: Line 1673.\n With argument(s): '(array(float64, 2d, C), array(datetime64[us], 1d, C), array(datetime64[us], 1d, C), timedelta64[ns], timedelta64[ns], bool, bool, float64, bool)':\n Rejected as the implementation raised a specific error:\n TypingError: Failed in nopython mode pipeline (step: nopython frontend)\n Failed in nopython mode pipeline (step: nopython frontend)\n No implementation of function Function() found for signature:\n \n >>> _realign_1d_nb(array(float64, 1d, A), array(datetime64[us], 1d, C), array(datetime64[us], 1d, C), timedelta64[ns], timedelta64[ns], bool, bool, float64, bool)\n \n There are 2 candidate implementations:\n - Of which 2 did not match due to:\n Overload in function '_realign_1d_nb': File: vectorbtpro/generic/nb/base.py: Line 1509.\n With argument(s): '(array(float64, 1d, A), array(datetime64[us], 1d, C), array(datetime64[us], 1d, C), timedelta64[ns], timedelta64[ns], bool, bool, float64, bool)':\n Rejected as the implementation raised a specific error:\n TypingError: Failed in nopython mode pipeline (step: nopython frontend)\n Cannot unify datetime64[ns] and datetime64[us] for 'source_val.4', defined at /Users/davidbrazda/Documents/Development/python/strategy-lab1/.venv/lib/python3.10/site-packages/vectorbtpro/generic/nb/base.py (1577)\n \n File \"../../.venv/lib/python3.10/site-packages/vectorbtpro/generic/nb/base.py\", line 1577:\n def impl(\n \n target_val = target_index[i] + target_freq\n if source_val > target_val:\n ^\n \n During: typing of assignment at /Users/davidbrazda/Documents/Development/python/strategy-lab1/.venv/lib/python3.10/site-packages/vectorbtpro/generic/nb/base.py (1577)\n \n File \"../../.venv/lib/python3.10/site-packages/vectorbtpro/generic/nb/base.py\", line 1577:\n def impl(\n \n target_val = target_index[i] + target_freq\n if source_val > target_val:\n ^\n \n raised from /Users/davidbrazda/Documents/Development/python/strategy-lab1/.venv/lib/python3.10/site-packages/numba/core/typeinfer.py:1091\n \n During: resolving callee type: Function()\n During: typing of call at /Users/davidbrazda/Documents/Development/python/strategy-lab1/.venv/lib/python3.10/site-packages/vectorbtpro/generic/nb/base.py (1660)\n \n \n File \"../../.venv/lib/python3.10/site-packages/vectorbtpro/generic/nb/base.py\", line 1660:\n def realign_1d_nb(\n \n If `arr` contains bar data, both indexes must represent the opening time.\"\"\"\n return _realign_1d_nb(\n ^\n \n During: resolving callee type: type(CPUDispatcher())\n During: typing of call at /Users/davidbrazda/Documents/Development/python/strategy-lab1/.venv/lib/python3.10/site-packages/vectorbtpro/generic/nb/base.py (1706)\n \n During: resolving callee type: type(CPUDispatcher())\n During: typing of call at /Users/davidbrazda/Documents/Development/python/strategy-lab1/.venv/lib/python3.10/site-packages/vectorbtpro/generic/nb/base.py (1706)\n \n During: resolving callee type: type(CPUDispatcher())\n During: typing of call at /Users/davidbrazda/Documents/Development/python/strategy-lab1/.venv/lib/python3.10/site-packages/vectorbtpro/generic/nb/base.py (1706)\n \n During: resolving callee type: type(CPUDispatcher())\n During: typing of call at /Users/davidbrazda/Documents/Development/python/strategy-lab1/.venv/lib/python3.10/site-packages/vectorbtpro/generic/nb/base.py (1706)\n \n During: resolving callee type: type(CPUDispatcher())\n During: typing of call at /Users/davidbrazda/Documents/Development/python/strategy-lab1/.venv/lib/python3.10/site-packages/vectorbtpro/generic/nb/base.py (1706)\n \n During: resolving callee type: type(CPUDispatcher())\n During: typing of call at /Users/davidbrazda/Documents/Development/python/strategy-lab1/.venv/lib/python3.10/site-packages/vectorbtpro/generic/nb/base.py (1706)\n \n \n File \"../../.venv/lib/python3.10/site-packages/vectorbtpro/generic/nb/base.py\", line 1706:\n def impl(\n \n for col in prange(arr.shape[1]):\n out[:, col] = realign_1d_nb(\n ^\n\n raised from /Users/davidbrazda/Documents/Development/python/strategy-lab1/.venv/lib/python3.10/site-packages/numba/core/typeinfer.py:1091\n - Of which 1 did not match due to:\n Overload in function '_realign_nb': File: vectorbtpro/generic/nb/base.py: Line 1673.\n With argument(s): '(array(float64, 2d, C), array(datetime64[us], 1d, C), array(datetime64[us], 1d, C), timedelta64[ns], timedelta64[ns], bool, bool, float64, bool)':\n Rejected as the implementation raised a specific error:\n TypingError: Failed in nopython mode pipeline (step: nopython frontend)\n Failed in nopython mode pipeline (step: nopython frontend)\n No implementation of function Function() found for signature:\n \n >>> _realign_1d_nb(array(float64, 1d, A), array(datetime64[us], 1d, C), array(datetime64[us], 1d, C), timedelta64[ns], timedelta64[ns], bool, bool, float64, bool)\n \n There are 2 candidate implementations:\n - Of which 2 did not match due to:\n Overload in function '_realign_1d_nb': File: vectorbtpro/generic/nb/base.py: Line 1509.\n With argument(s): '(array(float64, 1d, A), array(datetime64[us], 1d, C), array(datetime64[us], 1d, C), timedelta64[ns], timedelta64[ns], bool, bool, float64, bool)':\n Rejected as the implementation raised a specific error:\n TypingError: Failed in nopython mode pipeline (step: nopython frontend)\n Cannot unify datetime64[ns] and datetime64[us] for 'source_val.4', defined at /Users/davidbrazda/Documents/Development/python/strategy-lab1/.venv/lib/python3.10/site-packages/vectorbtpro/generic/nb/base.py (1577)\n \n File \"../../.venv/lib/python3.10/site-packages/vectorbtpro/generic/nb/base.py\", line 1577:\n def impl(\n \n target_val = target_index[i] + target_freq\n if source_val > target_val:\n ^\n \n During: typing of assignment at /Users/davidbrazda/Documents/Development/python/strategy-lab1/.venv/lib/python3.10/site-packages/vectorbtpro/generic/nb/base.py (1577)\n \n File \"../../.venv/lib/python3.10/site-packages/vectorbtpro/generic/nb/base.py\", line 1577:\n def impl(\n \n target_val = target_index[i] + target_freq\n if source_val > target_val:\n ^\n \n raised from /Users/davidbrazda/Documents/Development/python/strategy-lab1/.venv/lib/python3.10/site-packages/numba/core/typeinfer.py:1091\n \n During: resolving callee type: Function()\n During: typing of call at /Users/davidbrazda/Documents/Development/python/strategy-lab1/.venv/lib/python3.10/site-packages/vectorbtpro/generic/nb/base.py (1660)\n \n \n File \"../../.venv/lib/python3.10/site-packages/vectorbtpro/generic/nb/base.py\", line 1660:\n def realign_1d_nb(\n \n If `arr` contains bar data, both indexes must represent the opening time.\"\"\"\n return _realign_1d_nb(\n ^\n \n During: resolving callee type: type(CPUDispatcher())\n During: typing of call at /Users/davidbrazda/Documents/Development/python/strategy-lab1/.venv/lib/python3.10/site-packages/vectorbtpro/generic/nb/base.py (1706)\n \n During: resolving callee type: type(CPUDispatcher())\n During: typing of call at /Users/davidbrazda/Documents/Development/python/strategy-lab1/.venv/lib/python3.10/site-packages/vectorbtpro/generic/nb/base.py (1706)\n \n During: resolving callee type: type(CPUDispatcher())\n During: typing of call at /Users/davidbrazda/Documents/Development/python/strategy-lab1/.venv/lib/python3.10/site-packages/vectorbtpro/generic/nb/base.py (1706)\n \n During: resolving callee type: type(CPUDispatcher())\n During: typing of call at /Users/davidbrazda/Documents/Development/python/strategy-lab1/.venv/lib/python3.10/site-packages/vectorbtpro/generic/nb/base.py (1706)\n \n During: resolving callee type: type(CPUDispatcher())\n During: typing of call at /Users/davidbrazda/Documents/Development/python/strategy-lab1/.venv/lib/python3.10/site-packages/vectorbtpro/generic/nb/base.py (1706)\n \n During: resolving callee type: type(CPUDispatcher())\n During: typing of call at /Users/davidbrazda/Documents/Development/python/strategy-lab1/.venv/lib/python3.10/site-packages/vectorbtpro/generic/nb/base.py (1706)\n \n During: resolving callee type: type(CPUDispatcher())\n During: typing of call at /Users/davidbrazda/Documents/Development/python/strategy-lab1/.venv/lib/python3.10/site-packages/vectorbtpro/generic/nb/base.py (1706)\n \n During: resolving callee type: type(CPUDispatcher())\n During: typing of call at /Users/davidbrazda/Documents/Development/python/strategy-lab1/.venv/lib/python3.10/site-packages/vectorbtpro/generic/nb/base.py (1706)\n \n \n File \"../../.venv/lib/python3.10/site-packages/vectorbtpro/generic/nb/base.py\", line 1706:\n def impl(\n \n for col in prange(arr.shape[1]):\n out[:, col] = realign_1d_nb(\n ^\n\n raised from /Users/davidbrazda/Documents/Development/python/strategy-lab1/.venv/lib/python3.10/site-packages/numba/core/typeinfer.py:1091\n\nDuring: resolving callee type: Function()\nDuring: typing of call at /Users/davidbrazda/Documents/Development/python/strategy-lab1/.venv/lib/python3.10/site-packages/vectorbtpro/generic/nb/base.py (1766)\n\n\nFile \"../../.venv/lib/python3.10/site-packages/vectorbtpro/generic/nb/base.py\", line 1766:\ndef realign_nb(\n \n \"\"\"2-dim version of `realign_1d_nb`.\"\"\"\n return _realign_nb(\n ^\n" + ] + } + ], "source": [ "s1data = basic_data[['open', 'high', 'low', 'close', 'volume','vwap','buyvolume','trades','sellvolume']]\n", "\n", diff --git a/research/strat_STUMPY/STUMPY_OHLCV.ipynb b/research/strat_STUMPY/STUMPY_OHLCV.ipynb index 34f51cb..20d7836 100644 --- a/research/strat_STUMPY/STUMPY_OHLCV.ipynb +++ b/research/strat_STUMPY/STUMPY_OHLCV.ipynb @@ -9,10 +9,10 @@ "Idea is to create little pipeline to identify patterns(motifs), pick the top ones and evaluate them by profitability(or some other condition). \n", "\n", "* load/resample data\n", - "* stationarity - apply differencing/logs, \n", - "* identify motifs with length N\n", + "* experiment with stationarity - apply differencing/logs or work with trending data\n", + "* identify motifs with length M, extract M most prominent motifs each with X members\n", "* visually analyse the motifs, candidates selection\n", - "* evaluate candidates(either backtest with certain SL/TP or timebased exit or some other condition)" + "* evaluate candidates(either backtest with certain SL/TP or timebased exit or some other condition) - make sure the dataset is not skewed" ] }, { @@ -26,31 +26,17 @@ }, { "cell_type": "code", - "execution_count": 56, + "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n" ], "text/plain": [ @@ -181,21 +167,22 @@ "output_type": "display_data" }, { - "name": "stdout", - "output_type": "stream", - "text": [ - "/Users/davidbrazda/Library/Application Support/v2realbot\n", - "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" + "ename": "KeyboardInterrupt", + "evalue": "", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mKeyboardInterrupt\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[1], line 12\u001b[0m\n\u001b[1;32m 9\u001b[0m init_notebook_mode(all_interactive\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mTrue\u001b[39;00m)\n\u001b[1;32m 11\u001b[0m \u001b[38;5;66;03m#LOAD FROM PARQUET\u001b[39;00m\n\u001b[0;32m---> 12\u001b[0m DATA_DIR \u001b[38;5;241m=\u001b[39m \u001b[43muser_data_dir\u001b[49m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mv2realbot\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;28;01mFalse\u001b[39;00m)\n\u001b[1;32m 13\u001b[0m \u001b[38;5;28mdir\u001b[39m \u001b[38;5;241m=\u001b[39m DATA_DIR \u001b[38;5;241m+\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m/notebooks/\u001b[39m\u001b[38;5;124m\"\u001b[39m \u001b[38;5;66;03m#my local cache dir\u001b[39;00m\n\u001b[1;32m 14\u001b[0m \u001b[38;5;28mprint\u001b[39m(DATA_DIR)\n", + "Cell \u001b[0;32mIn[1], line 12\u001b[0m\n\u001b[1;32m 9\u001b[0m init_notebook_mode(all_interactive\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mTrue\u001b[39;00m)\n\u001b[1;32m 11\u001b[0m \u001b[38;5;66;03m#LOAD FROM PARQUET\u001b[39;00m\n\u001b[0;32m---> 12\u001b[0m DATA_DIR \u001b[38;5;241m=\u001b[39m \u001b[43muser_data_dir\u001b[49m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mv2realbot\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;28;01mFalse\u001b[39;00m)\n\u001b[1;32m 13\u001b[0m \u001b[38;5;28mdir\u001b[39m \u001b[38;5;241m=\u001b[39m DATA_DIR \u001b[38;5;241m+\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m/notebooks/\u001b[39m\u001b[38;5;124m\"\u001b[39m \u001b[38;5;66;03m#my local cache dir\u001b[39;00m\n\u001b[1;32m 14\u001b[0m \u001b[38;5;28mprint\u001b[39m(DATA_DIR)\n", + "File \u001b[0;32m_pydevd_bundle/pydevd_cython.pyx:1457\u001b[0m, in \u001b[0;36m_pydevd_bundle.pydevd_cython.SafeCallWrapper.__call__\u001b[0;34m()\u001b[0m\n", + "File \u001b[0;32m_pydevd_bundle/pydevd_cython.pyx:701\u001b[0m, in \u001b[0;36m_pydevd_bundle.pydevd_cython.PyDBFrame.trace_dispatch\u001b[0;34m()\u001b[0m\n", + "File \u001b[0;32m_pydevd_bundle/pydevd_cython.pyx:1152\u001b[0m, in \u001b[0;36m_pydevd_bundle.pydevd_cython.PyDBFrame.trace_dispatch\u001b[0;34m()\u001b[0m\n", + "File \u001b[0;32m_pydevd_bundle/pydevd_cython.pyx:1135\u001b[0m, in \u001b[0;36m_pydevd_bundle.pydevd_cython.PyDBFrame.trace_dispatch\u001b[0;34m()\u001b[0m\n", + "File \u001b[0;32m_pydevd_bundle/pydevd_cython.pyx:312\u001b[0m, in \u001b[0;36m_pydevd_bundle.pydevd_cython.PyDBFrame.do_wait_suspend\u001b[0;34m()\u001b[0m\n", + "File \u001b[0;32m~/Documents/Development/python/strategy-lab1/.venv/lib/python3.10/site-packages/debugpy/_vendored/pydevd/pydevd.py:2070\u001b[0m, in \u001b[0;36mPyDB.do_wait_suspend\u001b[0;34m(self, thread, frame, event, arg, exception_type)\u001b[0m\n\u001b[1;32m 2067\u001b[0m from_this_thread\u001b[38;5;241m.\u001b[39mappend(frame_custom_thread_id)\n\u001b[1;32m 2069\u001b[0m \u001b[38;5;28;01mwith\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_threads_suspended_single_notification\u001b[38;5;241m.\u001b[39mnotify_thread_suspended(thread_id, thread, stop_reason):\n\u001b[0;32m-> 2070\u001b[0m keep_suspended \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_do_wait_suspend\u001b[49m\u001b[43m(\u001b[49m\u001b[43mthread\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mframe\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mevent\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43marg\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43msuspend_type\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mfrom_this_thread\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mframes_tracker\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 2072\u001b[0m frames_list \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m\n\u001b[1;32m 2074\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m keep_suspended:\n\u001b[1;32m 2075\u001b[0m \u001b[38;5;66;03m# This means that we should pause again after a set next statement.\u001b[39;00m\n", + "File \u001b[0;32m~/Documents/Development/python/strategy-lab1/.venv/lib/python3.10/site-packages/debugpy/_vendored/pydevd/pydevd.py:2106\u001b[0m, in \u001b[0;36mPyDB._do_wait_suspend\u001b[0;34m(self, thread, frame, event, arg, suspend_type, from_this_thread, frames_tracker)\u001b[0m\n\u001b[1;32m 2103\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_call_input_hook()\n\u001b[1;32m 2105\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mprocess_internal_commands()\n\u001b[0;32m-> 2106\u001b[0m \u001b[43mtime\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43msleep\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m0.01\u001b[39;49m\u001b[43m)\u001b[49m\n\u001b[1;32m 2108\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mcancel_async_evaluation(get_current_thread_id(thread), \u001b[38;5;28mstr\u001b[39m(\u001b[38;5;28mid\u001b[39m(frame)))\n\u001b[1;32m 2110\u001b[0m \u001b[38;5;66;03m# process any stepping instructions\u001b[39;00m\n", + "\u001b[0;31mKeyboardInterrupt\u001b[0m: " ] } ], @@ -93971,13 +93958,13 @@ }, { "cell_type": "code", - "execution_count": 131, + "execution_count": 138, "metadata": {}, "outputs": [ { "data": { "text/html": [ - "" + "window.tsjyorjj.chart.timeScale().fitContent()</script></body></html>\">" ], "text/plain": [ "" @@ -94661,7 +94418,7 @@ } ], "source": [ - "visualize_motifs_on_timeseries_lw(timeseries_sr, motif_distances, motif_indices, N, max_distance=1, show_baseline=True, show_only=[0,1,2,3], ohlcv = None, size=\"l\")\n", + "visualize_motifs_on_timeseries_lw(timeseries_sr, motif_distances, motif_indices, N, max_distance=1, show_baseline=True, show_only=[0], ohlcv = None, size=\"s\")\n", "#we can see that those are Overnight Gaps" ] }, diff --git a/research/strat_SUPERTREND/SUPERTREND_v1_SINGLE.ipynb b/research/strat_SUPERTREND/SUPERTREND_v1_SINGLE.ipynb index 9eac6c3..c8f901c 100644 --- a/research/strat_SUPERTREND/SUPERTREND_v1_SINGLE.ipynb +++ b/research/strat_SUPERTREND/SUPERTREND_v1_SINGLE.ipynb @@ -11,9 +11,31 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": {}, - "outputs": [], + "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", @@ -22,6 +44,7 @@ "# 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", @@ -45,9 +68,37 @@ }, { "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-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", @@ -66,7 +117,7 @@ "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-14 16:00\"]\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", @@ -78,9 +129,33 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "metadata": {}, - "outputs": [], + "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()" ] @@ -94,7 +169,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "metadata": {}, "outputs": [], "source": [ @@ -130,7 +205,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "metadata": {}, "outputs": [], "source": [ @@ -150,8 +225,156 @@ "s1close = s1data.close\n", "t1close = t1data.close\n", "t30close = t30data.close\n", - "t30volume = t30data.volume\n", - "\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", @@ -163,9 +386,37 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 8, "metadata": {}, - "outputs": [], + "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)" @@ -180,7 +431,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 9, "metadata": {}, "outputs": [], "source": [ @@ -198,10 +449,468 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 10, "metadata": {}, - "outputs": [], + "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", @@ -223,7 +932,7 @@ " # (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\"))" + "ch = chart([pane1], size=\"s\" ) #xloc=slice(\"2024-05-1 09:30:00\",\"2024-05-25 16:00:00\"))" ] }, {