Live Multi-TF MA Alignment

hasanaksoy199264 · study · 74 行 · 点赞 9 · TradingView 原页

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

Pine Script

// 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("Live Multi-TF MA Alignment", shorttitle="MTF MA Alignment", overlay=true)
 
// ==================== INPUTS ====================
G_MA = "═══════════ 📈 MA SETTINGS ═══════════"
tf_in12 = input.timeframe("720", "Line 1 Timeframe (12H)", group=G_MA)
tf_in8  = input.timeframe("480", "Line 2 Timeframe (8H)",  group=G_MA)
tf_in6  = input.timeframe("360", "Line 3 Timeframe (6H)",  group=G_MA)
tf_in4  = input.timeframe("240", "Line 4 Timeframe (4H)",  group=G_MA)
 
ma_type   = input.string("EMA", "MA Type", options=["SMA", "EMA", "WMA", "VWMA"], group=G_MA)
ma_length = input.int(10, "MA Length", minval=5, maxval=200, group=G_MA)
 
// ==================== HELPER FUNCTIONS ====================
ma_func(src, len, type) =>
    switch type
        "SMA"  => ta.sma(src, len)
        "EMA"  => ta.ema(src, len)
        "WMA"  => ta.wma(src, len)
        "VWMA" => ta.vwma(src, len)
        =>        ta.ema(src, len)
 
f_valid(string tf) => timeframe.in_seconds(tf) >= timeframe.in_seconds()
 
// ==================== TIMEFRAMES ====================
tf12 = tf_in12
tf8  = tf_in8
tf6  = tf_in6
tf4  = tf_in4
 
// ==================== HEIKEN ASHI DATA ====================
ha_ticker = ticker.heikinashi(syminfo.tickerid)
 
ha_close_12h = f_valid(tf12) ? request.security(ha_ticker, tf12, close, lookahead=barmerge.lookahead_on) : na
ha_close_8h  = f_valid(tf8)  ? request.security(ha_ticker, tf8,  close, lookahead=barmerge.lookahead_on) : na
ha_close_6h  = f_valid(tf6)  ? request.security(ha_ticker, tf6,  close, lookahead=barmerge.lookahead_on) : na
ha_close_4h  = f_valid(tf4)  ? request.security(ha_ticker, tf4,  close, lookahead=barmerge.lookahead_on) : na
 
// ==================== MA CALCULATION ====================
ma_12h = ma_func(ha_close_12h, ma_length, ma_type)
ma_8h  = ma_func(ha_close_8h,  ma_length, ma_type)
ma_6h  = ma_func(ha_close_6h,  ma_length, ma_type)
ma_4h  = ma_func(ha_close_4h,  ma_length, ma_type)
 
// ==================== PLOTS ====================
plot(ma_12h, title="12H MA", color=color.new(color.white, 20), linewidth=2)
plot(ma_8h,  title="8H MA",  color=color.new(#2962FF,     0),  linewidth=2)
plot(ma_6h,  title="6H MA",  color=color.new(#FF6D00,     0),  linewidth=2)
plot(ma_4h,  title="4H MA",  color=color.new(#E91E63,     0),  linewidth=2)
 
// ==================== TIMEFRAME LABELS ====================
f_tf_text(string tf) =>
    int m = int(timeframe.in_seconds(tf) / 60)
    m % 1440 == 0 ? str.tostring(m / 1440) + "D" :
     m % 60 == 0 ? str.tostring(m / 60) + "H" :
     str.tostring(m) + "m"
 
f_end_label(float ma_val, string tf, color col) =>
    var label lb = na
    if barstate.islast and not na(ma_val)
        if na(lb)
            lb := label.new(bar_index + 1, ma_val, f_tf_text(tf),
                 style = label.style_label_left, color = color.new(col, 100),
                 textcolor = col, size = size.small)
        else
            label.set_xy(lb, bar_index + 1, ma_val)
            label.set_text(lb, f_tf_text(tf))
    lb
 
f_end_label(ma_12h, tf12, color.white)
f_end_label(ma_8h,  tf8,  #2962FF)
f_end_label(ma_6h,  tf6,  #FF6D00)
f_end_label(ma_4h,  tf4,  #E91E63)

← 返回列表