我在使用 TA-Lib 库中的 ATR-Indicator 时遇到了一些问题。其他所有指标似乎都运行良好。
这是python中的代码:
import json
import numpy
import talib
import websocket
# *******PARAMETERS
# Name of Socket for BTC in USDT 1min Candle
SOCKET = "wss://stream.binance.com:9443/ws/btcusdt@kline_1m"
# Arrays
closes = []
highs = []
lows = []
# ***Websocket definition***
def on_open_ws(ws):
print('opened connection')
def on_close_ws(ws):
print('closed connection')
def on_message_ws(ws, message):
json_message = json.loads(message)
# only watching the candle infos in received message
candle = json_message['k']
# getting close, high and low values
is_candle_closed = candle['x']
close = float(candle['c'])
high = float(candle['h'])
low = float(candle['l'])
if is_candle_closed:
closes.append(close) # add close price to closes-array
np_closes = numpy.array(closes) # convert closes-array in numpy
c_close = np_closes[-1] # current close
p_close = np_closes[-2] # previous close
print(len(closes))
highs.append(high)
np_highs = numpy.array(highs)
last_high = np_highs[-1]
lows.append(low)
np_lows = numpy.array(lows)
last_low = np_lows[-1]
# Set ATR
atr = talib.ATR(np_highs, np_lows, np_closes, timeperiod=14)
last_atr = atr[-1]
print('last atr')
print(last_atr)
ws = websocket.WebSocketApp(SOCKET, on_open=on_open_ws, on_close=on_close_ws, on_message=on_message_ws)
# Run websocket app
ws.run_forever()
最后一个命令 "print('last atr') 和 print(last_atr) 不打印,表明 atr 不工作。
不知何故知道可能是什么问题?
我尝试只使用高、低和关闭的最后一个值以及非 numpied 值,但这并没有改变任何东西。我什至没有得到前几个值的“nan”答案......