我看输入参数没有异常 还是出现了开仓失败
代码修改
[Python] 复制代码
# 循环
def handle_bar(context):
print("handle_bar()")
# 记录每次循环时间点
handleBarLogModel = jzt_db.HandleBarLogModel()
# handleBarLogModel.Insert("handle_bar_run")
# 操作时间点
target_time = jzt_tools.KVGet("target_time")
close_target_time = target_time
# 当前时间
formatted_time = time.strftime("%H:%M:%S", time.localtime())
today_date = time.strftime("%Y-%m-%d", time.localtime())
print("当前时间:" + formatted_time + " 触发时间:" + close_target_time)
# 记录每次运行开仓参数
test(context)
# 如果没有登录账户 直接退出
if len(context.account_list) < 1:
print("没有账户登录 直接推出")
return
# 如果不是在目标时间点 直接退出
if formatted_time != close_target_time:
return
sliceModel = jzt_db.SliceModel()
# 遍历账户
for account in context.account_list:
# 单个账户操作
# 获取持仓
where = "account = {account} AND is_close = 0".format(account=account)
order_list = sliceModel.Select(where, 0, context.slice_num, "pk_id ASC")
# 现价
price_now = get_dynainf(context.real_order_book_id, 7)
#print("现价:" + str(price_now))
# 平仓开始 start
# 遍历持仓
for i in order_list:
#print("pk_id:" + str(i['pk_id']))
order_list_pk_id = i['pk_id'] # pk_id
order_list_account = i['account'] # 账号
order_list_code = i['code'] # code
order_list_num = i['num'] # 购买数量
order_list_price = i['price'] # 购买价格
# 开仓手续费计算 转为每手价格
open_charge = jzt_tools.GetJDCharge(order_list_price)
# 平仓手续费计算 转为每手价格
close_charge = jzt_tools.GetJDCharge(price_now)
# 附加手续费计算 开空 所以减去 每手成本价 = 开仓价 - 开仓每手手续费 - 平仓每手手续费
my_price_total = order_list_price - open_charge - close_charge
print("现价:" + str(price_now) + " 持仓盈利价:" + str(my_price_total))
# 平仓检测 如果空仓价格盈利
if price_now < my_price_total:
print("执行平空")
close_id = buy_close(order_list_code, "Limit", price=price_now, volume=order_list_num, account=order_list_account, hedge_flag=False,serial_id = 1)
if close_id == -1:
print("平空失败 pk_id:"+str(order_list_pk_id))
handleBarLogModel.Insert("平空失败 pk_id:"+str(order_list_pk_id))
break
context.sell_object.append({"order_id": close_id, "pk_id" : order_list_pk_id})
# 平仓结束 end
# 开仓开始 start
# 如果今日已经开过仓 就不开
if today_date in context.open_buy_date:
print("今日已经开过仓")
break
# 如果持仓超过 slice_num 份就不开仓
if len(order_list) >= context.slice_num:
print("持仓超过"+str(context.slice_num)+"份")
break
# 20240625 购买手数改为 kv 中设置
num = context.slice_open_num
# 开仓
# 校验参数是否合法 202407041817
params = "order_book_id {order_book_id}; style {style}; price {price}; volume {volume}; account {account};".format(order_book_id=context.real_order_book_id, style="Limit", price=price_now, volume=num, account=account)
print(params)
handleBarLogModel.Insert(params)
open_id = sell_open(order_book_id=context.real_order_book_id, style="Limit", price=price_now, volume=num, account=account, hedge_flag=False ,serial_id = 2)
if open_id == -1:
print("开仓失败")
handleBarLogModel.Insert("开仓失败")
break
# 存储今日开单
context.open_buy_date.append(jzt_tools.GetNowDateFormat("%Y-%m-%d"))
context.open_order_id.append(open_id)
# 开仓结束 end
pass
数据库存储参数
信息窗口
运行日志
PleaceOrder.txt
(221.1 KB, 下载次数: 764)
|