Need help understanding some code in the zigzagHighLow indicator

muscleman

New member
I am learning how thinkscript works, and just realized that I can access the prior bar's variable values. I started reading the built in zigzagHighLow indicator code and there are a few pieces that are confusing to me. Can someone please help?

def state = {default init, undefined, uptrend, downtrend};
def maxPriceH;
def minPriceL;
def prevMaxH = GetValue(maxPriceH, 1); -- What happens when this code runs on the 1st bar on the left of the chart? maxPriceH would have been unavailable
def prevMinL = GetValue(minPriceL, 1);

if GetValue(state, 1) == GetValue(state.init, 0) { -- What does this mean? I am confused about GetValue(state.init, 0) vs GetValue(state, 0). I think the latter gets the current bar's state's value. But not sure what the former means.

def newState = GetValue(state, 0) != GetValue(state, 1); -- This time it compared GetValue(state, 0) with GetValue(state, 1); instead of GetValue(state.init, 0). What's the difference?


Thank you so much!
 
Solution
I am learning how thinkscript works, and just realized that I can access the prior bar's variable values. I started reading the built in zigzagHighLow indicator code and there are a few pieces that are confusing to me. Can someone please help?

def state = {default init, undefined, uptrend, downtrend};
def maxPriceH;
def minPriceL;
def prevMaxH = GetValue(maxPriceH, 1); -- What happens when this code runs on the 1st bar on the left of the chart? maxPriceH would have been unavailable
def prevMinL = GetValue(minPriceL, 1);

if GetValue(state, 1) == GetValue(state.init, 0) { -- What does this mean? I am confused about GetValue(state.init, 0) vs GetValue(state, 0). I think the latter gets the current bar's state's value. But not sure...
I am learning how thinkscript works, and just realized that I can access the prior bar's variable values. I started reading the built in zigzagHighLow indicator code and there are a few pieces that are confusing to me. Can someone please help?

def state = {default init, undefined, uptrend, downtrend};
def maxPriceH;
def minPriceL;
def prevMaxH = GetValue(maxPriceH, 1); -- What happens when this code runs on the 1st bar on the left of the chart? maxPriceH would have been unavailable
def prevMinL = GetValue(minPriceL, 1);

if GetValue(state, 1) == GetValue(state.init, 0) { -- What does this mean? I am confused about GetValue(state.init, 0) vs GetValue(state, 0). I think the latter gets the current bar's state's value. But not sure what the former means.

def newState = GetValue(state, 0) != GetValue(state, 1); -- This time it compared GetValue(state, 0) with GetValue(state, 1); instead of GetValue(state.init, 0). What's the difference?


Thank you so much!



def prevMaxH = GetValue(maxPriceH, 1);
-- What happens when this code runs on the 1st bar on the left of the chart?
maxPriceH would have been unavailable


it is reading high.
basic price data exists before the first bar, so it will have a value on bar #1.
this attribute allows averages to have a value on the first bar.


def state = {default init, undefined, uptrend, downtrend};
init is set as default, so on the first bar, the .init section code runs.


if GetValue(state, 1) == GetValue(state.init, 0) {
maxPriceH = priceH;

this sets maxPriceH = priceH
priceH is equal to high.
high is a fundamental data, that exists before bar #1.
\https://tlc.thinkorswim.com/center/reference/thinkScript/Functions/Fundamentals/high

if maxPriceH was set to some formula, the data probably wouldn't exist before bar #1.

-------------------


if GetValue(state, 1) == GetValue(state.init, 0) {
-- What does this mean? I am confused about GetValue(state.init, 0) vs GetValue(state, 0).
I think the latter gets the current bar's state's value. But not sure what the former means.


def state = {default init, undefined, uptrend, downtrend};

testing values of a variable , with text values like this, is a little different.
a simple method would be, ( the variable and the variable.value )

def g = if state == state.init then 1 else 0



getvalue(data , offset) reads a variable value , 'offset' bars from the current bar. just like date[offset].
sometimes data[offset] may not work, but getvalue() will.
getvalue() is used within fold loops.


if GetValue(state, 1) == GetValue(state.init, 0)
if the previous value of state ( 1 bar ago) is equal to the current value [0], which is state.init , then do stuff


----------------------

def newState = GetValue(state, 0) != GetValue(state, 1);
-- This time it compared GetValue(state, 0) with GetValue(state, 1);
instead of GetValue(state.init, 0). What's the difference?


GetValue(state, 0) , ,0 means to read the current bar
GetValue(state, 1) , ,1 means to read the value from 1 bar ago, the previous bar.
!= means not equal

is the current value of state not equal to the previous bar value of state ?
if true then do stuff


------------------------------


one way to figure out what a study is doing, is to add a bubble to display some values.

i copied the zigzag study,
added a bubble at the end of it,
to display some variable values on every bar.

experiment with this , change it, add some variables, see what happens.
if t1 or t2 is true, it will cause the bubble to change color, and to flip the bubble to be above the bars, instead of below them.


def t1 = (GetValue(state, 1) == GetValue(state.init, 0));
def t2 = (GetValue(state, 0) != GetValue(state, 1));

addchartbubble(1, low*0.995,
state + " stat\n" +
t1 + "\n" +
t2 + "\n" +
prevMaxH + " pmax\n"
, (if t1 then color.yellow else if t2 then color.magenta else color.gray)
, (if t1 or t2 then 1 else 0));



def state = {default init, undefined, uptrend, downtrend};

you may notice that in the bubble, the state variable doesn't display any text. it is a number. it is a number of the index that matches a value in the original formula.
if state is set to uptrend, that is the 3rd choice, so state will = 3.

def state = {default init, undefined, uptrend, downtrend};



Code:
# zigzag_info

#https://usethinkscript.com/threads/need-help-understanding-some-code-in-the-zigzaghighlow-indicator.14322/
#Need help understanding some code in the zigzagHighLow indicator

# zigzagHighLow 
# TD Ameritrade IP Company, Inc. (c) 2013-2022

input priceH = high;
input priceL = low;
input percentageReversal = 5.0;
input absoluteReversal = 0.0;
input atrLength = 5;
input atrReversal = 1.5;
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(high, close, low), atrLength) / close * 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();

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


# def state = {default init, undefined, uptrend, downtrend};

def t1 = (GetValue(state, 1) == GetValue(state.init, 0));
def t2 = (GetValue(state, 0) != GetValue(state, 1));

addchartbubble(1, low*0.995,
state + " stat\n" +
t1 + "\n" +
t2 + "\n" +
prevMaxH + " pmax\n" 
, (if t1 then color.yellow else if t2 then color.magenta else color.gray)
, (if t1 or t2 then 1 else 0));
#
 
Solution

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
574 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