0

Someone please help me with the following code? It aims to get data from kraken exchange account with api keys stored in an excel file.

Running it, return the following error, but I really do not understand how to solve the issue here.

value = float(balances['result'][currency]) * float(prices['{} 
{}'.format(currency, 'USD')]['c'][0])


TypeError: list indices must be integers or slices, not str
Process finished with exit code 1

The python script is the following:

import CurrencyViewer as cv
import krakenex
import openpyxl

# Load API keys from Excel file
workbook = openpyxl.load_workbook('../API_KEYS.XLSX')
sheet = workbook['Kraken']
kraken_api_key = sheet['CELL1'].value
kraken_secret_key = sheet['CELL2'].value

# Create a Kraken API client using the API keys
kraken_client = krakenex.API(key=kraken_api_key, secret=kraken_secret_key)

# Get the user's balances for all currencies
balances = kraken_client.query_private('Balance')

# Get the list of currencies owned by the user
currencies = list(balances['result'].keys())

# Get the list of markets concerned by the user's currencies
markets = ['{}{}'.format(currency, 'USD') for currency in currencies]

# Get the current prices for all markets
prices = kraken_client.query_public('Ticker', {'pair': ','.join(markets)})
market_key = list(prices.keys())[0]  # Get the dynamic key
prices = prices[market_key]

# Create a dictionary of current values in fiat money the user's crypto currencies balance is equivalent to
values = {}
for currency in currencies:
    value = float(balances['result'][currency]) * float(prices['{}{}'.format(currency, 'USD')]['c'][0])
    values[currency] = value

# Get the user's trade history
trade_history = kraken_client.query_private('TradesHistory')

# Print the results
print('Currencies: {}'.format(currencies))
print('Balances: {}'.format(balances['result']))
print('Markets: {}'.format(markets))
print('Prices: {}'.format(prices))
print('Values: {}'.format(values))
print('Trade History: {}'.format(trade_history['result']['trades']))

I expect it return:

currencies : List of differents currencies owned by user
balance : List of the differents amount of crypto currencies owned
market : List of markets concerned by currencies in user's wallet (same order as price)
values : Dictionnary of current values in fiat money user's crypto currencies balance is equivalent to (according to real-time markets) Keys are dynamically generated by markets concerned by user
Trade history: history of trades.
Sydney_dev
  • 1,448
  • 2
  • 17
  • 24
Dek
  • 1
  • 1
  • What do `balance` and `prices` look like? try to execute the code from the line that has the error bit by bit, to track where the error comes from exactly. Since there's a lot going on on that line, it's easier if you can isolate where exactly the error occurs. Once you found that, look at the object. Chances are, something that you think is a `dict` is actually a `list`, hence the error you are getting – Florent Monin Mar 14 '23 at 10:01

0 Answers0