第一次建仓价格的K线的最低价减去2倍的atr如何编写成函数,感谢
// 自定义函数:计算持仓首次建仓价 - 2倍ATR
Params
Numeric Length(14); // ATR计算周期
Vars
Numeric atrValue;
Numeric targetValue;
Begin
// 1. 计算ATR值
atrValue = AvgTrueRange(Length);
// 2. 初始化目标值为无效数值
targetValue = InvalidNumeric;
// 3. 检查是否有持仓,有则计算目标值
If(CurrentContracts() > 0)
{
targetValue = EntryPrice - 2 * atrValue;
}
// 4. 返回计算结果
Return targetValue;
End
这样写对吗