Comments (13)
Wasted a day tracking this down till I found jholland1's nuclear flyswatter. Bad documentation. Fix it.
from alpaca-trade-api-python.
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.
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.
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.
@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.
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.
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.
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.
@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.
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.
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.
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.
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)
- [Bug]: "TypeError: string indices must be integers" with incoming error messages HOT 1
- asyncio.run() RuntimeError following ReadMe example HOT 1
- Indentation of await self._consume() in stream.py
- [Bug]: data_get() still uses v1
- Move TimeFrame Units To Class Definition
- [Bug]: Status pending_cancel does not exist HOT 1
- [Bug]: OTO take profit bypass the PDT Protection HOT 1
- [Bug]: List orders in rest.py not returning all orders HOT 1
- [Bug]:
- [Bug]: asyncio.run() cannot be called from a running event loop HOT 2
- [Bug]:can't install alpaca-trade-api-python HOT 1
- [Bug]: Fails to install with Python 3.11 due to `aiohttp==3.8.1` dependency HOT 8
- [Bug]: Cannot fill order for a short sale with a trailing stop
- [Bug]: Unable to retrieve any bars. HOT 2
- [Bug]: martingale : self.last_price rounding issue
- [Bug]: Installation error: command '/usr/bin/clang' failed with exit code 1 on macOS Ventura 13.3.1 and Python 3.11.3 and Python 3.10.11 HOT 4
- [Bug]: Get Open Position Error for Crypto Holdings HOT 1
- [Question]: history per minutes
- [Bug]: ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:992)
- Please use a billing/subscription-related exception type
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from alpaca-trade-api-python.