{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Load data\n", "\n", "Make sure you have .env file in ttools or any parent dir with your Alpaca keys.\n", "\n", "```bash\n", "ACCOUNT1_LIVE_API_KEY=api_key\n", "ACCOUNT1_LIVE_SECRET_KEY=secret_key\n", "```\n", "\n", "### Cache directories\n", "\n", "Daily trade files - DATADIR/tradecache\n", "Agg data cache - DATADIR/aggcache\n", "\n", "`DATADIR` - user_data_dir from appdirs library - see `config.py`" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Loaded env variables from file /Users/davidbrazda/Documents/Development/python/.env\n" ] } ], "source": [ "import pandas as pd\n", "import numpy as np\n", "from ttools.utils import AggType\n", "from datetime import datetime\n", "from ttools.aggregator_vectorized import generate_time_bars_nb, aggregate_trades\n", "from ttools.loaders import load_data, prepare_trade_cache\n", "from ttools.utils import zoneNY\n", "import vectorbtpro as vbt\n", "from lightweight_charts import PlotDFAccessor, PlotSRAccessor\n", "\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\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Fetching aggregated data\n", "\n", "Available aggregation types: \n", "- time based bars - AggType.OHLCV\n", "- volume based bars - AggType.OHLCV_VOL, resolution = volume threshold\n", "- dollar based bars - AggType.OHLCV_DOL, resolution = dollar threshold\n", "- renko bars - AggType.OHLCV_RENKO resolution = bricksize" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Contains 3 market days\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Processing market days: 100%|██████████| 3/3 [00:00<00:00, 4557.37it/s]\n", "Processing market days to fetch: 100%|██████████| 3/3 [00:00<00:00, 481.22it/s]\n", "Fetching data: 0%| | 0/3 [00:00" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "#This is how to call LOAD function\n", "symbol = [\"BAC\"]\n", "#datetime in zoneNY \n", "day_start = datetime(2024, 10, 14, 9, 45, 0)\n", "day_stop = datetime(2024, 10, 16, 15, 1, 0)\n", "day_start = zoneNY.localize(day_start)\n", "day_stop = zoneNY.localize(day_stop)\n", "\n", "#requested AGG\n", "resolution = 12 #12s bars\n", "agg_type = AggType.OHLCV #other types AggType.OHLCV_VOL, AggType.OHLCV_DOL, AggType.OHLCV_RENKO\n", "exclude_conditions = ['C','O','4','B','7','V','P','W','U','Z','F','9','M','6'] #None to defaults\n", "minsize = 100 #min trade size to include\n", "main_session_only = True\n", "force_remote = True\n", "\n", "ohlcv_df = load_data(symbol = symbol,\n", " agg_type = agg_type,\n", " resolution = resolution,\n", " start_date = day_start,\n", " end_date = day_stop,\n", " #exclude_conditions = None,\n", " minsize = minsize,\n", " main_session_only = main_session_only,\n", " force_remote = False\n", " )\n", "\n", "#returns symbol keyed dict with pd.DataFrame as values\n", "bac_df = ohlcv_df[\"BAC\"]\n", "\n", "basic_data = vbt.Data.from_data(vbt.symbol_dict(ohlcv_df), tz_convert=zoneNY)\n", "vbt.settings['plotting']['auto_rangebreaks'] = True\n", "#basic_data.ohlcv.plot()\n", "basic_data.ohlcv.data[symbol[0]].lw.plot()\n" ] }, { "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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
openhighlowclosevolume
time
2024-10-14 09:45:00.080054045-04:0041.965042.04041.95042.00550000.0
2024-10-14 09:46:12.714761019-04:0042.005042.10042.00042.09550000.0
2024-10-14 09:47:20.320812941-04:0042.095042.11542.06542.10550000.0
2024-10-14 09:48:26.167362928-04:0042.105042.13042.09042.10550000.0
2024-10-14 09:49:06.589205027-04:0042.100142.11042.06042.10550000.0
..................
2024-10-16 14:56:44.563248873-04:0042.890042.89042.88042.88050000.0
2024-10-16 14:57:38.830776930-04:0042.890042.91542.88042.91050000.0
2024-10-16 14:58:41.628561020-04:0042.915042.92042.90542.91050000.0
2024-10-16 14:59:50.505049944-04:0042.910042.92042.91042.91050000.0
2024-10-16 15:00:47.022783041-04:0042.910042.91042.88042.88019063.0
\n", "

1352 rows × 5 columns

\n", "
" ], "text/plain": [ " open high low close volume\n", "time \n", "2024-10-14 09:45:00.080054045-04:00 41.9650 42.040 41.950 42.005 50000.0\n", "2024-10-14 09:46:12.714761019-04:00 42.0050 42.100 42.000 42.095 50000.0\n", "2024-10-14 09:47:20.320812941-04:00 42.0950 42.115 42.065 42.105 50000.0\n", "2024-10-14 09:48:26.167362928-04:00 42.1050 42.130 42.090 42.105 50000.0\n", "2024-10-14 09:49:06.589205027-04:00 42.1001 42.110 42.060 42.105 50000.0\n", "... ... ... ... ... ...\n", "2024-10-16 14:56:44.563248873-04:00 42.8900 42.890 42.880 42.880 50000.0\n", "2024-10-16 14:57:38.830776930-04:00 42.8900 42.915 42.880 42.910 50000.0\n", "2024-10-16 14:58:41.628561020-04:00 42.9150 42.920 42.905 42.910 50000.0\n", "2024-10-16 14:59:50.505049944-04:00 42.9100 42.920 42.910 42.910 50000.0\n", "2024-10-16 15:00:47.022783041-04:00 42.9100 42.910 42.880 42.880 19063.0\n", "\n", "[1352 rows x 5 columns]" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "basic_data.ohlcv.data[symbol[0]]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Prepare daily trade cache\n", "This is how to prepare trade cache for given symbol and period (if daily trades are not cached they are remotely fetched.)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Started for BAC\n", "Contains 10 market days\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Processing market days: 100%|██████████| 10/10 [00:00<00:00, 1347.18it/s]\n", "Processing market days to fetch: 100%|██████████| 10/10 [00:00<00:00, 181.29it/s]\n", "Fetching data: 0%| | 0/10 [00:00