Python Forum
I keep getting the same error over and over again.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
I keep getting the same error over and over again.
#1
Hello. I am trying to write a trading algorithm for kraken's crypto exchange. I keep getting an error where it says "result"; here is an example:

I am trying to get the daily close price from kraken but I keep getting an error. Here is the code:

import krakenex
import pandas as pd

# create a Kraken API client
api = krakenex.API()

# set the currency pair and interval
pair = 'XBT/USD'
interval = 1440  # 1440 minutes = 1 day

# make the API request for OHLC data
ohlc_data = api.query_public('OHLC', {'pair': pair, 'interval': interval})

# convert the OHLC data into a pandas dataframe
df = pd.DataFrame(ohlc_data['result'][pair], columns=['time', 'open', 'high', 'low', 'close', 'vwap', 'volume', 'count'])
df['time'] = pd.to_datetime(df['time'], unit='s')
df.set_index('time', inplace=True)

# get the daily close price
daily_close = df['close'].resample('D').last()

# print the most recent daily close price
print(daily_close.iloc[-1])
In the terminal, I am getting this error:


The error I am getting is: line 15, in <module>
    df = pd.DataFrame(ohlc_data['result'][pair], columns=['time', 'open', 'high', 'low', 'close', 'vwap', 'volume', 'count'])
KeyError: 'result'.
I've had this error from several bits of code I have wrote. All with the word result. I am new to programming and I'm using chatgpt to help me. Could anyone give me some feedback please?
Reply
#2
Post the error message, including the traceback.

What is ohlc_data when you get this error?
Reply
#3
(Apr-19-2023, 12:19 PM)deanhystad Wrote: Post the error message, including the traceback.

This is the full error message:

line 15, in <module>
    df = pd.DataFrame(ohlc_data['result'][pair], columns=['time', 'open', 'high', 'low', 'close', 'vwap', 'volume', 'count'])
                      ~~~~~~~~~^^^^^^^^^^
KeyError: 'result'
the only other thing I can include is the folders it's in.


Quote:What is ohlc_data when you get this error?

pd.DataFrame(ohlc_data) is a method in the pandas library in Python that creates a new DataFrame object from the data returned by the Kraken API query for OHLC (open-high-low-close) data.

The ohlc_data variable is a dictionary object that contains the OHLC data for the requested currency pair and time interval. The pd.DataFrame method converts this dictionary object into a DataFrame with columns named 'time', 'open', 'high', 'low', 'close', 'vwap', 'volume', and 'count', which correspond to the different OHLC parameters returned by the Kraken API.

The resulting DataFrame can be used to easily manipulate and analyze the OHLC data using pandas' powerful and intuitive methods. In the code example provided, we resample the OHLC data into daily intervals and extract the daily close prices using pandas' resample and last methods.
Reply
#4
But what is ohlc_data when you get the error? It a dictionary, but it does not have a key named "result". What keys does it have? Maybe an error occurred, or there are no results matching the request. Find out what information is returned and write code to handle the different cases.
Reply
#5
(Apr-19-2023, 12:50 PM)deanhystad Wrote: But what is ohlc_data when you get the error? It a dictionary, but it does not have a key named "result". What keys does it have? Maybe an error occurred, or there are no results matching the request. Find out what information is returned and write code to handle the different cases.

Thanks for helping me, and forgive my ignorance. How would I find out what information is returned?
Reply
#6
You could set breakpoints in your debugger and look at ohc_data after it is returned but before you create df.

If you are not comfortable using a debugger you could modify the code to check if olhc_data contains a result key.
if 'result' in olhc_data:
If 'result' is not on ohlc_data, print the dictionary, or the dictionary keys and quit, otherwise process the result as before.
if 'result' not in olhc_data:
    print(ohlc_data)  # Try one of these
    print(list(ohlc_data.keys())
    quit()
And if you don't like doing that, you can run python and enter these lines one at a time.
import krakenex
api = krakenex.API()
data = api.query_public('OHLC', {'pair': 'XBT/USD', 'interval': 1440})
data  # will print dictionary
Reply
#7
I tried your third option. I ran the code one line at a time and then the whole lot. Each time, nothing happened. The terminal just reset to how it was before. Could it be that where chat gpt is getting it's data from has been revised and no longer relevant?
Reply
#8
This is wrong pair = 'XBT/USD'
If you had look at ohlc_data as mention.
(dl_env) G:\div_code\dl_env
λ python -i kr.py
>>> ohlc_data
{'error': ['EQuery:Unknown asset pair']}
Change to pair = 'XXBTZUSD'
(dl_env) G:\div_code\dl_env
λ python -i kr.py
>>> ohlc_data
{'error': [], 'result': {'XXBTZUSD': [[1619740800, '53579.7', '57987.8', '53071.6',.....
Reply
#9
Oh yes. I did actually try a few different asset pair combinations but I didn't land on the right one.

So why did it say the problem was the 'result' ?
Reply
#10
(Apr-19-2023, 02:58 PM)SuchUmami Wrote: So why did it say the problem was the 'result' ?
Because Api return a Python dictionary and when key not in in dictionary,it will give a KeyError.
When there is a error there is only a error key in dictionary.
>>> ohlc_data = {'error': ['EQuery:Unknown asset pair']}
>>> ohlc_data['result']
Traceback (most recent call last):
  File "<interactive input>", line 1, in <module>
KeyError: 'result'

>>> ohlc_data['error']
['EQuery:Unknown asset pair']

>>> ohlc_data.keys()
dict_keys(['error'])
>>> ohlc_data.values()
dict_values([['EQuery:Unknown asset pair']])
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020