Getting multiple cryptos' ohlc values in ccxt or binance python api?

3.3k views Asked by At

I'm trying to create an automated bot according to my strategy but I'm struggling with a few things:

1- In ccxt, I can get OHLC data of a specified symbol. But I couldn't find any data on both documentation and google etc, about getting multiple symbols' ohlc data.

For example: ohlcv = exchange.fetch_ohlcv(symbol, '1d', limit=500)

With this code if I change symbol to BTCUSDT and I can get the data. But I want multiple symbols at once. Like couple of rows including Btcusdt, ethusdt, xrpusdt etc...

My desired output is:

pair       open    high   low    close
btcusdt    38000   38005  37995  38002
ethusdt    1234     2132   1233    1434
xrpusd       1      2      0.9      1.1

2- I also tried binance api and websocket connection. with multiple stream url, I can get each symbols' 5m candlestick data at once. I get an output like I shared below but I can't make them work cuz each symbol has different tables. Also I can filter the data as getting only closed candles (True) but after it gets its first output and it keep repeating it. I can't get new candles.

     Symbol Frame    Open   Close    High     Low    Candle Time  Candle Closed
0  CTSIUSDT    5m  0.2720  0.2716  0.2722  0.2705  1651601399999          False
    Symbol Frame     Open  ...      Low    Candle Time Candle Closed
0  RENUSDT    5m  0.28050  ...  0.28010  1651601399999         False

[1 rows x 8 columns]
     Symbol Frame     Open  ...      Low    Candle Time Candle Closed
0  AAVEUSDT    5m  140.320  ...  140.010  1651601399999         False

[1 rows x 8 columns]
base_url = "wss://fstream.binance.com/stream?streams="
symbols_wo = ["1000shibusdt@kline_5m/1000xecusdt@kline_5m/1inchusdt@kline_5m/aaveusdt@kline_5m/adausdt@kline_5m/akrousdt@kline_5m/algousdt@kline_5m/aliceusdt@kline_5m/alphausdt@kline_5m/ancusdt@kline_5m/ankrusdt@kline_5m/antusdt@kline_5m/apeusdt@kline_5m/api3usdt@kline_5m/arpausdt@kline_5m/arusdt@kline_5m/atausdt@kline_5m/atomusdt@kline_5m/audiousdt@kline_5m/avaxusdt@kline_5m/axsusdt@kline_5m/bakeusdt@kline_5m/balusdt@kline_5m/bandusdt@kline_5m/batusdt@kline_5m/bchusdt@kline_5m/belusdt@kline_5m/blzusdt@kline_5m/bnbusdt@kline_5m/bnxusdt@kline_5m/btcdomusdt@kline_5m/btcusdt@kline_5m/btsusdt@kline_5m/c98usdt@kline_5m/celousdt@kline_5m/celrusdt@kline_5m/chrusdt@kline_5m/chzusdt@kline_5m/compusdt@kline_5m/cotiusdt@kline_5m/crvusdt@kline_5m/ctkusdt@kline_5m/ctsiusdt@kline_5m/cvcusdt@kline_5m/darusdt@kline_5m/dashusdt@kline_5m/defiusdt@kline_5m/dentusdt@kline_5m/dgbusdt@kline_5m/dodousdt@kline_5m/dogeusdt@kline_5m/dotusdt@kline_5m/duskusdt@kline_5m/dydxusdt@kline_5m/egldusdt@kline_5m/enjusdt@kline_5m/ensusdt@kline_5m/eosusdt@kline_5m/etcusdt@kline_5m/ethusdt@kline_5m/filusdt@kline_5m/flmusdt@kline_5m/flowusdt@kline_5m/ftmusdt@kline_5m/fttusdt@kline_5m/galausdt@kline_5m/gmtusdt@kline_5m/grtusdt@kline_5m/gtcusdt@kline_5m/hbarusdt@kline_5m/hntusdt@kline_5m/hotusdt@kline_5m/icpusdt@kline_5m/icxusdt@kline_5m/imxusdt@kline_5m/iostusdt@kline_5m/iotausdt@kline_5m/iotxusdt@kline_5m/jasmyusdt@kline_5m/kavausdt@kline_5m/klayusdt@kline_5m/kncusdt@kline_5m/ksmusdt@kline_5m/linausdt@kline_5m/linkusdt@kline_5m/litusdt@kline_5m/lptusdt@kline_5m/lrcusdt@kline_5m/ltcusdt@kline_5m/lunausdt@kline_5m/manausdt@kline_5m/maskusdt@kline_5m/maticusdt@kline_5m/mkrusdt@kline_5m/mtlusdt@kline_5m/nearusdt@kline_5m/neousdt@kline_5m/nknusdt@kline_5m/oceanusdt@kline_5m/ognusdt@kline_5m/omgusdt@kline_5m/oneusdt@kline_5m/ontusdt@kline_5m/peopleusdt@kline_5m/qtumusdt@kline_5m/rayusdt@kline_5m/reefusdt@kline_5m/renusdt@kline_5m/rlcusdt@kline_5m/roseusdt@kline_5m/rsrusdt@kline_5m/runeusdt@kline_5m/rvnusdt@kline_5m/sandusdt@kline_5m/scusdt@kline_5m/sfpusdt@kline_5m/sklusdt@kline_5m/snxusdt@kline_5m/solusdt@kline_5m/srmusdt@kline_5m/stmxusdt@kline_5m/storjusdt@kline_5m/sushiusdt@kline_5m/sxpusdt@kline_5m/thetausdt@kline_5m/tlmusdt@kline_5m/tomousdt@kline_5m/trbusdt@kline_5m/trxusdt@kline_5m/unfiusdt@kline_5m/uniusdt@kline_5m/vetusdt@kline_5m/wavesusdt@kline_5m/woousdt@kline_5m/xemusdt@kline_5m/xlmusdt@kline_5m/xmrusdt@kline_5m/xrpusdt@kline_5m/xtzusdt@kline_5m/yfiusdt@kline_5m/zecusdt@kline_5m/zenusdt@kline_5m/zilusdt@kline_5m/zrxusdt@kline_5m/"]

base_url = "wss://fstream.binance.com/stream?streams="

SOCKET = base_url + symbols_wo

print(SOCKET)

def on_open(ws):
    print('opened')

def on_close(ws):
    print("closed")

def on_message(ws, message):
    json_message = json.loads(message)

    symbol = json_message["data"]["s"]
    candle_time = json_message["data"]["k"]["T"]
    time_frame = json_message["data"]["k"]["i"]
    open = json_message["data"]["k"]["o"]
    close = json_message["data"]["k"]["c"]
    high = json_message["data"]["k"]["h"]
    low = json_message["data"]["k"]["l"]
    candle_closed = json_message["data"]["k"]["x"]
  
    candlestick = [{"Symbol": symbol,
             "Frame": time_frame,
             "Open": open,
             "Close": close,
             "High": high,
             "Low": low,
             "Candle Time": candle_time,
             "Candle Closed": candle_closed,
                         }]
    df = pd.DataFrame(candlestick)
    df

ws = websocket.WebSocketApp(SOCKET, on_open=on_open, on_close=on_close, on_message=on_message)
ws.run_forever()

What I desire is to get at least last 20 "5m candlesticks" of each pair I chose.

1

There are 1 answers

2
yktrcn On BEST ANSWER

After 15 days since i started learning python finally i solved it! Hopefully it will help others too.

in fetch_ohlcv function you have to provide a symbol. Timeframe and limit are optional. My solution is:

my_symbol = ['BTCUSDT', 'ETHUSDT']
def test (symbol):
return binanceusdm.fetch_ohlcv(symbol, "5m", limit=3)
output = list(map(test, my_symbol))
print(output)

and the output is last 3 (5m candles) of Btc and eth pairs. you can check unix timestamp codes verify them.

[[[1651932900000, 35881.0, 35912.9, 35867.7, 35892.8, 397.392], [1651933200000, 35892.8, 35920.9, 35892.7, 35907.9, 250.833], [1651933500000, 35907.9, 35918.5, 35904.5, 35918.5, 59.591]], [[1651932900000, 2672.99, 2677.2, 2672.54, 2675.37, 2220.193], [1651933200000, 2675.37, 2677.66, 2674.55, 2675.53, 1426.824], [1651933500000, 2675.54, 2676.49, 2675.2, 2676.49, 328.509]]]