//------------------------------------------------------------------------
// 简称: EMA4_15-20-25-30_TB_Final_V3
// 名称: 纯EMA4 15-20-25-30 前K确认 跌破所有EMA平仓
// 类别: 交易策略
// 类型: 用户策略
// 版本: 3.0
//------------------------------------------------------------------------
Params
Numeric Lots(1);
Numeric EMA15_Period(15);
Numeric EMA20_Period(20);
Numeric EMA25_Period(25);
Numeric EMA30_Period(30);
;
Vars
Series<Numeric> ema15;
Series<Numeric> ema20;
Series<Numeric> ema25;
Series<Numeric> ema30;
Series<Bool> isLongTrend;
Series<Bool> isShortTrend;
Bool closeAboveAllEMA;
Bool closeBelowAllEMA;
Bool buySignal;
Bool sellShortSignal;
Bool exitLongSignal;
Bool exitShortSignal;
Bool plotBuy;
Bool plotSell;
Bool plotExitLong;
Bool plotExitShort;
;
Events
OnBar(ArrayRef<Integer> indexs)
{
Range[0:DataSourceSize() - 1]
{
buySignal = False;
sellShortSignal = False;
exitLongSignal = False;
exitShortSignal = False;
plotBuy = False;
plotSell = False;
plotExitLong = False;
plotExitShort = False;
// 计算4根EMA
ema15 = XAverage(Close, EMA15_Period);
ema20 = XAverage(Close, EMA20_Period);
ema25 = XAverage(Close, EMA25_Period);
ema30 = XAverage(Close, EMA30_Period);
If(CurrentBar < EMA30_Period)
{
Return;
}
// 多头排列:15>20>25>30
isLongTrend = (ema15 > ema20) And (ema20 > ema25) And (ema25 > ema30);
// 空头排列:15<20<25<30
isShortTrend = (ema15 < ema20) And (ema20 < ema25) And (ema25 < ema30);
// 收盘价 突破/跌破 全部4根EMA
closeAboveAllEMA = (Close > ema15) And (Close > ema20) And (Close > ema25) And (Close > ema30);
closeBelowAllEMA = (Close < ema15) And (Close < ema20) And (Close < ema25) And (Close < ema30);
// ================= 开仓 =================
If(MarketPosition == 0)
{
buySignal = isLongTrend[1];
sellShortSignal = isShortTrend[1];
plotBuy = buySignal;
plotSell = sellShortSignal;
}
// ================= 平仓 =================
If(MarketPosition == 1)
{
// 多单:跌破所有4根EMA → 下根K开盘平
exitLongSignal = closeBelowAllEMA;
plotExitLong = exitLongSignal;
}
If(MarketPosition == -1)
{
// 空单:突破所有4根EMA → 下根K开盘平
exitShortSignal = closeAboveAllEMA;
plotExitShort = exitShortSignal;
}
// ================= 下单执行 =================
If(MarketPosition == 1 && exitLongSignal)
{
Sell(Lots, Open);
}
If(MarketPosition == -1 && exitShortSignal)
{
BuyToCover(Lots, Open);
}
If(MarketPosition == 0 && buySignal)
{
Buy(Lots, Open);
}
If(MarketPosition == 0 && sellShortSignal)
{
SellShort(Lots, Open);
}
// ================= 绘图 =================
PlotNumeric("EMA15", ema15, RGB(255,0,0), Enum_Line);
PlotNumeric("EMA20", ema20, RGB(0,255,0), Enum_Line);
PlotNumeric("EMA25", ema25, RGB(0,0,255), Enum_Line);
PlotNumeric("EMA30", ema30, RGB(255,255,0), Enum_Line);
If(plotBuy)
{
PlotString("Buy", "B", Low - MinMove*PriceScale, RGB(255,0,0));
}
If(plotSell)
{
PlotString("Short", "S", High + MinMove*PriceScale, RGB(0,0,255));
}
If(plotExitLong)
{
PlotString("ExitLong", "LP", Low - MinMove*PriceScale*2, RGB(255,100,100));
}
If(plotExitShort)
{
PlotString("ExitShort", "SP", High + MinMove*PriceScale*2, RGB(100,100,255));
}
}
}
此策略也是出现信号闪烁,之前使用了一段时间都没有问题,先说说情况,确认前几天都没有出现这种情况,并且使用了一段时间,都没什么问题,信号闪烁,胡乱开仓,此条策略是其中一条。不符合开仓的位置甚至开倒仓,本应该开空单并没有开,下行一段,跌破均线七八根k线才平多单,反正各种乱。帮排查一下什么问题和原因,上面第一条发给你的也是出现一样问题。
........
基本上就是零基础课程里第一个就会讲到的经典错误用法案例
想知道为什么就去看零基础课程里看吧
如果懒得学那就把代码里的所有close都改成close[1]