alpacahq / Momentum-Trading-Example

An example algorithm for a momentum-based day trading strategy.
657 stars 218 forks source link

404 error #14

Open autoparts494 opened 4 years ago

autoparts494 commented 4 years ago

Getting current ticker data... Success. Getting list of assets... Success. Tracking 201 symbols. Getting historical data... Traceback (most recent call last): File "C:/Users/colef/PycharmProjects/Code/Python/Momentum-Trading-Example-master/algo.py", line 400, in run(get_tickers(), market_open, market_close) File "C:/Users/colef/PycharmProjects/Code/Python/Momentum-Trading-Example-master/algo.py", line 90, in run minute_history = get_1000m_history_data(symbols) File "C:/Users/colef/PycharmProjects/Code/Python/Momentum-Trading-Example-master/algo.py", line 40, in get_1000m_history_data size="minute", symbol=symbol, limit=1000 File "C:\Users\colef\Anaconda3\lib\site-packages\alpaca_trade_api\polygon\rest.py", line 123, in historic_agg raw = self.get(path, params) File "C:\Users\colef\Anaconda3\lib\site-packages\alpaca_trade_api\polygon\rest.py", line 37, in get return self._request('GET', path, params=params, version=version) File "C:\Users\colef\Anaconda3\lib\site-packages\alpaca_trade_api\polygon\rest.py", line 33, in _request resp.raise_for_status() File "C:\Users\colef\Anaconda3\lib\site-packages\requests\models.py", line 940, in raise_for_status raise HTTPError(http_error_msg, response=self) requests.exceptions.HTTPError: 404 Client Error: Not Found for url: https://api.polygon.io/v1/historic/agg/minute/ARLO?limit=1000&apiKey=xxxxxxxxxxxxxxxxxxxxxxxxxxxx

Process finished with exit code 1

Can someone please help me with this. I have a valid Brokerage account but it still gives me this error.

erod998 commented 4 years ago

I also have this issue using Python 3.8.2. Tried changing the HTTP endpoints in the rest.py, but have not been able to get it to work. Using Postman, I can GET and POST to Alpaca using https://paper-api.alpaca.markets/v2/ as my endpoint. However, the algo still does not have the right endpoints and updating rest.py with different urls has not fixed it. Any help will be appreciated.

FlavorOfTheDay commented 4 years ago

I'm also having this same issue: HTTPError: 404 Client Error: Not Found for url: https://api.polygon.io/v1/historic/agg/minute/

erod998 commented 4 years ago

This may be completely wrong, but I believe the V2 looks like this: Calling API: minute_history[symbol] = api.polygon.historic_agg_v2( timespan="minute", symbol=symbol, limit=1000, multiplier=1, _from='2020-04-05', to='2020-04-06' ).df

Updated polygon code.

The API now has a v2 function: def historic_agg_v2(self, symbol, multiplier, timespan, _from, to, unadjusted=False, limit=None): path = '/aggs/ticker/{}/range/{}/{}/{}/{}'.format( symbol, multiplier, timespan, _from, to ) params = {} params['unadjusted'] = unadjusted if limit: params['limit'] = limit raw = self.get(path, params, version='v2') return Aggsv2(raw) Obviously the dates cannot be hardcoded into the api call but I was testing it, I don't know if this is the right step in the right direction.

FlavorOfTheDay commented 4 years ago

This was the fix. Nice work, and thank you.

It was running up until this error occurred: TypeError: 'NoneType' object is not iterable

On Tue, Apr 7, 2020 at 1:36 PM erod998 notifications@github.com wrote:

This may be completely wrong, but I believe the V2 looks like this: Calling API: minute_history[symbol] = api.polygon.historic_agg_v2( timespan="minute", symbol=symbol, limit=1000, multiplier=1, _from='2020-04-05', to='2020-04-06' ).df

Updated polygon https://github.com/alpacahq/alpaca-trade-api-python/tree/master/alpaca_trade_api/polygon code.

The API now has a v2 function: def historic_agg_v2(self, symbol, multiplier, timespan, _from, to, unadjusted=False, limit=None): path = '/aggs/ticker/{}/range/{}/{}/{}/{}'.format( symbol, multiplier, timespan, _from, to ) params = {} params['unadjusted'] = unadjusted if limit: params['limit'] = limit raw = self.get(path, params, version='v2') return Aggsv2(raw) Obviously the dates cannot be hardcoded into the api call but I was testing it, I don't know if this is the right step in the right direction.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/alpacahq/Momentum-Trading-Example/issues/14#issuecomment-610522457, or unsubscribe https://github.com/notifications/unsubscribe-auth/AIDXXXX56AZESLQRCTQQOMTRLNQAHANCNFSM4LWUVQLA .

erod998 commented 4 years ago

I also got that error. Can't figure it out yet as it does not happen each time the algo is ran. I am not sure if the GET request is getting the wrong info or if the JSON isn't being parsed correctly. The current way I call the V2 API is incredibly slow. Not sure if it is the code or if its limitations of the API. Takes about 5 minutes to populate the list of the aggregated data.

This is the response from GET https://api.polygon.io/v2/aggs/ticker/AAL/range/1/minute/2020-04-01/2020-04-06?apiKey=xxxxxxxxxxxx. { "ticker": "AAL", "status": "OK", "queryCount": 3241, "resultsCount": 3241, "adjusted": true, "results": [ { "v": 1761, "vw": 11.727, "o": 11.81, "c": 11.5, "h": 11.81, "l": 11.5, "t": 1585728000000, "n": 23 }, { "v": 600, "vw": 11.5, "o": 11.5, "c": 11.5, "h": 11.5, "l": 11.5, "t": 1585728060000, "n": 9 },...............

FlavorOfTheDay commented 4 years ago

here's the code for capturing the time if that's what you were looking for?

[image: image.png]

I'm getting a different error this time. Could it be because I have it using a paper trading API key?

On Tue, Apr 7, 2020 at 1:44 PM erod998 notifications@github.com wrote:

I also got that error. Can't figure it out yet as it does not happen each time the algo is ran. Can you point me in the direction to change the time the algo starts? I would like to do testing and have not had the chance to have the algo run before market open.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/alpacahq/Momentum-Trading-Example/issues/14#issuecomment-610527581, or unsubscribe https://github.com/notifications/unsubscribe-auth/AIDXXXUEIQD2ECQLPC5I2E3RLNRATANCNFSM4LWUVQLA .

FlavorOfTheDay commented 4 years ago

Now I'm getting an error "Cannot close a running event loop." Do you have any thoughts on this? [image: image.png]

On Tue, Apr 7, 2020 at 4:50 PM Aric Recker aricrecker@gmail.com wrote:

here's the code for capturing the time if that's what you were looking for?

[image: image.png]

I'm getting a different error this time. Could it be because I have it using a paper trading API key?

On Tue, Apr 7, 2020 at 1:44 PM erod998 notifications@github.com wrote:

I also got that error. Can't figure it out yet as it does not happen each time the algo is ran. Can you point me in the direction to change the time the algo starts? I would like to do testing and have not had the chance to have the algo run before market open.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/alpacahq/Momentum-Trading-Example/issues/14#issuecomment-610527581, or unsubscribe https://github.com/notifications/unsubscribe-auth/AIDXXXUEIQD2ECQLPC5I2E3RLNRATANCNFSM4LWUVQLA .

thedeadrobots commented 4 years ago

Added generation of yesterday/today dates to the historical data function using api.polygon.historic_agg_v2 of the API, that was mentioned here: #https://github.com/alpacahq/Momentum-Trading-Example/issues/14#issuecomment-610522457

#add date to imports
from datetime import date, datetime, timedelta

def get_1000m_history_data(symbols):
    print('Getting historical data...')
    minute_history = {}
    c = 0
    today = date.today()
    yesterday = date.today() - timedelta(days=1)

    for symbol in symbols:
        minute_history[symbol] = api.polygon.historic_agg_v2(
            timespan="minute", symbol=symbol, limit=1000, multiplier=1, _from=yesterday.strftime("%Y-%m-%d"), to=today.strftime("%Y-%m-%d")
        ).df
        c += 1
        print('{}/{}'.format(c, len(symbols)))
    print('Success.')
    return minute_history
Hfinnell commented 4 years ago

I am also having this issue but I can't seem to figure out what you did to fix it

Getting current ticker data... Success. Tracking 127 symbols. Getting historical data... Traceback (most recent call last): File "algof.py", line 396, in run(get_tickers(), market_open, market_close) File "algof.py", line 87, in run minute_history = get_1000m_history_data(symbols) File "algof.py", line 38, in get_1000m_history_data minute_history[symbol] = api.polygon.historic_agg( AttributeError: 'REST' object has no attribute 'historic_agg'

thedeadrobots commented 4 years ago

try this: api.polygon.historic_agg_v2