3 分钟快速生成代码
输入想法,AI 即刻生成可运行代码
在 ptrade 量化交易平台中,当策略运行或手动下单时一直提示**“可用资源不足”,通常意味着您的账户当前没有足够的资金或持仓**来执行您所请求的交易指令。这是一种非常常见的交易保护机制,旨在防止透支或卖空(在普通账户中)。
以下是导致该提示的常见原因及详细的排查与解决方法:
当您尝试买入股票(或开仓)时,系统会计算所需的总资金(包括股票市值、预估佣金、印花税、过户费等)。如果该总金额大于您账户当前的可用资金(Cash),就会提示资源不足。
常见场景:
order_value(security, context.portfolio.cash) 时,由于未扣除手续费,实际所需资金会略大于 cash,导致下单失败。解决方法与代码排查:
在下单前,务必检查可用资金,并预留一定比例的手续费空间。
def handle_data(context, data):
security = '600570.SS'
current_price = data[security]['close']
# 获取当前可用资金
available_cash = context.portfolio.cash
# 预留 1% 作为手续费和滑点缓冲
safe_cash = available_cash * 0.99
# 计算可买入的股数(向下取整到100的倍数)
amount = int(safe_cash / current_price / 100) * 100
if amount >= 100:
order(security, amount)
log.info(f"成功下达买入指令,数量:{amount}")
else:
log.warning("可用资金不足以买入1手股票")
当您尝试卖出股票(或平仓)时,如果请求卖出的数量大于您账户中该股票的可用持仓(Enable Amount),就会提示资源不足。
常见场景:
解决方法与代码排查:
在卖出前,务必使用 get_position 获取该标的的 enable_amount(可用数量),而不是 amount(总数量)。
def handle_data(context, data):
security = '600570.SS'
# 获取该股票的持仓信息
pos = get_position(security)
# 获取可用持仓数量(已扣除T+1未交收和挂单冻结的部分)
enable_amount = pos.enable_amount
if enable_amount > 0:
# 仅卖出可用部分
order(security, -enable_amount)
log.info(f"成功下达卖出指令,数量:{enable_amount}")
else:
log.warning(f"{security} 当前无可用持仓,无法卖出")
如果您使用的是两融账户,资源不足的含义会更广泛:
解决方法:
使用两融专用查询接口,在下单前获取最大可操作数量。
def handle_data(context, data):
security = '600570.SS'
# 查询最大可融资买入数量
margincash_dict = get_margincash_open_amount(security)
if margincash_dict:
max_buy_amount = margincash_dict.get(security, 0)
if max_buy_amount >= 100:
margincash_open(security, 100)
# 查询最大可融券卖出数量
marginsec_dict = get_marginsec_open_amount(security)
if marginsec_dict:
max_sell_amount = marginsec_dict.get(security, 0)
if max_sell_amount >= 100:
marginsec_open(security, 100)
很多时候,策略逻辑没有问题,但由于市场价格波动,之前的限价单一直没有成交(处于“已报”状态)。这些**未完成订单(Open Orders)**会持续占用资金或持仓。
排查与清理未完成订单:
您可以定期(例如每天盘后或每次下单前)检查并撤销不需要的挂单,以释放资源。
def handle_data(context, data):
# 获取所有未完成的订单
open_orders = get_open_orders()
for order_obj in open_orders:
# 如果订单挂了很久没成交,可以选择撤单释放资源
cancel_order(order_obj.id)
log.info(f"撤销未成交订单:{order_obj.id},释放资源")
为了避免在 ptrade 实盘或回测中频繁遇到“可用资源不足”的报错,建议在编写策略时养成良好的习惯:
context.portfolio.cash 和 position.enable_amount 进行下单数量的计算。