PeakTrough indicator[PETRUK] for ThinkOrSwim

samer800

Moderator - Expert
VIP
Lifetime
sRaM0IX.png

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
 

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
485 Online
Create Post

Similar threads

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top