当您运行该文件时,将打开一个用 TKInter+Async 编写的界面窗口。当您按下“START”按钮时,将创建主程序循环并启动 main() 方法。该算法通过多个网络套接字流连接到币安,并开始搜索和打开交易。在这个阶段,一切都进展顺利。但是:当我尝试调用on_close_orders()函数时,它会给出错误Timeout context manager should be use inside a task。我认为这是因为该函数被调用或者位于主循环的上下文之外,但我不明白需要做什么才能将其放在那里。我尝试通过asyncio.create_taks(on_close_orders())调用函数来解决问题,但结果是相同的。完整的错误报告如下: 告诉我如何修复此代码,以便在按下按钮时正常调用该函数?
import asyncio
import threading
import time
import binance_folder
import indicators
from config import API_KEY, SECRET_KEY
import sys
import traceback
import datetime
from indicators import SuperTrend_numpy
import numpy as np
from async_tkinter_loop import async_handler, async_mainloop
from tkinter import ttk
from tkinter import *
async def btn_click_start():
global main
global btn_start
btn_start.state(['disabled'])
ws_main = threading.Thread(target=asyncio.run, args=(main(),)).start()
async def btn_click_close():
global client
if client:
await on_close_orders()
root = Tk()
root['bg'] = '#ffffff'
root.title('MCG ROBOT')
root.geometry('500x500')
root.resizable(width=False, height=False)
frame_input = Frame(root, bg='gray')
frame_input.place(relx=0.02, rely=0.02, relwidth=0.35, relheight=0.95)
frame_control = Frame(root, bg='white')
frame_control.place(relx=0.39, rely=0.87, relwidth=0.59, relheight=0.1)
title = Label(frame_input, text='Входные параметры', bg='gray', font=40)
title.pack()
btn_start = ttk.Button(frame_control, text='START', command=async_handler(btn_click_start))
btn_start.pack()
btn_start.place(x=10, y=15)
btn_stop = ttk.Button(frame_control, text='STOP')
btn_stop.pack()
btn_stop.place(x=105, y=15)
btn_close = ttk.Button(frame_control, text='CLOSE ALL', command=async_handler(btn_click_close))
btn_close.pack()
btn_close.place(x=200, y=15)
loginInput = Entry(frame_input, bg='white')
loginInput.pack()
loginInput.place(x=10, y=15)
passField = Entry(frame_input, bg='white', show='*')
passField.pack()
passField.place(x=10, y=50)
async def on_close_orders():
acc = await client.account()
print('Пытаемся закрыть все открытые позиции')
for i in acc['positions']:
if float(i['positionAmt']) != 0.0:
print(f'Нашли открытую позицию по {i["symbol"]}')
try:
if float(i['positionAmt']) > 0:
print('Определили, что ордер открыт в покупку')
print(f"price={float(i['notional']) * 1.01 / float(i['positionAmt'])}")
close = await client.new_order(symbol=i['symbol'], side='SELL', type='MARKET', timeInForce="GTC",
reduceOnly=True, quantity=abs(float(i['positionAmt'])))
print(f"по монетке {i['symbol']} закрыли позицию")
elif float(i['positionAmt']) < 0:
print('Определили, что ордер открыт в продажу')
close = await client.new_order(symbol=i['symbol'], side='BUY', type='MARKET', timeInForce="GTC",
reduceOnly=True, quantity=abs(float(i['positionAmt'])))
print(f"по монетке {i['symbol']} закрыли позицию")
except Exception as e:
print(f"Позиция не закрыта по причине: ")
print(e)
async def main():
global client
global all_symbols
client = binance_folder.Futures(api_key=API_KEY, secret_key=SECRET_KEY, asynced=True, testnet=False)
all_symbols = await client.load_symbols()
while not all_symbols:
await asyncio.sleep(0.5)
await filter_symbols()
await limited_historycal_klines_requests(symbols)
await create_topics()
asyncio.create_task(execute_order_pool())
while True:
await asyncio.sleep(5)
if __name__ == '__main__':
if sys.platform.startswith('win'):
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
async_mainloop(root)
> Task exception was never retrieved
> future: <Task finished name='Task-837' coro=<on_close_orders() done, defined at
> C:\PycharmProjects\python_robot_MCG\Pattern_bot_+TKinter.py:482>
> exception=RuntimeError('Timeout context manager should be used inside
> a task')>
> Traceback (most recent call last):
> File "C:\PycharmProjects\python_robot_MCG\Pattern_bot_+TKinter.py",
> line 483, in on_close_orders
> acc = await client.account()
> File "C:\PycharmProjects\python_robot_MCG\binance_folder\client.py",
> line 88, in _request_async
> async with getattr(self.session, method)(self.base_url + url, **kwargs) as response:
> File "C:\PycharmProjects\python_robot_MCG\.venv\lib\site-packages\aiohttp\client.py",
> line 1357, in __aenter__
> self._resp: _RetType = await self._coro
> File "C:\PycharmProjects\python_robot_MCG\.venv\lib\site-packages\aiohttp\client.py",
> line 577, in _request
> with timer:
> File "C:\PycharmProjects\python_robot_MCG\.venv\lib\site-packages\aiohttp\helpers.py",
> line 712, in __enter__
> raise RuntimeError(
> RuntimeError: Timeout context manager should be used inside a task


