【发布时间】:2021-05-12 19:40:45
【问题描述】:
我有一个用于流式传输在线数据的代码块,但与此同时,我希望不时运行另一个代码块以进行分析。
class steaming_price():
def on_open(ws):
print('opened connection')
def on_close(ws):
print('closed connection')
def on_message(ws, message):
global closes, in_position, current_time
print('received message')
json_message = json.loads(message)
#pprint.pprint(json_message)
candle = json_message['k'] #all kline data
is_candle_closed = candle['x'] #if its closed
close = candle['c'] #the Close Price
#Print the Close Price
if is_candle_closed:
print("candle closed at {}".format(close))
closes.append(float(close))
current_time.append(datetime.datetime.now())
print(f"Time:{datetime.datetime.now()} Close:{close}")
ws = websocket.WebSocketApp(SOCKET, on_open=on_open, on_close=on_close, on_message=on_message)
ws.run_forever()
以下应该每 1 分钟运行一次以进行分析
class strategy():
def __init__(self):
self.closes = deque(maxlen=500)
def strategy(self, data):
self.macd, self.macd_signal, self.macd_hist = talib.MACD(data, fastperiod=12, slowperiod=26, signalperiod=9)
return self.macd, self.macd_signal, self.macd_hist
第一个代码块每秒都会获取数据。 我想让第二个代码块每 1 分钟并行运行一次以进行分析。例如,计算 20 bar 移动平均线并发出买单。
我考虑过异步,但它只运行一次(我也无法让它工作)。
有什么想法吗?非常感谢
【问题讨论】:
标签: python websocket async-await