Why market data from API are wrong?


These are the data from Apple today, traded all above 173/174 all day.

these are the data from the csv file I get the 15min data from the API.

|AAPL|2023-05-18 08:00:00+00:00|172.84|173|172.76|172.91|13649|232|172.903303|
|AAPL|2023-05-18 08:15:00+00:00|172.91|172.91|172.86|172.86|1240|32|172.890492|
|AAPL|2023-05-18 08:30:00+00:00|172.85|172.85|172.82|172.82|1822|25|172.834034|
|AAPL|2023-05-18 08:45:00+00:00|172.8|172.8|172.8|172.8|1377|22|172.800726|
|AAPL|2023-05-18 09:00:00+00:00|172.76|172.8|172.75|172.8|2163|40|172.763713|
|AAPL|2023-05-18 09:15:00+00:00|172.83|172.87|172.78|172.78|3099|60|172.831242|
|AAPL|2023-05-18 09:45:00+00:00|172.8|172.85|172.8|172.83|1917|48|172.814257|
|AAPL|2023-05-18 10:00:00+00:00|172.77|172.84|172.77|172.78|4448|69|172.817723|
|AAPL|2023-05-18 10:15:00+00:00|172.81|172.81|172.81|172.81|212|9|172.806698|
|AAPL|2023-05-18 10:30:00+00:00|172.84|172.91|172.84|172.91|2629|84|172.883682|
|AAPL|2023-05-18 10:45:00+00:00|172.91|172.92|172.81|172.81|6270|73|172.883312|
|AAPL|2023-05-18 11:00:00+00:00|172.81|172.92|172.8|172.85|11889|270|172.852919|
|AAPL|2023-05-18 11:15:00+00:00|172.85|172.92|172.77|172.78|20054|253|172.840548|
|AAPL|2023-05-18 11:30:00+00:00|172.77|172.85|172.69|172.78|12122|321|172.779019|
|AAPL|2023-05-18 11:45:00+00:00|172.8|172.82|172.77|172.79|6604|118|172.795765|
|AAPL|2023-05-18 12:00:00+00:00|172.89|172.92|172.43|172.65|63522|1612|172.793241|
|AAPL|2023-05-18 12:15:00+00:00|172.68|172.73|172.54|172.73|2211610|811|172.68814|
|AAPL|2023-05-18 12:30:00+00:00|172.63|172.65|172.3|172.44|62698|1261|172.475775|
|AAPL|2023-05-18 12:45:00+00:00|172.45|172.45|172.22|172.389|69937|1121|172.372475|
|AAPL|2023-05-18 13:00:00+00:00|172.39|172.6479|172.38|172.45|69518|1442|172.508372|
|AAPL|2023-05-18 13:15:00+00:00|172.48|173.2|172.47|173.09|167025|2611|172.879312|
|AAPL|2023-05-18 13:30:00+00:00|173|173.5|172.745|172.78|4009548|37775|173.146782|
|AAPL|2023-05-18 13:45:00+00:00|172.78|172.9|172.58|172.7587|1546697|29182|172.752725|

at 13.45 the value should be 175ish not 173ish, why there is this difference?

this is the function i am using to get this csv file
from alpaca.data.timeframe import TimeFrame,TimeFrameUnit
from alpaca.data.requests import StockBarsRequest
from alpaca.data.historical import StockHistoricalDataClient# Create stock historical data client

client = StockHistoricalDataClient(API_KEY, SECRET_KEY)# Create request

def stockrequest(stock,csvfile,start1,end1):
request_params = StockBarsRequest(
symbol_or_symbols = stock,
timeframe=TimeFrame(15, TimeFrameUnit.Minute),
start=start1,
end=end1
)

bars = client.get_stock_bars(request_params)
bars_df = bars.df
df=bars_df.to_csv(csvfile,index=True, mode='w+')
return 

stockrequest(stock,csvfile,start1,end1)

1 Like

Checked the data with Alphavantage, and they match, but shifted by 4 hours. 8 am data matchs with the 4.15 am. so it’s a matter of UTC timing? how do i call the real time market matching a specific timezone?

I literally just hardcoded -4 hours when dealing with Alpaca times lol. There should be ways to do the conversion with timezones but I just replace the tzinfo with None and subtract 4 hours, like below:

from datetime import timedelta
import pandas as pd

Convert the Timestamp to a datetime object

alpaca_submit = pd.Timestamp(β€˜2023-07-26 15:31:04.720803980+00:00’)
dt_submit = alpaca_submit.to_pydatetime()

Remove the timezone information from the datetime object

dt_submit = dt_submit.replace(tzinfo=None)

Subtract 4 hours

dt_submit -= timedelta(hours=4)

print(dt_submit)