Turtle Trader Dynamic Breakouts & Risk Levels (20D/55D + 2xATR)

topefu · study · 103 行 · 点赞 17 · TradingView 原页

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

Pine Script

//@version=6
indicator("Turtle Levels (20 & 55) + 2xATR Stops [v6]", overlay = true)
 
// --- Inputs: System 1 (20-Day) ---
group_s1 = "System 1 (20-Day)"
show_20  = input.bool(true, "Show 20-Day Levels", group = group_s1)
len_20   = input.int(20, "Period", minval = 1, group = group_s1)
col_20_h = input.color(#00E676, "20-Day High (Green)", group = group_s1)
col_20_l = input.color(#FF5252, "20-Day Low (Red)", group = group_s1)
w_20     = input.int(2, "Width 20-Day", minval = 1, maxval = 6, group = group_s1)
 
// --- Inputs: System 2 (55-Day) ---
group_s2 = "System 2 (55-Day)"
show_55  = input.bool(true, "Show 55-Day Levels", group = group_s2)
len_55   = input.int(55, "Period", minval = 1, group = group_s2)
col_55_h = input.color(#00B0FF, "55-Day High (Blue)", group = group_s2)
col_55_l = input.color(#FF9100, "55-Day Low (Orange)", group = group_s2)
w_55     = input.int(4, "Width 55-Day", minval = 1, maxval = 6, group = group_s2)
 
// --- Inputs: ATR / Turtle Stops ---
group_atr = "Turtle Risk & Stops (2x ATR)"
show_stop = input.bool(true, "Show 2-ATR Stop Loss Lines", group = group_atr)
atr_len   = input.int(14, "ATR Period (Turtle N)", minval = 1, group = group_atr)
atr_mult  = input.float(2.0, "ATR Multiplier", minval = 0.5, step = 0.5, group = group_atr)
col_stop  = input.color(#FF1744, "Stop Loss Color", group = group_atr)
 
// --- Inputs: Labels ---
group_lbl = "Label Setup"
lbl_size  = input.string(size.normal, "Label Size", options = [size.tiny, size.small, size.normal, size.large], group = group_lbl)
ext_bars  = input.int(6, "Right Projection (Bars)", minval = 2, maxval = 30, group = group_lbl)
 
// --- Daily MTF Data Feed (1D) ---
d_h20 = request.security(syminfo.tickerid, "1D", ta.highest(high[1], len_20), lookahead = barmerge.lookahead_off)
d_l20 = request.security(syminfo.tickerid, "1D", ta.lowest(low[1], len_20),   lookahead = barmerge.lookahead_off)
 
d_h55 = request.security(syminfo.tickerid, "1D", ta.highest(high[1], len_55), lookahead = barmerge.lookahead_off)
d_l55 = request.security(syminfo.tickerid, "1D", ta.lowest(low[1], len_55),   lookahead = barmerge.lookahead_off)
 
// Daily ATR (N)
d_atr = request.security(syminfo.tickerid, "1D", ta.atr(atr_len)[1], lookahead = barmerge.lookahead_off)
 
// Stop-Loss Calculation
stop_long_20  = d_h20 - (atr_mult * d_atr)
stop_short_20 = d_l20 + (atr_mult * d_atr)
 
// Preisformatierung
f_price(float val) =>
    pattern = syminfo.mintick < 0.01 ? "{0,number,#.#####}" : "{0,number,#.##}"
    str.format(pattern, val)
 
// Speicher-Arrays
var line[]  active_lines  = array.new<line>()
var label[] active_labels = array.new<label>()
 
if barstate.islast
    // Cleanup
    for l in active_lines
        line.delete(l)
    array.clear(active_lines)
 
    for b in active_labels
        label.delete(b)
    array.clear(active_labels)
 
    int cur_bar  = bar_index
    int target_x = cur_bar + ext_bars
 
    bool same_high = (d_h20 == d_h55) and show_20 and show_55
    bool same_low  = (d_l20 == d_l55) and show_20 and show_55
 
    // --- HIGHS ZEICHNEN ---
    if same_high
        array.push(active_lines, line.new(cur_bar - 35, d_h55, target_x, d_h55, color = col_55_h, width = w_55, style = line.style_solid))
        array.push(active_labels, label.new(target_x, d_h55, text = str.format("20D & 55D High: {0}", f_price(d_h55)), style = label.style_label_left, color = col_55_h, textcolor = color.black, size = lbl_size))
    else
        if show_55
            array.push(active_lines, line.new(cur_bar - 35, d_h55, target_x, d_h55, color = col_55_h, width = w_55, style = line.style_solid))
            array.push(active_labels, label.new(target_x, d_h55, text = str.format("55D High: {0}", f_price(d_h55)), style = label.style_label_left, color = col_55_h, textcolor = color.black, size = lbl_size))
        if show_20
            array.push(active_lines, line.new(cur_bar - 20, d_h20, target_x, d_h20, color = col_20_h, width = w_20, style = line.style_dashed))
            array.push(active_labels, label.new(target_x, d_h20, text = str.format("20D High: {0}", f_price(d_h20)), style = label.style_label_left, color = col_20_h, textcolor = color.black, size = lbl_size))
 
    // --- LOWS ZEICHNEN ---
    if same_low
        array.push(active_lines, line.new(cur_bar - 35, d_l55, target_x, d_l55, color = col_55_l, width = w_55, style = line.style_solid))
        array.push(active_labels, label.new(target_x, d_l55, text = str.format("20D & 55D Low: {0}", f_price(d_l55)), style = label.style_label_left, color = col_55_l, textcolor = color.white, size = lbl_size))
    else
        if show_55
            array.push(active_lines, line.new(cur_bar - 35, d_l55, target_x, d_l55, color = col_55_l, width = w_55, style = line.style_solid))
            array.push(active_labels, label.new(target_x, d_l55, text = str.format("55D Low: {0}", f_price(d_l55)), style = label.style_label_left, color = col_55_l, textcolor = color.white, size = lbl_size))
        if show_20
            array.push(active_lines, line.new(cur_bar - 20, d_l20, target_x, d_l20, color = col_20_l, width = w_20, style = line.style_dashed))
            array.push(active_labels, label.new(target_x, d_l20, text = str.format("20D Low: {0}", f_price(d_l20)), style = label.style_label_left, color = col_20_l, textcolor = color.white, size = lbl_size))
 
    // --- 2-ATR STOPS ZEICHNEN ---
    if show_stop
        // Long Initial Stop Line
        array.push(active_lines, line.new(cur_bar - 15, stop_long_20, target_x, stop_long_20, color = col_stop, width = 1, style = line.style_dotted))
        array.push(active_labels, label.new(target_x, stop_long_20, text = str.format("Stop (Long 2xATR): {0}", f_price(stop_long_20)), style = label.style_label_left, color = color.new(col_stop, 20), textcolor = color.white, size = size.small))
 
        // Short Initial Stop Line
        array.push(active_lines, line.new(cur_bar - 15, stop_short_20, target_x, stop_short_20, color = col_stop, width = 1, style = line.style_dotted))
        array.push(active_labels, label.new(target_x, stop_short_20, text = str.format("Stop (Short 2xATR): {0}", f_price(stop_short_20)), style = label.style_label_left, color = color.new(col_stop, 20), textcolor = color.white, size = size.small))

← 返回列表