#!/usr/bin/python3
# https://binance-docs.github.io/apidocs/spot/en/#test-new-order-trade

# we have one thread to maintain the dataset, another thread would run the pytorch model every 5 seconds, to see if it's time to place order. 

import json, time
from websocket import create_connection

# [(Snapshot_Time, [(buy_price1, amount1), ...] , [(sell_price1, amount1), ...]), ...]
# [(1620457034392, [(56000, 0.01), (55900, 1), (55700, 30), ...] , [(57000, 0.01), (57100, 1), ...] ), (1620457034394, [...]), ...]
# The snapshots should has almost identical time-interval. Good for LSTM. 
# Time axis: [history, older, newer, ..., latest]
realtime_shortterm_dataset_aggtrade = []
realtime_shortterm_dataset_aggtrade_size = 1024

# [(Trade_Time, PRICE, AMOUNT), ...]
# [(1620457034392, 56000, 0.5), (1620457034394, 56001, 0.05), ...]
# The trades usually have various time-interval. TODO: transform it to [(WeightedAvgPrice, Volume), ...] for every 1 minutes? 
# Time axis: [history, older, newer, ..., latest]
realtime_shortterm_dataset_depth = []
realtime_shortterm_dataset_depth_size = 1024*1024

def dataset_maintain_thread_main():
    global realtime_shortterm_dataset_aggtrade, realtime_shortterm_dataset_aggtrade_size, realtime_shortterm_dataset_depth, realtime_shortterm_dataset_depth_size

    ws = create_connection("wss://stream.binance.com:9443/ws/btcusdt")
    ws.send(json.dumps({
        "method": "SUBSCRIBE",
        "params": ["btcusdt@depth"],
        "id": 1,
    }))

    subs_response = ws.recv()
    if '"result":null' not in subs_response:
        raise RuntimeError("Failed to subscribe: server says: " + subs_response)
    
    _debug_tmp = 0
    while True:
        message = ws.recv()
        # print("DEBUG: message=", message)
        if len(message) > 1000:
            # Is a depth snapshot update
            a = 1
        else:
            print("D: ", message)

        ###### DEBUG ONLY
        # if int(time.time()) % 60 == 0 and _debug_tmp != int(time.time()):
        #     _debug_tmp = int(time.time())
        #     print("DEBUG: dumping realtime_shortterm_dataset_aggtrade================")
        #     print(realtime_shortterm_dataset_aggtrade)
        #     print("DEBUG: dumping realtime_shortterm_dataset_depth================")
        #     print(realtime_shortterm_dataset_depth)
        #     print("DEBUG: dumping END ==|||||||||||||||||||||||||||||================")
    
    ws.close()

dataset_maintain_thread_main()

