Trend Suggestions
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © VVVfV

//@version=5
indicator(title="Trend Suggestions",shorttitle="Trend Suggestions",overlay=true)

//Notes
//This brings together a number of variables to produce trend predictions that could be utilized as decision-making tools.

//Uses the aforementioned price and volume derivatives
//- A moving average and three weighted moving averages (WMA1, WMA2, WMA3)
//- Super Trend Line (ST)
//- Opening Range Breakout on Five Minutes, Resistance Bands Pocket pivots , support, and price volume

//The Direction is determined by the High and Low Bands of WMAs and the Supertrend Line, which are used to determine the Upper and Lower Lines around the Price. When the price passes below the lower boundary of the band, a downtrend is said to have begun.

//Similarly, for an uptrend, this continues until the price passes over the upper edge of the band. Teal for an uptrend and fuchsia for a downturn area shared by the band to identify the trend.

//The first five minutes of the breakout lines have a tiny buffer augmentation of 11% applied to them.

//Based on what has been observed, support and resistance zones have been somewhat changed from the figures that are often utilized (might work other markets as well)

//The markings that may be seen are as follows:
//- Blue Triangle indicates a pocket pivot with an upward bias;
//- Maroon Triangle indicates a pocket pivot with a downward bias;
//- Teal colored Diamonds indicate price upthrusts and potential trend confirmation locations, depending on success or failure.
//- Similar backdrop color changes that look as vertical shading are also used to identify them.
//- Fuchsia-colored diamonds indicate price declines and a potential trend, depending on whether it persists or fails.
//- Dark green and maroon square boxes indicate potential price reversals in the support and resistance bands, respectively.

//Added the ability to choose the time frame for the opening band
//Added calculation for Candle Strength
//Candle Strength is based on the clos of a bar in relation to its present range, lookback period range, and placement in relation to band

//This is disabled by default to reduce clutter, but it can be enabled if one wants to give the trend status a numerical value.

//It goes without saying that this work is derived from numerous other open-source community initiatives.

//Feel free to adjust anything you'd like, and we appreciate any feedback.

//paramters

FASTERMA = input.int(title='WMA LENGTH FAST (Min 2)', defval=20,minval=2, step=2)
SLOWERMA  = input.int(title='WMA LENGTH SLOW (Min 6)', defval=40,minval=6, step=2)
MIDMA = math.round((FASTERMA+SLOWERMA)/2)

//supertrend calculation
Periods = input(title='ATR Period', defval=10)
src = input(hl2, title='Source')
Multiplier = input.float(title='ATR Multiplier', step=0.1, defval=1.5)
changeATR = input(title='Change ATR Calculation Method ?', defval=true)
atr2 = ta.sma(ta.tr, Periods)
atr = changeATR ? ta.atr(Periods) : atr2
up = src - Multiplier * atr
up1 = nz(up[1], up)
up := close[1] > up1 ? math.max(up, up1) : up
dn = src + Multiplier * atr
dn1 = nz(dn[1], dn)
dn := close[1] < dn1 ? math.min(dn, dn1) : dn 
trend = 1
trend := nz(trend[1], trend)
trend := trend == -1 and close > dn1 ? 1 : trend == 1 and close < up1 ? -1 : trend
ST = if trend == 1
    up
else 
    dn 

//wma calculation
WMA1 = ta.wma(close, FASTERMA)
WMA2 = ta.wma(close, MIDMA)
WMA3 = ta.wma(close, SLOWERMA)

MAXLINE = math.max(ST,WMA1,WMA2,WMA3)
MINLINE = math.min(ST,WMA1,WMA2,WMA3)

DIRECTION = ta.barssince(close<MINLINE)-ta.barssince(close>MAXLINE)+ta.barssince(close<MAXLINE)-ta.barssince(close>MINLINE)

plotMAXLINE = plot(MAXLINE,"MAXLINE",color = color.new(color.black,10),linewidth = 1,style=plot.style_stepline)
plotMINLINE = plot(MINLINE,"MINLINE",color = color.new(color.black,10),linewidth = 1,style=plot.style_stepline)

fill(plotMAXLINE, plotMINLINE, color = DIRECTION>0?color.new(color.teal, 80):color.new(color.fuchsia, 80))

//Line Based on First 5 Min Range
openbarfrequency = input.string(defval="5",title="Open Range Fequency",options=["5","15","30","45","60","120","240"])
upobon = input(true, title='Opening Range High')
downobon = input(true, title='Opening Range Low')

is_newbar(res) =>
    ta.change(time(res)) != 0

adopt(r, s) =>
    request.security(syminfo.tickerid, r, s)

//high_range = valuewhen(is_newbar('D'),high,0)
//low_range = valuewhen(is_newbar('D'),low,0)

high_rangeL = ta.valuewhen(is_newbar('D'), high, 0)
low_rangeL = ta.valuewhen(is_newbar('D'), low, 0)
diff = (high_rangeL - low_rangeL) * 0.11
upob = plot(upobon and timeframe.isminutes ? adopt(openbarfrequency, high_rangeL) : na, color=color.new(#009900, 0), linewidth=1, style=plot.style_line)
downob = plot(downobon and timeframe.isminutes ? adopt(openbarfrequency, low_rangeL) : na, color=color.new(#ff0000, 0), linewidth=1, style=plot.style_line)

diffupob = plot(upobon and timeframe.isminutes ? adopt(openbarfrequency, high_rangeL + diff) : na, color=color.new(#009900, 0), linewidth=1, style=plot.style_line)
diffdownob = plot(downobon and timeframe.isminutes ? adopt(openbarfrequency, low_rangeL - diff) : na, color=color.new(#ff0000, 0), linewidth=1, style=plot.style_line)

//pocket pivot

myMaxVolume = volume
greenDay = close > open

myVolume1 = volume[1]
myVolume2 = volume[2]
myVolume3 = volume[3]
myVolume4 = volume[4]
myVolume5 = volume[5]
myVolume6 = volume[6]
myVolume7 = volume[7]
myVolume8 = volume[8]
myVolume9 = volume[9]


//make all upday volumes = 0

if close[1] > open[1]
    myVolume1 := 0
    myVolume1
if close[2] > open[2]
    myVolume2 := 0
    myVolume2
if close[3] > open[3]
    myVolume3 := 0
    myVolume3
if close[4] > open[4]
    myVolume4 := 0
    myVolume4
if close[5] > open[5]
    myVolume5 := 0
    myVolume5
if close[6] > open[6]
    myVolume6 := 0
    myVolume6
if close[7] > open[7]
    myVolume7 := 0
    myVolume7
if close[8] > open[8]
    myVolume8 := 0
    myVolume8
if close[9] > open[9]
    myVolume9 := 0
    myVolume9



// if any down days have volumes greater then the current volume, then it's not a pocket pivot
pocketPivot = myVolume1 < myMaxVolume and myVolume2 < myMaxVolume and myVolume3 < myMaxVolume and myVolume4 < myMaxVolume and myVolume5 < myMaxVolume and myVolume6 < myMaxVolume and myVolume7 < myMaxVolume and myVolume8 < myMaxVolume and myVolume9 < myMaxVolume

pocketPivotDay = pocketPivot and greenDay  //only show pocket pivots on up days

plotshape(pocketPivotDay ? 1 : 0, style=shape.triangleup, location=location.belowbar, color=color.new(color.blue, 0), size=size.small)

//pocket pivot Dn

myMaxVolumeD = volume
redDay = close < open

myVolume1D = volume[1]
myVolume2D = volume[2]
myVolume3D = volume[3]
myVolume4D = volume[4]
myVolume5D = volume[5]
myVolume6D = volume[6]
myVolume7D = volume[7]
myVolume8D = volume[8]
myVolume9D = volume[9]


//make all upday volumes = 0

if close[1] < open[1]
    myVolume1D := 0
    myVolume1D
if close[2] < open[2]
    myVolume2D := 0
    myVolume2D
if close[3] < open[3]
    myVolume3D := 0
    myVolume3D
if close[4] < open[4]
    myVolume4D := 0
    myVolume4D
if close[5] < open[5]
    myVolume5D := 0
    myVolume5D
if close[6] < open[6]
    myVolume6D := 0
    myVolume6D
if close[7] < open[7]
    myVolume7D := 0
    myVolume7D
if close[8] < open[8]
    myVolume8D := 0
    myVolume8D
if close[9] < open[9]
    myVolume9D := 0
    myVolume9D



// if any down days have volumes greater then the current volume, then it's not a pocket pivot
pocketPivotD = myVolume1D < myMaxVolumeD and myVolume2D < myMaxVolumeD and myVolume3D < myMaxVolumeD and myVolume4D < myMaxVolumeD and myVolume5D < myMaxVolumeD and myVolume6D < myMaxVolumeD and myVolume7D < myMaxVolumeD and myVolume8D < myMaxVolumeD and myVolume9D < myMaxVolumeD

pocketPivotDayD = pocketPivotD and redDay  //only show pocket pivots on dn days

plotshape(pocketPivotDayD ? 1 : 0, style=shape.triangledown, location=location.abovebar, color=color.new(color.maroon, 0), size=size.small)

//support resistance lines

P1 = high - ((high-low)*0.77)
P2 = low + ((high-low)*0.77)
S1 = low - ((high-low)*0.22)
S2 = low - ((high-low)*0.44)
R1 = high + ((high-low)*0.22)
R2 = high + ((high-low)*0.44)

//Checkbox inputs
CPlot = input(title='Plot Central Pivot?', defval=true)
DayS1R1 = input(title='Plot Daiy S1/R1?', defval=true)
DayS2R2 = input(title='Plot Daiy S2/R2?', defval=true)

//******************DAYWISE CPR & PIVOTS**************************
// Getting daywise CPR
DayP1 = request.security(syminfo.tickerid, 'D', P1[1], barmerge.gaps_off, barmerge.lookahead_on)
DayP2 = request.security(syminfo.tickerid, 'D', P2[1], barmerge.gaps_off, barmerge.lookahead_on)

//Adding linebreaks daywse for CPR
P1Colour = DayP1 != DayP1[1] ? na : color.purple
P2Colour = DayP2 != DayP2[1] ? na : color.purple

//Plotting daywise CPR
plot(CPlot ? DayP1 : na, title='P1', color=P1Colour, style=plot.style_line, linewidth=1)
plot(CPlot ? DayP2 : na, title='P2', color=P2Colour, style=plot.style_line, linewidth=1)

// Getting daywise Support levels
DayS1 = request.security(syminfo.tickerid, 'D', S1[1], barmerge.gaps_off, barmerge.lookahead_on)
DayS2 = request.security(syminfo.tickerid, 'D', S2[1], barmerge.gaps_off, barmerge.lookahead_on)

//Adding linebreaks for daywise Support levels 
DayS1Color = DayS1 != DayS1[1] ? na : color.black
DayS2Color = DayS2 != DayS2[1] ? na : color.black

//Plotting daywise Support levels
PLOTS1 = plot(DayS1R1 ? DayS1 : na, title='D-S1', color=DayS1Color, style=plot.style_line, linewidth=2)
PLOTS2 = plot(DayS2R2 ? DayS2 : na, title='D-S2', color=DayS2Color, style=plot.style_line, linewidth=1)

// Getting daywise Resistance levels
DayR1 = request.security(syminfo.tickerid, 'D', R1[1], barmerge.gaps_off, barmerge.lookahead_on)
DayR2 = request.security(syminfo.tickerid, 'D', R2[1], barmerge.gaps_off, barmerge.lookahead_on)

//Adding linebreaks for daywise Support levels
DayR1Color = DayR1 != DayR1[1] ? na : color.maroon
DayR2Color = DayR2 != DayR2[1] ? na : color.maroon

//Plotting daywise Resistance levels
PLOTR1 = plot(DayS1R1 ? DayR1 : na, title='D-R1', color=DayR1Color, style=plot.style_line, linewidth=3)
PLOTR2 = plot(DayS2R2 ? DayR2 : na, title='D-R2', color=DayR2Color, style=plot.style_line, linewidth=2)

fill(PLOTR1, PLOTR2, color=color.new(color.purple, 90), title='R1R2')
fill(PLOTS1, PLOTS2, color=color.new(color.teal, 90), title='S1S2')

//TREND SUGGESTIONS
//backgroundcolor
bgcolor(close>ta.highest(low,SLOWERMA) and close> (ta.highest(high,5)+ta.lowest(high,5))/2 and close > (high+low)/2 and close > WMA3 and (close>DayR2 or close<DayR1) and MAXLINE > MAXLINE[1] and MINLINE > MINLINE[1] and MAXLINE > WMA3 and close > DayP1 and close > DayS1 and close > adopt('5', low_rangeL - diff)? color.new(color.teal, 90) : color.new(color.gray, 100))
plotshape(close>ta.highest(low,SLOWERMA) and close> (ta.highest(high,5)+ta.lowest(high,5))/2 and close > (high+low)/2 and close > WMA3 and (close>DayR2 or close<DayR1) and MAXLINE > MAXLINE[1] and MINLINE > MINLINE[1] and MAXLINE > WMA3 and close > DayP1 and close > DayS1 and close > adopt('5', low_rangeL - diff), style=shape.diamond, color=color.new(color.teal, 0),location=location.bottom,size=size.small)
plotshape(close>ta.lowest(high,SLOWERMA) and close> (ta.highest(low,2)+ta.lowest(low,2))/2 and close > (high+low)/2 and MAXLINE>DayS2 and low < DayS1 and high > DayS2, style=shape.square, color=color.new(#184419, 0),location=location.bottom,size=size.small)
bgcolor(close>ta.lowest(high,SLOWERMA) and close> (ta.highest(low,2)+ta.lowest(low,2))/2 and close > (high+low)/2 and MAXLINE>DayS2 and low < DayS1 and high > DayS2? color.new(color.blue, 90) : color.new(color.gray, 100))

bgcolor(close<ta.lowest(high,SLOWERMA) and close< (ta.highest(low,5)+ta.lowest(low,5))/2 and close < (high+low)/2 and close < WMA3 and (close>DayS1 or close<DayS2) and MAXLINE < MAXLINE[1] and MINLINE < MINLINE[1] and MINLINE < WMA3 and close < DayP2 and close < DayR1 and close < adopt('5', high_rangeL + diff)? color.new(color.fuchsia, 90) : color.new(color.gray, 100))
plotshape(close<ta.lowest(high,SLOWERMA) and close< (ta.highest(low,5)+ta.lowest(low,5))/2 and close < (high+low)/2 and close < WMA3 and (close>DayS1 or close<DayS2) and MAXLINE < MAXLINE[1] and MINLINE < MINLINE[1] and MINLINE < WMA3 and close < DayP2 and close < DayR1 and close < adopt('5', high_rangeL + diff), style=shape.diamond, color=color.new(color.fuchsia, 0),location=location.top,size=size.small)
plotshape(close<ta.highest(low,SLOWERMA) and close< (ta.highest(high,2)+ta.lowest(high,2))/2 and close < (high+low)/2 and MINLINE<DayR2 and high < DayR2 and low > DayR1, style=shape.square, color=color.new(color.maroon, 0),location=location.top,size=size.small)
bgcolor(close<ta.highest(low,SLOWERMA) and close< (ta.highest(high,2)+ta.lowest(high,2))/2 and close < (high+low)/2 and MINLINE<DayR2 and high < DayR2 and low > DayR1? color.new(color.maroon, 90) : color.new(color.gray, 100))


//Candle Strength
candlestrength = input(false, title='Label Candle Strength')

C1TO_PC = close > close[1]?1:-1
C1TO_PH = close > high[1]?1:0
C1TO_PL = close < low[1]?-1:0
C2TO_O = close > open?1:-1
C2TO_H = close == high?1:0
C2TO_L = close == low?-1:0
C2TO_M = close > (high+low)/2?1:-1
C3TO_H = close > math.max(ta.highest(low,5),ta.lowest(high,5))?1:0
C3TO_L = close < math.min(ta.highest(low,5),ta.lowest(high,5))?-1:0
C3TO_M = close > (ta.highest(low,5)+ta.lowest(high,5))/2?1:-1
C4TO_H = close > math.max(ta.highest(low,SLOWERMA),ta.lowest(high,SLOWERMA))?1:0
C4TO_L = close < math.min(ta.highest(low,SLOWERMA),ta.lowest(high,SLOWERMA))?-1:0
C4TO_M = close > (ta.highest(low,SLOWERMA)+ta.lowest(high,SLOWERMA))/2?1:-1
C5TO_H = close > math.max(ta.highest(low,SLOWERMA*2),ta.lowest(high,SLOWERMA*2))?1:0
C5TO_L = close < math.min(ta.highest(low,SLOWERMA*2),ta.lowest(high,SLOWERMA*2))?-1:0
C5TO_M = close > (ta.highest(low,SLOWERMA*2)+ta.lowest(high,SLOWERMA*2))/2?1:-1
C6BNDH = close > MAXLINE?1:-1
C6BNDL = close < MINLINE?-1:1

STRENGTH = math.round((C1TO_PC + C1TO_PH + C1TO_PL + C2TO_O  + C2TO_H + C2TO_L + C2TO_M + C3TO_H + C3TO_L + C3TO_M + C4TO_H + C4TO_L + C4TO_M + C5TO_H + C5TO_L + C5TO_M + C6BNDH + C6BNDL)/2)

if candlestrength
    LABEL = label.new(bar_index, low-ta.atr(1),str.tostring(STRENGTH),yloc=STRENGTH > 0 ?yloc.belowbar:yloc.abovebar, color=STRENGTH > 0 ? color.rgb(32, 10, 33) : color.rgb(107, 10, 32), textcolor=color.white, style=STRENGTH > 0 ?label.style_label_up : label.style_label_down,size=size.small,text_font_family=font.family_monospace)

//end
