0

With python I get a list like below from the server. But when I want to return it in "for" loop, it gives me a list error. How can I fix this?

So in summary, I want to access data such as "Open, Close" in the incoming "klines" in the "for" loop below.

Error

 Traceback (most recent call last):
 File "C:\Users\winuser\Desktop\proje\Python Projects\main.py", line 53, in <module>
 if is_bullish_engulfing(candle=klines, index=i):
 File "C:\Users\winuser\Desktop\proje\Python Projects\main.py", line 21, in is_bullish_engulfing
 current_candle = candle(index)
 TypeError: 'list' object is not callable

Code

from binance.client import Client
import time


class BinanceConnection:
    def __init__(self, file):
        self.connect(file)

    def connect(self, file):
        lines = [line.rstrip('\n') for line in open(file)]
        key = lines[0]
        secret = lines[1]
        self.client = Client(key, secret)


def is_bearish_candlestick(candle):
    return candle['Close'] < candle['Open']


def is_bullish_engulfing(candle, index):
    current_candle = candle(index)
    previous_candle = candle(index-1)

    if is_bearish_candlestick(previous_candle) \
            and current_candle['Close'] > previous_candle['Open'] and current_candle['Open'] < previous_candle['Close']:
        return True
    return False


if __name__ == '__main__':
    filename = 'credentials.txt'
    connection = BinanceConnection(filename)

    symbol = 'ADAUSDT'
    interval = '1m'
    limit = 100

    while True:
        time.sleep(10)

        try:
            klines = connection.client.get_klines(symbol=symbol, interval=interval, limit=limit)
        except Exception as exp:
            print(exp.status_code, flush=True)
            print(exp.message, flush=True)

        open = [float(entry[1]) for entry in klines]
        high = [float(entry[2]) for entry in klines]
        low = [float(entry[3]) for entry in klines]
        close = [float(entry[4]) for entry in klines]

        for i in range(1, len(klines)):
            if is_bullish_engulfing(candle=klines, index=i):
                print("{} it is a bullish engulfing".format(klines[i]['Date']))
johnvayne
  • 79
  • 3
  • 8
  • based on the error, it looks like `candle` is a list object so you can't do `candle(index)`, did you mean: `candle[index]`? – Krishna Chaurasia Mar 17 '21 at 08:01
  • 1
    I assume you already know python. In that case, look at the message really hard, especially at the line `current_candle = candle(index)` and ask yourself one question: How can I access an element of a list given an index? I'm positive you'll find the solution by yourself. – Tobias Brösamle Mar 17 '21 at 08:01
  • Yes, the error was fixed as candle [index], but now I get an error like the following. – johnvayne Mar 17 '21 at 08:06
  • `Traceback (most recent call last): File "C: \ Users \ winuser\ Desktop \ project \ Python Projects \ main.py", line 53, in if is_bullish_engulfing (candle = klines, index = i): File "C: \ Users \ winuser \ Desktop \ project \ \ main.py", line 24, in is_bullish_engulfing if is_bearish_candlestick (previous_candle) \ File "C: \ Users \ winuser\ Desktop \ project \ Python Projects \ main.py", line 17, in is_bearish_candlestick return candle ['Close'] – johnvayne Mar 17 '21 at 08:06
  • 1
    do you notice the error clearly say: `return candle ['Close'] – Krishna Chaurasia Mar 17 '21 at 08:09

0 Answers0