在 ccxt 或 binance python api 中获取多个密码的 ohlc 值?
Getting multiple cryptos' ohlc values in ccxt or binance python api?
我正在尝试根据我的策略创建一个自动机器人,但我正在努力解决一些问题:
1- 在ccxt中,我可以得到指定交易品种的OHLC数据。但是我在文档和 google 等方面找不到任何关于获取多个符号的 ohlc 数据的数据。
例如:ohlcv = exchange.fetch_ohlcv(symbol, '1d', limit=500)
使用此代码,如果我将符号更改为 BTCUSDT,我可以获得数据。但是我一次想要多个符号。像几行,包括 Btcusdt、ethusdt、xrpusdt 等...
我想要的输出是:
pair open high low close
btcusdt 38000 38005 37995 38002
ethusdt 1234 2132 1233 1434
xrpusd 1 2 0.9 1.1
2- 我还尝试了 binance api 和 websocket 连接。使用多个流 url,我可以一次获取每个符号的 5m 烛条数据。我得到了如下所示的输出,但我无法让它们工作,因为每个符号都有不同的表格。我也可以将数据过滤为仅获取闭合蜡烛 (True),但在它获得第一个输出后,它会不断重复。我买不到新蜡烛。
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()
我想要的是至少获得我选择的每对的最后 20 个“5m 烛台”。
学习了15天python终于解决了!希望它也能帮助其他人。
在 fetch_ohlcv 函数中你必须提供一个符号。时间范围和限制是可选的。我的解决方案是:
my_symbol = ['BTCUSDT', 'ETHUSDT']
def test (symbol):
return binanceusdm.fetch_ohlcv(symbol, "5m", limit=3)
output = list(map(test, my_symbol))
print(output)
并且输出是 Btc 和 eth 对的最后 3 个(500 万根蜡烛)。你可以检查 unix 时间戳代码来验证它们。
[[[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]]]
我正在尝试根据我的策略创建一个自动机器人,但我正在努力解决一些问题:
1- 在ccxt中,我可以得到指定交易品种的OHLC数据。但是我在文档和 google 等方面找不到任何关于获取多个符号的 ohlc 数据的数据。
例如:ohlcv = exchange.fetch_ohlcv(symbol, '1d', limit=500)
使用此代码,如果我将符号更改为 BTCUSDT,我可以获得数据。但是我一次想要多个符号。像几行,包括 Btcusdt、ethusdt、xrpusdt 等...
我想要的输出是:
pair open high low close
btcusdt 38000 38005 37995 38002
ethusdt 1234 2132 1233 1434
xrpusd 1 2 0.9 1.1
2- 我还尝试了 binance api 和 websocket 连接。使用多个流 url,我可以一次获取每个符号的 5m 烛条数据。我得到了如下所示的输出,但我无法让它们工作,因为每个符号都有不同的表格。我也可以将数据过滤为仅获取闭合蜡烛 (True),但在它获得第一个输出后,它会不断重复。我买不到新蜡烛。
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()
我想要的是至少获得我选择的每对的最后 20 个“5m 烛台”。
学习了15天python终于解决了!希望它也能帮助其他人。
在 fetch_ohlcv 函数中你必须提供一个符号。时间范围和限制是可选的。我的解决方案是:
my_symbol = ['BTCUSDT', 'ETHUSDT']
def test (symbol):
return binanceusdm.fetch_ohlcv(symbol, "5m", limit=3)
output = list(map(test, my_symbol))
print(output)
并且输出是 Btc 和 eth 对的最后 3 个(500 万根蜡烛)。你可以检查 unix 时间戳代码来验证它们。
[[[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]]]