jane-cloud / Open-API-SDK-V5

360 stars 212 forks source link

Latencyt problem #8

Open Srcanyildiz opened 3 years ago

Srcanyildiz commented 3 years ago

I get latency problem with below code. Do you have any idea how to fix that?

error_o = 1
while error_o <= 1:
    try:
        wso = create_connection("wss://wsaws.okex.com:8443/ws/v5/public")
        error_o = 2
        continue
    except:
        print('connect ws error,retry...')
        error_o = 0
        time.sleep(5)

trade_okex ="""
    {
      "op": "subscribe",
      "args": [
        {
          "channel": "trades",
          "instId": "BTC-USDT"
        }
      ]
    }
    """
wso.send(trade_okex)

db_one_o = []
db2_o =[]

dif = dt.datetime.now()

while True :
try:
    db_one_o.append(json.loads(wso.recv()))

    for i in range(1,len(db_one_o)):
        if db_one_o[i]['data'][0]['side'] == 'buy':
            drct = 1 
        else:
            drct = -1     

        db2_o.append({'datetime' : datetime.fromtimestamp(float(db_one_o[i]['data'][0]['ts'])/1000),
                    'date_time' : datetime.fromtimestamp(float(db_one_o[i]['data'][0]['ts'])/1000).strftime('%Y-%m-%d %H:%M'),
                    'price': float(db_one_o[i]['data'][0]['px']),
                    'qty': float(db_one_o[i]['data'][0]['sz'])*drct, 
                    'Volume' : float(db_one_o[i]['data'][0]['px'])*float(db_one_o[i]['data'][0]['sz'])*drct})
except:
    continue

def send(msg, chat_id, token=token):
    bot = telegram.Bot(token=token)
    bot.sendMessage(chat_id=chat_id, text= str(msg))  

try:
    db3_o = pd.DataFrame(db2_o)
    db3_o.drop_duplicates(keep='first',inplace=True)
    db3_o.reset_index(drop=True)
    db4_o = db3_o.groupby('date_time').agg({'qty':['sum'],'price':['mean'],'Volume':['sum']}).reset_index()
    db5_o = pd.DataFrame({'Time':db4_o['date_time'], 'qty':db4_o['qty']['sum'], 'price':db4_o['price']['mean'], 'Volume':db4_o['Volume']['sum']})  
except:
    continue`