skip to Main Content

I’m trying to read the TradesHistory from krakenn but it doesn’t work with the index. It only works if I enter the right trade. How can I step through each individual trade individually?

With the following command I get all the trades. It works.

print(resp.json()['result']['trades'])

If I wanted to access the first index now, it doesn’t work.

print(resp.json()['result']['trades'][0]['ordertxid'])

Only after entering the correct trade id can I access the following index.

print(resp.json()['result']['trades']['12345-abcde-ZYXWE']['ordertxid'])

What am I doing wrong? How can I access the correct index without first knowing the ID?

{
   "error":[
      
   ],
   "result":{
      "count":2,
      "trades":{
         "12345-abcde-ZYXWE":{},
         "ZYXWE-12345-abcde":{
            "ordertxid":"xyz",
         }
      }
   }
}

various Python index commands with different JSON formats

2

Answers


  1. In order to go over all the trades, you can loop over the trades that were made and extract their information.

    You can use a for loop like this:

    trades = resp.json()['result']['trades']
    for trade in trades: # accessing each trades
        if trades[trade] == {}:
            print('trade {} is empty.'.format(trade))
            continue
        print('{}'s trade information:'.format(trade))
        for value in trades[trade]: # accessing each value inside the trade dictionary
            print(value, ':', trades[trade][value])
    
    Login or Signup to reply.
  2. I tried with this code below it is working.

    import json
    f = open('test.json')
    test  = json.load(f)
    test['result']['trades']['ZYXWE-12345-abcde']['ordertxid']
    
    Output
    'xyz'
    

    And looking at your code it looks like you are referring to wrong index here [‘12345-abcde-ZYXWE’]

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search