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

如何在Pine Script中实现Quasimodo指标的显示/隐藏功能?

实现Quasimodo Pattern指标的显示/隐藏功能

你的问题出在错误使用了显示控制方式——这个指标是通过line.new()和label.new()绘制形态,而非plot()函数,所以之前的plot控制逻辑无效。正确的做法是添加一个布尔开关输入,用它控制所有形态绘制代码的执行。

修改步骤

  1. 添加显示开关输入:在INPUTS区块新增布尔类型输入项,作为显示/隐藏的控制开关。
  2. 控制绘制逻辑:将所有创建线条和标签的代码,包裹在开关条件判断内,开关关闭时不执行绘制操作。

修改后的完整代码

//@version=5
indicator("Quasimodo Pattern", "QML", overlay=true, max_bars_back=5000, max_labels_count=500, max_lines_count=500)

//INPUTS
zigzag_len = input.int(13, "ZigZag Length", inline = "01")
// 添加显示/隐藏开关
show_qm_pattern = input.bool(true, "显示Quasimodo形态", inline="02")

var float[] high_points_arr = array.new_float(5)
var int[] high_index_arr = array.new_int(5)
var float[] low_points_arr = array.new_float(5)
var int[] low_index_arr = array.new_int(5)

//Indicators
to_up = high >= ta.highest(zigzag_len)
to_down = low <= ta.lowest(zigzag_len)
trend = 1
trend := nz(trend[1], 1)
trend := trend == 1 and to_down ? -1 : trend == -1 and to_up ? 1 : trend
last_trend_up_since = ta.barssince(to_up[1])
low_val = ta.lowest(nz(last_trend_up_since > 0 ? last_trend_up_since : 1, 1))
low_index = bar_index - ta.barssince(low_val == low)
last_trend_down_since = ta.barssince(to_down[1])
high_val = ta.highest(nz(last_trend_down_since > 0 ? last_trend_down_since : 1, 1))
high_index = bar_index - ta.barssince(high_val == high)

//Conditions
if ta.change(trend) != 0
    if trend == 1
        array.push(low_points_arr, low_val)
        array.push(low_index_arr, low_index)
    if trend == -1
        array.push(high_points_arr, high_val)
        array.push(high_index_arr, high_index)

f_get_high(ind) =>
    [array.get(high_points_arr, array.size(high_points_arr) - 1 - ind), array.get(high_index_arr, array.size(high_index_arr) - 1 - ind)]
f_get_low(ind) =>
    [array.get(low_points_arr, array.size(low_points_arr) - 1 - ind), array.get(low_index_arr, array.size(low_index_arr) - 1 - ind)]

[h0, h0i] = f_get_high(0)
[l0, l0i] = f_get_low(0)
[h1, h1i] = f_get_high(1)
[l1, l1i] = f_get_low(1)
[h2, h2i] = f_get_high(2)
[l2, l2i] = f_get_low(2)

bu_cond = trend == -1 and h2 > h1 and l1 > l0 and h0 > h1 and close > l1
be_cond = trend == 1 and l2 < l1 and h1 < h0 and l0 < l1 and close < h1

// 用开关控制看涨形态的绘制
if show_qm_pattern and bu_cond and not bu_cond[1]
    line.new(h2i, h2, l1i, l1, color=color.green, width=2)
    line.new(l1i, l1, h1i, h1, color=color.green, width=2)
    line.new(h1i, h1, l0i, l0, color=color.green, width=2)
    line.new(l0i, l0, h0i, h0, color=color.green, width=2)
    line.new(l1i, l1, bar_index, l1, color=color.green, width=2)
    label.new(bar_index, l1, "QM!", style=label.style_label_up, textcolor=color.white, color=color.green, size=size.tiny)
    alert("Bullish QM!", alert.freq_once_per_bar)

// 用开关控制看跌形态的绘制
if show_qm_pattern and be_cond and not be_cond[1]
    line.new(l2i, l2, h1i, h1, color=color.red, width=2)
    line.new(h1i, h1, l1i, l1, color=color.red, width=2)
    line.new(l1i, l1, h0i, h0, color=color.red, width=2)
    line.new(h0i, h0, l0i, l0, color=color.red, width=2)
    line.new(h1i, h1, bar_index, h1, color=color.red, width=2)
    label.new(bar_index, h1, "QM!", style=label.style_label_down, textcolor=color.white, color=color.red, size=size.tiny)
    alert("Bearish QM!", alert.freq_once_per_bar)

关键说明

  • 新增的show_qm_pattern开关会出现在指标设置面板中,勾选/取消勾选即可控制形态的显示与隐藏。
  • 指标的核心计算逻辑(趋势判断、极值点存储)仍会正常运行,关闭开关时仅停止绘制图表元素,不影响后续策略的信号逻辑。

内容的提问来源于stack exchange,提问作者Hell Bent Schnazzer

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.08 16:40:37