//@version=2
study("Trend Line", overlay=true)
//I like tips :D
//look back distance
shortl = input(20)
longl = input(100)
//establish timeline in bars
timeline = nz(timeline[1]) + 1
//find bottoms
middle = round(1) //half way through shortl round(shortl/2)
//a = valuewhen(low[middle]==lowest(low, shortl), low[middle], 0) //what is lowest point in the short range
//whena = valuewhen(low[middle]==lowest(low, shortl), fixnan(timeline[middle]), 0) //when is lowest point in the range
a = valuewhen(low[middle]==lowest(low, shortl), low[middle], 0)
whena = valuewhen(low[middle]==lowest(low, shortl), fixnan(timeline[middle]), 0)
middlelong = round(longl/2)
//b= valuewhen(fixnan(low[middle])==lowest(low, shortl) , fixnan(low[middle]), 1) //
//whenb = valuewhen(fixnan(low[middle])==lowest(low, shortl) , timeline[middle], 1) //
b= valuewhen(fixnan(low[middlelong])==lowest(low, longl) , fixnan(low[middlelong]), 0) //
whenb = valuewhen(fixnan(low[middlelong])==lowest(low, longl) , fixnan(timeline[middlelong]), 0) //
b := b==a? valuewhen(fixnan(low[middlelong])==lowest(low, longl) , fixnan(low[middlelong]), 1) : b
whenb := b==a? valuewhen(fixnan(low[middlelong])==lowest(low, longl) , fixnan(timeline[middlelong]), 1) : whenb
//m = ((b-a)/(whenb-whena))
//trendprice = m*(timeline-whena-0) + a
//backtrendprice = m*(timeline-whena-middle) + a
m = (((b)-(a))/(whenb-whena))
trendprice = (m*(timeline-whena-0)) + a
backtrendprice = (m*(timeline-whena-middle)) + a
sametrend = a==fixnan(a[1]) and b==fixnan(b[1]) and a!=0 and b!=0 and whena!=whenb
//plot
plot(sametrend? trendprice: na, style=linebr, color=green, linewidth=2, offset=0, transp=50)
plot(sametrend? backtrendprice: na, style=linebr, color=green, linewidth=1, offset=-middle, transp=50)
forwardtrendprice = (m*(timeline-whena+middle)) + a
plot(sametrend? forwardtrendprice: na, style=linebr, color=green, linewidth=1, offset=middle, transp=70)
//find tops
a2 = valuewhen(high[middle]==highest(high, shortl), high[middle], 0) //
whena2 = valuewhen(high[middle]==highest(high, shortl), timeline[middle], 0) //
b2= valuewhen(fixnan(high[middlelong])==highest(high, longl) , fixnan(high[middlelong]), 0) //
whenb2 = valuewhen(fixnan(high[middlelong])==highest(high, longl) , fixnan(timeline[middlelong]), 0) //
b2 := b2==a2? valuewhen(fixnan(high[middlelong])==highest(high, longl) , fixnan(high[middlelong]), 1) : b2
whenb2 := b2==a2? valuewhen(fixnan(high[middlelong])==highest(high, longl) , fixnan(timeline[middlelong]), 1) : whenb2
m2 = ((b2-a2)/(whenb2-whena2))
trendprice2 = m2*(timeline-whena2-0) + a2
backtrendprice2 = m2*(timeline-whena2-shortl) + a2
sametrend2 = a2==fixnan(a2[1]) and b2==fixnan(b2[1]) and a2!=0 and b2!=0 and whena2!=whenb2
//plot
plot(sametrend2? trendprice2:na, style=linebr, color=red, linewidth=2, offset=-0, transp=50)
plot(sametrend2? backtrendprice2:na, style=linebr, color=red, linewidth=1, offset=-shortl, transp=50)
forwardtrendprice2 = (m2*(timeline-whena2+middle)) + a2
plot(sametrend2? forwardtrendprice2: na, style=linebr, color=red, linewidth=1, offset=middle, transp=70)
Disclaimer:
All the pine script’s posted in this section are for learning purpose. Website does not necessarily own these pine script’s and we don’t have any intellectual property rights on them. We might copy useful pine script’s from public forums and post it in this section in a presentable format. The intent is not to copy anybody’s work but to share knowledge. If you find any misleading or non-reproducible content then please inform us at tradingwithpawan@gmail.com