Gann Levels (Fixed Yesterday Close Anchor) v6

svennela · study · 120 行 · 点赞 29 · 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/
// © svennela
 
//@version=6
indicator("Gann Levels (Fixed Yesterday Close Anchor) v6", overlay=true, max_lines_count=500, max_labels_count=500)
 
// --- Inputs ---
line_width  = input.int(1, "Line Width", minval=1, maxval=5)
show_labels = input.bool(true, "Show Text Labels")
 
// Colors
color_res   = color.red
color_sup   = color.green
color_pivot = color.blue
color_text  = color.white
 
// --- Fetch Strict Yesterday's Closing Price ---
// Requesting the current daily close with lookahead_on means we get the value of the active day. 
// By using [1] on the daily resolution level itself, we strictly pull the completed close of yesterday.
daily_close = request.security(syminfo.tickerid, "D", close, lookahead=barmerge.lookahead_on)[1]
 
// --- Detect New Session Day ---
is_new_day = ta.change(time("D")) != 0
 
// --- Gann Square of 9 Core Logic ---
get_gann_level(base_price, angle) =>
    math.pow(math.sqrt(base_price) + (angle / 180.0), 2)
 
// Calculate Levels
r_45  = get_gann_level(daily_close, 45)
r_90  = get_gann_level(daily_close, 90)
r_135 = get_gann_level(daily_close, 135)
r_180 = get_gann_level(daily_close, 180)
r_225 = get_gann_level(daily_close, 225)
r_270 = get_gann_level(daily_close, 270)
r_315 = get_gann_level(daily_close, 315)
r_360 = get_gann_level(daily_close, 360)
 
s_45  = get_gann_level(daily_close, -45)
s_90  = get_gann_level(daily_close, -90)
s_135 = get_gann_level(daily_close, -135)
s_180 = get_gann_level(daily_close, -180)
s_225 = get_gann_level(daily_close, -225)
s_270 = get_gann_level(daily_close, -270)
s_315 = get_gann_level(daily_close, -315)
s_360 = get_gann_level(daily_close, -360)
 
// --- Persistent Variables for Lines & Labels ---
var line line_pivot = na
var line line_r45 = na,  var line line_r90 = na,  var line line_r135 = na, var line line_r180 = na
var line line_r225 = na, var line line_r270 = na, var line line_r315 = na, var line line_r360 = na
var line line_s45 = na,  var line line_s90 = na,  var line line_s135 = na, var line line_s180 = na
var line line_s225 = na, var line line_s270 = na, var line line_s315 = na, var line line_s360 = na
 
var label lbl_pivot = na
var label lbl_r45 = na,  var label lbl_r90 = na,  var label lbl_r135 = na, var label lbl_r180 = na
var label lbl_r225 = na, var label lbl_r270 = na, var label lbl_r315 = na, var label lbl_r360 = na
var label lbl_s45 = na,  var label lbl_s90 = na,  var label lbl_s135 = na, var label lbl_s180 = na
var label lbl_s225 = na, var label lbl_s270 = na, var label lbl_s315 = na, var label lbl_s360 = na
 
// Helper function to format prices cleanly
format_lbl(title, val) => title + " (" + str.tostring(val, "#.##") + ")"
 
// Instantiate lines at the beginning of each session boundary
if (is_new_day or na(line_pivot)) and not na(daily_close)
    line_pivot := line.new(x1=bar_index, y1=daily_close, x2=bar_index, y2=daily_close, color=color_pivot, width=line_width + 1, style=line.style_solid)
    
    line_r45  := line.new(x1=bar_index, y1=r_45,  x2=bar_index, y2=r_45,  color=color_res, width=line_width, style=line.style_dashed)
    line_r90  := line.new(x1=bar_index, y1=r_90,  x2=bar_index, y2=r_90,  color=color_res, width=line_width, style=line.style_solid)
    line_r135 := line.new(x1=bar_index, y1=r_135, x2=bar_index, y2=r_135, color=color_res, width=line_width, style=line.style_dashed)
    line_r180 := line.new(x1=bar_index, y1=r_180, x2=bar_index, y2=r_180, color=color_res, width=line_width + 1, style=line.style_solid)
    line_r225 := line.new(x1=bar_index, y1=r_225, x2=bar_index, y2=r_225, color=color_res, width=line_width, style=line.style_dashed)
    line_r270 := line.new(x1=bar_index, y1=r_270, x2=bar_index, y2=r_270, color=color_res, width=line_width, style=line.style_solid)
    line_r315 := line.new(x1=bar_index, y1=r_315, x2=bar_index, y2=r_315, color=color_res, width=line_width, style=line.style_dashed)
    line_r360 := line.new(x1=bar_index, y1=r_360, x2=bar_index, y2=r_360, color=color_res, width=line_width + 1, style=line.style_solid)
    
    line_s45  := line.new(x1=bar_index, y1=s_45,  x2=bar_index, y2=s_45,  color=color_sup, width=line_width, style=line.style_dashed)
    line_s90  := line.new(x1=bar_index, y1=s_90,  x2=bar_index, y2=s_90,  color=color_sup, width=line_width, style=line.style_solid)
    line_s135 := line.new(x1=bar_index, y1=s_135, x2=bar_index, y2=s_135, color=color_sup, width=line_width, style=line.style_dashed)
    line_s180 := line.new(x1=bar_index, y1=s_180, x2=bar_index, y2=s_180, color=color_sup, width=line_width + 1, style=line.style_solid)
    line_s225 := line.new(x1=bar_index, y1=s_225, x2=bar_index, y2=s_225, color=color_sup, width=line_width, style=line.style_dashed)
    line_s270 := line.new(x1=bar_index, y1=s_270, x2=bar_index, y2=s_270, color=color_sup, width=line_width, style=line.style_solid)
    line_s315 := line.new(x1=bar_index, y1=s_315, x2=bar_index, y2=s_315, color=color_sup, width=line_width, style=line.style_dashed)
    line_s360 := line.new(x1=bar_index, y1=s_360, x2=bar_index, y2=s_360, color=color_sup, width=line_width + 1, style=line.style_solid)
 
    if show_labels
        lbl_pivot := label.new(x=bar_index, y=daily_close, text=format_lbl("Yesterday Close", daily_close), color=color_pivot, textcolor=color_text, style=label.style_label_left, size=size.small)
        
        lbl_r45   := label.new(x=bar_index, y=r_45,  text=format_lbl("R 45°", r_45),   color=color_res, textcolor=color_text, style=label.style_label_left, size=size.small)
        lbl_r90   := label.new(x=bar_index, y=r_90,  text=format_lbl("R 90°", r_90),   color=color_res, textcolor=color_text, style=label.style_label_left, size=size.small)
        lbl_r135  := label.new(x=bar_index, y=r_135, text=format_lbl("R 135°", r_135), color=color_res, textcolor=color_text, style=label.style_label_left, size=size.small)
        lbl_r180  := label.new(x=bar_index, y=r_180, text=format_lbl("R 180°", r_180), color=color_res, textcolor=color_text, style=label.style_label_left, size=size.small)
        lbl_r225  := label.new(x=bar_index, y=r_225, text=format_lbl("R 225°", r_225), color=color_res, textcolor=color_text, style=label.style_label_left, size=size.small)
        lbl_r270  := label.new(x=bar_index, y=r_270, text=format_lbl("R 270°", r_270), color=color_res, textcolor=color_text, style=label.style_label_left, size=size.small)
        lbl_r315  := label.new(x=bar_index, y=r_315, text=format_lbl("R 315°", r_315), color=color_res, textcolor=color_text, style=label.style_label_left, size=size.small)
        lbl_r360  := label.new(x=bar_index, y=r_360, text=format_lbl("R 360°", r_360), color=color_res, textcolor=color_text, style=label.style_label_left, size=size.small)
        
        lbl_s45   := label.new(x=bar_index, y=s_45,  text=format_lbl("S 45°", s_45),   color=color_sup, textcolor=color_text, style=label.style_label_left, size=size.small)
        lbl_s90   := label.new(x=bar_index, y=s_90,  text=format_lbl("S 90°", s_90),   color=color_sup, textcolor=color_text, style=label.style_label_left, size=size.small)
        lbl_s135  := label.new(x=bar_index, y=s_135, text=format_lbl("S 135°", s_135), color=color_sup, textcolor=color_text, style=label.style_label_left, size=size.small)
        lbl_s180  := label.new(x=bar_index, y=s_180, text=format_lbl("S 180°", s_180), color=color_sup, textcolor=color_text, style=label.style_label_left, size=size.small)
        lbl_s225  := label.new(x=bar_index, y=s_225, text=format_lbl("S 225°", s_225), color=color_sup, textcolor=color_text, style=label.style_label_left, size=size.small)
        lbl_s270  := label.new(x=bar_index, y=s_270, text=format_lbl("S 270°", s_270), color=color_sup, textcolor=color_text, style=label.style_label_left, size=size.small)
        lbl_s315  := label.new(x=bar_index, y=s_315, text=format_lbl("S 315°", s_315), color=color_sup, textcolor=color_text, style=label.style_label_left, size=size.small)
        lbl_s360  := label.new(x=bar_index, y=s_360, text=format_lbl("S 360°", s_360), color=color_sup, textcolor=color_text, style=label.style_label_left, size=size.small)
 
// Lock line extensions to follow the exact live bar index identically across all windows
if not na(line_pivot)
    line.set_x2(line_pivot, bar_index)
    line.set_x2(line_r45,  bar_index), line.set_x2(line_r90,  bar_index), line.set_x2(line_r135, bar_index), line.set_x2(line_r180, bar_index)
    line.set_x2(line_r225, bar_index), line.set_x2(line_r270, bar_index), line.set_x2(line_r315, bar_index), line.set_x2(line_r360, bar_index)
    line.set_x2(line_s45,  bar_index), line.set_x2(line_s90,  bar_index), line.set_x2(line_s135, bar_index), line.set_x2(line_s180, bar_index)
    line.set_x2(line_s225, bar_index), line.set_x2(line_s270, bar_index), line.set_x2(line_s315, bar_index), line.set_x2(line_s360, bar_index)
 
    if show_labels
        label.set_x(lbl_pivot, bar_index)
        label.set_x(lbl_r45,  bar_index), label.set_x(lbl_r90,  bar_index), label.set_x(lbl_r135, bar_index), label.set_x(lbl_r180, bar_index)
        label.set_x(lbl_r225, bar_index), label.set_x(lbl_r270, bar_index), label.set_x(lbl_r315, bar_index), label.set_x(lbl_r360, bar_index)
        label.set_x(lbl_s45,  bar_index), label.set_x(lbl_s90,  bar_index), label.set_x(lbl_s135, bar_index), label.set_x(lbl_s180, bar_index)
        label.set_x(lbl_s225, bar_index), label.set_x(lbl_s270, bar_index), label.set_x(lbl_s315, bar_index), label.set_x(lbl_s360, bar_index)

← 返回列表