如何在策略中进行账户资金管理?
account_info = get_account() # 获取已登录期货账户信息
Available = account_info[“Available”] # 可用资金
Balance = account_info[“Balance”] # 总权益
if Available / Balance < 1 – context.market_value_rate * 0.01 :
put_log(f”可用资金{Available},总权益{Balance},市值权益占总仓位比例超过最大比例”,level=’USER_LOG’)
return False
symbolinfo = get_symbolinfo(context.main_symbol) # 获取合约信息
volume_multiple = symbolinfo[“volume_multiple”] # 合约乘数
LongMarginRatio = symbolinfo[“LongMarginRatio”] if context.run_mode else symbolinfo[“long_margin_ratio”]# 保证金比例
tick = get_tick(context.main_symbol) # 获取tick数据
current_price = float(tick.get(“LastPrice”, 0.0)) ## 当前价格
context.volume = math.floor(context.single_rate * 0.01 * Balance/ (current_price * volume_multiple * LongMarginRatio))
if context.volume < 1:
put_log(“按比例算出交易手数小于1手不继续计算指标”,level=’USER_LOG’)
return
put_log(f”交易手数{context.volume},可用资金{Available},总权益{Balance},当前价格{current_price},合约乘数{volume_multiple},保证金比例{LongMarginRatio}”,level=’USER_LOG’)
if context.volume * current_price*volume_multiple * margin_ratio > context.single_rate * 0.01 * Balance:
put_log(f”单次仓位{context.volume},总权益{Balance},当前比例{context.volume * current_price / Balance}单个合约占总权益比例超过最大比例”,level=’USER_LOG’)
return False
遍历所有周期(交易周期=context.periods[0],共振周期=context.periods[1] 小周期=context.periods[2],大周期=context.periods[3])





请登录后查看回复内容