Pivot Channel

asdfbasdf · study · 229 行 · 点赞 1,899 · TradingView 原页

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

Pine Script

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © atolelole
 
//@version=5
indicator("RSI S/R", overlay=true, max_bars_back=3000)
 
length                   = input(21, group="Pivot points")
max_h_pivot_points       = input(5, group="Pivot points")
max_l_pivot_points       = input(5, group="Pivot points")
min_difference           = input.float(0.0, group="Pivot points")
channel_extension_length = input(14, group="Channel fib")
source                   = input(close, group="Pivot points")
 
var ph_index = array.new_int()
var pl_index = array.new_int()
var ph_value = array.new_float()
var pl_value = array.new_float()
 
interp(l, h, s) => l + (h - l) * s
 
ph = ta.pivothigh(source, length, length)
if ph
    ok = true
    
    ph := high[length]
    
    if array.size(ph_index) > 0
        if array.size(ph_index) >= max_h_pivot_points
            array.shift(ph_index)
            array.shift(ph_value)
 
        for i = 0 to array.size(ph_value) - 1
            if math.abs(ph - array.get(ph_value, i)) < min_difference
                ok := false
                if ph > array.get(ph_value, i)
                    array.set(ph_value, i , ph)
                    array.set(ph_index, i , bar_index[length])
    
    if ok
        array.push(ph_index, bar_index[length])
        array.push(ph_value, ph)
    
pl = ta.pivotlow(source, length, length)  
if pl
    ok = true
    
    pl := low[length]
    
    if array.size(pl_index) > 0
        if array.size(pl_index) >= max_l_pivot_points
            array.shift(pl_index)
            array.shift(pl_value)
    
        for i = 0 to array.size(pl_value) - 1
            if math.abs(pl - array.get(pl_value, i)) < min_difference
                ok := false
                if pl < array.get(pl_value, i)
                    array.set(pl_value, i , pl)
                    array.set(pl_index, i , bar_index[length])
    if ok
        array.push(pl_index, bar_index[length])
        array.push(pl_value, pl)
        
var lines  = array.new_line()
var labels = array.new_label()
var fibs   = array.new_line()
 
var line line_fc_up   = line(na)
var line line_fc_down = line(na)
var linefill fill_fc  = linefill(na)
 
if barstate.isconfirmed
    ph_count = array.size(ph_index)
    pl_count = array.size(pl_index)
    
    for i = 0 to array.size(lines)
        if array.size(lines) > 0
            line.delete(array.shift(lines))
            
    for i = 0 to array.size(labels)
        if array.size(labels) > 0
            label.delete(array.shift(labels))
    
    if ph_count > 0
        for i = 0 to ph_count - 1
            alpha = 80//math.min(100 - int((i / (ph_count - 1)) * 100), 80)
            clr   = color.from_gradient(100 - alpha, 0, 100, #E8E965, #99d31b)
            if input.bool(true, "Draw high pivot labels", group="Pivot points")
                array.push(labels, label.new(array.get(ph_index, i), array.get(ph_value, i), "", style=label.style_label_down,  color=color.new(clr, alpha), size=size.tiny))
            if input.bool(true, "Draw high pivot lines", group="Pivot points")   
                array.push(lines, line.new(array.get(ph_index, i), array.get(ph_value, i), bar_index + channel_extension_length, array.get(ph_value, i) ,  color=color.new(clr, 60), style=line.style_dotted, extend=extend.none))
    if pl_count > 0
        for i = 0 to pl_count - 1
            alpha = 80//math.min(100 - int((i / (pl_count - 1)) * 100), 80)
            clr   = color.from_gradient(100 - alpha, 0, 100, #64C5C7, #FA5032)
            if input.bool(true, "Draw low pivot labels", group="Pivot points")
                array.push(labels, label.new(array.get(pl_index, i), array.get(pl_value, i), "", style=label.style_label_up  ,  color=color.new(#ff4500, alpha), size=size.tiny))
            if input.bool(true, "Draw low pivot lines", group="Pivot points")   
                array.push(lines, line.new(array.get(pl_index, i), array.get(pl_value, i), bar_index + channel_extension_length, array.get(pl_value, i) ,  color=color.new(#ff4500, 60), style=line.style_dotted, extend=extend.none))
            
    if input.bool(true, "Draw channel high", group="Channel fib")
        if ph_count > 1
            tmp_arr = array.new_line()
            for i = 0 to ph_count - 1
                for j = 0 to ph_count - 1
                    h0  = array.get(ph_value, i)
                    h1  = array.get(ph_value, j)
                    h0i = array.get(ph_index, i) //array.indexof(ph_value, h0))
                    h1i = array.get(ph_index, j) //array.indexof(ph_value, h1))
                    if h0i < h1i 
                        array.push(tmp_arr, line.new(h0i, h0, h1i, h1, color=color.new(#99d31b, 0), extend=extend.none, style=line.style_dashed))
                    if h0i > h1i 
                        array.push(tmp_arr, line.new(h1i, h1, h0i, h0, color=color.new(#99d31b, 0), extend=extend.none, style=line.style_dashed))
            
            int   min_ind = na
            float min_val = 100000.0
            for i = 0 to array.size(tmp_arr) - 1
                lp = line.get_price(array.get(tmp_arr, i), bar_index)
                if lp > high
                    if min_val > math.abs(lp - close)
                        min_val := math.abs(lp - close)
                        min_ind := i
                        
            if not na(min_ind)
                line.delete(line_fc_up[1])
                best_line = array.get(tmp_arr, min_ind)
                line_fc_up := line.new(line.get_x1(best_line), line.get_y1(best_line), line.get_x2(best_line), line.get_y2(best_line), color=color.new(#99d31b, 0), extend=extend.none, style=line.style_solid)
                
            for i = 0 to array.size(tmp_arr) - 1
                if array.size(tmp_arr) > 0
                    line.delete(array.shift(tmp_arr))
                    
            for l in labels
                if label.get_x(l) == line.get_x1(line_fc_up) or label.get_x(l) == line.get_x2(line_fc_up)
                    label.set_color(l, color.new(#99d31b, 0))
                    
            for l in lines
                if line.get_x1(l) == line.get_x1(line_fc_up) or line.get_x1(l) == line.get_x2(line_fc_up)
                    line.set_color(l, color.new(#99d31b, 0))
                    line.set_style(l, line.style_dashed)
            
        line.set_y2(line_fc_up, line.get_price(line_fc_up, bar_index + channel_extension_length))     
        line.set_x2(line_fc_up, bar_index + channel_extension_length)
        
    if input.bool(true, "Draw channel low", group="Channel fib")
        if pl_count > 1
            tmp_arr = array.new_line()
            for i = 0 to pl_count - 1
                for j = 0 to pl_count - 1
                    l0  = array.get(pl_value, i)
                    l1  = array.get(pl_value, j)
                    l0i = array.get(pl_index, i) //array.indexof(pl_value, l0))
                    l1i = array.get(pl_index, j) //array.indexof(pl_value, l1))
                    if l0i < l1i
                        array.push(tmp_arr, line.new(l0i, l0, l1i, l1, color=color.new(#FA5032, 0), extend=extend.none, style=line.style_dashed))
                    if l0i > l1i
                        array.push(tmp_arr, line.new(l1i, l1, l0i, l0, color=color.new(#FA5032, 0), extend=extend.none, style=line.style_dashed))
 
            int   min_ind = na
            float min_val = 100000.0
            for i = 0 to array.size(tmp_arr) - 1
                lp = line.get_price(array.get(tmp_arr, i), bar_index)
                if lp < low
                    if min_val > math.abs(lp - close)
                        min_val := math.abs(lp - close)
                        min_ind := i
                        
            if not na(min_ind)
                line.delete(line_fc_down[1])
                best_line = array.get(tmp_arr, min_ind)
                line_fc_down := line.new(line.get_x1(best_line), line.get_y1(best_line), line.get_x2(best_line), line.get_y2(best_line), color=color.new(#FA5032, 0), extend=extend.none, style=line.style_solid)
                
            for i = 0 to array.size(tmp_arr) - 1
                if array.size(tmp_arr) > 0
                    line.delete(array.shift(tmp_arr))
                    
            for l in labels
                if label.get_x(l) == line.get_x1(line_fc_down) or label.get_x(l) == line.get_x2(line_fc_down)
                    label.set_color(l, color.new(#FA5032, 0))
            
            for l in lines
                if line.get_x1(l) == line.get_x1(line_fc_down) or line.get_x1(l) == line.get_x2(line_fc_down)
                    line.set_color(l, color.new(#FA5032, 0))
                    line.set_style(l, line.style_dashed)
    
            line.set_y2(line_fc_down, line.get_price(line_fc_down, bar_index + channel_extension_length))     
            line.set_x2(line_fc_down, bar_index + channel_extension_length)
 
    if not na(line_fc_up) and not na(line_fc_down) and input.bool(true, "Fill channel background", group="Channel fib")
        linefill.delete(fill_fc[1])
        fill_color = color.new(color.from_gradient(ta.rsi(close, 14), 30, 70, color.red, color.green), 97)
        fill_fc   := linefill.new(line_fc_up, line_fc_down, fill_color)
    
    if not na(line_fc_up) and not na(line_fc_down) and input.bool(true, "Draw fib lines", inline="fiblines", group="Channel fib")
        for i = 0 to array.size(fibs) - 1
            if array.size(fibs) > 0
                line.delete(array.shift(fibs))
                
        left  = math.min(line.get_x1(line_fc_up), line.get_x1(line_fc_down))
        right = bar_index + channel_extension_length
        left_val   = interp(line.get_price(line_fc_down, left) , line.get_price(line_fc_up, left) , 0.236)
        right_val  = interp(line.get_price(line_fc_down, right), line.get_price(line_fc_up, right), 0.236)
        array.push(fibs, line.new(left, left_val, right, right_val, style=line.style_dotted, color=color.from_gradient(right_val, line.get_price(line_fc_down, right), line.get_price(line_fc_up, right), color.new(#FA5032, 0) ,color.new(#99d31b, 0))))
        left_val  := interp(line.get_price(line_fc_down, left) , line.get_price(line_fc_up, left) , 0.382)
        right_val := interp(line.get_price(line_fc_down, right), line.get_price(line_fc_up, right), 0.382)
        array.push(fibs, line.new(left, left_val, right, right_val, style=line.style_dotted, color=color.from_gradient(right_val, line.get_price(line_fc_down, right), line.get_price(line_fc_up, right), color.new(#FA5032, 0) ,color.new(#99d31b, 0))))
        left_val  := interp(line.get_price(line_fc_down, left) , line.get_price(line_fc_up, left) , 0.5)
        right_val := interp(line.get_price(line_fc_down, right), line.get_price(line_fc_up, right), 0.5)
        array.push(fibs, line.new(left, left_val, right, right_val, style=line.style_dotted, color=color.from_gradient(right_val, line.get_price(line_fc_down, right), line.get_price(line_fc_up, right), color.new(#FA5032, 0) ,color.new(#99d31b, 0))))
        left_val  := interp(line.get_price(line_fc_down, left) , line.get_price(line_fc_up, left) , 0.618)
        right_val := interp(line.get_price(line_fc_down, right), line.get_price(line_fc_up, right), 0.618)
        array.push(fibs, line.new(left, left_val, right, right_val, style=line.style_dotted, color=color.from_gradient(right_val, line.get_price(line_fc_down, right), line.get_price(line_fc_up, right), color.new(#FA5032, 0) ,color.new(#99d31b, 0))))
        left_val  := interp(line.get_price(line_fc_down, left) , line.get_price(line_fc_up, left) , 0.75)
        right_val := interp(line.get_price(line_fc_down, right), line.get_price(line_fc_up, right), 0.75)
        array.push(fibs, line.new(left, left_val, right, right_val, style=line.style_dotted, color=color.from_gradient(right_val, line.get_price(line_fc_down, right), line.get_price(line_fc_up, right), color.new(#FA5032, 0) ,color.new(#99d31b, 0))))
        left_val  := interp(line.get_price(line_fc_down, left) , line.get_price(line_fc_up, left) , 1.618)
        right_val := interp(line.get_price(line_fc_down, right), line.get_price(line_fc_up, right), 1.618)
        array.push(fibs, line.new(left, left_val, right, right_val, style=line.style_dotted, color=color.from_gradient(right_val, line.get_price(line_fc_down, right), line.get_price(line_fc_up, right), color.new(#FA5032, 0) ,color.new(#99d31b, 0))))
    
        if input.bool(false, "Draw fib lines > 1.618", inline="fiblines", group="Channel fib")
            left_val  := interp(line.get_price(line_fc_down, left) , line.get_price(line_fc_up, left) , 2.618)
            right_val := interp(line.get_price(line_fc_down, right), line.get_price(line_fc_up, right), 2.618)
            array.push(fibs, line.new(left, left_val, right, right_val, style=line.style_dotted, color=color.from_gradient(right_val, line.get_price(line_fc_down, right), line.get_price(line_fc_up, right), color.new(#FA5032, 0) ,color.new(#99d31b, 0))))
            left_val  := interp(line.get_price(line_fc_down, left) , line.get_price(line_fc_up, left) , 3.618)
            right_val := interp(line.get_price(line_fc_down, right), line.get_price(line_fc_up, right), 3.618)
            array.push(fibs, line.new(left, left_val, right, right_val, style=line.style_dotted, color=color.from_gradient(right_val, line.get_price(line_fc_down, right), line.get_price(line_fc_up, right), color.new(#FA5032, 0) ,color.new(#99d31b, 0))))
            left_val  := interp(line.get_price(line_fc_down, left) , line.get_price(line_fc_up, left) , 4.236)
            right_val := interp(line.get_price(line_fc_down, right), line.get_price(line_fc_up, right), 4.236)
            array.push(fibs, line.new(left, left_val, right, right_val, style=line.style_dotted, color=color.from_gradient(right_val, line.get_price(line_fc_down, right), line.get_price(line_fc_up, right), color.new(#FA5032, 0) ,color.new(#99d31b, 0))))

← 返回列表