You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

QuadCode脚本优化:仅在趋势首根K线生成买卖信号

优化QuadCode脚本:仅在趋势启动首根K线生成买卖信号

问题背景

我有一款适配IQOption平台的QuadCode脚本,基于EMA13与MACD柱线的条件为K线上色:

  • 当ema13 > ema13[1]且hist > hist[1]时,K线为绿色(看涨)
  • 当ema13 < ema13[1]且hist < hist[1]时,K线为红色(看跌)
  • 其余情况为中性色

统计显示,90%的情况下同色K线会连续出现1-2根。目前脚本会给所有符合上色条件的K线标注买卖信号,需要优化为仅在趋势启动的第一根符合上色条件的K线上生成信号。

解决方案思路

要实现仅在趋势启动首根K线出信号,核心是判断:

  • 当前K线满足对应方向的上色条件
  • 前一根K线不满足该方向的上色条件(即前一根为中性色或相反方向)

通过对比当前与前一根的条件状态,过滤掉连续同方向的信号,只保留趋势启动的第一根信号。

修改后的完整脚本

instrument {
    name = 'ETM PROFIT',
    short_name = 'ETM',
    icon = 'https://etmtradingbucket.s3.amazonaws.com/public/logo.png',
    overlay = true
}

Signal_period = input(5,"VIP | Signals Period",input.integer,1,1000,1)

input_group {
    "Compra Proxima vela",
    colorBuy = input { default = "green", type = input.color }, 
    visibleBuy = input { default = true, type = input.plot_visibility }
}

input_group {
    "Venda Proxima vela",
    colorSell = input { default = "red", type = input.color },
    visibleSell = input { default = true, type = input.plot_visibility }
}

input_group {
    "MACD",
    "Slow and fast EMA periods, used in MACD calculation",
    fast = input (12, "front.platform.fast period", input.integer, 1, 250),
    slow = input (26, "front.platform.fast period", input.integer, 1, 250)
}
input_group {
    "front.platform.signal-line",
    "Reference signal series period",
    signal_period = input (9, "front.period", input.integer, 1, 250)
}
input_group {
    "front.newind.emaperiod",
    ema_period = input (13, "front.period", input.integer, 1, 250)
}
input_group {
    "front.newind.barcolors",
    positive = input { default = "#2CAC40",   type = input.color },
    neutral  = input { default = "#C7CAD1", type = input.color },
    negative = input { default = "#DB4931",   type = input.color },
}
fastMA = ema(close, fast)
slowMA = ema(close, slow)
macd = fastMA - slowMA
signal = sma(macd, signal_period)
hist = macd - signal
ema13 = ema (close, ema_period)
local bar_color
        
if ema13 > ema13 [1] and hist > hist [1] then
    bar_color = positive
elseif ema13 < ema13 [1] and hist < hist [1] then
    bar_color = negative
else
    bar_color = neutral
end
plot_candle (open, high, low, close, "ES", bar_color)

// 基础条件:判断当前K线是否满足看涨/看跌上色条件
currentBuy = ema13 > ema13[1] and hist > hist[1]
currentSell = ema13 < ema13[1] and hist < hist[1]

// 前一根K线的条件状态
prevBuy = ema13[1] > ema13[2] and hist[1] > hist[2]
prevSell = ema13[1] < ema13[2] and hist[1] < hist[2]

// 优化后的信号条件:仅当前满足且前一根不满足对应方向时触发
buySignal = currentBuy and not prevBuy
sellSignal = currentSell and not prevSell

plot_shape(
    buySignal,
    "",
    shape_style.triangleup,
    shape_size.huge,    
    colorBuy,    
    shape_location.belowbar,
    0,
    "RX-Buy",
    "pink"
) 

plot_shape(
    sellSignal,
    "ENTER",
    shape_style.triangledown,
    shape_size.huge,
    colorSell,
    shape_location.abovebar,
    0,
    "RX-Sell",
    "salmon"
)

关键修改说明

  1. 拆分基础条件与信号条件:
    • currentBuy/currentSell:判断当前K线是否符合上色条件
    • prevBuy/prevSell:判断前一根K线是否符合对应方向的上色条件
  2. 优化信号触发逻辑:
    • buySignal:当前满足看涨条件,且前一根不满足看涨条件(趋势刚启动)
    • sellSignal:当前满足看跌条件,且前一根不满足看跌条件(趋势刚启动)
  3. 替换原plot_shape的触发条件为新的buySignal和sellSignal

内容的提问来源于stack exchange,提问作者ROCKI

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.09 10:17:03