Difference Between Two Zigzag High Pivot Points

british43

Member
VIP
I'm working on a script to find the different between two Zigzag high pivot points (would also like to do this for the low side also). Please see image. The bubble at "1" would green with the value of "93.74 and 43.01 (difference between previous high and current value of the Zigzag). If flip the scenario the bubble at "1" would red. Any help would be great.

This is the line of code I think I'm messing up.

def ATRSave = if !IsNaN(ZZ) then SMI1 else GetValue(ATRSave, 1);
def ATRchg = round(SMI1 - GetValue(ATRSave, 1));
plot ATRisUp = ATRchg >= 0;
ATRisUp.Hide();
def ATRisDown = ATRchg <= 0;
def isConf = AbsValue(ATRchg) >= reversalAmount or (IsNaN(getValue(ZZ, 1)) and getValue(isConf, 1));

plot ATRDiff= ATRchg;
ATRDiff.hide();

AddChartBubble(1, ZZ, SMI1+" "+ATRchg, if ATRisUp then Color.GREEN else if ATRisDown then Color.RED else Color.RED, ATRisUp);
[/CODE]

XC9SDhv.png


Ruby:
declare lower;

input length = 14;
input averageType1 = AverageType.WILDERS;

plot ADX = DMI(length, averageType1).ADX;
ADX.SetDefaultColor(GetColor(5));

input KPeriod = 8;
input n3 = 5;
input priceHl = high;
input priceLl = low;
input priceCl = close;
input slowing_period = 3;
input averageType = AverageType.SIMPLE;
input showBreakoutSignals = {default "No", "On FullK", "On FullD", "On FullK & FullD"};
input showlabels = yes;

def lowest_k = Lowest(priceLl, KPeriod);
def c1 = priceCl - lowest_k;
def c2 = Highest(priceHl, KPeriod) - lowest_k;
def FastK = if c2 != 0 then c1 / c2 * 100 else 0;

plot SMI1 = round(MovingAverage(averageType, FastK, slowing_period));

def o = open;
def h = high;
def l = low;
def c = close;
def priceH = SMI1;
def priceL = SMI1;
def priceC = SMI1;

input percentageReversal = 5.0;
input absoluteReversal = 0.0;
input atrLength = 5;
input atrReversal = 1;
input tickReversal = 0;

Assert(percentageReversal >= 0, "'percentage reversal' must not be negative: " + percentageReversal);
Assert(absoluteReversal >= 0, "'absolute reversal' must not be negative: " + absoluteReversal);
Assert(atrReversal >= 0, "'atr reversal' must not be negative: " + atrReversal);
Assert(tickReversal >= 0, "'ticks' must not be negative: " + tickReversal);
Assert(percentageReversal != 0 or absoluteReversal != 0 or atrReversal != 0 or tickReversal != 0, "Either 'percentage reversal' or 'absolute reversal' or 'atr reversal' or 'tick reversal' must not be zero");

def absReversal;
if (absoluteReversal != 0) {
    absReversal = absoluteReversal;
} else {
    absReversal =  tickReversal * TickSize();
}

def hlPivot;
if (atrReversal != 0) {
    hlPivot = percentageReversal / 100 + WildersAverage(TrueRange(SMI1, SMI1, SMI1), atrLength) / SMI1 * atrReversal;
} else {
    hlPivot = percentageReversal / 100;
}
def state = {default init, undefined, uptrend, downtrend};
def maxPriceH;
def minPriceL;
def newMax;
def newMin;
def prevMaxH = GetValue(maxPriceH, 1);
def prevMinL = GetValue(minPriceL, 1);

if GetValue(state, 1) == GetValue(state.init, 0) {
    maxPriceH = priceH;
    minPriceL = priceL;
    newMax = yes;
    newMin = yes;
    state = state.undefined;
} else if GetValue(state, 1) == GetValue(state.undefined, 0) {
    if priceH >= prevMaxH {
        state = state.uptrend;
        maxPriceH = priceH;
        minPriceL = prevMinL;
        newMax = yes;
        newMin = no;
    } else if priceL <= prevMinL {
        state = state.downtrend;
        maxPriceH = prevMaxH;
        minPriceL = priceL;
        newMax = no;
        newMin = yes;
    } else {
        state = state.undefined;
        maxPriceH = prevMaxH;
        minPriceL = prevMinL;
        newMax = no;
        newMin = no;
    }
} else if GetValue(state, 1) == GetValue(state.uptrend, 0) {
    if priceL <= prevMaxH - prevMaxH * hlPivot - absReversal {
        state = state.downtrend;
        maxPriceH = prevMaxH;
        minPriceL = priceL;
        newMax = no;
        newMin = yes;
    } else {
        state = state.uptrend;
        if (priceH >= prevMaxH) {
            maxPriceH = priceH;
            newMax = yes;
        } else {
            maxPriceH = prevMaxH;
            newMax = no;
        }
        minPriceL = prevMinL;
        newMin = no;
    }
} else {
    if priceH >= prevMinL + prevMinL * hlPivot + absReversal {
        state = state.uptrend;
        maxPriceH = priceH;
        minPriceL = prevMinL;
        newMax = yes;
        newMin = no;
    } else {
        state = state.downtrend;
        maxPriceH = prevMaxH;
        newMax = no;
        if (priceL <= prevMinL) {
            minPriceL = priceL;
            newMin = yes;
        } else {
            minPriceL = prevMinL;
            newMin = no;
        }
    }
}

def barNumber = BarNumber();
def barCount = HighestAll(If(IsNaN(priceH), 0, barNumber));
def newState = GetValue(state, 0) != GetValue(state, 1);
def offset = barCount - barNumber + 1;
def highPoint = state == state.uptrend and priceH == maxPriceH;
def lowPoint = state == state.downtrend and priceL == minPriceL;

def lastH;
if highPoint and offset > 1 {
    lastH = fold iH = 1 to offset with tH = priceH while !IsNaN(tH) and !GetValue(newState, -iH) do if GetValue(newMax, -iH) or iH == offset - 1 and GetValue(priceH, -iH) == tH then Double.NaN else tH;
} else {
    lastH = Double.NaN;
}

def lastL;
if lowPoint and offset > 1 {
    lastL = fold iL = 1 to offset with tL = priceL while !IsNaN(tL) and !GetValue(newState, -iL) do if GetValue(newMin, -iL) or iL == offset - 1 and GetValue(priceL, -iL) == tL then Double.NaN else tL;
} else {
    lastL = Double.NaN;
}

plot ZZ;
if barNumber == 1 {
    ZZ = fold iF = 1 to offset with tP = Double.NaN while IsNaN(tP) do if GetValue(state, -iF) == GetValue(state.uptrend, 0) then priceL else if GetValue(state, -iF) == GetValue(state.downtrend, 0) then priceH else Double.NaN;
} else if barNumber == barCount {
    ZZ = if highPoint or state == state.downtrend and priceL > minPriceL then priceH else if lowPoint or state == state.uptrend and priceH < maxPriceH then priceL else Double.NaN;
} else {
    ZZ = if !IsNaN(lastH) then lastH else if !IsNaN(lastL) then lastL else Double.NaN;
}
ZZ.SetDefaultColor(GetColor(1));
ZZ.EnableApproximation();

#ZZ.hide();

def bar = BarNumber();

input n = 10;
def CurrMACDh = if SMI1 > 0
                then fold i = 1 to n + 1
                with p = 1
                while p
                do SMI1 > GetValue(SMI1, -i)
                else 0;
def CurrMACDPivotH = if (bar > n and
                         SMI1 == Highest(SMI1, n) and
                         CurrMACDh)
                     then SMI1
                     else Double.NaN;
def CurrMACDl = if SMI1 > 0
                then fold j = 1 to n + 1
                with q = 1
                while q
                do SMI1 < GetValue(SMI1, -j)
                else 0;
def CurrMACDPivotL = if (bar > n and
                         SMI1 == Lowest(SMI1, n) and
                         CurrMACDl)
                     then SMI1
                     else Double.NaN;
def CurrPHBar = if !IsNaN(CurrMACDPivotH)
                then bar
                else CurrPHBar[1];
def CurrPLBar = if !IsNaN(CurrMACDPivotL)
                then bar
                else CurrPLBar[1];
def PHpoint = if !IsNaN(CurrMACDPivotH)
              then CurrMACDPivotH
              else PHpoint[1];
def priorPHBar = if PHpoint != PHpoint[1]
                 then CurrPHBar[1]
                 else priorPHBar[1];
def PLpoint = if !IsNaN(CurrMACDPivotL)
              then CurrMACDPivotL
              else PLpoint[1];
def priorPLBar = if PLpoint != PLpoint[1]
                 then CurrPLBar[1]
                 else priorPLBar[1];
def HighPivots = bar >= HighestAll(priorPHBar);
def LowPivots = bar >= HighestAll(priorPLBar);
def pivotHigh = if HighPivots
                then CurrMACDPivotH
                else Double.NaN;
plot PlotHline = pivotHigh;
PlotHline.EnableApproximation();
PlotHline.SetDefaultColor(GetColor(7));
PlotHline.SetStyle(Curve.SHORT_DASH);
plot pivotLow = if LowPivots
                then CurrMACDPivotL
                else Double.NaN;
pivotLow.EnableApproximation();
pivotLow.SetDefaultColor(GetColor(7));
pivotLow.SetStyle(Curve.SHORT_DASH);
plot PivotDot = if !IsNaN(pivotHigh)
                then pivotHigh
                else if !IsNaN(pivotLow)
                     then pivotLow
                     else Double.NaN;
PivotDot.SetDefaultColor(GetColor(7));
PivotDot.SetPaintingStrategy(PaintingStrategy.POINTS);
PivotDot.SetLineWeight(3);

input reversalamount =8.0;

[B]def ATRSave = if  !IsNaN(ZZ) then SMI1 else GetValue(ATRSave, 1);
def ATRchg = round(SMI1 - GetValue(ATRSave, 1));
plot ATRisUp = ATRchg >= 0;
ATRisUp.Hide();
def ATRisDown = ATRchg <= 0;
def isConf = AbsValue(ATRchg) >= reversalAmount or (IsNaN(getValue(ZZ, 1)) and getValue(isConf, 1));

plot ATRDiff= ATRchg;
ATRDiff.hide();

AddChartBubble(1, ZZ, SMI1+" "+ATRchg, if ATRisUp then Color.GREEN else if ATRisDown then Color.RED else Color.RED, ATRisUp);
[/B]
 
Last edited by a moderator:

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

@SleepyZ I'm trying to build a Elliot Wave study that is similar to the image below:

Z0Jcda6.png


I tried modifying the code above but still running into a wall.

Ruby:
AddChartBubble(showHH_HL_LH_LL_bubble,
                  ZZ,
                  if ATRisUp and (ATRSave - ATRSave1) > 0
                  then "1"
                  else if (ATRisUp > ATRisUp[1]) and ((ATRSave > ATRSave[1])  -  ATRSave1 > ATRSave1[1]) > 0
                  then "1"
                  else if ATRisUp
                  then "2"
                  else if ATRisDown and (ATRSave - ATRSave1) > 0
                  then "3"
                  else if ATRisDown and (ATRSave - ATRSave1) < 0
                  then "4"
                  else "5",
                  if ATRisUp then Color.GREEN
                  else if ATRisDown then Color.RED
                  else Color.WHITE, ATRisUp);
 
Last edited:
@SleepyZ I'm trying to build a Elliot Wave study that is similar to the image below:

Z0Jcda6.png


I tried modifying the code above but still running into a wall.

Ruby:
AddChartBubble(showHH_HL_LH_LL_bubble,
                  ZZ,
                  if ATRisUp and (ATRSave - ATRSave1) > 0
                  then "1"
                  else if (ATRisUp > ATRisUp[1]) and ((ATRSave > ATRSave[1])  -  ATRSave1 > ATRSave1[1]) > 0
                  then "1"
                  else if ATRisUp
                  then "2"
                  else if ATRisDown and (ATRSave - ATRSave1) > 0
                  then "3"
                  else if ATRisDown and (ATRSave - ATRSave1) < 0
                  then "4"
                  else "5",
                  if ATRisUp then Color.GREEN
                  else if ATRisDown then Color.RED
                  else Color.WHITE, ATRisUp);
Idk how to help you, but i had been studying Elliot waves theory, the important is the fundamental. I think is more important to predict wave 3 or C and wave 5 to take advantage.
For example in case of 1A 2B 3C pattern, an important zone to pullback and then go on wave 5 is 38.2%-50% fib retracement of wave 3 plus 100% fib exp of wave 1. Wave 4 cant exceed pivot of wave 1. Also, is important the pullback to zero of elliot wave.
But what happen if i can know the strength of wave 3 versus possible wave 4?
I though that i can create an parameter as PTI of advanced get, but still dont know how to create the code, is very simple.
The formula would be:
ATR of all wave 3 / ATR of possible wave 4.
In the moment of a possible reversion, the idea would be to take a look of this indicator and take a relation of the probabilities if are in my favor to enter the trade, but i would need to gather data and see if it work.
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
310 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