OHL of active trading session

Suppose the market opens at 9:30, and at e.g. 11:36, I want to retrieve the Open, and current High + Low (essentially the OHL bar for the interval 9:30 - 11:36). What is the best way to do this using the SDK?

@fisch If one wants to fetch the current day’s open high and low use the daily bar. The current daily bar is updated every minute with the most recent data. The open will remain constant, but the close will be the close of the last minute bar, the high and low will be the values of the day up to the current time.

If you are using the alpaca-pi SDK it could be coded something like this.

!pip install -q alpaca-py

from alpaca.data.historical import StockHistoricalDataClient
from alpaca.data.requests import StockBarsRequest
from alpaca.data.timeframe import TimeFrame

# Initialize a StockHistoricalDataClient
data_client = StockHistoricalDataClient(api_key='xxxx', secret_key='xxxx')

# Create a request. Omit the start and end to get the current daily bar.
request_params = StockBarsRequest(
    symbol_or_symbols=["AAPL"],  # Replace with one or more desired symbol(s)
    timeframe=TimeFrame.Day,
)

# Fetch the daily bar(s) and convert to a dataframe with the .df method
# Reset the index for easier manipulation
daily_bars = data_client.get_stock_bars(request_params).df.reset_index('timestamp')

This results in dataframe like this

The close, high, and low can be fetched like this

aapl_close = daily_bars.at['AAPL','close']
aapl_high = daily_bars.at['AAPL','high']
aapl_low = daily_bars.at['AAPL','low']

Hope that helps.

1 Like

I will try this, thanks!