简体   繁体   中英

Cancel plot if bar crosses it

I am trying to plot some lines with the below code, however cant work out how to get them to stop plotting further if, (or once)another candle crosses it as currently its just going straight through them. I am also struggling with how to extend all lines equally to 30 bars ahead of time regardless where they are on the chart so that I can then add price labels to them.

You will notice that it is plotting the highs of bearish candles but I also want to plot / not plot the lows of the bullish candles in the same way.

I want to be able to control how many candles this will plot for which I believe will be something to do with an input function?

I have gone round and round trying to work out whether this will all need to be done through arrays or loops or even both? but I just keep hitting a brick wall when trying to research it.

Any help would be greatly appreciated

形象问题 图片

//@version=5
indicator('123456', overlay=true)

bars_back = input(-30, 'Bars Forward')
n = input(1, 'back')


//Lines
bear = open > close
highestHigh = if bear
    ta.highest(high, n)
bullline = line.new(bar_index - bars_back, highestHigh, bar_index, highestHigh, extend=extend.none, color=color.red)

Blake, we need to put these lines into an array; a list of lines so we can perform actions on them as a group. When we store things in a list, we can run a loop through the list and check to see if conditions are met. example: are bananas on list, if so remove bananas. In this way we dont need to know where the items are in the list or how long the list is, just that there is a list that we can check. So I have taken your line and we add them to the list if your condition is met. Then we can run a few loops on the list to see if a) the high breaches your lines so we can stop plotting and remove from list, and b) if they are still on the list we update the x2 for each so that their end points align on the right margin. I have included notes in the code to explain what each line is doing.

//@version=5
indicator("My Script", overlay=true, max_lines_count=500)

bars = input(30, 'Bars Forward') // make this positive. No need to draw our lines backwards 
n = input(1, 'back')

var line[]  highs = array.new_line()  // declare an empty array to store our lines in 
var label[] lab   = array.new_label() // declare an empty array to store our labels in 

//Lines

bear = open > close // our condition 
highestHigh = ta.highest(high, n) // move this out of local scope - can be calculated each bar 

if bear     // instead of just drawing a line, we push it into an array, a list of lines so we can loop through the list and perform actions on all lines 
    array.unshift(highs, line.new(bar_index, highestHigh, bar_index + bars, highestHigh, extend=extend.none, color=color.red)) // unshift just adds the line to the top of the shopping list 
    array.unshift(lab, label.new(bar_index+bars, highestHigh, "Vol: " + str.tostring(volume, "#.00"), color=na, style=label.style_label_down, textcolor=color.red)) // push a label with displaying volume into a list

if array.size(highs) > 0                                  // check that we dont have an empty list, or it will error 
    int i = 0                                             // declare i (necessary for while loops)
    while i < array.size(highs)-1                         // run a continuous loop through the list index 
        l = line.get_price(array.get(highs,i), bar_index) // on each iteration we assign l to a given line in the list 
        if high > l                                       // check to see if our high breaches that line 
            line.set_x2(array.get(highs,i), bar_index)    // if it does, we set the line x2 to the current bar index so it doesnt carry forward 
            array.remove(highs, i)                        // we remove the line from the array so as not to extend it with the other lines in the next function - stop tracking the line 
            label.delete(array.get(lab, i))               // delete the label if the high is greater than the line 
            array.remove(lab, i)                          // remove the label from the label list 
            continue                                      // continue to check for highs crossing lines 
        i += 1                                            // increment i to next index 

    for i = 0 to array.size(highs)-1                      // run a for loop on each index in the array 
        line.set_x2(array.get(highs, i), bar_index+bars)  // for each line in the array we update the x2 to barindex + 30 bars so that all right sides align. This occurs each bar and continuews to move in time 
        label.set_x(array.get(lab,   i), bar_index+bars)  // we also update our label to alligh on right margin 
 

Cheers, and best of luck in your coding and trading

I suppose you want something like this. The algorithm is collecting the lines into an array and checking for all elements of the array on each bar whether the candlestick crosses the line from the array, if yes, then fix the x2 coordinate on the current bar, if not, then extend its x2 coordinate by current bar + offset. Check out the sample code

//@version=5

MAX_LINES_COUNT = 100

indicator('My Script', overlay=true, max_lines_count=MAX_LINES_COUNT)

lines_forward_offset = input.int(30, 'Bars Forward', minval=0)
lines_count = input.int(10, 'Count Of Lines', minval=0, maxval=MAX_LINES_COUNT)

var fixedLines = array.new_line()
var allLines = array.new_line()

cross(value) =>
    high >= value and low <= value

// checking the number of lines drawn
if array.size(allLines) > lines_count
    removedLine = array.shift(allLines)
    if array.includes(fixedLines, removedLine)
        indexOfRemovedLine = array.indexof(fixedLines, removedLine)
        array.remove(fixedLines, indexOfRemovedLine)
    line.delete(removedLine)

// setting for all lines the index of the current bar + offset as a coordinate x2
// except for those that were fixed after crossing the candle
for currentLine in allLines
    if array.includes(fixedLines, currentLine)
        continue
        
    line.set_x2(currentLine, bar_index+lines_forward_offset)
    linePrice = line.get_y1(currentLine)
    if cross(linePrice)
        line.set_x2(currentLine, bar_index)
        array.push(fixedLines, currentLine)

bearishHigh = open > close ? high : na
if not na(bearishHigh)
    l1 = line.new(bar_index, bearishHigh, bar_index, bearishHigh, extend=extend.none, color=color.red)
    array.push(allLines, l1)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM