//------------------------------------------------------------------------
// 简称:TrendFollowing
// 类型:自动交易策略
// 版本:v2.2(符合TB事件驱动模型)
//------------------------------------------------------------------------
Params
Numeric FastLength(5);
Numeric SlowLength(20);
Numeric ATRLength(14);
Numeric StopLossK(2);
Numeric TakeProfitK(3);
Vars
Series<Numeric> MA_Fast;
Series<Numeric> MA_Slow;
Series<Numeric> ATRVal;
Series<Numeric> StopLossPrice(0);
Series<Numeric> TakeProfitPrice(0);
Series<Numeric> TrueRange;
Begin
// 指标计算(必须放在OnBar事件外)
MA_Fast = AverageFC(Close,FastLength);
MA_Slow = AverageFC(Close,SlowLength);
// ATR计算(符合TB事件驱动模型)
TrueRange = Max(Max(High - Low, Abs(High - Close[1])), Abs(Low - Close[1]));
ATRVal = AverageFC(TrueRange, ATRLength);
// 事件驱动逻辑
OnBar()
{
// 开仓信号
If(CrossOver(MA_Fast,MA_Slow) && MarketPosition !=1)
{
Buy(0,Close);
StopLossPrice = Close - StopLossK*ATRVal;
TakeProfitPrice = Close + TakeProfitK*ATRVal;
}
End If
If(CrossUnder(MA_Fast,MA_Slow) && MarketPosition !=-1)
{
SellShort(0,Close);
StopLossPrice = Close + StopLossK*ATRVal;
TakeProfitPrice = Close - TakeProfitK*ATRVal;
}
End If
// 多头平仓逻辑
If(MarketPosition ==1)
{
StopLossPrice = Max(StopLossPrice, High[1] - StopLossK*ATRVal);
If(Low <= StopLossPrice || High >= TakeProfitPrice)
{
Sell(0,Min(Open,StopLossPrice));
}
End If
}
End If
// 空头平仓逻辑
If(MarketPosition ==-1)
{
StopLossPrice = Min(StopLossPrice, Low[1] + StopLossK*ATRVal);
If(High >= StopLossPrice || Low <= TakeProfitPrice)
{
BuyToCover(0,Max(Open,StopLossPrice));
}
End If
}
End If
}
End
该代码在编译时总是提示begin-end不匹配,请问大家时怎么回事?可以帮忙完善一下吗?
//------------------------------------------------------------------------
// 简称:TrendFollowing
// 类型:自动交易策略
// 版本:v2.2(符合TB事件驱动模型)
//------------------------------------------------------------------------
Params
Numeric FastLength(5);
Numeric SlowLength(20);
Numeric ATRLength(14);
Numeric StopLossK(2);
Numeric TakeProfitK(3);
Vars
Series<Numeric> MA_Fast;
Series<Numeric> MA_Slow;
Series<Numeric> ATRVal;
Series<Numeric> StopLossPrice(0);
Series<Numeric> TakeProfitPrice(0);
Series<Numeric> TrueRange;
Events
OnBar(ArrayRef<Integer> indexs)
{
MA_Fast = AverageFC(Close,FastLength);
MA_Slow = AverageFC(Close,SlowLength);
// ATR计算(符合TB事件驱动模型)
TrueRange = Max(Max(High - Low, Abs(High - Close[1])), Abs(Low - Close[1]));
ATRVal = AverageFC(TrueRange, ATRLength);
If(CrossOver(MA_Fast,MA_Slow) && MarketPosition !=1)
{
Buy(0,Close);
StopLossPrice = Close - StopLossK*ATRVal;
TakeProfitPrice = Close + TakeProfitK*ATRVal;
}
If(CrossUnder(MA_Fast,MA_Slow) && MarketPosition !=-1)
{
SellShort(0,Close);
StopLossPrice = Close + StopLossK*ATRVal;
TakeProfitPrice = Close - TakeProfitK*ATRVal;
}
// 多头平仓逻辑
If(MarketPosition ==1)
{
StopLossPrice = Max(StopLossPrice, High[1] - StopLossK*ATRVal);
If(Low <= StopLossPrice || High >= TakeProfitPrice)
{
Sell(0,Min(Open,StopLossPrice));
}
}
If(MarketPosition ==-1)
{
StopLossPrice = Min(StopLossPrice, Low[1] + StopLossK*ATRVal);
If(High >= StopLossPrice || Low <= TakeProfitPrice)
{
BuyToCover(0,Max(Open,StopLossPrice));
}
}
}
👍
👍,谢谢了