Simplest Trend Following Indicator
The Peak is the nearest strong resistant
The Trough is the nearest strong support.
Multi time frame option added.
Signal wedges added with Volume Avg.
StickyBand option added.
CODE:
CSS:
#https://www.tradingview.com/script/sQprJne8-PeakTrough-Indicator/
#// © agungkuswanto
#indicator("PETRUK", "", true)
# Converted and mod by Sam4cok@Samer800 - 10/2022
input useChartTime = yes;
input aggregation = AggregationPeriod.FIFTEEN_MIN;
input ShowCrosses = yes;
input VolumeAvgType = {default SMA, SMMA, LSMA, EMA};
input maLength = 30;
input stickyBand = no;
script nz {
input data = close;
input repl = 0;
def ret_val = if isNaN(data) then repl else data;
plot return = ret_val;
}
#valuewhen (Cond, source, lbr, occurrence)
script valuewhen {
input cond = 0;
input src = close;
input occurrence = 0;
def n = occurrence + 1;
def offset = fold j = 0 to 200 with p=1 while p < n + 1
do p + ( if p == n then j - n else if cond[j] == yes then 1 else 0 );
plot price = GetValue(src, offset - 1);
}
#getStickyRange(highsource, lowsource, upper, lower, sticky=false)=>
script getStickyRange {
input highsource = high;
input lowsource = low;
input upper = high;
input lower = low;
input sticky = no;
def newUpper;
def newLower;
newUpper = if IsNaN(newUpper[1]) then upper else
if highsource[1] >= newUpper[1] or lowsource[1] <= newLower[1] or !sticky
then upper else nz(newUpper[1], upper);
newLower = if IsNaN(newLower[1]) then lower else
if highsource[1] >= newUpper[1] or lowsource[1] <= newLower[1] or !sticky
then lower else nz(newLower[1], lower);
plot UpBand = newUpper;
plot LoBand = newLower;
}
def na = Double.NaN;
def h4daysago;
def h3daysago;
def h2daysago;
def h1daysago;
def l4daysago;
def l3daysago;
def l2daysago;
def l1daysago;
def h = high;
def l = low;
def v = volume;
if useChartTime
then {
h4daysago = high[4];
h3daysago = high[3];
h2daysago = high[2];
h1daysago = high[1];
l4daysago = low[4];
l3daysago = low[3];
l2daysago = low[2];
l1daysago = low[1];
} else {
h4daysago = high(Period = aggregation)[4];
h3daysago = high(Period = aggregation)[3];
h2daysago = high(Period = aggregation)[2];
h1daysago = high(Period = aggregation)[1];
l4daysago = low(Period = aggregation)[4];
l3daysago = low(Period = aggregation)[3];
l2daysago = low(Period = aggregation)[2];
l1daysago = low(Period = aggregation)[1];
}
def thePeakCond = h4daysago <= h3daysago and h3daysago >= h2daysago and h3daysago >= h1daysago and h3daysago > h;
def thePeak = valuewhen(thePeakCond, h3daysago, 0);
def theTroughCond = l4daysago >= l3daysago and l3daysago <= l2daysago and l2daysago <= l1daysago and l3daysago <= l;
def theTrough = valuewhen(theTroughCond, l3daysago, 0);
def Upper = getStickyRange(h, l, thePeak, theTrough, stickyBand).UpBand;
def Lower = getStickyRange(h, l, thePeak, theTrough, stickyBand).LoBand;
plot peak = Upper;
plot trough = Lower;
peak.SetDefaultColor(CreateColor(49, 121, 245));
trough.SetDefaultColor(CreateColor(247, 82, 95));
#----- Signals
def w = WMA(v, maLength);
def Avg;
switch (VolumeAvgType) {
case LSMA:
Avg = Inertia(v, maLength);
case EMA:
Avg = ExpAverage(v, maLength);
case SMA:
Avg = SimpleMovingAvg(v, maLength);
case SMMA:
Avg = if isNaN(w[1]) then SimpleMovingAvg(v, maLength) else
(w[1] * (maLength - 1) + v) / maLength;
}
def VolAvg = Avg;
def crossUp = if !ShowCrosses then na else close crosses above peak and v>=VolAvg;
def crossDn = if !ShowCrosses then na else close crosses below trough and v>=VolAvg;
plot PointUp = if(crossUp,l,na);
plot PointDn = if(crossDn,h,na);
PointUp.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_DOWN);
PointDn.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_UP);
PointUp.SetDefaultColor(Color.UPTICK);
PointDn.SetDefaultColor(Color.DOWNTICK);
#----END Code