修改设置和实际图标时间周期对应问题,修改字体normal

This commit is contained in:
jackyu66git
2025-09-28 18:49:19 +08:00
parent a85f47fedf
commit cdc36aab9e
2 changed files with 171 additions and 40 deletions
+83 -17
View File
@@ -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 / TEMAPine 无内置 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(
// ) 转换成秒以进行相对大小比较。