I’m writing a program that collects cryptocurrency price information and I want to perform technical analysis on two different kline time intervals. I’ve been struggling to achieve this as I’m new to programming and even newer to Python specifically.
Below is the code I’m currently trying to get two different kline streams in at the same time.
JavaScript
x
40
40
1
import asyncio
2
from datetime import datetime
3
from binance import AsyncClient, BinanceSocketManager
4
from threading import Thread
5
6
def analyze(res):
7
kline = res['k']
8
if kline['x']: #candle is completed
9
print('{} start_sleeping {} {}'.format(
10
datetime.now(),
11
kline['s'],
12
datetime.fromtimestamp(kline['t'] / 1000),
13
))
14
time.sleep(5)
15
print('{} finish_sleeping {}'.format(datetime.now(), kline['s']))
16
17
async def open_binance_stream(symbol,interval):
18
client = AsyncClient(config.API_KEY, config.API_SECRET)
19
bm = BinanceSocketManager(client)
20
ts = bm.kline_socket(symbol,interval)
21
async with ts as stream:
22
while True:
23
res = await stream.recv()
24
Thread(target=analyze, args=(res)).start()
25
await client.close_connection()
26
27
async def main():
28
func = False
29
if not func:
30
await on_open()
31
func = await asyncio.gather(
32
open_binance_stream(TRADE_SYMBOL,interval=KLINE_INTERVAL_1MINUTE),
33
open_binance_stream(TRADE_SYMBOL,interval=KLINE_INTERVAL_15MINUTE)
34
)
35
36
if __name__ == "__main__":
37
try:
38
loop = asyncio.get_event_loop()
39
loop.run_until_complete(main())
40
Unfortunately I’m getting these errors:
JavaScript
1
16
16
1
++connection opened with Binance++
2
Exception in thread Thread-1:
3
Traceback (most recent call last):
4
Exception in thread Thread-2:
5
File "C:Program FilesWindowsAppsPythonSoftwareFoundation.Python.3.8_3.8.2800.0_x64__qbz5n2kfra8p0libthreading.py", line 932, in _bootstrap_inner
6
Traceback (most recent call last):
7
File "C:Program FilesWindowsAppsPythonSoftwareFoundation.Python.3.8_3.8.2800.0_x64__qbz5n2kfra8p0libthreading.py", line 932, in _bootstrap_inner
8
self.run()
9
File "C:Program FilesWindowsAppsPythonSoftwareFoundation.Python.3.8_3.8.2800.0_x64__qbz5n2kfra8p0libthreading.py", line 870, in run
10
self.run()
11
File "C:Program FilesWindowsAppsPythonSoftwareFoundation.Python.3.8_3.8.2800.0_x64__qbz5n2kfra8p0libthreading.py", line 870, in run
12
self._target(*self._args, **self._kwargs)
13
TypeError: analyze() takes 1 positional argument but 4 were given
14
self._target(*self._args, **self._kwargs)
15
TypeError: analyze() takes 1 positional argument but 4 were given
16
I don’t feel like I’m passing 4 arguments, but apperently I am. What am I doing wrong here? Any help would be greatly appreciated!
Skurring
Advertisement
Answer
Using double asterisks will make res a dictionary of all the four arguments:
JavaScript
1
12
12
1
def analyze(**res):
2
print(res)
3
kline = res['k']
4
if kline['x']: # candle is completed
5
print('{} start_sleeping {} {}'.format(
6
datetime.now(),
7
kline['s'],
8
datetime.fromtimestamp(kline['t'] / 1000),
9
))
10
time.sleep(5)
11
print('{} finish_sleeping {}'.format(datetime.now(), kline['s']))
12
Thread arguments should be sent with kwargs instead of args (this will pack arguments into a dictionary):
JavaScript
1
9
1
async def open_binance_stream(symbol, interval):
2
client = AsyncClient(config.API_KEY, config.API_SECRET)
3
bm = BinanceSocketManager(client)
4
ts = bm.kline_socket(symbol, interval)
5
async with ts as stream:
6
while True:
7
res = await stream.recv()
8
Thread(target=analyze, kwargs=res).start()
9