remote fetch

This commit is contained in:
David Brazda
2024-11-10 14:08:41 +01:00
parent cf6bcede48
commit b23a772836
6 changed files with 543 additions and 5 deletions

View File

@ -97,6 +97,28 @@ python3 prepare_cache.py --symbols BAC AAPL --day_start 2024-10-14 --day_stop 20
```
## remote loaders
Remote bars of given resolutions from Alpaca.
Available resolutions Minute, Hours, Day. It s not possible to limit included trades.
Use only when no precision required.
```python
from ttools.external_loaders import load_history_bars
from ttools.config import zoneNY
from datetime import datetime, time
from alpaca.data.timeframe import TimeFrame, TimeFrameUnit
symbol = "AAPL"
start_date = zoneNY.localize(datetime(2023, 2, 27, 18, 51, 38))
end_date = zoneNY.localize(datetime(2023, 4, 27, 21, 51, 39))
timeframe = TimeFrame(amount=1,unit=TimeFrameUnit.Minute)
df = load_history_bars(symbol, start_date, end_date, timeframe, main_session_only=True)
df.loc[('AAPL',)]
```
# vbtutils
Contains helpers for vbtpro

View File

@ -2,7 +2,7 @@ from setuptools import setup, find_packages
setup(
name='ttools',
version='0.7.1',
version='0.7.2',
packages=find_packages(),
install_requires=[
# list your dependencies here

460
tests/alpaca_loader.ipynb Normal file
View File

@ -0,0 +1,460 @@
{
"cells": [
{
"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": [
"from ttools.external_loaders import load_history_bars\n",
"from ttools.config import zoneNY\n",
"from datetime import datetime, time\n",
"from alpaca.data.timeframe import TimeFrame, TimeFrameUnit\n",
"\n",
"symbol = \"AAPL\"\n",
"start_date = zoneNY.localize(datetime(2023, 2, 27, 18, 51, 38))\n",
"end_date = zoneNY.localize(datetime(2023, 4, 27, 21, 51, 39))\n",
"timeframe = TimeFrame(amount=1,unit=TimeFrameUnit.Minute)\n",
"\n",
"df = load_history_bars(symbol, start_date, end_date, timeframe, True)\n",
"df.loc[('AAPL',)]"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>open</th>\n",
" <th>high</th>\n",
" <th>low</th>\n",
" <th>close</th>\n",
" <th>volume</th>\n",
" <th>trade_count</th>\n",
" <th>vwap</th>\n",
" </tr>\n",
" <tr>\n",
" <th>timestamp</th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>2023-02-28 09:30:00-05:00</th>\n",
" <td>147.050</td>\n",
" <td>147.380</td>\n",
" <td>146.830</td>\n",
" <td>147.2700</td>\n",
" <td>1554100.0</td>\n",
" <td>6447.0</td>\n",
" <td>146.914560</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2023-02-28 09:31:00-05:00</th>\n",
" <td>147.250</td>\n",
" <td>147.320</td>\n",
" <td>147.180</td>\n",
" <td>147.2942</td>\n",
" <td>159387.0</td>\n",
" <td>6855.0</td>\n",
" <td>147.252171</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2023-02-28 09:32:00-05:00</th>\n",
" <td>147.305</td>\n",
" <td>147.330</td>\n",
" <td>147.090</td>\n",
" <td>147.1600</td>\n",
" <td>214536.0</td>\n",
" <td>7435.0</td>\n",
" <td>147.210128</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2023-02-28 09:33:00-05:00</th>\n",
" <td>147.140</td>\n",
" <td>147.230</td>\n",
" <td>147.090</td>\n",
" <td>147.1500</td>\n",
" <td>171487.0</td>\n",
" <td>7235.0</td>\n",
" <td>147.154832</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2023-02-28 09:34:00-05:00</th>\n",
" <td>147.160</td>\n",
" <td>147.160</td>\n",
" <td>146.880</td>\n",
" <td>146.9850</td>\n",
" <td>235915.0</td>\n",
" <td>4965.0</td>\n",
" <td>147.001762</td>\n",
" </tr>\n",
" <tr>\n",
" <th>...</th>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2023-04-27 15:26:00-04:00</th>\n",
" <td>168.400</td>\n",
" <td>168.415</td>\n",
" <td>168.340</td>\n",
" <td>168.3601</td>\n",
" <td>163973.0</td>\n",
" <td>1398.0</td>\n",
" <td>168.368809</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2023-04-27 15:27:00-04:00</th>\n",
" <td>168.360</td>\n",
" <td>168.400</td>\n",
" <td>168.330</td>\n",
" <td>168.3800</td>\n",
" <td>130968.0</td>\n",
" <td>1420.0</td>\n",
" <td>168.364799</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2023-04-27 15:28:00-04:00</th>\n",
" <td>168.380</td>\n",
" <td>168.430</td>\n",
" <td>168.320</td>\n",
" <td>168.3285</td>\n",
" <td>152193.0</td>\n",
" <td>1361.0</td>\n",
" <td>168.372671</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2023-04-27 15:29:00-04:00</th>\n",
" <td>168.325</td>\n",
" <td>168.330</td>\n",
" <td>168.260</td>\n",
" <td>168.2850</td>\n",
" <td>208426.0</td>\n",
" <td>1736.0</td>\n",
" <td>168.297379</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2023-04-27 15:30:00-04:00</th>\n",
" <td>168.280</td>\n",
" <td>168.350</td>\n",
" <td>168.255</td>\n",
" <td>168.3450</td>\n",
" <td>218077.0</td>\n",
" <td>1694.0</td>\n",
" <td>168.308873</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>15162 rows × 7 columns</p>\n",
"</div>"
],
"text/plain": [
" open high low close volume \\\n",
"timestamp \n",
"2023-02-28 09:30:00-05:00 147.050 147.380 146.830 147.2700 1554100.0 \n",
"2023-02-28 09:31:00-05:00 147.250 147.320 147.180 147.2942 159387.0 \n",
"2023-02-28 09:32:00-05:00 147.305 147.330 147.090 147.1600 214536.0 \n",
"2023-02-28 09:33:00-05:00 147.140 147.230 147.090 147.1500 171487.0 \n",
"2023-02-28 09:34:00-05:00 147.160 147.160 146.880 146.9850 235915.0 \n",
"... ... ... ... ... ... \n",
"2023-04-27 15:26:00-04:00 168.400 168.415 168.340 168.3601 163973.0 \n",
"2023-04-27 15:27:00-04:00 168.360 168.400 168.330 168.3800 130968.0 \n",
"2023-04-27 15:28:00-04:00 168.380 168.430 168.320 168.3285 152193.0 \n",
"2023-04-27 15:29:00-04:00 168.325 168.330 168.260 168.2850 208426.0 \n",
"2023-04-27 15:30:00-04:00 168.280 168.350 168.255 168.3450 218077.0 \n",
"\n",
" trade_count vwap \n",
"timestamp \n",
"2023-02-28 09:30:00-05:00 6447.0 146.914560 \n",
"2023-02-28 09:31:00-05:00 6855.0 147.252171 \n",
"2023-02-28 09:32:00-05:00 7435.0 147.210128 \n",
"2023-02-28 09:33:00-05:00 7235.0 147.154832 \n",
"2023-02-28 09:34:00-05:00 4965.0 147.001762 \n",
"... ... ... \n",
"2023-04-27 15:26:00-04:00 1398.0 168.368809 \n",
"2023-04-27 15:27:00-04:00 1420.0 168.364799 \n",
"2023-04-27 15:28:00-04:00 1361.0 168.372671 \n",
"2023-04-27 15:29:00-04:00 1736.0 168.297379 \n",
"2023-04-27 15:30:00-04:00 1694.0 168.308873 \n",
"\n",
"[15162 rows x 7 columns]"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.loc[('AAPL',)]"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th></th>\n",
" <th>open</th>\n",
" <th>high</th>\n",
" <th>low</th>\n",
" <th>close</th>\n",
" <th>volume</th>\n",
" <th>trade_count</th>\n",
" <th>vwap</th>\n",
" </tr>\n",
" <tr>\n",
" <th>symbol</th>\n",
" <th>timestamp</th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th rowspan=\"11\" valign=\"top\">AAPL</th>\n",
" <th>2023-02-27 18:52:00-05:00</th>\n",
" <td>148.0200</td>\n",
" <td>148.02</td>\n",
" <td>148.0200</td>\n",
" <td>148.02</td>\n",
" <td>112.0</td>\n",
" <td>7.0</td>\n",
" <td>148.020000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2023-02-27 18:56:00-05:00</th>\n",
" <td>148.0200</td>\n",
" <td>148.02</td>\n",
" <td>148.0200</td>\n",
" <td>148.02</td>\n",
" <td>175.0</td>\n",
" <td>10.0</td>\n",
" <td>148.020000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2023-02-27 19:00:00-05:00</th>\n",
" <td>148.0299</td>\n",
" <td>148.03</td>\n",
" <td>148.0299</td>\n",
" <td>148.03</td>\n",
" <td>1957.0</td>\n",
" <td>10.0</td>\n",
" <td>148.029993</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2023-02-27 19:06:00-05:00</th>\n",
" <td>148.0600</td>\n",
" <td>148.06</td>\n",
" <td>148.0600</td>\n",
" <td>148.06</td>\n",
" <td>122.0</td>\n",
" <td>7.0</td>\n",
" <td>148.060000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2023-02-27 19:09:00-05:00</th>\n",
" <td>148.0500</td>\n",
" <td>148.10</td>\n",
" <td>148.0500</td>\n",
" <td>148.10</td>\n",
" <td>1604.0</td>\n",
" <td>33.0</td>\n",
" <td>148.075109</td>\n",
" </tr>\n",
" <tr>\n",
" <th>...</th>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2023-04-27 19:54:00-04:00</th>\n",
" <td>167.8000</td>\n",
" <td>167.80</td>\n",
" <td>167.8000</td>\n",
" <td>167.80</td>\n",
" <td>534.0</td>\n",
" <td>15.0</td>\n",
" <td>167.800000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2023-04-27 19:56:00-04:00</th>\n",
" <td>167.8800</td>\n",
" <td>167.88</td>\n",
" <td>167.8800</td>\n",
" <td>167.88</td>\n",
" <td>1386.0</td>\n",
" <td>28.0</td>\n",
" <td>167.880000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2023-04-27 19:57:00-04:00</th>\n",
" <td>167.8000</td>\n",
" <td>167.80</td>\n",
" <td>167.8000</td>\n",
" <td>167.80</td>\n",
" <td>912.0</td>\n",
" <td>60.0</td>\n",
" <td>167.800000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2023-04-27 19:58:00-04:00</th>\n",
" <td>167.8000</td>\n",
" <td>167.88</td>\n",
" <td>167.8000</td>\n",
" <td>167.88</td>\n",
" <td>3311.0</td>\n",
" <td>22.0</td>\n",
" <td>167.877333</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2023-04-27 19:59:00-04:00</th>\n",
" <td>167.9000</td>\n",
" <td>167.94</td>\n",
" <td>167.9000</td>\n",
" <td>167.94</td>\n",
" <td>1969.0</td>\n",
" <td>64.0</td>\n",
" <td>167.918150</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>31217 rows × 7 columns</p>\n",
"</div>"
],
"text/plain": [
" open high low close volume \\\n",
"symbol timestamp \n",
"AAPL 2023-02-27 18:52:00-05:00 148.0200 148.02 148.0200 148.02 112.0 \n",
" 2023-02-27 18:56:00-05:00 148.0200 148.02 148.0200 148.02 175.0 \n",
" 2023-02-27 19:00:00-05:00 148.0299 148.03 148.0299 148.03 1957.0 \n",
" 2023-02-27 19:06:00-05:00 148.0600 148.06 148.0600 148.06 122.0 \n",
" 2023-02-27 19:09:00-05:00 148.0500 148.10 148.0500 148.10 1604.0 \n",
"... ... ... ... ... ... \n",
" 2023-04-27 19:54:00-04:00 167.8000 167.80 167.8000 167.80 534.0 \n",
" 2023-04-27 19:56:00-04:00 167.8800 167.88 167.8800 167.88 1386.0 \n",
" 2023-04-27 19:57:00-04:00 167.8000 167.80 167.8000 167.80 912.0 \n",
" 2023-04-27 19:58:00-04:00 167.8000 167.88 167.8000 167.88 3311.0 \n",
" 2023-04-27 19:59:00-04:00 167.9000 167.94 167.9000 167.94 1969.0 \n",
"\n",
" trade_count vwap \n",
"symbol timestamp \n",
"AAPL 2023-02-27 18:52:00-05:00 7.0 148.020000 \n",
" 2023-02-27 18:56:00-05:00 10.0 148.020000 \n",
" 2023-02-27 19:00:00-05:00 10.0 148.029993 \n",
" 2023-02-27 19:06:00-05:00 7.0 148.060000 \n",
" 2023-02-27 19:09:00-05:00 33.0 148.075109 \n",
"... ... ... \n",
" 2023-04-27 19:54:00-04:00 15.0 167.800000 \n",
" 2023-04-27 19:56:00-04:00 28.0 167.880000 \n",
" 2023-04-27 19:57:00-04:00 60.0 167.800000 \n",
" 2023-04-27 19:58:00-04:00 22.0 167.877333 \n",
" 2023-04-27 19:59:00-04:00 64.0 167.918150 \n",
"\n",
"[31217 rows x 7 columns]"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df"
]
}
],
"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
}

View File

@ -729,7 +729,7 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
@ -741,7 +741,7 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 8,
"metadata": {},
"outputs": [
{
@ -919,7 +919,7 @@
"[222754 rows x 6 columns]"
]
},
"execution_count": 4,
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}

View File

@ -1,4 +1,5 @@
from .vbtutils import AnchoredIndicator, create_mask_from_window, isrising, isfalling, isrisingc, isfallingc, trades2entries_exits, figs2cell
from .vbtindicators import register_custom_inds
from .utils import AggType, zoneNY, zonePRG, zoneUTC
from .loaders import load_data, prepare_trade_cache
from .loaders import load_data, prepare_trade_cache
from .external_loaders import load_history_bars

View File

@ -0,0 +1,55 @@
from ctypes import Union
from ttools import zoneUTC
from ttools.config import *
from datetime import datetime
from alpaca.data.historical import StockHistoricalDataClient
from ttools.config import ACCOUNT1_LIVE_API_KEY, ACCOUNT1_LIVE_SECRET_KEY
from datetime import timedelta, datetime, time
from alpaca.data.enums import DataFeed
from typing import List, Union
import pandas as pd
from alpaca.data.historical import StockHistoricalDataClient
from alpaca.data.requests import StockBarsRequest
from alpaca.data.enums import DataFeed
from alpaca.data.timeframe import TimeFrame, TimeFrameUnit
def load_history_bars(symbol: Union[str, List[str]], datetime_object_from: datetime, datetime_object_to: datetime, timeframe: TimeFrame, main_session_only: bool = True):
"""Returns dataframe fetched remotely from Alpaca.
Args:
symbol: symbol or list of symbols
datetime_object_from: datetime in zoneNY
datetime_object_to: datetime in zoneNY
timeframe: timeframe
main_session_only: boolean to fetch only main session data
Returns:
dataframe
Example:
```python
from ttools.external_loaders import load_history_bars
from ttools.config import zoneNY
from datetime import datetime
from alpaca.data.timeframe import TimeFrame, TimeFrameUnit
symbol = "AAPL"
start_date = zoneNY.localize(datetime(2023, 2, 27, 18, 51, 38))
end_date = zoneNY.localize(datetime(2023, 4, 27, 21, 51, 39))
timeframe = TimeFrame(amount=1,unit=TimeFrameUnit.Minute)
df = load_history_bars(symbol, start_date, end_date, timeframe)
```
"""
client = StockHistoricalDataClient(ACCOUNT1_LIVE_API_KEY, ACCOUNT1_LIVE_SECRET_KEY, raw_data=False)
#datetime_object_from = datetime(2023, 2, 27, 18, 51, 38, tzinfo=datetime.timezone.utc)
#datetime_object_to = datetime(2023, 2, 27, 21, 51, 39, tzinfo=datetime.timezone.utc)
bar_request = StockBarsRequest(symbol_or_symbols=symbol,timeframe=timeframe, start=datetime_object_from, end=datetime_object_to, feed=DataFeed.SIP)
#print("before df")
df = client.get_stock_bars(bar_request).df
df.index = df.index.set_levels(df.index.get_level_values(1).tz_convert(zoneNY), level=1)
if main_session_only:
start_time = time(9, 30, 0)
end_time = time(15, 30, 0)
df = df.loc[(df.index.get_level_values(1).time >= start_time) & (df.index.get_level_values(1).time <= end_time)]
return df