You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

TradingView ADR开源脚本问题:无法消除与前日线条的合并

解决TradingView ADR脚本跨交易日线条合并问题

我修改了一款优秀的TradingView ADR开源脚本,但无法移除脚本中与前一日合并的线条。自己不擅长脚本编写,尝试了所有可行方法都没找到解决方案,恳请协助。

ADR脚本线条合并问题

原脚本代码

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © cryptoweeknd

//@version=5
indicator("ADR", overlay=true)

//ADR side of script below

text_ADR1_high = 'ADR Upper'
text_ADR2_high = 'ADR Upper'
text_ADR1_low = 'ADR Lower'
text_ADR2_low = 'ADR Lower'

var lime = #00FF00 //ADR2 Low
var limes = #00FF00 //ADR1 Low
var red = #FF4000 //ADR1 High
var reds = #FF4000 //ADR2 High

//***Start of Inputs

var labels_enabled = input.bool(defval=true, title='Show Labels')

var color_ADR1_high = input.color(defval=color.new(red,0), title=text_ADR1_high)
var color_ADR2_high = input.color(defval=color.new(reds,0), title=text_ADR2_high)

var color_ADR1_low = input.color(defval=color.new(lime,0), title=text_ADR1_low)
var color_ADR2_low = input.color(defval=color.new(limes,0), title=text_ADR2_low)

adrUppercolorfill = input.color(color.red, "ADR Upper Fill",tooltip="Color of zone between Upper Adr 1 and 2")
adrlowercolorfill = input.color(color.green, "ADR Lower Fill",tooltip="Color of zone between Lower Adr 1 and 2")

//***End of Inputs

//***Start of local functions definiton***

var adr_1 = input.int(title='ADR 1', defval = 10, minval = 1)
var adr_2 = input.int(title='ADR 2', defval = 5, minval = 1)

draw_line(_x1, _y1, _x2, _y2, _xloc, _extend, _color, _style, _width) =>
    dline = line.new(x1=_x1, y1=_y1, x2=_x2, y2=_y2, xloc=_xloc, extend=_extend, color=_color, style=_style, width=_width)
    line.delete(dline[1])
    
draw_label(_x, _y, _text, _xloc, _yloc, _color, _style, _textcolor, _size, _textalign, _tooltip) =>
    dlabel = label.new(x=_x, y=_y, text=_text, xloc=_xloc, yloc=_yloc, color=_color, style=_style, textcolor=_textcolor, size=_size, textalign=_textalign, tooltip=_tooltip)
    label.delete(dlabel[1])

//If security is futures - replace the ticker with continuous contract
tickerid_func() =>
    tickerid = syminfo.tickerid
    if syminfo.type == 'futures'
        tickerid := syminfo.root + '1!'

//Function to calculate ADR levels
//Parameters:
//  * lengthInput - ADR period
//  * hi_low - true-->High, else-->Low 
adr_func(lengthInput,hi_low) =>
    float result = 0
    float adr = 0
    open_ = request.security(tickerid_func(), "D", open, barmerge.gaps_off, barmerge.lookahead_on)
    adr_High = request.security(tickerid_func(), "D", ta.sma(high[1], lengthInput), barmerge.gaps_off, barmerge.lookahead_on)
    adr_Low = request.security(tickerid_func(), "D", ta.sma(low[1], lengthInput), barmerge.gaps_off, barmerge.lookahead_on)
    adr := (adr_High - adr_Low)
    
    if hi_low//High
        result := adr/2 + open_
    else    //Low
        result := open_ - adr/2    

//Workaround to disable color management on the standard tab Style for plots
//Custom inputs for colors should be used instead
transp_func() =>
    transp_0 = 0

//***End of local functions definiton***

//***Start of getting data

start_time = request.security(tickerid_func(), "D", time_close[1],barmerge.gaps_off,barmerge.lookahead_on)
open__ = request.security(tickerid_func(), "D", open, barmerge.gaps_off, barmerge.lookahead_on)

start_time1 = request.security(tickerid_func(), "W", time_close[1],barmerge.gaps_off,barmerge.lookahead_on)

adr1_high = adr_func(adr_1,true)
adr1_low = adr_func(adr_1,false)
adr2_high = adr_func(adr_2,true)
adr2_low = adr_func(adr_2,false)

//***End of getting data

float _adr1_high = na
float _adr1_low = na
float _adr2_high = na
float _adr2_low = na
float _open = na

//*********************

//***Start of plotting*

//Decide if we can show the chart:
//Daily levels should be visible on intraday chart only

var show_chart = false

if timeframe.isintraday
    show_chart := true
    
//Plot all days
if show_chart
    _adr1_high := adr1_high
    _adr1_low  := adr1_low
    _adr2_high := adr2_high
    _adr2_low  := adr2_low
    _open      := open__

ADR1_high = plot(_adr1_high, title=text_ADR1_high, color=red, linewidth=1)
ADR2_high = plot(_adr2_high, title=text_ADR2_high, color=reds, linewidth=1)
ADR1_low = plot(_adr1_low, title=text_ADR1_low, color=lime, linewidth=1)
ADR2_low = plot(_adr2_low, title=text_ADR2_low, color=limes, linewidth=1)
fill(plot1 = ADR1_high, plot2 = ADR2_high, color = color.new(adrUppercolorfill, 80))
fill(plot1 = ADR1_low, plot2 = ADR2_low, color = color.new(adrlowercolorfill, 80))

//***Start of Labels***

label_ADR1_high = ''
label_ADR2_high = ''

label_ADR1_low = ''
label_ADR2_low = ''

if labels_enabled == true
    label_ADR1_high := str.tostring(math.round_to_mintick(adr1_high))
    label_ADR2_high := str.tostring(math.round_to_mintick(adr2_high))
    
    label_ADR1_low  := str.tostring(math.round_to_mintick(adr1_low))
    label_ADR2_low  := str.tostring(math.round_to_mintick(adr2_low))
        
string_ADR1_high = ' (' + label_ADR1_high + ')'
string_ADR2_high = ' (' + label_ADR2_high + ')'

string_ADR1_low = ' (' + label_ADR1_low + ')'
string_ADR2_low = ' (' + label_ADR2_low + ')'

//Labels
if show_chart and labels_enabled == true
    draw_label(bar_index + 1, adr1_high, text_ADR1_high + string_ADR1_high, xloc.bar_index, yloc.price, color.new(color.white, 100), label.style_label_left, color.new(color_ADR1_high,transp_func()), size.normal, text.align_left, '') 
    draw_label(bar_index + 1, adr2_high, text_ADR2_high + string_ADR2_high, xloc.bar_index, yloc.price, color.new(color.white, 100), label.style_label_left, color.new(color_ADR2_high,transp_func()), size.normal, text.align_left, '')
  
    draw_label(bar_index + 1, adr1_low,  text_ADR1_low + string_ADR1_low, xloc.bar_index, yloc.price, color.new(color.white, 100), label.style_label_left, color.new(color_ADR1_low,transp_func()), size.normal, text.align_left, '')   
    draw_label(bar_index + 1, adr2_low,  text_ADR2_low + string_ADR2_low, xloc.bar_index, yloc.price, color.new(color.white, 100), label.style_label_left, color.new(color_ADR2_low,transp_func()), size.normal, text.align_left, '')  

///////////////////////////////////////////////////////////////////////////////////////////

问题原因

原脚本使用plot()绘制ADR水平时,未区分交易日,不同交易日的ADR数值会被连续绘制,导致线条跨日连接。

修改后的脚本代码

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © cryptoweeknd

//@version=5
indicator("ADR", overlay=true)

//ADR side of script below

text_ADR1_high = 'ADR Upper'
text_ADR2_high = 'ADR Upper'
text_ADR1_low = 'ADR Lower'
text_ADR2_low = 'ADR Lower'

var lime = #00FF00 //ADR2 Low
var limes = #00FF00 //ADR1 Low
var red = #FF4000 //ADR1 High
var reds = #FF4000 //ADR2 High

//***Start of Inputs

var labels_enabled = input.bool(defval=true, title='Show Labels')

var color_ADR1_high = input.color(defval=color.new(red,0), title=text_ADR1_high)
var color_ADR2_high = input.color(defval=color.new(reds,0), title=text_ADR2_high)

var color_ADR1_low = input.color(defval=color.new(lime,0), title=text_ADR1_low)
var color_ADR2_low = input.color(defval=color.new(limes,0), title=text_ADR2_low)

adrUppercolorfill = input.color(color.red, "ADR Upper Fill",tooltip="Color of zone between Upper Adr 1 and 2")
adrlowercolorfill = input.color(color.green, "ADR Lower Fill",tooltip="Color of zone between Lower Adr 1 and 2")

//***End of Inputs

//***Start of local functions definiton***

var adr_1 = input.int(title='ADR 1', defval = 10, minval = 1)
var adr_2 = input.int(title='ADR 2', defval = 5, minval = 1)

draw_line(_x1, _y1, _x2, _y2, _xloc, _extend, _color, _style, _width) =>
    dline = line.new(x1=_x1, y1=_y1, x2=_x2, y2=_y2, xloc=_xloc, extend=_extend, color=_color, style=_style, width=_width)
    line.delete(dline[1])
    
draw_label(_x, _y, _text, _xloc, _yloc, _color, _style, _textcolor, _size, _textalign, _tooltip) =>
    dlabel = label.new(x=_x, y=_y, text=_text, xloc=_xloc, yloc=_yloc, color=_color, style=_style, textcolor=_textcolor, size=_size, textalign=_textalign, tooltip=_tooltip)
    label.delete(dlabel[1])

//If security is futures - replace the ticker with continuous contract
tickerid_func() =>
    tickerid = syminfo.tickerid
    if syminfo.type == 'futures'
        tickerid := syminfo.root + '1!'

//Function to calculate ADR levels
//Parameters:
//  * lengthInput - ADR period
//  * hi_low - true-->High, else-->Low 
adr_func(lengthInput,hi_low) =>
    float result = 0
    float adr = 0
    open_ = request.security(tickerid_func(), "D", open, barmerge.gaps_off, barmerge.lookahead_on)
    adr_High = request.security(tickerid_func(), "D", ta.sma(high[1], lengthInput), barmerge.gaps_off, barmerge.lookahead_on)
    adr_Low = request.security(tickerid_func(), "D", ta.sma(low[1], lengthInput), barmerge.gaps_off, barmerge.lookahead_on)
    adr := (adr_High - adr_Low)
    
    if hi_low//High
        result := adr/2 + open_
    else    //Low
        result := open_ - adr/2    

//Workaround to disable color management on the standard tab Style for plots
//Custom inputs for colors should be used instead
transp_func() =>
    transp_0 = 0

//***End of local functions definiton***

//***Start of getting data

// 获取当日开盘时间
daily_open_time = request.security(tickerid_func(), "D", time, barmerge.gaps_off, barmerge.lookahead_on)
// 判断当前K线是否属于当日
is_current_day = time >= daily_open_time

open__ = request.security(tickerid_func(), "D", open, barmerge.gaps_off, barmerge.lookahead_on)

adr1_high = adr_func(adr_1,true)
adr1_low = adr_func(adr_1,false)
adr2_high = adr_func(adr_2,true)
adr2_low = adr_func(adr_2,false)

//***End of getting data

float _adr1_high = na
float _adr1_low = na
float _adr2_high = na
float _adr2_low = na
float _open = na

//*********************

//***Start of plotting*

//Decide if we can show the chart:
//Daily levels should be visible on intraday chart only

var show_chart = false

if timeframe.isintraday
    show_chart := true
    
// 仅在当日K线显示ADR水平
if show_chart and is_current_day
    _adr1_high := adr1_high
    _adr1_low  := adr1_low
    _adr2_high := adr2_high
    _adr2_low  := adr2_low
    _open      := open__

ADR1_high = plot(_adr1_high, title=text_ADR1_high, color=red, linewidth=1)
ADR2_high = plot(_adr2_high, title=text_ADR2_high, color=reds, linewidth=1)
ADR1_low = plot(_adr1_low, title=text_ADR1_low, color=lime, linewidth=1)
ADR2_low = plot(_adr2_low, title=text_ADR2_low, color=limes, linewidth=1)
fill(plot1 = ADR1_high, plot2 = ADR2_high, color = color.new(adrUppercolorfill, 80))
fill(plot1 = ADR1_low, plot2 = ADR2_low, color = color.new(adrlowercolorfill, 80))

//***Start of Labels***

label_ADR1_high = ''
label_ADR2_high = ''

label_ADR1_low = ''
label_ADR2_low = ''

if labels_enabled == true
    label_ADR1_high := str.tostring(math.round_to_mintick(adr1_high))
    label_ADR2_high := str.tostring(math.round_to_mintick(adr2_high))
    
    label_ADR1_low  := str.tostring(math.round_to_mintick(adr1_low))
    label_ADR2_low  := str.tostring(math.round_to_mintick(adr2_low))
        
string_ADR1_high = ' (' + label_ADR1_high + ')'
string_ADR2_high = ' (' + label_ADR2_high + ')'

string_ADR1_low = ' (' + label_ADR1_low + ')'
string_ADR2_low = ' (' + label_ADR2_low + ')'

//Labels
if show_chart and labels_enabled == true and is_current_day
    draw_label(bar_index + 1, adr1_high, text_ADR1_high + string_ADR1_high, xloc.bar_index, yloc.price, color.new(color.white, 100), label.style_label_left, color.new(color_ADR1_high,transp_func()), size.normal, text.align_left, '') 
    draw_label(bar_index + 1, adr2_high, text_ADR2_high + string_ADR2_high, xloc.bar_index, yloc.price, color.new(color.white, 100), label.style_label_left, color.new(color_ADR2_high,transp_func()), size.normal, text.align_left, '')
  
    draw_label(bar_index + 1, adr1_low,  text_ADR1_low + string_ADR1_low, xloc.bar_index, yloc.price, color.new(color.white, 100), label.style_label_left, color.new(color_AD
相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.22 11:36:14