Thank you. I tried what you suggested.
api.get_latest_bars([‘SOXL’])
Out[24]:
{‘SOXL’: BarV2({ ‘c’: 14.6,
‘h’: 14.6,
‘l’: 14.6,
‘n’: 1,
‘o’: 14.6,
‘t’: ‘2023-01-24T21:23:00Z’,
‘v’: 2000,
‘vw’: 14.6})}
SOXL closed today at 14.44 at the end of regular market hours. In the after hours, it last traded at 13.98. I was expecting the bars data to give me 13.98.
Next, I tried getting the latest bid/ask prices to see if that will get me to 13.98.
from alpaca.data.requests import StockLatestQuoteRequest
from alpaca.data.historical import StockHistoricalDataClient
client = StockHistoricalDataClient(alpkey, alpsec)
def get_latest_bid_ask_for_symbol(symbol):
# multi symbol request - single symbol is similar
multisymbol_request_params = StockLatestQuoteRequest(symbol_or_symbols=[symbol])
latest_multisymbol_quotes = client.get_stock_latest_quote(multisymbol_request_params)
latest_ask_price = latest_multisymbol_quotes[symbol].ask_price
latest_bid_price = latest_multisymbol_quotes[symbol].bid_price
return [latest_bid_price,latest_ask_price]
symbol = ‘SOXL’
[bid, ask] = get_latest_bid_ask_for_symbol(symbol)
print(f’bid and ask prices of {symbol} are {bid, ask}')
From this code, I get:
bid and ask prices of SOXL are (14.6, 14.62)
Next, I tried the snapshots endpoint as you suggested.
import requests
url = “https://data.alpaca.markets/v2/stocks/snapshots”
params = {“symbols”: symbol}
headers = {
“APCA-API-KEY-ID”: alpkey,
“APCA-API-SECRET-KEY”: alpsec
}
response = requests.get(url, params=params, headers=headers)
if response.status_code == 200:
data = response.json()
print(data)
else:
print(“Error:”, response.status_code)
I get this output:
{‘SOXL’: {‘latestTrade’: {‘t’: ‘2023-01-24T21:23:12.41730688Z’,
‘x’: ‘V’,
‘p’: 14.6,
‘s’: 2000,
‘c’: [’ ', ‘T’],
‘i’: 57459102012777,
‘z’: ‘B’},
‘latestQuote’: {‘t’: ‘2023-01-24T21:35:00.086067712Z’,
‘ax’: ‘V’,
‘ap’: 14.62,
‘as’: 3,
‘bx’: ‘V’,
‘bp’: 14.6,
‘bs’: 10,
‘c’: [‘R’],
‘z’: ‘B’},
‘minuteBar’: {‘t’: ‘2023-01-24T21:23:00Z’,
‘o’: 14.6,
‘h’: 14.6,
‘l’: 14.6,
‘c’: 14.6,
‘v’: 2000,
‘n’: 1,
‘vw’: 14.6},
‘dailyBar’: {‘t’: ‘2023-01-24T05:00:00Z’,
‘o’: 14.3,
‘h’: 14.845,
‘l’: 14.18,
‘c’: 14.42,
‘v’: 454438,
‘n’: 2439,
‘vw’: 14.402522},
‘prevDailyBar’: {‘t’: ‘2023-01-23T05:00:00Z’,
‘o’: 13.19,
‘h’: 14.9,
‘l’: 13.13,
‘c’: 14.77,
‘v’: 786856,
‘n’: 3252,
‘vw’: 14.109428}}}
once again, not what I expected, but consistent with previous results.
Thanks.