pine图中标签制作及美化
编辑
0
2025-07-19

背景
pine图上标签制作及美化
代码
pine脚本记录
// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
//@version=6
indicator("我的脚本", overlay = true)
// 制作label合集
i_offsetLabel = input(5, "Data Dashboard Offset")
offset = i_offsetLabel * (time - time[1])
title = "Data Dashboard", splitter = "---------------------------------------", nl = "\n"
string dynamicText = title + nl + splitter + nl
var label id = na
label.delete(id)
id := label.new(x=time+offset, y=high, xloc = xloc.bar_time, text = dynamicText)
i_lookback = input(100, "Lookback Period")
i_showStats = input(true, "Show Statistics")
i_showMoms = input(true, "Show Momentum Oscillators")
i_showVolume = input(true, "Show Volume Stuff")
i_showSignal = input(true, "Show Signals")
f_round(_val, _decimals) =>
if _decimals == -1
_val
else
_p = math.pow(10, _decimals)
math.round(math.abs(_val) * _p)/ _p * math.sign(_val)
f_strHelp(_prefix, _var, _round) =>
_res = str.tostring(f_round(_var, _round))
_prefix + ": " + _res + nl
// // Statistics Section
statsTitle = splitter + nl + "Statistics" + nl + nl
std = ta.stdev(close, i_lookback)
mean = ta.sma(close, i_lookback)
max = ta.highest(high, i_lookback)
min = ta.lowest(low, i_lookback)
stats = i_showStats ? statsTitle + f_strHelp("Stdev", std, -1) + f_strHelp("Mean", mean, -1) + f_strHelp("Local Max", max, -1) + f_strHelp("Local Min", min, -1) : na
// Momentum Section
momsTitle = splitter + nl + "Momentum" + nl + nl
rsi = ta.rsi(close, 14)
cci = ta.cci(close, 20)
mom = ta.mom(close, 10)
mfi = ta.mfi(close, 5)
moms = i_showMoms ? momsTitle + f_strHelp("RSI", rsi, 1) + f_strHelp("CCI", cci, 1) + f_strHelp("MOM", mom, 1) + f_strHelp("MFI", mfi, 1) : na
// Volatility & Volume Section
volTitle = splitter + nl + "Volume & Volatility" + nl + nl
atr = ta.atr(14)
avgVol = ta.sma(volume, i_lookback)
vols = i_showVolume ? volTitle + f_strHelp("ATR", atr, -1) + f_strHelp("Average Volume", avgVol, -1) : na
// Signals
sigTitle = splitter + nl + "Signals" + nl + nl
[macd, macdsignal, macdhist] = ta.macd(close, fastlen = 12, slowlen = 26, siglen = 9)
trend = macdhist > 0 ? "Bullish" : "Bearish"
sigs = i_showSignal ? sigTitle + "Trend: " + trend + nl : na
dynamicText := dynamicText + stats + moms + vols + sigs
label.set_text(id, text=dynamicText)
// 向左对齐
label.set_textalign(id, text.align_left)
label.set_color(id, c~~o~~lor=color.black)
label.set_textcolor(id, textcolor=color.white)
label.set_style(id, label.style_label_left)
// 获取文本
labelText = label.get_text(id)
// labelText := str.replace_all(labelText, "Data", "&")
// label.set_text(id, labelText)
// label.set_tooltip(id, tooltip = stats)
if trend == "Bullish"
labelText := label.get_text(id)
labelText := str.replace_all(labelText, splitter, "^^^^^^^^^^^^^^^^^^^^^^^")
label.set_text(id, labelText)
label.set_color(id,color.green)
当sigs中趋势是Bullish牛市,标签为绿色。
文本靠左,多个部分(stats + moms + vols + sigs)组成标签,直观。
- 0
- 0
-
赞助
支付宝
微信
-
分享