0

So I wrote the following code:

@bot.command()
async def check(context, ticker):
    print(f"Checking the history of a stock")

    # Make sure the symbol is upper case
    if isinstance(ticker, str):
        ticker = ticker.upper()
    # Retrieve the last 100 days of trading data
    bars = alpaca_api.get_bars(symbol=ticker, timeframe=tradeapi.TimeFrame(1, tradeapi.TimeFrameUnit.Day), limit=100).df

    # This bytes buffer will hole the image we send back
    fig = io.BytesIO()
    # Grab the last closing price
    last_price = bars.tail(1)['close'].values[0]
    # Make a chart from the data we retrieved
    plt.title(f"{ticker} -- Last Price ${last_price:.02f}")
    plt.xlabel("Last 100 days")
    plt.plot(bars["close"])
    # Save the image to the buffer we created earlier
    plt.savefig(fig, format="png")
    fig.seek(0)
    # Sending back the image to the user.
    await context.send(file=discord.File(fig, f"{ticker}.png"))
    plt.close()

and as an example I gave it gme as the ticker, but when I run the command in discord, I get the following error:

[2022-11-06 15:37:44] [ERROR   ] discord.ext.commands.bot: Ignoring exception in command check
Traceback (most recent call last):
  File "C:\Users\Person\Desktop\Discord Bots\stonks-bot\venv\lib\site-packages\pandas\core\indexes\base.py", line 3803, in get_loc
    return self._engine.get_loc(casted_key)
  File "pandas\_libs\index.pyx", line 138, in pandas._libs.index.IndexEngine.get_loc
  File "pandas\_libs\index.pyx", line 165, in pandas._libs.index.IndexEngine.get_loc
  File "pandas\_libs\hashtable_class_helper.pxi", line 5745, in pandas._libs.hashtable.PyObjectHashTable.get_item
  File "pandas\_libs\hashtable_class_helper.pxi", line 5753, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 'close'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\Person\Desktop\Discord Bots\stonks-bot\venv\lib\site-packages\discord\ext\commands\core.py", line 190, in wrapped
    ret = await coro(*args, **kwargs)
  File "C:\Users\Person\Desktop\Discord Bots\stonks-bot\bot.py", line 96, in check
    last_price = bars.tail(1)['close'].values[0]
  File "C:\Users\Person\Desktop\Discord Bots\stonks-bot\venv\lib\site-packages\pandas\core\frame.py", line 3804, in __getitem__
    indexer = self.columns.get_loc(key)
  File "C:\Users\Person\Desktop\Discord Bots\stonks-bot\venv\lib\site-packages\pandas\core\indexes\base.py", line 3805, in get_loc
    raise KeyError(key) from err
KeyError: 'close'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\Person\Desktop\Discord Bots\stonks-bot\venv\lib\site-packages\discord\ext\commands\bot.py", line 1347, in invoke
    await ctx.command.invoke(ctx)
  File "C:\Users\Person\Desktop\Discord Bots\stonks-bot\venv\lib\site-packages\discord\ext\commands\core.py", line 986, in invoke
    await injected(*ctx.args, **ctx.kwargs)  # type: ignore
  File "C:\Users\Person\Desktop\Discord Bots\stonks-bot\venv\lib\site-packages\discord\ext\commands\core.py", line 199, in wrapped
    raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: KeyError: 'close'

I read some tutorials on how to accomplish what I am trying to do, but they all seem to be outdated and do not work anymore. does anyone know how to resolve this error? Thanks!

CoderMan
  • 37
  • 8
  • Please [edit] your question and post the [*full text* of any errors or tracebacks](https://meta.stackoverflow.com/q/359146). – MattDMo Nov 06 '22 at 16:15

0 Answers0