Dear Alpaca team and community,
I’m looking for a way to use the API to get the last X minute bars from a point in time. For instance, the last 5 minutes from now. In the API one can provide a start date, an end date and a limit. However, the results is from
start date time to
start date time + limit*TimeFrameUnit, and I would like to get them from
end date time - limit*TimeFrameUnit to
end date time. The main reason is that it is quite difficult to compute the correct date in the past of the market for a symbol (like now - 200 minutes, if now is 9:30am utc-4) due to pre-market, low trading activity, trading being alted, and so on. I can always over dimension the request and then do a tailing like
df.tail(mylimit) but I find it quite inefficient especially if you do it over a large number of symbols. So far, I take the previous market day from the calendar endpoint and compute the difference from now jumping back to the previous market day if I cross over the market open time of the current day.
Did I miss an option in the API? is there a nice way to obtain this functionality?
Has anyone built such a functionality?
Example of my current solution:
>>> now = datetime.datetime(2022,8,31,9,30,0,0) >>> get_market_datetime_x_minutes_ago(now, minutes=200) datetime.datetime(2022, 8, 30, 12, 40) >>> df = api.get_bars( ['BBBY'], TimeFrame.Minute, start='2022-08-30T16:40:00.0Z', end='2022-08-31T13:30:00.0Z', feed='sip', limit=10000).df >>> df.info() <class 'pandas.core.frame.DataFrame'> DatetimeIndex: 741 entries, 2022-08-30 16:40:00+00:00 to 2022-08-31 13:30:00+00:00 Data columns (total 8 columns): # Column Non-Null Count Dtype --- ------ -------------- ----- 0 open 741 non-null float64 1 high 741 non-null float64 2 low 741 non-null float64 3 close 741 non-null float64 4 volume 741 non-null int64 5 trade_count 741 non-null int64 6 vwap 741 non-null float64 7 symbol 741 non-null object dtypes: float64(5), int64(2), object(1) memory usage: 52.1+ KB
I wanted 200 pips but needed to retrieve 741 instead.