Websocket ile 2 farklı zaman dilimine ait mum verilerini aynı anda almak

Merhaba arkadaşlar aynı anda 2 farklı zamanın mum verilerini çekmek istiyorum bu şekilde yaptım fakat randıman alır mıyım ? bunun daha stabil olanı var mıdır ? yapılabilir mi ?
yardımcı olacak arkadaşlara şimdiden tşk ederim. Amacım websocket ile 2 farklı zaman dilimine ait mum verileri aynı anda almak…

[code]
import json, time
from threading import Thread
from websocket import create_connection, WebSocketConnectionClosedException
from datetime import datetime

def mum_verileri(dakika1,saat4):
print(dakika1,saat4, "ZAMAN : ",datetime.now())

def main():

ws = None
thread = None
thread_running = False
thread_keepalive = None
def websocket_thread():
global ws
ws = create_connection(“wss://stream.binance.com:9443/ws”)
ws.send(
json.dumps(
{
“method”: “SUBSCRIBE”,
“params”: [“btcusdt@kline_1m”],
“id”: 1,
}
)
)
wst = create_connection(“wss://stream.binance.com:9443/ws”)
wst.send(
json.dumps(
{
“method”: “SUBSCRIBE”,
“params”: [“btcusdt@kline_4h”],
“id”: 1,
}
)
)
thread_keepalive.start()
rakam = 0
while not thread_running:

try:
data = ws.recv()
datam = wst.recv()
if data != “”:
msg = json.loads(data)
msgi = json.loads(datam)
else:
msg = {}
except ValueError as e:
print(e)
print("{} - data: {}".format(e, data))
except Exception as e:
print(e)
print("{} - data: {}".format(e, data))
else:

if “result” not in msg:

if msg[‘k’][‘i’]==‘1m’:
durum_1m = msg[‘k’][‘o’]
if msgi[‘k’][‘i’]==‘4h’:
durum_4h = msgi[‘k’][‘o’]

mum_verileri(durum_1m,durum_4h)

try:
if ws:
ws.close()
except WebSocketConnectionClosedException:
pass
finally:
thread_keepalive.join()

def websocket_keepalive(interval=30):
global ws
while ws.connected:
ws.ping(“keepalive”)
time.sleep(interval)

thread = Thread(target=websocket_thread)
thread_keepalive = Thread(target=websocket_keepalive)
thread.start()

if name == “main”:
main()[/code]

bkz: Soru Sorarken Sıkça Düşülen Hatalar #1

hocam r10dada aynı soruyu sormuşsunuz kodları düzgün atmamışsınız yine, bir düzenlerseniz insanlar yardımcı olucak.

1 Beğeni

düzenleme süresi geçti sayfaya biraz yabancı kaldım bende kod bloğu tag yerini bulamadım :frowning:

Hm evet linkteki yazi hataliymis, tesekkurler, hemen duzeltiyorum. Bu arada buraya yeni bir “yanit” olarak (hemen ustteki mesaj gibi) da eklenebilir kod.

import json, time
from threading import Thread
from websocket import create_connection, WebSocketConnectionClosedException
from datetime import datetime

def mum_verileri(dakika1,saat4):
print(dakika1,saat4, "ZAMAN : ",datetime.now())

def main():

ws = None
thread = None
thread_running = False
thread_keepalive = None
def websocket_thread():
global ws
ws = create_connection(“wss://stream.binance.com:9443/ws”)
ws.send(
json.dumps(
{
“method”: “SUBSCRIBE”,
“params”: [“btcusdt@kline_1m”],
“id”: 1,
}
)
)
wst = create_connection(“wss://stream.binance.com:9443/ws”)
wst.send(
json.dumps(
{
“method”: “SUBSCRIBE”,
“params”: [“btcusdt@kline_4h”],
“id”: 1,
}
)
)
thread_keepalive.start()
rakam = 0
while not thread_running:

try:
data = ws.recv()
datam = wst.recv()
if data != “”:
msg = json.loads(data)
msgi = json.loads(datam)
else:
msg = {}
except ValueError as e:
print(e)
print("{} - data: {}".format(e, data))
except Exception as e:
print(e)
print("{} - data: {}".format(e, data))
else:

if “result” not in msg:

if msg[‘k’][‘i’]==‘1m’:
durum_1m = msg[‘k’][‘o’]
if msgi[‘k’][‘i’]==‘4h’:
durum_4h = msgi[‘k’][‘o’]

mum_verileri(durum_1m,durum_4h)

try:
if ws:
ws.close()
except WebSocketConnectionClosedException:
pass
finally:
thread_keepalive.join()

def websocket_keepalive(interval=30):
global ws
while ws.connected:
ws.ping(“keepalive”)
time.sleep(interval)

thread = Thread(target=websocket_thread)
thread_keepalive = Thread(target=websocket_keepalive)
thread.start()

if name == “main”:
main()