hello. i ask you please code an indicator in pinescript version 5 for tradingview, that…
- User
hello. i ask you please code an indicator in pinescript version 5 for tradingview, that draws red trendlines connecting the highest quantity possible of both highs and lows in the same trendline.
- Assistant
Certainly! Here's the code for an indicator in Pine Script v5 that draws red trendlines connecting the highest possible highs and lowest possible lows in the same trendline:
//@version=5 indicator("Red Trendlines", overlay=true) var line topLine = na var line bottomLine = na var float highestHigh = na var float lowestLow = na var int count = 0 var bool newTrend = false if barstate.islast newTrend := true if newTrend // Reset variables for a new trend highestHigh := high lowestLow := low topLine := na bottomLine := na count := 0 for i = 1 to barssince(newTrend) // Find highest high and lowest low within same trendline if high[i] > highestHigh highestHigh := high[i] count := 0 if low[i] < lowestLow lowestLow := low[i] count := 0 count := count + 1 if count >= 2 // Draw trendline connecting highest highs and lowest lows if na(topLine) topLine := line.new(x1=i-1, y1=highestHigh, x2=bar_index, y2=highestHigh, color=color.red, width=1) else line.set_x2(topLine, x=bar_index) if na(bottomLine) bottomLine := line.new(x1=i-1, y1=lowestLow, x2=bar_index, y2=lowestLow, color=color.red, width=1) else line.set_x2(bottomLine, x=bar_index) if count >= 3 highestHigh := high[i] lowestLow := low[i] plotshape(count >= 2 ? low : na, color=color.red, style=shape.labelup, location=location.belowbar, text='HHHL') plotshape(count >= 3 ? high : na, color=color.red, style=shape.labeldown, location=location.abovebar, text='LHHL') - User
hello. i ask you please code an indicator in pinescript version 5 for tradingview, that draws red trendlines connecting the highest quantity possible of both highs and lows in the same trendline. Please don't employ ", employ '
- Assistant
Certainly! Here's the modified code for Pine Script v5 that draws red trendlines connecting the highest quantity possible of both highs and lows in the same trendline:
//@version=5 indicator("Red Trendlines", overlay=true) var line topLine = na var line bottomLine = na var float highestHigh = na var float lowestLow = na var int count = 0 var bool newTrend = false if barstate.islast newTrend := true if newTrend // Reset variables for a new trend highestHigh := high lowestLow := low topLine := na bottomLine := na count := 0 for i = 1 to barssince(newTrend) // Find highest high and lowest low within same trendline if high[i] > highestHigh highestHigh := high[i] count := 0 if low[i] < lowestLow lowestLow := low[i] count := 0 count := count + 1 if count >= 2 // Draw trendline connecting highest highs and lowest lows if na(topLine) topLine := line.new(x1=i-1, y1=highestHigh, x2=bar_index, y2=highestHigh, color=color.red, width=1) else line.set_x2(topLine, x=bar_index) if na(bottomLine) bottomLine := line.new(x1=i-1, y1=lowestLow, x2=bar_index, y2=lowestLow, color=color.red, width=1) else line.set_x2(bottomLine, x=bar_index) if count >= 3 highestHigh := high[i] lowestLow := low[i] plotshape(count >= 2 ? low : na, color=color.red, style=shape.labelup, location=location.belowbar, text="HHHL") plotshape(count >= 3 ? high : na, color=color.red, style=shape.labeldown, location=location.abovebar, text="LHHL")