// 使用A_GetMarginRate获取实时保证金率
MarginRate marginRateStruct; // 定义保证金率结构体
Bool ret = A_GetMarginRate(Symbol, marginRateStruct); // 获取当前合约的保证金率
// 从结构体中提取保证金率
if(ret)
{
longMarginRate = marginRateStruct.longMarginRatio; // 多头保证金率
shortMarginRate = marginRateStruct.shortMarginRatio; // 空头保证金率
}
else
{
// 如果获取失败,使用默认值
longMarginRate = DefaultMarginRate;
shortMarginRate = DefaultMarginRate;
}
// 保证金率有效性检查
if(longMarginRate <= 0 or longMarginRate >= 1)
longMarginRate = DefaultMarginRate;
if(shortMarginRate <= 0 or shortMarginRate >= 1)
shortMarginRate = DefaultMarginRate;
// 根据当前持仓确定使用的保证金率
if(my_MarketPosition == 1 or (my_MarketPosition == 0 and b == 1 and s == 0))
marginRateToUse = longMarginRate;
else if(my_MarketPosition == -1 or (my_MarketPosition == 0 and b == 0 and s == 1))
marginRateToUse = shortMarginRate;
else
marginRateToUse = Max(longMarginRate, shortMarginRate); // 默认使用较大的保证金率
// =====================================================================
// 12. 动态手数计算(按L1_TSF方式)
// =====================================================================
lotsToUse = Lot; // 默认使用固定手数
if(UseMarginPercent)
{
Numeric initCapital = Portfolio_InitCapital();
if(initCapital > 0)
{
Numeric targetMargin = initCapital * CapitalPercent;
// 确定合约乘数:优先使用ContractUnit,若其<=1则使用手动设置的乘数
realContractUnit = ContractUnit;
if(realContractUnit <= 1)
realContractUnit = ContractUnit_Manual;
// 计算每手保证金
Numeric marginPerLot = Open * realContractUnit * marginRateToUse;
if(marginPerLot > 0)
{
// 计算理论手数
Numeric calcLots = targetMargin / marginPerLot;
calcLots = IntPart(calcLots);
if(calcLots < 1)
calcLots = 1;
// 可用资金上限检查
Numeric currentCapital = Portfolio_CurrentCapital();
if(currentCapital > 0)
{
Numeric maxLotsByCapital = IntPart(currentCapital / marginPerLot);
if(maxLotsByCapital < 1)
maxLotsByCapital = 1;
// 双重限制
calcLots = Min(calcLots, maxLotsByCapital);
}
if(calcLots > MaxLots)
calcLots = MaxLots;
lotsToUse = calcLots;
这是动态仓位计算部分的代码,请管理员给看看啥原因。