Pivot Trendlines with Breaks [HG] for ThinkOrSwim

samer800

Moderator - Expert
VIP
Lifetime
I5K5CSL.png

Author Message: - Not Exact Conversion

Calculating pivot points helps traders identify moments at which the market's attitude can shift from bullish to bearish. In the background, the script tracks pivot events for trendlines and uses a system that prevents any leakage between the trendlines before they are drawn.

CODE:
CSS:
#/ This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
#// © HoanGhetti
#indicator("Pivot Trendlines with Breaks [HG]", overlay = true, max_labels_count = 50, max_lines_count = 50)
# Converted by Sam4Cok@Samer800 - 04/2023 - Not Typical conv

input PivotLength = 20;     # 'Pivot Length'
input repaint   = no;    # 'Repainting'
input ShowTodayOnly = no;
input ShowBrakouts = {"Don't Show Breakouts", default "Show Confirmed Breakouts"};
input SaourceCalcMethod = {default "Wicks", "Candle Close"};

def na = Double.NaN;
def rep = !repaint;
def c = close;
def o = open;
def h = high;
def l = low;
def n = AbsValue(CompoundValue(1, BarNumber(), 0));
def today =  GetDay() == GetLastDay();
def wick = SaourceCalcMethod == SaourceCalcMethod."Wicks";
def ShowSignal = ShowBrakouts == ShowBrakouts."Show Confirmed Breakouts";

#valuewhen (cond, source, occurrence) =>
script valuewhen {
    input cond = 0;
    input source = 0;
    input occurrence = 0;
    def n = occurrence + 1;
    def offset2 = fold j = 0 to 500 with p while p < n + 1 do
                  p + ( if p == n then j - n else if GetValue(cond, j) then 1 else 0);
    plot price = GetValue(source, offset2 - 1);
}
script FindPivots {
    input dat = close; # default data or study being evaluated
    input HL  = 0;    # default high or low pivot designation, -1 low, +1 high
    input lbL  = 20;    # default Pivot Lookback Left
    input lbR  = 20;    # default Pivot Lookback Right
    ##############
    def _nan;    # used for non-number returns
    def _BN;     # the current barnumber
    def _VStop;  # confirms that the lookforward period continues the pivot trend
    def _V;      # the Value at the actual pivot point
    ##############
    _BN  = AbsValue(CompoundValue(1, BarNumber(), 0));
    _nan = Double.NaN;
    _VStop = if !IsNaN(dat) and lbR > 0 and lbL > 0 then
                fold a = 1 to lbR + 1 with b=1 while b do
                    if HL > 0 then dat > GetValue(dat, -a) else dat < GetValue(dat, -a) else _nan;
    if (HL > 0) {
        _V = if _BN > lbL and dat == Highest(dat, lbL + 1) and _VStop
            then dat else _nan;
    } else {
        _V = if _BN > lbL and dat == Lowest(dat, lbL + 1) and _VStop
            then dat else _nan;
    }
    plot result = if !IsNaN(_V) and _VStop then _V else _nan;
}
script pivot {
    input pType = 1;
    input ph = high;
    input pl = low;
    def bar_index = AbsValue(CompoundValue(1, BarNumber(), 0));
    def pivot = if pType == -1 then  pl else ph;
    def change = pivot!=pivot[1];
    def xAxis = valuewhen(change, bar_index, 0) - valuewhen(change, bar_index, 1);
    def prevPivot = valuewhen(change, pivot, 1);
    def pivotCond = change and (if pType == -1 then pivot > prevPivot else pivot < prevPivot);
#    pData = tl.new(x_axis = xAxis, offset = input_len, strictMode = true, strictType = pType == pl ? 0 : 1)
#    pData.drawLine(pivotCond, prevPivot, pivot)
    plot pvt = pivot;
    plot prePvt = xAxis;
    plot conPvt = pivotCond;
}
def wickHi = if wick then h else Max(c, o);
def wickLo = if wick then l else Min(c, o);
def p_h = findpivots(wickHi, 1, PivotLength, PivotLength);
def p_l = findpivots(wickLo, -1, PivotLength, PivotLength);

def ph = if !IsNaN(p_h) then p_h else ph[1];
def pl = if !IsNaN(p_l) then p_l else pl[1];

def plDataPvt = pivot(-1, ph, pl).pvt;
def plDataPrePvt = pivot(-1, ph, pl).prePvt;
def plDataConPvt = pivot(-1, ph, pl).conPvt;

def phDataPvt = pivot( 1, ph, pl).pvt;
def phDataPrePvt = pivot( 1, ph, pl).prePvt;
def phDataConPvt = pivot( 1, ph, pl).conPvt;

def r1 = if plDataConPvt then plDataPvt else na;
#r1.EnableApproximation();
#plot r2 = if plDataConPvt then plDataPrePvt else na;
#r2.EnableApproximation();

def s1 = if phDataConPvt then phDataPvt else na;
#s1.SetDefaultColor(Color.GREEN);
#s1.EnableApproximation();
#plot s2 = if phDataConPvt then phDataPrePvt else na;
#s2.EnableApproximation();

def s11 = if phDataConPvt then phDataPvt else s11[1];

#plot s_11 = if !isNaN(s1) then na else
#            fold i=0 to input_len + 1 with p=high do
#            if GetValue(phDataConPvt,input_len - i) then GetValue(phDataPvt,input_len - i) else na;
#s_11.EnableApproximation();

def pvt0 = valuewhen(phDataConPvt, wickHi, 0);
def pvt1 = valuewhen(phDataConPvt, wickHi, 1);
def diff = AbsValue(pvt1 - pvt0);
def indx = phDataPrePvt;

def pvt00 = valuewhen(plDataConPvt, wickLo, 0);
def pvt11 = valuewhen(plDataConPvt, wickLo, 1);
def diff1 = AbsValue(pvt00 - pvt11);
def indx1 = plDataPrePvt;

#----------------------------

#//----
def slopHi = if phDataConPvt then tan(diff/indx) else na;
def slopLo = if plDataConPvt then tan(diff1/indx1) else na;

def slope_ph = if !IsNaN(p_h) then slopHi else slope_ph[1];
def slope_pl = if !IsNaN(p_l) then slopLo else slope_pl[1];

def upper; def lower;
def upper_ = if (isNaN(upper[1]) or upper[1]==0) then wickHi else upper[1];
def lower_ = if (isNaN(lower[1]) or lower[1]==0) then wickLo else lower[1];

    upper = if (n < PivotLength-1) then na else if !IsNaN(p_h) then p_h else upper_ - slope_ph;
    lower = if (n < PivotLength-1) then na else if !IsNaN(p_l) then p_l else lower_ + slope_pl;
#//----
def single_upper = if (c[rep] > upper) then 0 else
                   if !IsNaN(ph) then 1 else single_upper[1];
def single_lower = if (c[rep] < lower) then 0 else
                   if !IsNaN(pl) then 1 else single_lower[1];

def upper_breakout = single_upper[1] and c[rep] > upper
                    and (if ShowSignal then c > c[1] else 1);
def lower_breakout = single_lower[1] and c[rep] < lower
                    and (if ShowSignal then c < c[1] else 1);

def UpBreak = If upper_breakout then l[rep] else na;
def LoBreak = If lower_breakout then h[rep] else na;

def Labelup = if !ShowSignal then na else if !ShowTodayOnly then UpBreak[-rep] else
              if !today then na else UpBreak[-rep];
def Labeldn = if !ShowSignal then na else if !ShowTodayOnly then LoBreak[-rep] else
              if !today then na else LoBreak[-rep];

#//----

def UpPH   = if isNaN(p_h) and !phDataConPvt then upper else na;
def LoLH   = if isNaN(p_l) and !plDataConPvt then lower else na;

#plot upperPH = if ShowTodayOnly then if UpPH == 0 or !today then na else UpPH else
#               if UpPH == 0 then na else UpPH ;
plot upperPH = if c[rep] > upper then na else
               if !ShowTodayOnly then UpPH else
               if !today then na else UpPH;
upperPH.SetDefaultColor(CreateColor(38,166,154));

plot LowerLH = if c[rep] < lower then na else
               if !ShowTodayOnly then LoLH else
               if !today then na else LoLH;
LowerLH.SetDefaultColor(CreateColor(239, 83, 80));

plot upperDash = if !ShowTodayOnly then
                 if isNaN(upperPH) then UpPH else na else
                 if !today then na else if isNaN(upperPH) then UpPH else na;
upperDash.SetDefaultColor(CreateColor(38,166,154));
upperDash.SetStyle(Curve.MEDIUM_DASH);

plot LowerDash = if !ShowTodayOnly then
                 if isNaN(LowerLH) then LoLH else na else
                 if !today then na else if isNaN(LowerLH) then LoLH else na;
LowerDash.SetDefaultColor(CreateColor(239, 83, 80));
LowerDash.SetStyle(Curve.MEDIUM_DASH);

#-------- Breakout Bubbles
AddChartBubble(ShowSignal and Labelup, Labelup, "Break", Color.CYAN, no);
AddChartBubble(ShowSignal and Labeldn, Labeldn, "Break", Color.MAGENTA, yes);



#-- ENd of 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
434 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