{ "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": [ "TTOOLS: 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, fetch_daily_stock_trades\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": 7, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
| \n", " | open | \n", "high | \n", "low | \n", "close | \n", "volume | \n", "
|---|---|---|---|---|---|
| time | \n", "\n", " | \n", " | \n", " | \n", " | \n", " |
| 2024-09-16 04:01:24-04:00 | \n", "562.22 | \n", "562.22 | \n", "562.22 | \n", "562.22 | \n", "200.0 | \n", "
| 2024-09-16 04:02:24-04:00 | \n", "562.17 | \n", "562.17 | \n", "562.17 | \n", "562.17 | \n", "293.0 | \n", "
| 2024-09-16 04:04:36-04:00 | \n", "562.54 | \n", "562.54 | \n", "562.54 | \n", "562.54 | \n", "100.0 | \n", "
| 2024-09-16 04:10:00-04:00 | \n", "562.39 | \n", "562.39 | \n", "562.39 | \n", "562.39 | \n", "102.0 | \n", "
| 2024-09-16 04:10:24-04:00 | \n", "562.44 | \n", "562.44 | \n", "562.44 | \n", "562.44 | \n", "371.0 | \n", "
| ... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "
| 2024-10-18 19:57:24-04:00 | \n", "584.80 | \n", "584.80 | \n", "584.80 | \n", "584.80 | \n", "100.0 | \n", "
| 2024-10-18 19:57:48-04:00 | \n", "584.84 | \n", "584.84 | \n", "584.84 | \n", "584.84 | \n", "622.0 | \n", "
| 2024-10-18 19:58:48-04:00 | \n", "584.77 | \n", "584.79 | \n", "584.77 | \n", "584.79 | \n", "4158.0 | \n", "
| 2024-10-18 19:59:36-04:00 | \n", "584.80 | \n", "584.82 | \n", "584.80 | \n", "584.82 | \n", "298.0 | \n", "
| 2024-10-18 19:59:48-04:00 | \n", "584.76 | \n", "584.76 | \n", "584.72 | \n", "584.72 | \n", "258.0 | \n", "
64218 rows × 5 columns
\n", "