Skip to content

Commit

Permalink
feat: Adjust crypto historical quote (#497)
Browse files Browse the repository at this point in the history
* feat: adjust crypto historical quote

* doc: fix missing tittle underline
  • Loading branch information
hiohiohio authored Aug 21, 2024
1 parent 43fa7a6 commit d0622bd
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 5 deletions.
4 changes: 2 additions & 2 deletions alpaca/data/historical/crypto.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@
format_latest_data_response,
parse_obj_as_symbol_dict,
)
from alpaca.data.models import BarSet, Orderbook, Quote, Trade, TradeSet, QuoteSet
from alpaca.data.models import BarSet, Orderbook, Quote, QuoteSet, Trade, TradeSet
from alpaca.data.requests import (
CryptoBarsRequest,
CryptoQuoteRequest,
CryptoLatestBarRequest,
CryptoLatestOrderbookRequest,
CryptoLatestQuoteRequest,
CryptoLatestTradeRequest,
CryptoQuoteRequest,
CryptoSnapshotRequest,
CryptoTradesRequest,
)
Expand Down
6 changes: 6 additions & 0 deletions docs/api_reference/data/crypto/historical.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ Get Crypto Bars
.. automethod:: alpaca.data.historical.crypto.CryptoHistoricalDataClient.get_crypto_bars


Get Crypto Quotes
-----------------

.. automethod:: alpaca.data.historical.crypto.CryptoHistoricalDataClient.get_crypto_quotes


Get Crypto Trades
-----------------

Expand Down
6 changes: 6 additions & 0 deletions docs/api_reference/data/crypto/requests.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ CryptoBarsRequest
.. autoclass:: alpaca.data.requests.CryptoBarsRequest


CryptoQuoteRequest
------------------

.. autoclass:: alpaca.data.requests.CryptoQuoteRequest


CryptoTradesRequest
-------------------

Expand Down
23 changes: 21 additions & 2 deletions examples/crypto-trading-basic.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
"\n",
"from alpaca.data.requests import (\n",
" CryptoBarsRequest,\n",
" CryptoQuoteRequest,\n",
" CryptoTradesRequest,\n",
" CryptoLatestQuoteRequest\n",
" )\n",
Expand Down Expand Up @@ -438,13 +439,31 @@
"req = CryptoBarsRequest(\n",
" symbol_or_symbols = [symbol],\n",
" timeframe=TimeFrame(amount = 1, unit = TimeFrameUnit.Hour), # specify timeframe\n",
" start = now - timedelta(days = 5), # specify start datetime, default=the beginning of the current day.\n",
" start = now - timedelta(days = 1), # specify start datetime, default=the beginning of the current day.\n",
" # end_date=None, # specify end datetime, default=now\n",
" limit = 2, # specify limit\n",
")\n",
"crypto_historical_data_client.get_crypto_bars(req).df"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# get historical quote by symbol\n",
"# ref. https://docs.alpaca.markets/reference/cryptoquotes-1\n",
"now = datetime.now(ZoneInfo(\"America/New_York\"))\n",
"req = CryptoQuoteRequest(\n",
" symbol_or_symbols = [symbol],\n",
" start = now - timedelta(days = 1), # specify start datetime, default=the beginning of the current day.\n",
" # end_date=None, # specify end datetime, default=now\n",
" limit = 2, # specify limit\n",
")\n",
"crypto_historical_data_client.get_crypto_quotes(req).df"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand All @@ -454,7 +473,7 @@
"# get historical trades by symbol\n",
"req = CryptoTradesRequest(\n",
" symbol_or_symbols = [symbol],\n",
" start = now - timedelta(days = 5), # specify start datetime, default=the beginning of the current day.\n",
" start = now - timedelta(days = 1), # specify start datetime, default=the beginning of the current day.\n",
" # end=None, # specify end datetime, default=now\n",
" limit = 2, # specify limit\n",
")\n",
Expand Down
54 changes: 53 additions & 1 deletion tests/data/test_historical_crypto_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@

from alpaca.data import Bar, Quote, Trade
from alpaca.data.historical.crypto import CryptoHistoricalDataClient
from alpaca.data.models import BarSet, Snapshot, TradeSet
from alpaca.data.models import BarSet, QuoteSet, Snapshot, TradeSet
from alpaca.data.requests import (
CryptoBarsRequest,
CryptoLatestBarRequest,
CryptoLatestQuoteRequest,
CryptoLatestTradeRequest,
CryptoQuoteRequest,
CryptoSnapshotRequest,
CryptoTradesRequest,
)
Expand Down Expand Up @@ -76,6 +77,57 @@ def test_get_crypto_bars(reqmock, crypto_client: CryptoHistoricalDataClient):
assert barset.df.index.nlevels == 2


def test_get_crypto_quotes(reqmock, crypto_client: CryptoHistoricalDataClient):
# test multisymbol request
symbols = ["BTC/USD", "ETH/USD"]
start = datetime(2022, 5, 26)
end = datetime(2022, 5, 26)
_symbols_in_url = "%2C".join(s for s in symbols)

_start_in_url = urllib.parse.quote_plus(
start.replace(tzinfo=timezone.utc).isoformat()
)
_end_in_url = urllib.parse.quote_plus(end.replace(tzinfo=timezone.utc).isoformat())
reqmock.get(
f"https://data.alpaca.markets/v1beta3/crypto/us/quotes?start={_start_in_url}&end={_end_in_url}&symbols={_symbols_in_url}",
text="""
{
"quotes": {
"BTC/USD": [
{
"t": "2022-05-26T11:47:18.44347136Z",
"bp": 29058,
"bs": 0.3544,
"ap": 29059,
"as": 3.252
}
],
"ETH/USD": [
{
"t": "2022-05-26T11:47:18.499478272Z",
"bp": 1817,
"bs": 4.76,
"ap": 1817.7,
"as": 6.137
}
]
},
"next_page_token": null
}
""",
)
request = CryptoQuoteRequest(symbol_or_symbols=symbols, start=start, end=end)
quoteset = crypto_client.get_crypto_quotes(request)

assert isinstance(quoteset, QuoteSet)

assert quoteset["BTC/USD"][0].bid_price == 29058
assert quoteset["ETH/USD"][0].ask_size == 6.137

assert quoteset.df.index[0][1].day == 26
assert quoteset.df.index.nlevels == 2


def test_get_trades(reqmock, crypto_client: CryptoHistoricalDataClient):
# test multisymbol request
symbols = ["BTC/USD", "ETH/USD"]
Expand Down

0 comments on commit d0622bd

Please sign in to comment.