From cdc36aab9e3d0570f02162a73384d9ca2a14f670 Mon Sep 17 00:00:00 2001 From: jackyu66git Date: Sun, 28 Sep 2025 18:49:19 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E8=AE=BE=E7=BD=AE=E5=92=8C?= =?UTF-8?q?=E5=AE=9E=E9=99=85=E5=9B=BE=E6=A0=87=E6=97=B6=E9=97=B4=E5=91=A8?= =?UTF-8?q?=E6=9C=9F=E5=AF=B9=E5=BA=94=E9=97=AE=E9=A2=98=EF=BC=8C=E4=BF=AE?= =?UTF-8?q?=E6=94=B9=E5=AD=97=E4=BD=93normal?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- EMA52横线版.pine | 111 +++++++++++++++++++++++++++++++++++++---------- 多周期EMA52.pine | 100 ++++++++++++++++++++++++++++++++++-------- 2 files changed, 171 insertions(+), 40 deletions(-) diff --git a/EMA52横线版.pine b/EMA52横线版.pine index 493aff2..631aec9 100644 --- a/EMA52横线版.pine +++ b/EMA52横线版.pine @@ -4,7 +4,7 @@ indicator("EMA52横线版", overlay=true, max_labels_count=500, max_lines_count= // ============ 输入 ============ enableHTF = input.bool(true, "启用长级别", group="显示") enableLTF = input.bool(true, "启用次级别", group="显示") -maxHTFInput = input.int(10, "最多显示长级别数量", minval=1, maxval=12, group="显示") +maxHTFInput = input.int(10, "最多显示长级别数量", minval=1, maxval=13, group="显示") maxLTFInput = input.int(3, "最多显示次级别数量", minval=1, maxval=4, group="显示") includeSeconds = input.bool(false, "包含秒级别(需高级套餐)", group="显示") protectAutoscale = input.bool(true, "防拉伸: 仅在可视区间内显示", group="显示") @@ -12,7 +12,7 @@ lookbackBars = input.int(300, "可视区间近多少根K线", minval=50, padPercent = input.float(2.0, "可视边界留白(%)", minval=0.0, maxval=20.0, step=0.1, group="显示") labelBgAlpha = input.int(85, "标签背景透明(0-100)", minval=0, maxval=100, group="标注") -labelSize = input.string("tiny", "标签字号", options=["tiny","small","normal","large","huge"], group="标注") +labelSize = input.string("normal", "标签字号", options=["tiny","small","normal","large","huge"], group="标注") labelOffsetBars = input.int(2, "标签右移K线的距离(根数)", minval=0, maxval=50, group="标注") showLegend = input.bool(true, "显示右侧图例(周期映射)", group="标注") @@ -68,6 +68,12 @@ hColor12 = input.color(color.new(color.silver, 0), "颜色 1M", group=groupHTF) hWidth12 = input.int(1, "线宽 1M", minval=1, maxval=4, group=groupHTF) hStyle12 = input.string("dotted", "样式 1M", options=["solid","dashed","dotted"], group=groupHTF) +// 3日线(新增) +hShow13 = input.bool(true, "显示 3D", group=groupHTF) +hColor13 = input.color(color.new(color.gray, 0), "颜色 3D", group=groupHTF) +hWidth13 = input.int(1, "线宽 3D", minval=1, maxval=4, group=groupHTF) +hStyle13 = input.string("dotted", "样式 3D", options=["solid","dashed","dotted"], group=groupHTF) + // 次级别 groupLTF = "按线控制 - 次级别" lShow1 = input.bool(true, "显示 1m", group=groupLTF) @@ -87,14 +93,21 @@ lColor4 = input.color(color.new(color.aqua, 30),"颜色 15m", group=groupLTF) lWidth4 = input.int(1, "线宽 15m", minval=1, maxval=4, group=groupLTF) lStyle4 = input.string("dotted", "样式 15m", options=["solid","dashed","dotted"], group=groupLTF) +// 本级别 +groupCURR = "按线控制 - 本级别" +cShow = input.bool(true, "显示 本级别", group=groupCURR) +cColor = input.color(color.new(color.white, 0), "颜色 本级别", group=groupCURR) +cWidth = input.int(1, "线宽 本级别", minval=1, maxval=4, group=groupCURR) +cStyle = input.string("solid", "样式 本级别", options=["solid","dashed","dotted"], group=groupCURR) + // 样式字符串到 line.style 的映射 f_lineStyle(string s) => s == "solid" ? line.style_solid : s == "dashed" ? line.style_dashed : line.style_dotted -// 取对应序号的配置 -f_htfShow(int i) => i == 0 ? hShow1 : i == 1 ? hShow2 : i == 2 ? hShow3 : i == 3 ? hShow4 : i == 4 ? hShow5 : i == 5 ? hShow6 : i == 6 ? hShow7 : i == 7 ? hShow8 : i == 8 ? hShow9 : i == 9 ? hShow10 : i == 10 ? hShow11 : hShow12 -f_htfColor(int i) => i == 0 ? hColor1 : i == 1 ? hColor2 : i == 2 ? hColor3 : i == 3 ? hColor4 : i == 4 ? hColor5 : i == 5 ? hColor6 : i == 6 ? hColor7 : i == 7 ? hColor8 : i == 8 ? hColor9 : i == 9 ? hColor10 : i == 10 ? hColor11 : hColor12 -f_htfWidth(int i) => i == 0 ? hWidth1 : i == 1 ? hWidth2 : i == 2 ? hWidth3 : i == 3 ? hWidth4 : i == 4 ? hWidth5 : i == 5 ? hWidth6 : i == 6 ? hWidth7 : i == 7 ? hWidth8 : i == 8 ? hWidth9 : i == 9 ? hWidth10 : i == 10 ? hWidth11 : hWidth12 -f_htfStyle(int i) => f_lineStyle(i == 0 ? hStyle1 : i == 1 ? hStyle2 : i == 2 ? hStyle3 : i == 3 ? hStyle4 : i == 4 ? hStyle5 : i == 5 ? hStyle6 : i == 6 ? hStyle7 : i == 7 ? hStyle8 : i == 8 ? hStyle9 : i == 9 ? hStyle10 : i == 10 ? hStyle11 : hStyle12) +// 按周期键值映射到控件(避免索引与实际周期错位) +f_htfShowK(string key) => key == "30" ? hShow1 : key == "45" ? hShow2 : key == "60" ? hShow3 : key == "120" ? hShow4 : key == "240" ? hShow5 : key == "360" ? hShow6 : key == "480" ? hShow7 : key == "720" ? hShow8 : key == "D" ? hShow9 : key == "2D" ? hShow10 : key == "3D" ? hShow13 : key == "W" ? hShow11 : hShow12 +f_htfColorK(string key) => key == "30" ? hColor1 : key == "45" ? hColor2 : key == "60" ? hColor3 : key == "120" ? hColor4 : key == "240" ? hColor5 : key == "360" ? hColor6 : key == "480" ? hColor7 : key == "720" ? hColor8 : key == "D" ? hColor9 : key == "2D" ? hColor10 : key == "3D" ? hColor13 : key == "W" ? hColor11 : hColor12 +f_htfWidthK(string key) => key == "30" ? hWidth1 : key == "45" ? hWidth2 : key == "60" ? hWidth3 : key == "120" ? hWidth4 : key == "240" ? hWidth5 : key == "360" ? hWidth6 : key == "480" ? hWidth7 : key == "720" ? hWidth8 : key == "D" ? hWidth9 : key == "2D" ? hWidth10 : key == "3D" ? hWidth13 : key == "W" ? hWidth11 : hWidth12 +f_htfStyleK(string key) => f_lineStyle(key == "30" ? hStyle1 : key == "45" ? hStyle2 : key == "60" ? hStyle3 : key == "120" ? hStyle4 : key == "240" ? hStyle5 : key == "360" ? hStyle6 : key == "480" ? hStyle7 : key == "720" ? hStyle8 : key == "D" ? hStyle9 : key == "2D" ? hStyle10 : key == "3D" ? hStyle13 : key == "W" ? hStyle11 : hStyle12) f_ltfShow(int i) => i == 0 ? lShow1 : i == 1 ? lShow2 : i == 2 ? lShow3 : lShow4 f_ltfColor(int i) => i == 0 ? lColor1 : i == 1 ? lColor2 : i == 2 ? lColor3 : lColor4 @@ -115,6 +128,7 @@ if barstate.isfirst and array.size(HTF_KEYS) == 0 array.push(HTF_KEYS, "720") array.push(HTF_KEYS, "D") array.push(HTF_KEYS, "2D") + array.push(HTF_KEYS, "3D") array.push(HTF_KEYS, "W") array.push(HTF_KEYS, "M") array.push(LTF_KEYS, "1") @@ -259,21 +273,22 @@ ltfsz = array.size(ltfRes) if ltfsz > 0 take = math.min(maxLTFInput, 4, ltfsz) for k = 0 to take - 1 - array.push(ltfSel, array.get(ltfRes, ltfsz - 1 - k)) + array.push(ltfSel, array.get(ltfRes, k)) -htfCountSelected = math.min(array.size(htfRes), maxHTFInput, 12) +htfCountSelected = math.min(array.size(htfRes), maxHTFInput, 13) ltfCountSelected = math.min(array.size(ltfSel), 4) // ============ 计算最新 EMA52 ============ var float[] htfVals = array.new_float() var float[] ltfVals = array.new_float() +currVal = ta.ema(close, 52) if barstate.isfirst - for _i = 0 to 11 + for _i = 0 to 12 array.push(htfVals, na) for _j = 0 to 3 array.push(ltfVals, na) -for i = 0 to 11 +for i = 0 to 12 float val = na if enableHTF and i < htfCountSelected tfStr = array.get(htfRes, i) @@ -302,11 +317,13 @@ var line[] htfLines = array.new_line() var line[] ltfLines = array.new_line() var label[] htfLabels = array.new_label() var label[] ltfLabels = array.new_label() +var line currLine = na +var label currLabel = na var string lastTicker = na var string lastTf = na var table legendTbl = na if barstate.isfirst - for _i = 0 to 11 + for _i = 0 to 12 array.push(htfLines, na) array.push(htfLabels, na) for _j = 0 to 3 @@ -321,7 +338,7 @@ if barstate.isfirst symbolChanged = lastTicker != syminfo.tickerid tfChanged = lastTf != timeframe.period if symbolChanged or tfChanged - for i = 0 to 11 + for i = 0 to 12 l = array.get(htfLines, i) if not na(l) line.delete(l) @@ -339,6 +356,12 @@ if symbolChanged or tfChanged if not na(lb) label.delete(lb) array.set(ltfLabels, i, na) + if not na(currLine) + line.delete(currLine) + currLine := na + if not na(currLabel) + label.delete(currLabel) + currLabel := na lastTicker := syminfo.tickerid lastTf := timeframe.period @@ -352,11 +375,14 @@ if barstate.islast table.cell(legendTbl, 1, r, "", text_color=color.new(color.white, 0), text_halign=text.align_right, bgcolor=color.new(color.black, 100)) row = 0 // 长级别 - for i = 0 to 11 + for i = 0 to 12 v = array.get(htfVals, i) - if enableHTF and i < htfCountSelected and f_htfShow(i) and not na(v) and f_inRange(v) + if enableHTF and i < htfCountSelected and not na(v) and f_inRange(v) tfStr = array.get(htfRes, i) - col = f_htfColor(i) + show = f_htfShowK(tfStr) + col = f_htfColorK(tfStr) + if not show + continue table.cell(legendTbl, 0, row, f_prettyTf(tfStr), text_color=col, text_halign=text.align_left) table.cell(legendTbl, 1, row, str.tostring(v, format.mintick), text_color=col, text_halign=text.align_right) row += 1 @@ -369,26 +395,32 @@ if barstate.islast table.cell(legendTbl, 0, row, f_prettyTf(tfStr), text_color=col, text_halign=text.align_left) table.cell(legendTbl, 1, row, str.tostring(v, format.mintick), text_color=col, text_halign=text.align_right) row += 1 + // 本级别 + if cShow and not na(currVal) and f_inRange(currVal) + table.cell(legendTbl, 0, row, f_prettyTf(timeframe.period), text_color=cColor, text_halign=text.align_left) + table.cell(legendTbl, 1, row, str.tostring(currVal, format.mintick), text_color=cColor, text_halign=text.align_right) + row += 1 // 长级别 - for i = 0 to 11 - // 颜色/线宽/样式来自按线控制面板 - col = f_htfColor(i) + for i = 0 to 12 + // 颜色/线宽/样式来自按线控制面板(基于键值) v = array.get(htfVals, i) - show = enableHTF and i < htfCountSelected and f_htfShow(i) and not na(v) and f_inRange(v) + tfStr = i < array.size(htfRes) ? array.get(htfRes, i) : "" + show = enableHTF and i < htfCountSelected and not na(v) and f_inRange(v) and (tfStr != "" and f_htfShowK(tfStr)) + col = tfStr != "" ? f_htfColorK(tfStr) : color.new(color.gray, 80) ln = array.get(htfLines, i) lb = array.get(htfLabels, i) if show x1 = bar_index - lookbackBars x2 = bar_index + labelOffsetBars if na(ln) - ln := line.new(x1, v, x2, v, xloc=xloc.bar_index, extend=extend.none, color=col, width=f_htfWidth(i), style=f_htfStyle(i)) + ln := line.new(x1, v, x2, v, xloc=xloc.bar_index, extend=extend.none, color=col, width=f_htfWidthK(tfStr), style=f_htfStyleK(tfStr)) array.set(htfLines, i, ln) else line.set_xy1(ln, x1, v) line.set_xy2(ln, x2, v) line.set_color(ln, col) - line.set_width(ln, f_htfWidth(i)) - line.set_style(ln, f_htfStyle(i)) + line.set_width(ln, f_htfWidthK(tfStr)) + line.set_style(ln, f_htfStyleK(tfStr)) txt = f_prettyTf(array.get(htfRes, i)) + ' ' + str.tostring(v, format.mintick) if na(lb) lb := label.new(bar_index + labelOffsetBars, v, txt, xloc=xloc.bar_index, yloc=yloc.price, style=label.style_label_left, textcolor=col, color=color.new(col, labelBgAlpha), size=f_strToSize(labelSize)) @@ -447,4 +479,37 @@ if barstate.islast label.delete(lb) array.set(ltfLabels, i, na) + // 本级别 + colC = cColor + vC = currVal + showC = cShow and not na(vC) and f_inRange(vC) + if showC + x1 = bar_index - lookbackBars + x2 = bar_index + labelOffsetBars + if na(currLine) + currLine := line.new(x1, vC, x2, vC, xloc=xloc.bar_index, extend=extend.none, color=colC, width=(cWidth), style=f_lineStyle(cStyle)) + else + line.set_xy1(currLine, x1, vC) + line.set_xy2(currLine, x2, vC) + line.set_color(currLine, colC) + line.set_width(currLine, (cWidth)) + line.set_style(currLine, f_lineStyle(cStyle)) + txtC = f_prettyTf(timeframe.period) + ' ' + str.tostring(vC, format.mintick) + if na(currLabel) + currLabel := label.new(bar_index + labelOffsetBars, vC, txtC, xloc=xloc.bar_index, yloc=yloc.price, style=label.style_label_left, textcolor=colC, color=color.new(colC, labelBgAlpha), size=f_strToSize(labelSize)) + else + label.set_xy(currLabel, bar_index + labelOffsetBars, vC) + label.set_text(currLabel, txtC) + label.set_textcolor(currLabel, colC) + label.set_color(currLabel, color.new(colC, labelBgAlpha)) + label.set_style(currLabel, label.style_label_left) + label.set_size(currLabel, f_strToSize(labelSize)) + else + if not na(currLine) + line.delete(currLine) + currLine := na + if not na(currLabel) + label.delete(currLabel) + currLabel := na + diff --git a/多周期EMA52.pine b/多周期EMA52.pine index ba971df..b725cac 100644 --- a/多周期EMA52.pine +++ b/多周期EMA52.pine @@ -3,6 +3,7 @@ indicator("多周期EMA52 (HTF/LTF)", overlay=true, max_labels_count=500, max_li // ======================== 配置输入 ======================== // 主控 +enableCTF = input.bool(true, "启用本级别", group="显示") enableHTF = input.bool(true, "启用长级别", group="显示") enableLTF = input.bool(true, "启用次级别", group="显示") maxHTFInput = input.int(10, "最多显示长级别数量", minval=1, maxval=12, group="显示") @@ -19,8 +20,16 @@ dashLenLTF = input.int(2, "次级别虚线段长度", minval=1, maxval=50, gro dashGapLTF = input.int(3, "次级别虚线间隔", minval=1, maxval=50, group="线型") lineWidthHTF = input.int(1, "长级别线宽", minval=1, maxval=4, group="线型") lineWidthLTF = input.int(1, "次级别线宽", minval=1, maxval=4, group="线型") +lineWidthCTF = input.int(2, "本级别线宽", minval=1, maxval=4, group="线型") +enableSmooth = input.bool(true, "平滑显示(EMA)", group="线型") +smoothLenHTF = input.int(3, "长级别平滑长度", minval=1, maxval=50, group="线型") +smoothLenLTF = input.int(3, "次级别平滑长度", minval=1, maxval=50, group="线型") +smoothLenCTF = input.int(3, "本级别平滑长度", minval=1, maxval=50, group="线型") +smoothMethod = input.string("ALMA", "平滑方法", options=["EMA","DEMA","TEMA","WMA","HMA","ALMA","LinReg"], group="线型") +almaOffset = input.float(0.85, "ALMA偏移(0-1)", minval=0.0, maxval=1.0, step=0.05, group="线型") +almaSigma = input.float(6.0, "ALMA Sigma", minval=0.5, maxval=10.0, step=0.5, group="线型") labelBgAlpha = input.int(85, "标签背景透明(0-100)", minval=0, maxval=100, group="标注") -labelSize = input.string("tiny", "标签字号", options=["tiny","small","normal","large","huge"], group="标注") +labelSize = input.string("normal", "标签字号", options=["tiny","small","normal","large","huge"], group="标注") labelOffsetBars = input.int(2, "标签右移K线的距离(根数)", minval=0, maxval=50, group="标注") // 常量上限(编译期固定,用于生成固定数量的 plot 位) @@ -33,6 +42,29 @@ f_dash(series float v, int phase, int dashLen, int dashGap) => isShow = (bar_index + phase) % (dashLen + dashGap) < dashLen isShow ? v : na +// 自定义 DEMA / TEMA(Pine 无内置 dema/tema) +f_dema(series float v, int len) => + ema1 = ta.ema(v, len) + ema2 = ta.ema(ema1, len) + 2.0 * ema1 - ema2 + +f_tema(series float v, int len) => + ema1 = ta.ema(v, len) + ema2 = ta.ema(ema1, len) + ema3 = ta.ema(ema2, len) + 3.0 * (ema1 - ema2) + ema3 + +// 通用平滑(仅用于显示) +f_smooth(series float v, int len) => + not enableSmooth or len <= 1 ? v : + smoothMethod == "EMA" ? ta.ema(v, len) : + smoothMethod == "DEMA" ? f_dema(v, len) : + smoothMethod == "TEMA" ? f_tema(v, len) : + smoothMethod == "WMA" ? ta.wma(v, len) : + smoothMethod == "HMA" ? ta.hma(v, len) : + smoothMethod == "ALMA" ? ta.alma(v, len, almaOffset, almaSigma) : + ta.linreg(v, len, 0) + f_strToSize(string s) => switch s "tiny" => size.tiny "small" => size.small @@ -177,6 +209,7 @@ ltfCountSelected = math.min(array.size(ltfSel), MAX_LTF) // 预分配数值数组(保持固定最大长度) var float[] htfVals = array.new_float() var float[] ltfVals = array.new_float() +float ctfVal = na if barstate.isfirst for _i = 0 to MAX_HTF - 1 array.push(htfVals, na) @@ -201,29 +234,35 @@ for i = 0 to MAX_LTF - 1 val := request.security(syminfo.tickerid, tfStr, ta.ema(close, 52), barmerge.gaps_off, barmerge.lookahead_off) array.set(ltfVals, i, val) +// 本级别 EMA52 +if enableCTF + ctfVal := ta.ema(close, 52) + // ======================== 绘制(虚线 + 标签) ======================== // 顶层固定 plot(避免在局部/循环中调用 plot) -plot((enableHTF and 0 < htfCountSelected) ? f_gate(f_dash(array.get(htfVals, 0), 0 * 2, dashLenHTF, dashGapHTF)) : na, title='HTF EMA52 1', color=f_paletteColor(0), linewidth=lineWidthHTF, style=plot.style_line) -plot((enableHTF and 1 < htfCountSelected) ? f_gate(f_dash(array.get(htfVals, 1), 1 * 2, dashLenHTF, dashGapHTF)) : na, title='HTF EMA52 2', color=f_paletteColor(1), linewidth=lineWidthHTF, style=plot.style_line) -plot((enableHTF and 2 < htfCountSelected) ? f_gate(f_dash(array.get(htfVals, 2), 2 * 2, dashLenHTF, dashGapHTF)) : na, title='HTF EMA52 3', color=f_paletteColor(2), linewidth=lineWidthHTF, style=plot.style_line) -plot((enableHTF and 3 < htfCountSelected) ? f_gate(f_dash(array.get(htfVals, 3), 3 * 2, dashLenHTF, dashGapHTF)) : na, title='HTF EMA52 4', color=f_paletteColor(3), linewidth=lineWidthHTF, style=plot.style_line) -plot((enableHTF and 4 < htfCountSelected) ? f_gate(f_dash(array.get(htfVals, 4), 4 * 2, dashLenHTF, dashGapHTF)) : na, title='HTF EMA52 5', color=f_paletteColor(4), linewidth=lineWidthHTF, style=plot.style_line) -plot((enableHTF and 5 < htfCountSelected) ? f_gate(f_dash(array.get(htfVals, 5), 5 * 2, dashLenHTF, dashGapHTF)) : na, title='HTF EMA52 6', color=f_paletteColor(5), linewidth=lineWidthHTF, style=plot.style_line) -plot((enableHTF and 6 < htfCountSelected) ? f_gate(f_dash(array.get(htfVals, 6), 6 * 2, dashLenHTF, dashGapHTF)) : na, title='HTF EMA52 7', color=f_paletteColor(6), linewidth=lineWidthHTF, style=plot.style_line) -plot((enableHTF and 7 < htfCountSelected) ? f_gate(f_dash(array.get(htfVals, 7), 7 * 2, dashLenHTF, dashGapHTF)) : na, title='HTF EMA52 8', color=f_paletteColor(7), linewidth=lineWidthHTF, style=plot.style_line) -plot((enableHTF and 8 < htfCountSelected) ? f_gate(f_dash(array.get(htfVals, 8), 8 * 2, dashLenHTF, dashGapHTF)) : na, title='HTF EMA52 9', color=f_paletteColor(8), linewidth=lineWidthHTF, style=plot.style_line) -plot((enableHTF and 9 < htfCountSelected) ? f_gate(f_dash(array.get(htfVals, 9), 9 * 2, dashLenHTF, dashGapHTF)) : na, title='HTF EMA52 10', color=f_paletteColor(9), linewidth=lineWidthHTF, style=plot.style_line) -plot((enableHTF and 10 < htfCountSelected) ? f_gate(f_dash(array.get(htfVals, 10), 10 * 2, dashLenHTF, dashGapHTF)) : na, title='HTF EMA52 11', color=f_paletteColor(10), linewidth=lineWidthHTF, style=plot.style_line) -plot((enableHTF and 11 < htfCountSelected) ? f_gate(f_dash(array.get(htfVals, 11), 11 * 2, dashLenHTF, dashGapHTF)) : na, title='HTF EMA52 12', color=f_paletteColor(11), linewidth=lineWidthHTF, style=plot.style_line) +plot(enableCTF ? f_gate(f_smooth(ctfVal, smoothLenCTF)) : na, title='CTF EMA52', color=color.new(color.white, 0), linewidth=lineWidthCTF, style=plot.style_line) +plot((enableHTF and 0 < htfCountSelected) ? f_gate(f_dash(f_smooth(array.get(htfVals, 0), smoothLenHTF), 0 * 2, dashLenHTF, dashGapHTF)) : na, title='HTF EMA52 1', color=f_paletteColor(0), linewidth=lineWidthHTF, style=plot.style_line) +plot((enableHTF and 1 < htfCountSelected) ? f_gate(f_dash(f_smooth(array.get(htfVals, 1), smoothLenHTF), 1 * 2, dashLenHTF, dashGapHTF)) : na, title='HTF EMA52 2', color=f_paletteColor(1), linewidth=lineWidthHTF, style=plot.style_line) +plot((enableHTF and 2 < htfCountSelected) ? f_gate(f_dash(f_smooth(array.get(htfVals, 2), smoothLenHTF), 2 * 2, dashLenHTF, dashGapHTF)) : na, title='HTF EMA52 3', color=f_paletteColor(2), linewidth=lineWidthHTF, style=plot.style_line) +plot((enableHTF and 3 < htfCountSelected) ? f_gate(f_dash(f_smooth(array.get(htfVals, 3), smoothLenHTF), 3 * 2, dashLenHTF, dashGapHTF)) : na, title='HTF EMA52 4', color=f_paletteColor(3), linewidth=lineWidthHTF, style=plot.style_line) +plot((enableHTF and 4 < htfCountSelected) ? f_gate(f_dash(f_smooth(array.get(htfVals, 4), smoothLenHTF), 4 * 2, dashLenHTF, dashGapHTF)) : na, title='HTF EMA52 5', color=f_paletteColor(4), linewidth=lineWidthHTF, style=plot.style_line) +plot((enableHTF and 5 < htfCountSelected) ? f_gate(f_dash(f_smooth(array.get(htfVals, 5), smoothLenHTF), 5 * 2, dashLenHTF, dashGapHTF)) : na, title='HTF EMA52 6', color=f_paletteColor(5), linewidth=lineWidthHTF, style=plot.style_line) +plot((enableHTF and 6 < htfCountSelected) ? f_gate(f_dash(f_smooth(array.get(htfVals, 6), smoothLenHTF), 6 * 2, dashLenHTF, dashGapHTF)) : na, title='HTF EMA52 7', color=f_paletteColor(6), linewidth=lineWidthHTF, style=plot.style_line) +plot((enableHTF and 7 < htfCountSelected) ? f_gate(f_dash(f_smooth(array.get(htfVals, 7), smoothLenHTF), 7 * 2, dashLenHTF, dashGapHTF)) : na, title='HTF EMA52 8', color=f_paletteColor(7), linewidth=lineWidthHTF, style=plot.style_line) +plot((enableHTF and 8 < htfCountSelected) ? f_gate(f_dash(f_smooth(array.get(htfVals, 8), smoothLenHTF), 8 * 2, dashLenHTF, dashGapHTF)) : na, title='HTF EMA52 9', color=f_paletteColor(8), linewidth=lineWidthHTF, style=plot.style_line) +plot((enableHTF and 9 < htfCountSelected) ? f_gate(f_dash(f_smooth(array.get(htfVals, 9), smoothLenHTF), 9 * 2, dashLenHTF, dashGapHTF)) : na, title='HTF EMA52 10', color=f_paletteColor(9), linewidth=lineWidthHTF, style=plot.style_line) +plot((enableHTF and 10 < htfCountSelected) ? f_gate(f_dash(f_smooth(array.get(htfVals, 10), smoothLenHTF), 10 * 2, dashLenHTF, dashGapHTF)) : na, title='HTF EMA52 11', color=f_paletteColor(10), linewidth=lineWidthHTF, style=plot.style_line) +plot((enableHTF and 11 < htfCountSelected) ? f_gate(f_dash(f_smooth(array.get(htfVals, 11), smoothLenHTF), 11 * 2, dashLenHTF, dashGapHTF)) : na, title='HTF EMA52 12', color=f_paletteColor(11), linewidth=lineWidthHTF, style=plot.style_line) -plot((enableLTF and 0 < ltfCountSelected) ? f_gate(f_dash(array.get(ltfVals, 0), 0 * 3 + 1, dashLenLTF, dashGapLTF)) : na, title='LTF EMA52 1', color=color.new(f_paletteColor(0), 30), linewidth=lineWidthLTF, style=plot.style_line) -plot((enableLTF and 1 < ltfCountSelected) ? f_gate(f_dash(array.get(ltfVals, 1), 1 * 3 + 1, dashLenLTF, dashGapLTF)) : na, title='LTF EMA52 2', color=color.new(f_paletteColor(1), 30), linewidth=lineWidthLTF, style=plot.style_line) -plot((enableLTF and 2 < ltfCountSelected) ? f_gate(f_dash(array.get(ltfVals, 2), 2 * 3 + 1, dashLenLTF, dashGapLTF)) : na, title='LTF EMA52 3', color=color.new(f_paletteColor(2), 30), linewidth=lineWidthLTF, style=plot.style_line) -plot((enableLTF and 3 < ltfCountSelected) ? f_gate(f_dash(array.get(ltfVals, 3), 3 * 3 + 1, dashLenLTF, dashGapLTF)) : na, title='LTF EMA52 4', color=color.new(f_paletteColor(3), 30), linewidth=lineWidthLTF, style=plot.style_line) +plot((enableLTF and 0 < ltfCountSelected) ? f_gate(f_dash(f_smooth(array.get(ltfVals, 0), smoothLenLTF), 0 * 3 + 1, dashLenLTF, dashGapLTF)) : na, title='LTF EMA52 1', color=color.new(f_paletteColor(0), 30), linewidth=lineWidthLTF, style=plot.style_line) +plot((enableLTF and 1 < ltfCountSelected) ? f_gate(f_dash(f_smooth(array.get(ltfVals, 1), smoothLenLTF), 1 * 3 + 1, dashLenLTF, dashGapLTF)) : na, title='LTF EMA52 2', color=color.new(f_paletteColor(1), 30), linewidth=lineWidthLTF, style=plot.style_line) +plot((enableLTF and 2 < ltfCountSelected) ? f_gate(f_dash(f_smooth(array.get(ltfVals, 2), smoothLenLTF), 2 * 3 + 1, dashLenLTF, dashGapLTF)) : na, title='LTF EMA52 3', color=color.new(f_paletteColor(2), 30), linewidth=lineWidthLTF, style=plot.style_line) +plot((enableLTF and 3 < ltfCountSelected) ? f_gate(f_dash(f_smooth(array.get(ltfVals, 3), smoothLenLTF), 3 * 3 + 1, dashLenLTF, dashGapLTF)) : na, title='LTF EMA52 4', color=color.new(f_paletteColor(3), 30), linewidth=lineWidthLTF, style=plot.style_line) // 右侧标签(仅在最后一根柱更新,避免重复创建) var label[] htfLabels = array.new_label() var label[] ltfLabels = array.new_label() +var label ctfLabel = na var string lastTicker = na var string lastTf = na if barstate.isfirst @@ -249,6 +288,9 @@ if symbolChanged or tfChanged if not na(lab) label.delete(lab) array.set(ltfLabels, i, na) + if not na(ctfLabel) + label.delete(ctfLabel) + ctfLabel := na lastTicker := syminfo.tickerid lastTf := timeframe.period @@ -311,6 +353,30 @@ if barstate.islast label.delete(lab) array.set(ltfLabels, i, na) + // 本级别 + if enableCTF + v = ctfVal + col = color.new(color.white, 0) + txt = f_prettyTf(curResStr) + ' ' + str.tostring(v, format.mintick) + if protectAutoscale and not f_inRange(v) + if not na(ctfLabel) + label.delete(ctfLabel) + ctfLabel := na + else + if na(ctfLabel) + ctfLabel := label.new(bar_index + labelOffsetBars, v, txt, xloc=xloc.bar_index, yloc=yloc.price, style=label.style_label_left, textcolor=col, color=color.new(col, labelBgAlpha), size=f_strToSize(labelSize)) + else + label.set_xy(ctfLabel, bar_index + labelOffsetBars, v) + label.set_text(ctfLabel, txt) + label.set_textcolor(ctfLabel, col) + label.set_color(ctfLabel, color.new(col, labelBgAlpha)) + label.set_style(ctfLabel, label.style_label_left) + label.set_size(ctfLabel, f_strToSize(labelSize)) + else + if not na(ctfLabel) + label.delete(ctfLabel) + ctfLabel := na + // ======================== 备注 ======================== // 1) 当前周期通过 timeframe.period 自动识别,并用 timeframe.in_seconds( // ) 转换成秒以进行相对大小比较。