# Get last X minutes from end date time

**URL:** <https://forum.alpaca.markets/t/get-last-x-minutes-from-end-date-time/10514>\
**Category:** Alpaca Market Data\
**Created:** [September 2, 2022, 6:51pm UTC](https://forum.alpaca.markets/t/get-last-x-minutes-from-end-date-time/10514 "2022-09-02T18:51:23Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![lumax\_tr](https://avatars.discourse-cdn.com/v4/letter/l/e19adc/32.png) [@lumax\_tr](https://forum.alpaca.markets/u/lumax_tr)\
**Post date:** [September 2, 2022, 6:51pm UTC](https://forum.alpaca.markets/t/get-last-x-minutes-from-end-date-time/10514/1 "2022-09-02T18:51:23Z")

</div>

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?

Thanks!

Example of my current solution:

```auto
>>> 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.
