Code Monkey home page Code Monkey logo

Comments (13)

jabowery avatar jabowery commented on August 23, 2024 3

Wasted a day tracking this down till I found jholland1's nuclear flyswatter. Bad documentation. Fix it.

from alpaca-trade-api-python.

jeichenseerNRX avatar jeichenseerNRX commented on August 23, 2024 2

It's definitely fussy. Here's an example that seems to work.

def get_first_two_hours_minute_bars(symbol, timestamp):
    start = timestamp.replace(hour=9, minute=30).astimezone('GMT').isoformat()[:-6]+'Z'
    end = timestamp.replace(hour=11, minute=30).astimezone('GMT').isoformat()[:-6]+'Z'
    df = live_api.get_barset(symbol, '1Min', start=start, end=end).df
    return df

df = open_bars('AAPL', pandas.Timestamp('2019-09-10 00:00:00-0400', tz='America/New_York'))
print(df.head(), df.tail())

from alpaca-trade-api-python.

shlomiku avatar shlomiku commented on August 23, 2024 2

Hi guys.
this code snippet is an example on how to use the

import pandas as pd
NY = 'America/New_York'
start=pd.Timestamp('2020-08-01', tz=NY).isoformat()
end=pd.Timestamp('2020-08-30', tz=NY).isoformat()
print(api.get_barset(['AAPL', 'GOOG'], 'day', start=start, end=end).df)

also added it to the readme: #309

from alpaca-trade-api-python.

bojanglebells avatar bojanglebells commented on August 23, 2024 1

Hi there,

I was having the same issue. I was able to fix it by appending '-04:00' to what pd.Timestamp().isoformat() returns.

So:
good_date = pd.Timestamp().isoformat() + '-04:00'

It seems like the '-04:00' is important, as if it is not present or changed to something else, the .get_barset function seems to ignore the date entirely - even if formatted with .isoformat()

Hope that helps!

from alpaca-trade-api-python.

edwardyu avatar edwardyu commented on August 23, 2024

@bojanglebells thanks for your comment. I don't think this is the issue however, as isoformat() does include the timezone.

start = pd.Timestamp('2010-01-01', tz='America/New_York').isoformat()
print(start)

>>>  2010-01-01T00:00:00-05:00

from alpaca-trade-api-python.

bnicholl avatar bnicholl commented on August 23, 2024

Is there a fix to this? I'm not sure how anyone is supposed to use Alpaca to backtests anything if we can't get historical tick data based on a specific timeframe.

from alpaca-trade-api-python.

jholland1 avatar jholland1 commented on August 23, 2024

Same issue using Timestamp, but I did get it working by just entering strings in the precise format below. Couldn't get it to work with the suggested fix for #66 or adding -04:00 to the pd.Timestamp().isoformat() output.

fts='2018-05-08T09:45:00-04:00'
tts = '2019-05-08T09:45:00-04:00'
aapl_daily = api.get_barset('AAPL', 'minute', start=fts, end=tts).df
print(aapl_daily)

from alpaca-trade-api-python.

RDPEast avatar RDPEast commented on August 23, 2024

Has anyone had any luck with this? As bnicholl points out above this is basically unusable if there is no fix.

from alpaca-trade-api-python.

mfigurski80 avatar mfigurski80 commented on August 23, 2024

@jeichenseerNRX fixed this issue for me: it's primarily about the api requiring that specific formatting, which timestamp.isoformat() doesn't actually provide. Api endpoint accepts 2019-01-01T00:00:00Z, whereas isoformat generally returns something like this: 2020-02-08T04:28:33.287093. Notably, these differ by the addition of milliseconds and the 'Z' at the end.

This is current cli get_barset:

 def get_barset(self,
                   symbols,
                   timeframe,
                   limit=None,
                   start=None,
                   end=None,
                   after=None,
                   until=None):
        '''Get BarSet(dict[str]->list[Bar])
        The parameter symbols can be either a comma-split string
        or a list of string. Each symbol becomes the key of
        the returned value.
        '''
        if not isinstance(symbols, str):
            symbols = ','.join(symbols)
        params = {
            'symbols': symbols,
        }
        if limit is not None:
            params['limit'] = limit
        if start is not None:
            params['start'] = start
        if end is not None:
            params['end'] = end
        if after is not None:
            params['after'] = after
        if until is not None:
            params['until'] = until
        resp = self.data_get('/bars/{}'.format(timeframe), params)
        return BarSet(resp)

Personally, I would like to see one or both pairs of these variables dedicated to accepting a pandas timestamp object that it would then manipulate into a string of its own liking. Would be less of a headache

from alpaca-trade-api-python.

auwsom avatar auwsom commented on August 23, 2024

It looks to me like the API is working from newest backwards by default. So if you use the boilerplate from their API docs and add a start, it will not work. Only by either changing start to end, or having both as in jholland1's reply above will it work. You also do need the isoformat(), but can use a simple date as in the OP, it goes back to 20080102 for SPY. FYI, it looks like anytime you include 'limit', it will run backwards, basically ignoring start (unless maybe you have a very large limit).

barset = api.get_barset('AAPL', 'day', limit=5, end=pd.Timestamp('2019-01-01', tz='America/New_York').isoformat())
aapl_bars = barset['AAPL']

That may be what the 'after' key is for: alpaca.markets/docs/api-documentation/api-v2/market-data/bars/

from alpaca-trade-api-python.

bbeale avatar bbeale commented on August 23, 2024

I ran into this while trying to use the until parameter.

After struggling for a bit, something like this works for me if using datetimes until=dt.strftime('%Y-%m-%dT%H:%M:%S.%f-04:00'). If not using datetimes, whatever other means to get the date into that format should work.

from alpaca-trade-api-python.

mufniarz avatar mufniarz commented on August 23, 2024

DataFrame code


import pandas as pd
df1 = api.get_barset('GE', '5Min', limit=10, start=None, end=None, after=None, until=None).df

df2 = api.get_barset('GE', '5Min', limit=10, start=None, end=None, after=None, until=df1.first_valid_index().isoformat()).df

df3 = api.get_barset('GE', '5Min', limit=10, start=None, end=None, after=None, until=df2.first_valid_index().isoformat()).df

frames = [df3, df2, df1]

result = pd.concat(frames)
result

Bars object


obj1 = api.get_barset('GE', '5Min', limit=10, start=None, end=None, after=None, until=None)

obj2 = api.get_barset('GE', '5Min', limit=10, start=None, end=None, after=None, until=obj1['GE'][0].t.isoformat())

obj3 = api.get_barset('GE', '5Min', limit=10, start=None, end=None, after=None, until=obj2['GE'][0].t.isoformat())

frames = [obj3['GE'], obj2['GE'], obj1['GE']]

result = []
for frame in frames:
    result.extend(frame) 
result

Works fine for me.

from alpaca-trade-api-python.

riodda avatar riodda commented on August 23, 2024

Thanks mufniarz, i made this little function inspired by your code, I hope somebody will find it usefoul

def get_15Min_bars(_symbol,_number):
    _df=pd.DataFrame()
    _temp=pd.DataFrame()
    if _number<=1000:
        _df = api.get_barset(_symbol, '15Min', limit=_number, start=None, end=None, after=None, until=None).df
    else:
        _num_cycles, _residual = divmod(_number, 1000)
        _df = api.get_barset(_symbol, '15Min', limit=_residual, start=None, end=None, after=None, until=None).df
        for i in range(1,_num_cycles+1):
            _temp = api.get_barset(_symbol, '15Min', limit=1000, start=None, end=None, after=None, until=_df.first_valid_index().isoformat()).df
            print(_df.first_valid_index().isoformat())
            _df= pd.concat([_temp,_df,])
    return _df

from alpaca-trade-api-python.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.