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']))