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

IndexError: list index out of range in Eval.py #11

Closed GitHubEmploy closed 4 years ago

GitHubEmploy commented 4 years ago

Full Output: 2020-10-19 10:51:57 DESKTOP-IDQO3A4 root[10980] DEBUG Sell at: $83.99 | Position: +$0.24 2020-10-19 10:51:57 DESKTOP-IDQO3A4 root[10980] DEBUG Buy at: $83.77 2020-10-19 10:51:57 DESKTOP-IDQO3A4 root[10980] DEBUG Sell at: $83.90 | Position: +$0.13 2020-10-19 10:52:00 DESKTOP-IDQO3A4 root[10980] DEBUG Buy at: $82.80 2020-10-19 10:52:00 DESKTOP-IDQO3A4 root[10980] DEBUG Sell at: $82.80 | Position: +$0.00 2020-10-19 10:52:00 DESKTOP-IDQO3A4 root[10980] DEBUG Buy at: $82.10 2020-10-19 10:52:00 DESKTOP-IDQO3A4 root[10980] DEBUG Sell at: $82.00 | Position: -$0.10 2020-10-19 10:52:01 DESKTOP-IDQO3A4 root[10980] DEBUG Buy at: $81.26 2020-10-19 10:52:01 DESKTOP-IDQO3A4 root[10980] DEBUG Sell at: $81.29 | Position: +$0.03 2020-10-19 10:52:01 DESKTOP-IDQO3A4 root[10980] DEBUG Buy at: $82.04 2020-10-19 10:52:02 DESKTOP-IDQO3A4 root[10980] DEBUG Sell at: $81.99 | Position: -$0.05 2020-10-19 10:52:02 DESKTOP-IDQO3A4 root[10980] DEBUG Buy at: $81.80 2020-10-19 10:52:02 DESKTOP-IDQO3A4 root[10980] DEBUG Sell at: $81.80 | Position: +$0.00 2020-10-19 10:52:02 DESKTOP-IDQO3A4 root[10980] DEBUG Buy at: $82.53 2020-10-19 10:52:02 DESKTOP-IDQO3A4 root[10980] DEBUG Sell at: $82.68 | Position: +$0.15 2020-10-19 10:52:02 DESKTOP-IDQO3A4 root[10980] DEBUG Buy at: $82.68 2020-10-19 10:52:02 DESKTOP-IDQO3A4 root[10980] DEBUG Sell at: $82.68 | Position: +$0.00 2020-10-19 10:52:04 DESKTOP-IDQO3A4 root[10980] DEBUG Buy at: $82.60 2020-10-19 10:52:04 DESKTOP-IDQO3A4 root[10980] DEBUG Sell at: $82.75 | Position: +$0.15 2020-10-19 10:52:07 DESKTOP-IDQO3A4 root[10980] DEBUG Buy at: $84.18 2020-10-19 10:52:07 DESKTOP-IDQO3A4 root[10980] DEBUG Sell at: $84.05 | Position: -$0.14 2020-10-19 10:52:07 DESKTOP-IDQO3A4 root[10980] DEBUG Buy at: $83.77 2020-10-19 10:52:07 DESKTOP-IDQO3A4 root[10980] DEBUG Sell at: $83.86 | Position: +$0.09 2020-10-19 10:52:07 DESKTOP-IDQO3A4 root[10980] DEBUG Buy at: $83.64 2020-10-19 10:52:07 DESKTOP-IDQO3A4 root[10980] DEBUG Sell at: $83.63 | Position: -$0.01 2020-10-19 10:52:26 DESKTOP-IDQO3A4 root[10980] INFO model_AMD_alpha: +$0.23

2020-10-19 10:52:26 DESKTOP-IDQO3A4 urllib3.connectionpool[10980] DEBUG Starting new HTTPS connection (1): data.alpaca.markets:443 2020-10-19 10:52:27 DESKTOP-IDQO3A4 urllib3.connectionpool[10980] DEBUG https://data.alpaca.markets:443 "GET /v1/bars/minute?symbols=BTGN&limit=1000&end=2020-10-19+10%3A52%3A26.857044 HTTP/1.1" 200 17 2020-10-19 10:52:27 DESKTOP-IDQO3A4 root[10980] WARNING Running sentiment with Natural Language set to: False 2020-10-19 10:52:27,454 loading file C:\Users\Mohit.flair\models\sentiment-en-mix-distillbert_3.1.pt 2020-10-19 10:52:27 DESKTOP-IDQO3A4 urllib3.connectionpool[10980] DEBUG Starting new HTTPS connection (1): s3.amazonaws.com:443 2020-10-19 10:52:28 DESKTOP-IDQO3A4 urllib3.connectionpool[10980] DEBUG https://s3.amazonaws.com:443 "HEAD /models.huggingface.co/bert/distilbert-base-uncased-config.json HTTP/1.1" 200 0 2020-10-19 10:52:28 DESKTOP-IDQO3A4 urllib3.connectionpool[10980] DEBUG Starting new HTTPS connection (1): s3.amazonaws.com:443 2020-10-19 10:52:28 DESKTOP-IDQO3A4 urllib3.connectionpool[10980] DEBUG https://s3.amazonaws.com:443 "HEAD /models.huggingface.co/bert/bert-base-uncased-vocab.txt HTTP/1.1" 200 0 2020-10-19 10:52:28 DESKTOP-IDQO3A4 urllib3.connectionpool[10980] DEBUG Starting new HTTPS connection (1): newsapi.org:443 2020-10-19 10:52:29 DESKTOP-IDQO3A4 urllib3.connectionpool[10980] DEBUG https://newsapi.org:443 "GET /v2/everything?apiKey=d42e88f1fb624084891e89df549c06ff&qInTitle=%22BTGN%22&sources=reuters,%20the-wall-street-journal,%20cnbc&language=en&sortBy=publishedAt&pageSize=100 HTTP/1.1" 200 46 2020-10-19 10:52:29 DESKTOP-IDQO3A4 urllib3.connectionpool[10980] DEBUG Starting new HTTPS connection (1): api.polygon.io:443 2020-10-19 10:52:29 DESKTOP-IDQO3A4 urllib3.connectionpool[10980] DEBUG https://api.polygon.io:443 "GET /v1/meta/symbols/BTGN/news?apiKey=ZFA3HZJSCNVu7dJGm0Y4pNIBjjIRRg4c HTTP/1.1" 200 22 Hmmmmm... Right now im getting an IndexError. Which means its either the stock market is not open, in which case i would ask you to wait until the markets open, or its an internal error, which you have to investigate, [Line 26, File trading_bot/ops] Hmmmmm... Right now im getting an IndexError. Which means its either the stock market is not open, in which case i would ask you to wait until the markets open, or its an internal error, which you have to investigate, [Line 26, File trading_bot/ops] Traceback (most recent call last): File "eval.py", line 363, in alpaca_trading_bot(stock_name, window_size, model_name) File "eval.py", line 314, in alpaca_trading_bot decisions(agent, data, window_size, debug, stock_name, api) File "eval.py", line 187, in decisions agent.inventory.append(data[t]) IndexError: list index out of range

Flags: python eval.py test --window-size=10 --model-name=model_AMD_alpha --run-bot=y --stock-name=GOOG --debug

I'm not that good when it comes to this kind of stuff, and sorry if it seems like I'm asking a lot of questions, I'm just very interested in Q-learning at the moment and i want to examine this and debug it.

tamuseanmiller commented 4 years ago

Hey! I couldn't reproduce this error, is there anything that you changed?

Also, I super appreciate the activity though I'm definitely not the guy to talk to about Q-learning yet.

GitHubEmploy commented 4 years ago

Oh, yeah the only thing I changed was that I added the automatic QTY aka the 2nd push I did. We should probably hold off on that until this is fixed. Any ideas why this might be happening. Also, can I add a try and exception to stop this from happening?

GitHubEmploy commented 4 years ago

Okay, I printed out the list, and it's empty, could it be the same problem that happened with the previous index error?

tamuseanmiller commented 4 years ago

Oh, yeah the only thing I changed was that I added the automatic QTY aka the 2nd push I did. We should probably hold off on that until this is fixed. Any ideas why this might be happening. Also, can I add a try and exception to stop this from happening?

If you roll back your changes does the error get fixed?

GitHubEmploy commented 4 years ago

Ok, now it's happening in the Ops file again.

Traceback (most recent call last):
  File "eval.py", line 348, in <module>
    alpaca_trading_bot(stock_name, window_size, model_name)
  File "eval.py", line 299, in alpaca_trading_bot
    decisions(agent, data, window_size, debug, stock_name, api)
  File "eval.py", line 57, in decisions
    state = get_state(data, 0, window_size + 1)
  File "C:\Users\Mohit\PycharmProjects\SuperAIPR\newslstmstockbot\trading_bot\ops.py", line 25, in get_state
    block = data[d: t + 1] if d >= 0 else -d * [data[0]] + data[0: t + 1]  # pad with t0
IndexError: list index out of range

This is really weird, I have never seen this happen before.

tamuseanmiller commented 4 years ago

This means that data has no data. Check out the file ticker.csv and check to see if there are values inside of it.

GitHubEmploy commented 4 years ago

It just says, Adj Close, so how do I fix it?

tamuseanmiller commented 4 years ago

Check the lines in the alpaca_trading_bot method to see if it gets the history values.

GitHubEmploy commented 4 years ago
def alpaca_trading_bot(stock_name, window_size=10, model_name='model_debug'):
    # Alpaca API
    api = tradeapi.REST()

    # Create Agent Object
    agent = Agent(window_size, pretrained=True, model_name=model_name)

    # Get Ticker from last intraday times from Polygon
    file = open('ticker.csv', 'w')
    file.write('Adj Close\n')

    # Check for connection errors and retry 30 times
    cnt = 0
    while cnt <= 30:
        try:
            date = api.get_barset(timeframe='minute', symbols=stock_name, limit=1000, end=datetime.datetime.now())
            break

        except:
            logging.warning("Error retrieving initial 1000 prices, retrying in 30s (" + str(cnt) + "/30)")
            time.sleep(30)
            cnt += 1
            continue

    # Write ticker csv
    for minutes in date.get(stock_name):
        file.write(str(minutes.c))
        file.write('\n')

    file.close()

Im pretty sure that it is pulling info, and is writing it. The above is the first half of my function

tamuseanmiller commented 4 years ago

Try and debug by printing the date list and see if it has any values in it

GitHubEmploy commented 4 years ago

I think I just figured out the problem, the stock ticker that I put in wasn't a stock ticker, therefore, no info was written to file. Sorry for taking so long to figure this out, but I think that you should first check if the stock is real lol. Anyways, thank you for your help.

tamuseanmiller commented 4 years ago

I think I just figured out the problem, the stock ticker that I put in wasn't a stock ticker, therefore, no info was written to file. Sorry for taking so long to figure this out, but I think that you should first check if the stock is real lol. Anyways, thank you for your help.

👍 Definitely worth looking into, glad you figured it out