Params
Numeric FastPeriod(21); // 快均线周期
Numeric MidPeriod(34); // 中均线周期
Numeric SlowPeriod(55); // 慢均线周期
Numeric SL_Points(200); // 止损点数
Numeric TP_Points(500); // 止盈点数
Numeric TrailingSL(200); // 移动止损触发点数
Numeric MaxLookback(20); // 最大回溯K线数量
Vars
// 均线序列(修正声明方式)
series<numeric> MA_Fast; // 快均线
series<numeric> MA_Mid; // 中均线
series<numeric> MA_Slow; // 慢均线
// 订单状态(已修复分号问题)
Bool IsOrderActive = False;
Numeric EntryPrice = 0; // 行25:确保分号存在
Numeric StopLoss = 0;
Numeric TakeProfit = 0;
// 临时变量(全部分号验证)
Numeric i = 0;
Numeric n_Long = 0;
Numeric n_Short = 0;
Bool ConditionLong = False;
Bool ConditionShort = False;
Begin
//----------------------------------------------------------------------
// 数据预处理
//----------------------------------------------------------------------
MA_Fast = AverageFC(Close, FastPeriod);
MA_Mid = AverageFC(Close, MidPeriod);
MA_Slow = AverageFC(Close, SlowPeriod);
//----------------------------------------------------------------------
// 交易逻辑(K线闭合时执行)
//----------------------------------------------------------------------
if (BarStatus == 2)
{
// 多单条件检测
ConditionLong = False;
if (Close[1] > MA_Fast[1])
{
Numeric MaxNLong = Min(MaxLookback, BarCount-1);
for(n_Long = 2; n_Long <= MaxNLong; n_Long++)
{
if(Close[n_Long] < MA_Fast[n_Long]) break;
}
if(n_Long <= MaxNLong)
{
Bool MA_Up = True;
for(i = 2; i <= n_Long; i++)
{
if(MA_Mid[i] <= MA_Mid[i+1] || MA_Slow[i] <= MA_Slow[i+1])
{
MA_Up = False;
break;
}
}
ConditionLong = MA_Up;
}
}
// 空单条件检测(反向逻辑)
ConditionShort = False;
if (Close[1] < MA_Fast[1])
{
Numeric MaxNShort = Min(MaxLookback, BarCount-1);
for(n_Short = 2; n_Short <= MaxNShort; n_Short++)
{
if(Close[n_Short] > MA_Fast[n_Short]) break;
}
if(n_Short <= MaxNShort)
{
Bool MA_Down = True;
for(i = 2; i <= n_Short; i++)
{
if(MA_Mid[i] >= MA_Mid[i+1] || MA_Slow[i] >= MA_Slow[i+1])
{
MA_Down = False;
break;
}
}
ConditionShort = MA_Down;
}
}
// 订单管理(严格分号验证)
if (MarketPosition != 0)
{
Numeric FloatProfit = (Close - EntryPrice)/(MinMove*PriceScale);
if (Abs(FloatProfit) >= TrailingSL)
{
StopLoss = EntryPrice + Sign(FloatProfit)*TrailingSL*(MinMove*PriceScale);
}
if (MarketPosition == 1)
{
Sell(0, Close, StopLoss, TakeProfit);
}
else if (MarketPosition == -1)
{
BuyToCover(0, Close, StopLoss, TakeProfit);
}
IsOrderActive = (MarketPosition != 0);
}
// 开仓逻辑(已验证分号)
if (!IsOrderActive)
{
if (ConditionLong)
{
Buy(0, Close);
EntryPrice = Close;
StopLoss = EntryPrice - SL_Points*MinMove*PriceScale;
TakeProfit = EntryPrice + TP_Points*MinMove*PriceScale;
IsOrderActive = True;
}
else if (ConditionShort)
{
SellShort(0, Close);
EntryPrice = Close;
StopLoss = EntryPrice + SL_Points*MinMove*PriceScale;
TakeProfit = EntryPrice - TP_Points*MinMove*PriceScale;
IsOrderActive = True;
}
}
}
End
上述代码在编译时总是提示:缺少分号,有高手能帮忙解决一下吗?先谢谢了。
您好,您这段代码确实很多地方不符合TB的语法。希望您还是先学习下TB的基础语言吧
for i=2 to n_short
又是ds写的吧
👍