0

Loops is my weakness unfortunately... and I need to build a nested loop, unless it can be done differently somehow. Also this is in TradingView Pinescript v5.

So I have automated trendlines and I can capture crossings of each up or down at any point and time top_red_trendline_up_cross = ta.crossover(high, top_red_trendline_coordinates)

If I want to check previous candle high if it crosses that same line I would do: top_red_trendline_up_cross = ta.crossover(high[1], top_red_trendline_coordinates)

Or if previous candle high crosses 3 trend lines ago: top_red_trendline_up_cross = ta.crossover(high[1], top_red_trendline_coordinates[3])

And so on... FYI: each trend line becomes obsolete at some point one it is broken and new one is drawn, so the coordinates of the line change every time it happens.

Basically I want to write a loop which will look for these based on the input provided. The logic is: 1,1 | 1,2 | 1,3 | 1,4 | 2,x

2,1 | 2,2 | 2,3 | 2,4 | 2,x

y,1 | y,2 | y,3 | y,4 | y,a

How would I put it into the loop? Or nested loop? Tried this, but it shows these crossings at obviously wrong places =/

top_red_trendline_up_cross = false
for x = 0 to 10
    for y = 0 to 10
        top_red_trendline_up_cross := ta.crossover(high[x], top_red_trendline_coordinates[y])

plotshape(top_red_trendline_up_cross, style=shape.xcross, color=color.red, size=size.normal, location=location.abovebar)

I already got pretty advanced into Pinescript, but loops always kill me and you can't go around loops with such task.. Appreciate any help/suggestions!

  • How do you draw trendlines? After the formation of certain conditions, do you draw a line object from the current bar to N bars back? Or is it series? From your example, the value will be overwritten each time in the loop, and when you call the plotshape function, you will only have the last value from the loop. In addition, if you draw lines in the past, the serial function plotshape is not suitable for you, it can only display the resulting value on the current bar, if you want to mark all the intersections of the price with your lines in the past, then you need to use labels – vgladkov Dec 07 '22 at 16:46
  • @vgladkov, seems like loop was setup wrong as high must be always the current one, while the line has to be ANY line: current or the past. – Aleksandr Narkomandr Dec 08 '22 at 18:25
  • @vgladkov, The lines are drawn like this: SHOW 1 PREVIOUS LINE ONLY / THE REST ARE DELETED: pivot_trend_line_connector_high_prev1 = line.new(phb1[1], phv1[1], bar_index[1], res_y[1], style=line.style_dotted, color=color.rgb(214, 17, 17, 25), extend=extend.right) line.delete(pivot_trend_line_connector_high_prev1[2]) //-------------------------------------------------------------------------// SHOW ALL PREVIOUS LINES pivot_trend_line_connector_high_prev1 = line.new(phb1[1], phv1[1], bar_index[1], res_y[1], style=line.style_dotted, color=color.rgb(214, 17, 17, 25), extend=extend.right) – Aleksandr Narkomandr Dec 08 '22 at 18:28
  • I have no idea why comments section to not register new line and just prints everything as a whole text.. – Aleksandr Narkomandr Dec 08 '22 at 18:29
  • pivot_trend_line_connector_high_prev1 = line.new(phb1[1], phv1[1], bar_index[1], res_y[1], style=line.style_dotted, color=color.rgb(214, 17, 17, 25), extend=extend.right) line.delete(pivot_trend_line_connector_high_prev1[2]) – Aleksandr Narkomandr Dec 08 '22 at 18:30
  • pivot_trend_line_connector_high_prev1 = line.new(phb1[1], phv1[1], bar_index[1], res_y[1], style=line.style_dotted, color=color.rgb(214, 17, 17, 25), extend=extend.right) – Aleksandr Narkomandr Dec 08 '22 at 18:30
  • so if I obtain the line coordinates like: pivot_trend_line_connector_high_prev1_COORDINATES = line.get_price(pivot_trend_line_connector_high_prev1, bar_index) – Aleksandr Narkomandr Dec 08 '22 at 18:31
  • for x = 0 to 10 top_red_trendline_up_cross := ta.crossover(high, pivot_trend_line_connector_high_prev1_COORDINATES[x]) – Aleksandr Narkomandr Dec 08 '22 at 18:32
  • shouldn't it show all line crossovers? – Aleksandr Narkomandr Dec 08 '22 at 18:33
  • @vgladkov, I have tried using labels and still getting very strange results.. – Aleksandr Narkomandr Dec 08 '22 at 19:48
  • @vgladkov, code: https://ibb.co/s5g7KPp result: https://ibb.co/MRTsQqw – Aleksandr Narkomandr Dec 08 '22 at 19:50
  • You get a label every time the high of the current bar is greater than or equal to the projection of the line on the previous bars, each separately. Labels overlap each other. It is still not clear for me what result you expect to see, can you give an example by manually drawing one or more lines on the chart and what do you expect to see along with them? – vgladkov Dec 20 '22 at 18:33
  • i was finally able to solve it with arrays! – Aleksandr Narkomandr Dec 21 '22 at 19:09

0 Answers0