0

I created a Python script to fetch open orders from a cryptocurrency exchange.

When i connect to the api using:

order = exchange.fetch_open_orders(symbol)

I receive this response:

[{'info': {'symbol': 'ETHBTC', 
            'orderId': 507325551, 'orderListId': -1, 'clientOrderId': 
            'web_b75c7f9be90849beac14cd86f575ac01', 'price': '0.02504200', 
            'origQty': '0.02100000', 'executedQty': '0.00000000', 
            'cummulativeQuoteQty': '0.00000000', 'status': 'NEW', 
            'timeInForce': 'GTC', 'type': 'LIMIT', 'side': 'SELL', 
            'stopPrice': '0.00000000', 'icebergQty': '0.00000000', 
            'time': 1571163346981, 'updateTime': 1571163346981, 'isWorking': True}, 
            'id': '507325551', 'timestamp': 1571163346981, 'datetime': '2019-10-15T18:15:46.981Z', 
            'lastTradeTimestamp': None, 
            'symbol': 'ETH/BTC', 'type': 'limit', 
            'side': 'sell', 'price': 0.025042, 
            'amount': 0.021, 'cost': 0.0, 'average': None, 'filled': 0.0, 
            'remaining': 0.021, 'status': 'open', 'fee': None, 'trades': None}]

Which is a very long response and i don't want everything here, To print only some values i tried this:

for x in order:
    sym = x['symbol']
    price = x['price']
    status = x['status']
    amount = x['amount']
    side = x['side']
    orig = x['origQty']


print(sym, price, status, amount, side, orig)

This code works until the line orig = x['origQty'] where i get a KeyError: 'origQty'.

I don't understand where is this coming from since all the other variables are printed without any error and because origQty is in the response, whereas this error usually appears when i try to look for something that doesn't exist. Can someone help me find what i'm doing wrong?

1
  • The origQty key exists only in the inner dict. You are only getting items from the outer dict. Use x["info"] ["origQty"] instead. Commented Oct 15, 2019 at 19:51

2 Answers 2

1

You can see that your dict has an inner dict which contains origQty

so orig = x['info']['origQty']

Update

check this, i think it will make it more clear in visualaizing it :)

Sign up to request clarification or add additional context in comments.

4 Comments

It's working! But why don't i have to do this for all the other values? Give me 6 minutes to accept your answer
Because those keys are also present outside of info
@Jack022 i added a link, go check it, it display the json visual, which help you to understand it
Checking it out. Thanks again @Reznik!
1

'origQty' is nested under the 'info' key, try using x['info']['origQty'] instead.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.