maybethatguy
Pine Script Rookie
Pine Script Rookie
Posts: 4
Joined: May 3rd, 2023
Contact: TradingView Profile

Having trouble plotting horizontal lines along with a signal

Hey there everyone. I'm relatively new to coding and am having some trouble wrapping my head around arrays. I am pretty sure that I need to create an array to have my script do what I'd like it to. Right now it plots circles for divergences and up and down labels for bullish bottoms and bearish tops. I noticed that the bullish bottoms and bearish tops seem to be along support and resistance levels (or at least close). I'd like to be able to plot the bullish tops and bearish bottoms with a horizontal line extending to the right. I tried plotting the lines on the signals themselves, but I had a ton of extra lines that went back farther than my signals are being displayed. If someone could help me plot the lines on the high of a bearish top, and the low of a bearish bottom. I'd really appreciate it. Thanks a ton

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

//color picker
bullishcolor=input(color.blue, "Bullish Div. Color")
bearishcolor=input(color.white, "Bearish Div. Color")
bullishcolortb=input(color.blue, "Bullish Bottom Color")
bearishcolortb=input(color.white, "Bearish Top Color")

//adx
diLength = input.int(7, title = "DI Length", group = "ADX Settings")
adxSmoothing = input.int(7, title = "ADX Smoothing", group = "ADX Settings")
[plusdi, minusdi, adx] = ta.dmi(diLength, adxSmoothing)

//rsi
rsiLength=input.int(9, title="RSI Length", group="RSI Settings")
rsi=ta.rsi(close, rsiLength)

//moving averages
fastema = input(5, "Fast EMA", group = "EMA Div. Settings")
slowema = input(13, "Slow EMA", group = "EMA Div. Settings")
emafast = ta.ema(close, fastema)
emaslow = ta.ema(close, slowema)
trendline = ta.ema(close, 27)

////divergences
//stoch
periodK=input.int(9, title="%K Length", group="Stoch. Div. Settings")
smoothK=input.int(1, title="%K Smoothing", group="Stoch. Div. Settings")
periodD=input.int(3, title="%D Smoothing", group="Stoch. Div. Settings")
k=ta.sma(ta.stoch(close, high, low, periodK), smoothK)
d=ta.sma(k, periodD)
//macd
macdFastLength=input(5, title="Fast Length", group="MACD Div. Settings")
macdSlowLength=input(8, title="Slow Length", group="MACD Div. Settings")
macdSignalLength=input(3, title="Signal Length", group="MACD Div. Settings")
[macdLine, macdSignal, histLine]=ta.macd(close, macdFastLength, macdSlowLength, macdSignalLength)
macdcrossover=ta.crossover(macdLine, macdSignal)
macdcrossunder=ta.crossunder(macdLine, macdSignal)
//ema
fastsma=ta.sma(close, 50)
slowsma=ta.sma(close, 200)
//define variables
stochrising=ta.rising(k, 3)
stochfalling=ta.falling(k, 3)
macdrising=ta.rising(macdLine, 2)
macdfalling=ta.falling(macdLine, 2)
rsirising=ta.rising(rsi, 3)
rsifalling=ta.falling(rsi, 3)
//calculations
bullish=false
bearish=false
bullishtb=false
bearishtb=false
for i = 0 to 5
if close<close and stochrising and rsirising and macdrising or (histLine>-1 and histLine<1) and macdcrossover
bullish := true
if close>close and stochfalling and rsifalling and macdfalling or (histLine>-1 and histLine<1) and macdcrossunder
bearish := true
for e = 0 to 5
if emafast>=emaslow or macdLine<=macdSignal or macdcrossunder[e] or (histLine>0.75)
bullish := false
if emafast<=emaslow or macdLine>=macdSignal or macdcrossover[e] or (histLine<-0.75)
bearish := false

////tops and bottoms
//stoch
rsitb = ta.rsi(close, 9)
stochtb = ta.stoch(close, 1, 3, 5)
if rsitb<=20 and stochtb>stochtb[1] and rsitb<rsitb[1]
bullishtb := true
if rsitb>=80 and stochtb<stochtb[1] and rsitb>rsitb[1]
bearishtb := true
if k>k[1] or minusdi<minusdi[1]
bullishtb := false
if k<k[1] or plusdi<plusdi[1]
bearishtb := false

//plotting divergances, tops, and bottoms, smas
plot(fastsma, "50sma", color = color.aqua)
plot(slowsma, "200sma", color = color.purple)
if (bullish)
label.new(bar_index, low, yloc=yloc.belowbar, style = label.style_circle, color=bullishcolor, size=size.tiny)
if (bearish)
label.new(bar_index, low, yloc=yloc.abovebar, style = label.style_circle, color=bearishcolor, size=size.tiny)
if (bullishtb)
label.new(bar_index, low, yloc=yloc.belowbar, style = label.style_label_up, color=bullishcolortb, size=size.tiny)
if (bearishtb)
label.new(bar_index, low, yloc=yloc.abovebar, style = label.style_label_down, color=bearishcolortb, size=size. Tiny)

Return to “Pine Script Q&A”