【问题标题】:lines are cut when the price crosses them当价格越过它们时线被切断
【发布时间】:2021-08-12 07:17:06
【问题描述】:

帮帮我。 我需要当价格越过画线时,它们被削减。 如下图所示。

    //@version=4
var maxBarsBack = 10
study("Zones Supply and Demand", shorttitle="SD & RSI",overlay=true, max_bars_back=maxBarsBack)
ColorZDem=input(color.new(color.lime, 100), title="Color Zona Demanda")
ColorZSuply=input(color.new(color.red, 100), title="Color Zona Oferta")

// define a basing and explosive candles
basing_candle = ((abs(close - open)/abs(high - low)) < 0.51)
explosive_candle = (abs(close-open) / abs(high - low)) >= 0.51 and tr>tr[1] 

/// functions
bc_r = basing_candle and close < open
ex_g = explosive_candle and close > open
bc_rz = basing_candle and close > open
ex_gz = explosive_candle and close < open


// zone
demandzone = (bc_r[1] and ex_g and low>=low[1] and close>open[1]) 
supplyzone = (bc_rz[1] and ex_gz and high<=high[1] and close<open[1]) 


line l1 = na
line l2 = na

dz = if demandzone and barstate.isconfirmed
    l1 := line.new(x1 = bar_index[1] ,y1=low[1], x2=bar_index, y2= low[1], style=line.style_solid, extend=extend.right, color=ColorZDem, width=1)

sz = if supplyzone and barstate.isconfirmed
    l2 := line.new(x1 = bar_index[1] ,y1=high[1], x2=bar_index, y2= high[1], style=line.style_solid, extend=extend.right, color=ColorZSuply, width=1)



for i = 1 to maxBarsBack
    a=line.get_y1(l1[i])
    line.set_color(id=l1[i], color=cross(a,close[1])?color.new(color.lime,100):color.new(color.lime,0))
    //line.set_x2(id=l1[i], x=b)
    
    
        // We have identified a bar where a line was created.
//        line.delete(l1[i])

//    if not na(l2[i]) and close > high[i]
//        // We have identified a bar where a line was created.
//        line.delete(l2[i])

我希望在价格超过线时切断线,如图所示。

enter image description here

【问题讨论】:

  • 求助!!!求助!!!求助!!!求助!!!求助!!!

标签: pine-script trading


【解决方案1】:

这应该可以帮助您顺利上路。我们为需求和供应线维护了一系列不同的线。绘制的线条没有扩展,因此在每个条上,我们循环遍历线条数组并使用 line.set_x2(_currentLine, bar_index) 手动扩展线条。

close 越过一条线时,我们从数组中删除线 id,但将线本身保留在原位,以便继续显示。

必须为从未越线的情况做好准备。如果在i_maxLineLength 柱之后发生这种情况,则删除该行。

图表上最多可显示 500 条线(请参阅 study() 调用中的 max_lines_count = 500)。

//@version=4
var maxBarsBack = 10
study("Zones Supply and Demand", shorttitle="SD & RSI",overlay=true, max_lines_count = 500)
ColorZDem=input(color.new(color.lime, 0), title="Color Zona Demanda")
ColorZSuply=input(color.new(color.red, 0), title="Color Zona Oferta")
int i_maxLineLength = input(400, "Maximum line length in bars", minval = 2)

// define a basing and explosive candles
basing_candle = ((abs(close - open)/abs(high - low)) < 0.51)
explosive_candle = (abs(close-open) / abs(high - low)) >= 0.51 and tr>tr[1] 

/// functions
bc_r = basing_candle and close < open
ex_g = explosive_candle and close > open
bc_rz = basing_candle and close > open
ex_gz = explosive_candle and close < open


// zone
demandzone = (bc_r[1] and ex_g and low>=low[1] and close>open[1]) 
supplyzone = (bc_rz[1] and ex_gz and high<=high[1] and close<open[1]) 

var line[] demandLines = array.new_line(0)
var line[] supplyLines = array.new_line(0)

if demandzone and barstate.isconfirmed
    array.push(demandLines, line.new(x1 = bar_index[1], y1=low[1], x2=bar_index, y2= low[1], style=line.style_solid, color=ColorZDem))
else if supplyzone and barstate.isconfirmed
    array.push(supplyLines, line.new(x1 = bar_index[1], y1=high[1], x2=bar_index, y2= high[1], style=line.style_solid, color=ColorZSuply))

f_processLines(_arrayOfLines) =>
    int _qtyOfLines = array.size(_arrayOfLines)
    if _qtyOfLines > 0
        for _i = _qtyOfLines - 1 to 0
            line  _currentLine = array.get(_arrayOfLines, _i)
            float _lineLevel   = line.get_price(_currentLine, bar_index)
            bool  _lineCrossed = sign(close[1] - _lineLevel) != sign(close - _lineLevel)
            line.set_x2(_currentLine, bar_index)
            if _lineCrossed or bar_index - line.get_x1(_currentLine) > i_maxLineLength
                array.remove(_arrayOfLines, _i)

f_processLines(demandLines)
f_processLines(supplyLines)

// Debugging.
plotchar(demandzone, "demandzone", "▲", location.top, size = size.tiny)
plotchar(supplyzone, "supplyzone", "▼", location.top, size = size.tiny)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-11-12
    • 2018-07-03
    • 1970-01-01
    • 2015-01-13
    • 1970-01-01
    • 1970-01-01
    • 2017-01-03
    • 1970-01-01
    相关资源
    最近更新 更多