0

I'm trying to implement a Trading bot in Kraken using Krakenex api and Talib, but the code does not work it has this error:

Traceback (most recent call last): File "c:\Users\user1\OneDrive\tradingbot\krakenexBot.py", line 19, in timestamps = ohlc['XXBTZUSD']['timestamp'] TypeError: string indices must be integers

import krakenex
import talib
import time
import pandas as pd

# Set your Kraken API key and secret
api_key = 'cD8Jm4LaKHNuLHPxd1rotttPNwtVBp7iWzXl8hdqzFAPSaSTLhx3m5ZP'
api_secret = 'ynuaaCuD5h2JSu9FOfT1534qW0AYg9RoEhUvgwicrtALCkw88adQwjDORu+XDL3VDg5n8i4EgvHsduopa6f2jg=='

# Initialize the Kraken API client
kraken = krakenex.API(key=api_key, secret=api_secret)

pair = 'XXBTZEUR'
while True:
    # Get the daily OHLC data for BTC/USD
    ohlc, last = kraken.query_public('OHLC', data={"pair": "XXBTZUSD", "interval": 1440})

    # Extract the timestamp, open, high, low, and close prices from the OHLC data
    timestamps = ohlc['XXBTZUSD']['timestamp']
    opens = ohlc['XXBTZUSD']['open']
    highs = ohlc['XXBTZUSD']['high']
    lows = ohlc['XXBTZUSD']['low']
    closes = ohlc['XXBTZUSD']['close']
    rsi = talib.RSI(closes, timeperiod=14)
    # Check if the RSI is below 30 or above 70
    if rsi < 30:
        # Place a buy order
        #response = kraken.query_private('AddOrder', {'pair': pair, 'type': 'buy', 'ordertype': 'market', 'volume': '1'})
        print(f'Bought 1 unit at {price}')
    elif rsi > 70:
        # Place a sell order
        #response = kraken.query_private('AddOrder', {'pair': pair, 'type': 'sell', 'ordertype': 'market', 'volume': '1'})
        print(f'Sold 1 unit at {price}')

    # Sleep for a minute before checking again
    time.sleep(60)

I expected that the OHLC will get the data since its a dictionary, I think.

derloopkat
  • 6,232
  • 16
  • 38
  • 45
  • Instead of `ohlc['XXBTZUSD']['open']` you probably need `ohlc['XXBTZUSD'][1]` and so on for high, low, close, etc. Check out Kraken Rest api [documentation](https://docs.kraken.com/rest/#tag/Market-Data/operation/getOHLCData). Expand "200 OLHC data received" and expand "result". This should clarify what are the elements of the array. `[int – derloopkat Jan 16 '23 at 03:40

0 Answers0