[Abaco] MTF MAs Labelled Ribbon - Moving Averages

AbacoLab · study · 115 行 · 点赞 9 · TradingView 原页

本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。

Pine Script

//@version=6
indicator('[Abaco] MTF MAs Labelled Ribbon', overlay=true)
 
show_labels = input(true, 'Show Labels', group = 'Main Settings')
off_mult = input(4, 'Label Offset', group = 'Main Settings')
smooth = input.bool(true, 'Smooth the steps of a higher timeframe', group = 'Main Settings', tooltip = 'An average from a timeframe above the chart changes once per its own bar and draws steps. Smoothed, it runs in a straight line between its last two closed values: no repaint, but it trails the exact value by up to one bar of that timeframe.')
 
 
// STEP 1. Make a text-based input with different label sizes
labelSizeInput = input.string("small", title="Label Size",
     options=["tiny", "small", "normal", "large", "huge"], group = 'Main Settings')
 
// STEP 2. Translate the input's possible values into size.* constants
labelSize = switch labelSizeInput
    "tiny"      => size.tiny
    "small"     => size.small
    "normal"    => size.normal
    "large"     => size.large
    "huge"      => size.huge
 
var labelStyle     = label.style_label_left
labelOffset =  timenow + math.round(ta.change(time)*off_mult) // last_bar_index + off_mult
 
 
 
f_ma(source, length, type) =>
    type  == 'SMA' ? ta.sma(source, length) :
     type == 'EMA' ? ta.ema(source, length) :
     type == 'RMA' ? ta.rma(source, length) :
     type == 'WMA' ? ta.wma(source, length) :
     type == 'VWMA' ? ta.vwma(source, length) :
     na
 
 
f_tfname(_tf) =>
    tf = _tf == '' ? timeframe.period : _tf
    string out = tf
    if not (str.contains(tf, 'S') or str.contains(tf, 'D') or str.contains(tf, 'W') or str.contains(tf, 'M'))
        mins = int(str.tonumber(tf))
        out := mins % 60 == 0 ? str.tostring(mins / 60) + 'h' : str.tostring(mins) + 'm'
    out
 
f_lbl(_label, _type, _len, _tf) =>
    _label == '' ? _type + ' ' + str.tostring(_len) + ' (' + f_tfname(_tf) + ')' : _label
 
f_ma_h(_len, _type) =>
    f_ma(hlc3, _len, _type)
 
f_mtf(_tf, _len, _type) =>
    float out = request.security(syminfo.tickerid, _tf, f_ma_h(_len, _type))
    if smooth and timeframe.in_seconds(_tf) > timeframe.in_seconds(timeframe.period)
        p1 = request.security(syminfo.tickerid, _tf, f_ma_h(_len, _type)[1], lookahead = barmerge.lookahead_on)
        p2 = request.security(syminfo.tickerid, _tf, f_ma_h(_len, _type)[2], lookahead = barmerge.lookahead_on)
        t0 = request.security(syminfo.tickerid, _tf, time, lookahead = barmerge.lookahead_on)
        t1 = request.security(syminfo.tickerid, _tf, time_close, lookahead = barmerge.lookahead_on)
        frac = math.max(0.0, math.min(1.0, (time - t0) / (t1 - t0)))
        out := p2 + (p1 - p2) * frac
    out
 
f_drawLabel(_x, _y, _text, _textcolor, _style, _size) =>
    var _label = label.new(
     x          = _x,
     y          = _y,
     text       = _text,
     textcolor  = _textcolor,
     style      = _style,
     size       = _size,
     textalign  = text.align_right,    
     text_font_family = font.family_monospace,
     xloc       = xloc.bar_time
     )
    
    label.set_xy(_label, _x, _y)
    label.set_color(_label, color=#ffffff00)
 
show_ma1    = input             (true,      'MA 1',                                                         inline='MA 1A', group = 'MAs', display = display.data_window)
ma1_label   = input.string      ('',    'Label 1',                                                      inline='MA 1A', group = 'MAs')
ma1_color   = input             (#e07c4e, '',                                                            inline='MA 1A', group = 'MAs', display = display.data_window)
ma1_type    = input.string      ('EMA',     '',             options=['SMA', 'EMA', 'RMA', 'WMA', 'VWMA'],   inline='MA 1B', group = 'MAs')
ma1_length  = input.int         (200,        '',             minval=1,                                       inline='MA 1B', group = 'MAs')
ma1_tf      = input.timeframe   ('60',       '',                                                             inline='MA 1B', group = 'MAs')
ma1          = f_mtf(ma1_tf, ma1_length, ma1_type)
plot(show_ma1 ? ma1 : na, color = ma1_color, title='MA 1')
f_drawLabel(labelOffset, show_labels and show_ma1 ? ma1 : na, f_lbl(ma1_label, ma1_type, ma1_length, ma1_tf), ma1_color, labelStyle, labelSize) 
 
 
show_ma2    = input             (true,      'MA 2',                                                         inline='MA 2A', group = 'MAs', display = display.data_window)
ma2_label   = input.string      ('',    'Label 2',                                                      inline='MA 2A', group = 'MAs')
ma2_color   = input             (#c16c45, '',                                                            inline='MA 2A', group = 'MAs', display = display.data_window)
ma2_type    = input.string      ('EMA',     '',             options=['SMA', 'EMA', 'RMA', 'WMA', 'VWMA'],   inline='MA 2B', group = 'MAs')
ma2_length  = input.int         (100,        '',             minval=1,                                       inline='MA 2B', group = 'MAs')
ma2_tf      = input.timeframe   ('60',       '',                                                             inline='MA 2B', group = 'MAs')
ma2          = f_mtf(ma2_tf, ma2_length, ma2_type)
plot(show_ma2 ? ma2 : na, color = ma2_color, title='MA 2')
f_drawLabel(labelOffset, show_labels and show_ma2 ? ma2 : na, f_lbl(ma2_label, ma2_type, ma2_length, ma2_tf), ma2_color, labelStyle, labelSize) 
 
show_ma3    = input             (true,      'MA 3',                                                         inline='MA 3A', group = 'MAs', display = display.data_window)
ma3_label   = input.string      ('',    'Label 3',                                                      inline='MA 3A', group = 'MAs')
ma3_color   = input             (#a15b3c, '',                                                            inline='MA 3A', group = 'MAs', display = display.data_window)
ma3_type    = input.string      ('EMA',     '',             options=['SMA', 'EMA', 'RMA', 'WMA', 'VWMA'],   inline='MA 3B', group = 'MAs')
ma3_length  = input.int         (50,        '',             minval=1,                                       inline='MA 3B', group = 'MAs')
ma3_tf      = input.timeframe   ('60',       '',                                                             inline='MA 3B', group = 'MAs')
ma3          = f_mtf(ma3_tf, ma3_length, ma3_type)
plot(show_ma3 ? ma3 : na, color = ma3_color, title='MA 3')
f_drawLabel(labelOffset, show_labels and show_ma3 ? ma3 : na, f_lbl(ma3_label, ma3_type, ma3_length, ma3_tf), ma3_color, labelStyle, labelSize) 
 
show_ma4    = input             (true,      'MA 4',                                                         inline='MA 4A', group = 'MAs', display = display.data_window)
ma4_label   = input.string      ('',    'Label 4',                                                      inline='MA 4A', group = 'MAs')
ma4_color   = input             (#884e35, '',                                                            inline='MA 4A', group = 'MAs', display = display.data_window)
ma4_type    = input.string      ('EMA',     '',             options=['SMA', 'EMA', 'RMA', 'WMA', 'VWMA'],   inline='MA 4B', group = 'MAs')
ma4_length  = input.int         (20,        '',             minval=1,                                       inline='MA 4B', group = 'MAs')
ma4_tf      = input.timeframe   ('60',       '',                                                             inline='MA 4B', group = 'MAs')
ma4          = f_mtf(ma4_tf, ma4_length, ma4_type)
plot(show_ma4 ? ma4 : na, color = ma4_color, title='MA 4')
f_drawLabel(labelOffset, show_labels and show_ma4 ? ma4 : na, f_lbl(ma4_label, ma4_type, ma4_length, ma4_tf), ma4_color, labelStyle, labelSize)

← 返回列表