//--- // 简称: candel // 名称: EXPMA金叉死叉+吊灯止损_一次性执行 // 类别: 公式应用 // 类型: 用户应用 // 输出: Void //--- /* 策略说明: 手动指定方向的一次性执行工具。启动前设 TradeDir(1做多等金叉/-1做空等死叉)。 启动后等指定方向的 EXPMA 金叉/死叉进场;进场后用【吊灯线】与【固定止损】中 更紧的一条保护,盘中触价即平;反向交叉兜底平仓;平仓后不再进场(一次性)。 */ Params Numeric TradeDir(1); // 本次方向: 1=做多(等金叉) -1=做空(等死叉) Numeric EXPMA_Fast(21); // EXPMA 快线 Numeric EXPMA_Slow(42); // EXPMA 慢线 Numeric ATR_Length(20); // ATR 窗口 Numeric K(2.5); // 吊灯 ATR 倍数 Numeric MaxLoss(15); // 固定止损点数(0=关闭,只用吊灯) Numeric Lots(1); // 开仓手数 Vars Series ema_fast; // EXPMA 快线 Series ema_slow; // EXPMA 慢线 Series atr_val; // ATR Series golden_cross; // 金叉(前一根确认) Series death_cross; // 死叉(前一根确认) Numeric extreme; // 多:进场后最高 / 空:进场后最低 Numeric chandLine; // 吊灯线 Numeric fixedLine; // 固定止损线 Numeric stopLine; // 实际生效止损线(取更紧者) Global Bool tradeDone(False); // 一次性完成:平仓后不再进场 Global Numeric prevPos(0); // 上一根持仓方向 Events OnBar(ArrayRef indexs) { Range[0:DataSourceSize() - 1] { ema_fast = XAverage(Close, EXPMA_Fast); ema_slow = XAverage(Close, EXPMA_Slow); atr_val = AvgTrueRange(ATR_Length); golden_cross = CrossOver(ema_fast[1], ema_slow[1]); death_cross = CrossUnder(ema_fast[1], ema_slow[1]); } Range[0:0] { If(CurrentBar < 100) Return; If(EXPMA_Fast >= EXPMA_Slow) Return; PlotNumeric("EXPMA_Fast", ema_fast, 0, Yellow); PlotNumeric("EXPMA_Slow", ema_slow, 0, Red); // 一次性完成检测:上一根有仓、本根空仓 => 已平仓 If(prevPos <> 0 && MarketPosition == 0) { tradeDone = True; } // ---------- 进场:空仓且未完成,只做指定方向 ---------- If(MarketPosition == 0 && !tradeDone) { If(TradeDir == 1 && golden_cross) { Buy(Lots, Close); Commentary("金叉开多"); } Else If(TradeDir == -1 && death_cross) { SellShort(Lots, Close); Commentary("死叉开空"); } } // ---------- 持多:吊灯+固定止损(取更紧),死叉兜底 ---------- If(MarketPosition == 1 && BarsSinceEntry > 0) { If(BarsSinceEntry == 1) { extreme = High; } Else { extreme = Max(extreme, High); } chandLine = extreme - K * atr_val; If(MaxLoss > 0) { fixedLine = AvgEntryPrice - MaxLoss; } Else { fixedLine = -1000000; } stopLine = Max(chandLine, fixedLine); If(death_cross) { Sell(Lots, Close); Commentary("死叉平多"); } Else If(Low <= stopLine) { Sell(Lots, stopLine); Commentary("吊灯/固定止损平多"); } } // ---------- 持空:对称 ---------- If(MarketPosition == -1 && BarsSinceEntry > 0) { If(BarsSinceEntry == 1) { extreme = Low; } Else { extreme = Min(extreme, Low); } chandLine = extreme + K * atr_val; If(MaxLoss > 0) { fixedLine = AvgEntryPrice + MaxLoss; } Else { fixedLine = 1000000; } stopLine = Min(chandLine, fixedLine); If(golden_cross) { BuyToCover(Lots, Close); Commentary("金叉平空"); } Else If(High >= stopLine) { BuyToCover(Lots, stopLine); Commentary("吊灯/固定止损平空"); } } prevPos = MarketPosition; } }