
等级: 专业版
- 注册:
- 2021-10-19
- 曾用名:
|

楼主 |
发表于 2026-3-26 10:04
|
显示全部楼层
我想要延迟5秒钟后发出防止时间偏差导致的委托失败,我求助AI这样写对吗?编译可以通过,但是不知道是不是对的
import time
from datetime import datetime, timedelta
# 执行下单,并清空历史的未成交单记录
def place_order(context):
# 获取当前时间
current_time = datetime.now()
# 检查是否已经开盘
if not hasattr(context, 'market_opened_time'):
# 获取开盘时间(假设是9:30开盘)
market_open_time = current_time.replace(hour=9, minute=30, second=0, microsecond=0)
context.market_opened_time = market_open_time
# 计算开盘后5秒的时间点
order_execution_time = context.market_opened_time + timedelta(seconds=5)
# 如果当前时间还没到开盘后5秒,则跳过执行
if current_time < order_execution_time:
return
with open(file_name, "r") as file:
for line in file:
if not line.strip():
continue
book_id, side, position_effect, price, unfilled_quantity = line.split()
point = round(get_dynainf(book_id, 208), 7) # Point是最小变动价位的小数位
if int(point) > 0:
point = 0
else:
point = len(str(point).split(".")[1])
function_name = side + "_" + position_effect
# 从全局环境中直接获取到具体的函数
function_object = getattr(sys.modules[__name__], function_name)
try:
function_object(book_id, "Limit",
price=np.around(float(price), decimals=point+1),
volume=int(unfilled_quantity))
except Exception as ex:
raise |
|