交易策略怎么不产生交易信号
//------------------------------------------------------------------------// 简称: QSXZYZSCL// 名称: 趋势性移动止盈止损策略(ADX+均线+斐波那契)- 收盘价模型// 类别: 公式应用// 类型: 用户应用// 输出: //------------------------------------------------------------------------Params Numeric ADXLength(14); // ADX周期 Numeric ADXThreshold(25); // ADX趋势阈值 Numeric FastMA(12); // 快速均线 Numeric SlowMA(26); // 慢速均线 Numeric ATRLength(14); // ATR周期 Numeric InitStopATR(2.0); // 初始止损:开仓价±N倍ATR Numeric TrailMultiplier(2.0); // 移动止损:回撤N倍ATR Numeric EntryZoneATR(0.5); // 入场区域:偏离快均线N倍ATR内 Numeric Lots(1); // 基础手数 Numeric FibExt127(1.272); // 第一止盈扩展位 Numeric FibExt1618(1.618); // 第二止盈扩展位 Numeric LookBack(20); // 波段高低点回看周期Vars // 均线(_Last保存上一根值,避免[1]) Numeric EMA_Fast; Numeric EMA_Fast_Last; Numeric EMA_Slow; Numeric EMA_Slow_Last; // ADX(全部用_Last) Numeric AdxValue; Numeric AdxValue_Last; Numeric plusDM; Numeric minusDM; Numeric trForADX; Numeric avgPlusDM; Numeric avgPlusDM_Last; Numeric avgMinusDM; Numeric avgMinusDM_Last; Numeric avgTRforADX; Numeric avgTRforADX_Last; Numeric plusDI; Numeric minusDI; Numeric dx; // ATR Numeric ATRValue; Numeric tr; // 波段与斐波那契 Numeric SwingHigh; Numeric SwingLow; Numeric FibRange; Numeric FibExtLevel1; Numeric FibExtLevel2; // 持仓管理 Numeric MyEntryPrice; Numeric EntryBar; Numeric HighestSinceEntry; Numeric LowestSinceEntry; Numeric BarsSinceEntry; Numeric TrailingStopPrice; // 趋势与信号 Numeric TrendUp; Numeric TrendDown; Numeric StrongTrend; Numeric SignalExecuted; // 中间变量 Numeric SF; Numeric Divisor; Numeric ProfitInATR; Numeric DynamicStop; Numeric stopPrice; Numeric initStop; Numeric entryCond;Events OnInit() { } OnBar(ArrayRef<Integer> indexs) { // ========== 0. 保存上一根K线的用户变量值(避免[1]语法)========== EMA_Fast_Last = EMA_Fast; EMA_Slow_Last = EMA_Slow; avgPlusDM_Last = avgPlusDM; avgMinusDM_Last = avgMinusDM; avgTRforADX_Last = avgTRforADX; AdxValue_Last = AdxValue; SignalExecuted = 0; // ========== 1. EMA手动计算 ========== If (CurrentBar == 0) { EMA_Fast = Close; EMA_Slow = Close; } Else { EMA_Fast = EMA_Fast_Last + 2 / (FastMA + 1) * (Close - EMA_Fast_Last); EMA_Slow = EMA_Slow_Last + 2 / (SlowMA + 1) * (Close - EMA_Slow_Last); } // ========== 2. ADX手动计算(Wilder平滑) ========== SF = 1 / ADXLength; If (CurrentBar > ADXLength) { plusDM = High - High[1]; If (plusDM < 0 || plusDM <= (Low[1] - Low)) plusDM = 0; minusDM = Low[1] - Low; If (minusDM < 0 || minusDM <= (High - High[1])) minusDM = 0; trForADX = Max(Max(High - Low, Abs(High - Close[1])), Abs(Low - Close[1])); avgPlusDM = avgPlusDM_Last * (ADXLength - 1) / ADXLength + plusDM / ADXLength; avgMinusDM = avgMinusDM_Last * (ADXLength - 1) / ADXLength + minusDM / ADXLength; avgTRforADX = avgTRforADX_Last * (ADXLength - 1) / ADXLength + trForADX / ADXLength; } Else { avgPlusDM = 0; avgMinusDM = 0; avgTRforADX = 0; } If (avgTRforADX > 0) { plusDI = 100 * avgPlusDM / avgTRforADX; minusDI = 100 * avgMinusDM / avgTRforADX; } Else { plusDI = 0; minusDI = 0; } Divisor = plusDI + minusDI; If (Divisor > 0) dx = 100 * Abs(plusDI - minusDI) / Divisor; Else dx = 0; If (CurrentBar == 0) AdxValue = 0; Else AdxValue = AdxValue_Last * (ADXLength - 1) / ADXLength + dx / ADXLength; // ========== 3. ATR手动计算 ========== tr = Max(Max(High - Low, Abs(High - Close[1])), Abs(Low - Close[1])); ATRValue = Average(tr, ATRLength); // ========== 4. 波段高低点与斐波那契扩展位 ========== SwingHigh = Highest(High, LookBack); SwingLow = Lowest(Low, LookBack); FibRange = SwingHigh - SwingLow; // ========== 5. 趋势判断 ========== If (EMA_Fast > EMA_Slow) TrendUp = 1; Else TrendUp = 0; If (EMA_Fast < EMA_Slow) TrendDown = 1; Else TrendDown = 0; If (AdxValue > ADXThreshold) StrongTrend = 1; Else StrongTrend = 0; // ========== 6. 持仓跟踪与风控计算 ========== If (MarketPosition == 1) { If (BarsSinceEntry == 0 || EntryBar == 0) { MyEntryPrice = AvgEntryPrice; HighestSinceEntry = High; EntryBar = CurrentBar; FibExtLevel1 = MyEntryPrice + FibRange * FibExt127; FibExtLevel2 = MyEntryPrice + FibRange * FibExt1618; } Else { If (High > HighestSinceEntry) HighestSinceEntry = High; } BarsSinceEntry = CurrentBar - EntryBar + 1; // 止损价 = Max(初始止损, 移动止损) initStop = MyEntryPrice - InitStopATR * ATRValue; DynamicStop = HighestSinceEntry - TrailMultiplier * ATRValue; stopPrice = Max(initStop, DynamicStop); // 保本机制:盈利超1倍ATR后,止损不低于成本价 If (HighestSinceEntry - MyEntryPrice >= ATRValue && stopPrice < MyEntryPrice) stopPrice = MyEntryPrice; } Else If (MarketPosition == -1) { If (BarsSinceEntry == 0 || EntryBar == 0) { MyEntryPrice = AvgEntryPrice; LowestSinceEntry = Low; EntryBar = CurrentBar; FibExtLevel1 = MyEntryPrice - FibRange * FibExt127; FibExtLevel2 = MyEntryPrice - FibRange * FibExt1618; } Else { If (Low < LowestSinceEntry) LowestSinceEntry = Low; } BarsSinceEntry = CurrentBar - EntryBar + 1; initStop = MyEntryPrice + InitStopATR * ATRValue; DynamicStop = LowestSinceEntry + TrailMultiplier * ATRValue; stopPrice = Min(initStop, DynamicStop); // 保本机制 If (MyEntryPrice - LowestSinceEntry >= ATRValue && stopPrice > MyEntryPrice) stopPrice = MyEntryPrice; } Else { BarsSinceEntry = 0; EntryBar = 0; MyEntryPrice = 0; HighestSinceEntry = 0; LowestSinceEntry = 0; } // ========== 7. 出场逻辑(优先级:止损 > 止盈 > 均线破位 > ADX走弱)========== // 多头出场 If (MarketPosition == 1) { // 1) 止损(含初始、移动、保本) If (Close <= stopPrice) { Sell(0, Close); SignalExecuted = 1; } // 2) 第一止盈目标 Else If (Close >= FibExtLevel1 && BarsSinceEntry >= 3) { If (CurrentContracts >= Lots * 2) { Sell(Lots, Close); FibExtLevel1 = FibExtLevel2; // 剩余仓位看第二目标 SignalExecuted = 1; } Else { Sell(0, Close); SignalExecuted = 1; } } // 3) 第二止盈目标 Else If (Close >= FibExtLevel2 && BarsSinceEntry >= 3) { Sell(0, Close); SignalExecuted = 1; } // 4) 均线破位(跌破慢速均线,比死叉更灵敏) Else If (Close < EMA_Slow) { Sell(0, Close); SignalExecuted = 1; } // 5) ADX走弱(缓冲5点,避免阈值附近震荡) Else If (AdxValue < ADXThreshold - 5 && BarsSinceEntry >= 5) { Sell(0, Close); SignalExecuted = 1; } } // 空头出场 If (MarketPosition == -1 && SignalExecuted == 0) { If (Close >= stopPrice) { BuyToCover(0, Close); SignalExecuted = 1; } Else If (Close <= FibExtLevel1 && BarsSinceEntry >= 3) { If (CurrentContracts >= Lots * 2) { BuyToCover(Lots, Close); FibExtLevel1 = FibExtLevel2; SignalExecuted = 1; } Else { BuyToCover(0, Close); SignalExecuted = 1; } } Else If (Close <= FibExtLevel2 && BarsSinceEntry >= 3) { BuyToCover(0, Close); SignalExecuted = 1; } Else If (Close > EMA_Slow) { BuyToCover(0, Close); SignalExecuted = 1; } Else If (AdxValue < ADXThreshold - 5 && BarsSinceEntry >= 5) { BuyToCover(0, Close); SignalExecuted = 1; } } // 出场后本Bar不再开仓 If (SignalExecuted == 1) Return; // ========== 8. 入场逻辑(双模式:回调入场 + 突破入场)========== If (MarketPosition == 0 && CurrentBar > SlowMA && ATRValue > 0) { // --- 多头入场 --- If (StrongTrend == 1 && TrendUp == 1) { entryCond = 0; // 模式A:回调入场。价格回调至快均线附近(±EntryZoneATR*ATR),但未跌破慢均线 If (Close <= EMA_Fast + EntryZoneATR * ATRValue && Close >= EMA_Slow) entryCond = 1; // 模式B:突破入场。ADX极强(>阈值+5),且收盘价突破前一根K线高点 If (AdxValue > ADXThreshold + 5 && Close > High[1]) entryCond = 1; If (entryCond == 1) { Buy(Lots, Close); SignalExecuted = 1; } } // --- 空头入场 --- If (SignalExecuted == 0 && StrongTrend == 1 && TrendDown == 1) { entryCond = 0; // 模式A:反弹至快均线附近,但未涨破慢均线 If (Close >= EMA_Fast - EntryZoneATR * ATRValue && Close <= EMA_Slow) entryCond = 1; // 模式B:ADX极强,跌破前低 If (AdxValue > ADXThreshold + 5 && Close < Low[1]) entryCond = 1; If (entryCond == 1) { SellShort(Lots, Close); SignalExecuted = 1; } } } } OnBarOpen(ArrayRef<Integer> indexs) {} OnBarClose(ArrayRef<Integer> indexs) {} OnTick(TickRef evtTick) {} OnPosition(PositionRef pos) {} OnOrder(OrderRef ord) {} OnFill(FillRef ordFill) {} OnTimer(Integer id, Integer intervalMillsecs) {} OnEvent(StringRef evtName, MapRef<String,String> evtValue) {} OnExit() {}