Python Binance 多 kline 时间间隔
Python Binance multiple kline time intervals
我正在编写一个收集加密货币价格信息的程序,我想对两个不同的 kline 时间间隔进行技术分析。我一直在努力实现这一目标,因为我是编程的新手,甚至是 Python 的新手。
下面是我目前正在尝试同时获取两个不同 kline 流的代码。
import asyncio
from datetime import datetime
from binance import AsyncClient, BinanceSocketManager
from threading import Thread
def analyze(res):
kline = res['k']
if kline['x']: #candle is completed
print('{} start_sleeping {} {}'.format(
datetime.now(),
kline['s'],
datetime.fromtimestamp(kline['t'] / 1000),
))
time.sleep(5)
print('{} finish_sleeping {}'.format(datetime.now(), kline['s']))
async def open_binance_stream(symbol,interval):
client = AsyncClient(config.API_KEY, config.API_SECRET)
bm = BinanceSocketManager(client)
ts = bm.kline_socket(symbol,interval)
async with ts as stream:
while True:
res = await stream.recv()
Thread(target=analyze, args=(res)).start()
await client.close_connection()
async def main():
func = False
if not func:
await on_open()
func = await asyncio.gather(
open_binance_stream(TRADE_SYMBOL,interval=KLINE_INTERVAL_1MINUTE),
open_binance_stream(TRADE_SYMBOL,interval=KLINE_INTERVAL_15MINUTE)
)
if __name__ == "__main__":
try:
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
不幸的是,我遇到了这些错误:
++connection opened with Binance++
Exception in thread Thread-1:
Traceback (most recent call last):
Exception in thread Thread-2:
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.8_3.8.2800.0_x64__qbz5n2kfra8p0\lib\threading.py", line 932, in _bootstrap_inner
Traceback (most recent call last):
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.8_3.8.2800.0_x64__qbz5n2kfra8p0\lib\threading.py", line 932, in _bootstrap_inner
self.run()
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.8_3.8.2800.0_x64__qbz5n2kfra8p0\lib\threading.py", line 870, in run
self.run()
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.8_3.8.2800.0_x64__qbz5n2kfra8p0\lib\threading.py", line 870, in run
self._target(*self._args, **self._kwargs)
TypeError: analyze() takes 1 positional argument but 4 were given
self._target(*self._args, **self._kwargs)
TypeError: analyze() takes 1 positional argument but 4 were given
我不觉得我传递了 4 个参数,但显然我是。我在这里做错了什么?任何帮助将不胜感激!
滚滚
使用双星号将使 res 成为包含所有四个参数的字典:
def analyze(**res):
print(res)
kline = res['k']
if kline['x']: # candle is completed
print('{} start_sleeping {} {}'.format(
datetime.now(),
kline['s'],
datetime.fromtimestamp(kline['t'] / 1000),
))
time.sleep(5)
print('{} finish_sleeping {}'.format(datetime.now(), kline['s']))
线程参数应使用 kwargs 而不是 args 发送(这会将参数打包到字典中):
async def open_binance_stream(symbol, interval):
client = AsyncClient(config.API_KEY, config.API_SECRET)
bm = BinanceSocketManager(client)
ts = bm.kline_socket(symbol, interval)
async with ts as stream:
while True:
res = await stream.recv()
Thread(target=analyze, kwargs=res).start()
我正在编写一个收集加密货币价格信息的程序,我想对两个不同的 kline 时间间隔进行技术分析。我一直在努力实现这一目标,因为我是编程的新手,甚至是 Python 的新手。
下面是我目前正在尝试同时获取两个不同 kline 流的代码。
import asyncio
from datetime import datetime
from binance import AsyncClient, BinanceSocketManager
from threading import Thread
def analyze(res):
kline = res['k']
if kline['x']: #candle is completed
print('{} start_sleeping {} {}'.format(
datetime.now(),
kline['s'],
datetime.fromtimestamp(kline['t'] / 1000),
))
time.sleep(5)
print('{} finish_sleeping {}'.format(datetime.now(), kline['s']))
async def open_binance_stream(symbol,interval):
client = AsyncClient(config.API_KEY, config.API_SECRET)
bm = BinanceSocketManager(client)
ts = bm.kline_socket(symbol,interval)
async with ts as stream:
while True:
res = await stream.recv()
Thread(target=analyze, args=(res)).start()
await client.close_connection()
async def main():
func = False
if not func:
await on_open()
func = await asyncio.gather(
open_binance_stream(TRADE_SYMBOL,interval=KLINE_INTERVAL_1MINUTE),
open_binance_stream(TRADE_SYMBOL,interval=KLINE_INTERVAL_15MINUTE)
)
if __name__ == "__main__":
try:
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
不幸的是,我遇到了这些错误:
++connection opened with Binance++
Exception in thread Thread-1:
Traceback (most recent call last):
Exception in thread Thread-2:
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.8_3.8.2800.0_x64__qbz5n2kfra8p0\lib\threading.py", line 932, in _bootstrap_inner
Traceback (most recent call last):
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.8_3.8.2800.0_x64__qbz5n2kfra8p0\lib\threading.py", line 932, in _bootstrap_inner
self.run()
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.8_3.8.2800.0_x64__qbz5n2kfra8p0\lib\threading.py", line 870, in run
self.run()
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.8_3.8.2800.0_x64__qbz5n2kfra8p0\lib\threading.py", line 870, in run
self._target(*self._args, **self._kwargs)
TypeError: analyze() takes 1 positional argument but 4 were given
self._target(*self._args, **self._kwargs)
TypeError: analyze() takes 1 positional argument but 4 were given
我不觉得我传递了 4 个参数,但显然我是。我在这里做错了什么?任何帮助将不胜感激!
滚滚
使用双星号将使 res 成为包含所有四个参数的字典:
def analyze(**res):
print(res)
kline = res['k']
if kline['x']: # candle is completed
print('{} start_sleeping {} {}'.format(
datetime.now(),
kline['s'],
datetime.fromtimestamp(kline['t'] / 1000),
))
time.sleep(5)
print('{} finish_sleeping {}'.format(datetime.now(), kline['s']))
线程参数应使用 kwargs 而不是 args 发送(这会将参数打包到字典中):
async def open_binance_stream(symbol, interval):
client = AsyncClient(config.API_KEY, config.API_SECRET)
bm = BinanceSocketManager(client)
ts = bm.kline_socket(symbol, interval)
async with ts as stream:
while True:
res = await stream.recv()
Thread(target=analyze, kwargs=res).start()