tamuseanmiller / alpaca-stock-trading-bot

📈 Q-Learning and Sentiment Analysis Stock Trade Bot Powered by Alpaca
GNU General Public License v3.0
42 stars 13 forks source link

Polygon API Error #15

Closed riodda closed 3 years ago

riodda commented 3 years ago

I think that the api key for polygon is not working anymore, shouldn't be included in the export variables so is possible to use personal api keys ? Or i miss something ?

dario@dario-HP-255-G6:~/alpaca-stock-trading-bot$ python3 train.py 2 --window-size=10 --batch-size=30 --episode-count=10 --model-name=test_amd --stock-name=AMD --debug
2021-02-20 18:37:13 dario-HP-255-G6 root[25137] DEBUG switching to TensorFlow for CPU
2021-02-20 18:37:13 dario-HP-255-G6 urllib3.connectionpool[25137] DEBUG Starting new HTTPS connection (1): api.polygon.io:443
2021-02-20 18:37:14 dario-HP-255-G6 urllib3.connectionpool[25137] DEBUG https://api.polygon.io:443 "GET /v2/aggs/ticker/AMD/range/1/minute/2021-02-14/2021-02-17?unadjusted=False&limit=4320&apiKey=PKUN04292D828GARG52I HTTP/1.1" 401 92
2021-02-20 18:37:14 dario-HP-255-G6 root[25137] DEBUG Error connecting to Polygon, retrying in 30s (0/30)
2021-02-20 18:37:44 dario-HP-255-G6 urllib3.connectionpool[25137] DEBUG https://api.polygon.io:443 "GET /v2/aggs/ticker/AMD/range/1/minute/2021-02-14/2021-02-17?unadjusted=False&limit=4320&apiKey=PKUN04292D828GARG52I HTTP/1.1" 401 92
2021-02-20 18:37:44 dario-HP-255-G6 root[25137] DEBUG Error connecting to Polygon, retrying in 30s (1/30)
2021-02-20 18:38:14 dario-HP-255-G6 urllib3.connectionpool[25137] DEBUG https://api.polygon.io:443 "GET /v2/aggs/ticker/AMD/range/1/minute/2021-02-14/2021-02-17?unadjusted=False&limit=4320&apiKey=PKUN04292D828GARG52I HTTP/1.1" 401 92
2021-02-20 18:38:14 dario-HP-255-G6 root[25137] DEBUG Error connecting to Polygon, retrying in 30s (2/30)
2021-02-20 18:38:44 dario-HP-255-G6 urllib3.connectionpool[25137] DEBUG https://api.polygon.io:443 "GET /v2/aggs/ticker/AMD/range/1/minute/2021-02-14/2021-02-17?unadjusted=False&limit=4320&apiKey=PKUN04292D828GARG52I HTTP/1.1" 401 92
2021-02-20 18:38:44 dario-HP-255-G6 root[25137] DEBUG Error connecting to Polygon, retrying in 30s (3/30)
2021-02-20 18:39:14 dario-HP-255-G6 urllib3.connectionpool[25137] DEBUG https://api.polygon.io:443 "GET /v2/aggs/ticker/AMD/range/1/minute/2021-02-14/2021-02-17?unadjusted=False&limit=4320&apiKey=PKUN04292D828GARG52I HTTP/1.1" 401 92
2021-02-20 18:39:14 dario-HP-255-G6 root[25137] DEBUG Error connecting to Polygon, retrying in 30s (4/30)
2021-02-20 18:39:45 dario-HP-255-G6 urllib3.connectionpool[25137] DEBUG https://api.polygon.io:443 "GET /v2/aggs/ticker/AMD/range/1/minute/2021-02-14/2021-02-17?unadjusted=False&limit=4320&apiKey=PKUN04292D828GARG52I HTTP/1.1" 401 92
2021-02-20 18:39:45 dario-HP-255-G6 root[25137] DEBUG Error connecting to Polygon, retrying in 30s (5/30)
riodda commented 3 years ago

Actually the solution was quite easy, but it might worth to add it into the readme export POLYGON_KEY_ID=<your polygon key>

tamuseanmiller commented 3 years ago

Hey! Sorry for the late reply, just wanted to drop a few things in. The first is that Alpaca's keys could be used to gain access to polygon services so I didn't bother implementing it, the other is that they're dropping service for Polygon on February 26. At that point I will be changing everything that used Polygon to Alpaca's new data endpoint.

riodda commented 3 years ago

I've tryed paper trading keys and Polygon did not liked them so i need to add a new env variable for that but it worked. I think is a good idea to use alpaca for data collection as there is no request/minute limit also for paper trading. I have a quick and dirty function for alpaca and i'm happy to share with you is needed to overcome the 1000limit on the alpaca requests, here below is for the 15min but change for the 1min is quite straightforward

def alpaca_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)
        if _residual == 0:
            _df = api.get_barset(_symbol, '15Min', limit=1000, start=None, end=None, after=None, until=None).df
            _num_cycles -=1
        else:
            _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
            _df= pd.concat([_temp,_df,])
    return _df

Wold be really cool to create a standard data/command structure to be able to use all the code for other trading platforms such FXCM for europe o binance for crypto. I'm not a great coder but i'm happy to give an hand if is within my coding skills.

adanc204 commented 2 years ago

export POLYGON_KEY_ID=

Any Update on this ?

Hey, I mostly deprecated this project after Polygon dropped support for Alpaca, it will still work as long as this environment key is added and you sign up for a Polygon API key. The code would also only require minimal changes to work with the "new" Market Data API. If you want to make the change, feel free and create a PR and I will take a look.

tamuseanmiller commented 2 years ago

I've tryed paper trading keys and Polygon did not liked them so i need to add a new env variable for that but it worked. I think is a good idea to use alpaca for data collection as there is no request/minute limit also for paper trading. I have a quick and dirty function for alpaca and i'm happy to share with you is needed to overcome the 1000limit on the alpaca requests, here below is for the 15min but change for the 1min is quite straightforward

def alpaca_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)
        if _residual == 0:
            _df = api.get_barset(_symbol, '15Min', limit=1000, start=None, end=None, after=None, until=None).df
            _num_cycles -=1
        else:
            _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
            _df= pd.concat([_temp,_df,])
    return _df

Wold be really cool to create a standard data/command structure to be able to use all the code for other trading platforms such FXCM for europe o binance for crypto. I'm not a great coder but i'm happy to give an hand if is within my coding skills.

I'm not sure why I never responded to this. I'm definitely down to continue letting the community maintain this repo and turn it into something better than it is right now. If you need help just ask away but I'm not sure I'll be doing too much coding on this project currently.