Leg Counting Indicator

iAskQs

Member
I've been trying to write an indicator that will draw lines over price action (similar to zigzag) using simple bar (candle) counting rules. Despite my efforts, I've been unable to do so. Can anyone help me out with the code? Leg counting rules are as follows:

1. Upbar is up direction
2. Downbar is down direction
3. Inside and Outside bars continue direction of the previous bar.

Here is the code I have for the actual candles:

Def UpBar = high > high[1] and low >= low[1];
Def DownBar = low < low[1] and high <= high[1];
def insidebar = high <= high[1] and low >= low[1];
def outsidebar= high >= high[1] and low <= low[1];

I can't figure out the rest (everything I've thrown together has errors and is not worth adding here). Btw, here's a nice video on bar counting (and the specific logic I've mentioned above)
.

I don't currently care about labeling the actual leg counts. I just want the line to follow the legs. Thanks in advance for your help.
 

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

I believe the actual pattern you're looking for is subjective. You would need to start with what is given to you first. Then recognize the pattern based on the price action.

I could be wrong, but that's what I'm getting from the video.
 
I believe the actual pattern you're looking for is subjective. You would need to start with what is given to you first. Then recognize the pattern based on the price action.

I could be wrong, but that's what I'm getting from the video.
It's a purely objective leg counting approach. There is no room for subjectivity at all. Up leg continues if upbar or insidebar or outside bar. Down leg continues if downbar, insidebar, or outside bar. Upbar starts up leg and downbar starts downleg.
 
I've been trying to write an indicator that will draw lines over price action (similar to zigzag) using simple bar (candle) counting rules. Despite my efforts, I've been unable to do so. Can anyone help me out with the code? Leg counting rules are as follows:

1. Upbar is up direction
2. Downbar is down direction
3. Inside and Outside bars continue direction of the previous bar.

Here is the code I have for the actual candles:

Def UpBar = high > high[1] and low >= low[1];
Def DownBar = low < low[1] and high <= high[1];
def insidebar = high < high[1] and low > low[1];
def outsidebar= high >= high[1] and low <= low[1];

I can't figure out the rest (everything I've thrown together has errors and is not worth adding here). Btw, here's a nice video on bar counting (and the specific logic I've mentioned above)
.

I don't currently care about labeling the actual leg counts. I just want the line to follow the legs. Thanks in advance for your help.

i am curious about this and will look at it friday. (can't watch video now)

your rules, 1 & 2 , are not rules. they mean nothing.
up is up...?
what defines up? open to close? close to close?...
 
i am curious about this and will look at it friday. (can't watch video now)

your rules, 1 & 2 , are not rules. they mean nothing.
up is up...?
what defines up? open to close? close to close?...
I've defined the code for upbar and downbar in the original post (code is also in the video). Here's an image that may clarify for you: it's a 2m chart of NQ today (PST on the chart). The numbers mark each leg change according to the rules.

zrB9W56.png
 
I've defined the code for upbar and downbar in the original post (code is also in the video). Here's an image that may clarify for you: it's a 2m chart of NQ today (PST on the chart). The numbers mark each leg change according to the rules.

'up direction', is not a definition, it is not a rule. 'up' is not a price parameter on a chart.
you didn't understand my previous post, read it again.

will look at later
 
'up direction', is not a definition, it is not a rule. 'up' is not a price parameter on a chart.
you didn't understand my previous post, read it again.

will look at later
In the original post, I asked for a zigzag like line drawing indicator according to these rules. Upbar= Up direction means that when there's an upbar (as defined by the code I provided), the line of the zigzag should go in the up direction. Consider it an up leg if it's easier for you to understand it that way. And downbar = down direction, would be down leg. Since I'm discussing a line being drawn over price on the chart, and it only goes up and down (like a zigzag indicator), I've used terms up direction and down direction. Take a look at the image I posted and the video I posted, which may help in understanding the rules.

Here's an example of the same 2m chart I posted above with actual lines drawn over it as I would like the indicator to do (I did it manually with the trendline drawing tool).

3e28IJs.png
 
Last edited:
In the original post, I asked for a zigzag like line drawing indicator according to these rules. Upbar= Up direction means that when there's an upbar (as defined by the code I provided), the line of the zigzag should go in the up direction. Consider it an up leg if it's easier for you to understand it that way. And downbar = down direction, would be down leg. Since I'm discussing a line being drawn over price on the chart, and it only goes up and down (like a zigzag indicator), I've used terms up direction and down direction. Take a look at the image I posted and the video I posted, which may help in understanding the rules.

Here's an example of the same 2m chart I posted above with actual lines drawn over it as I would like the indicator to do (I did it manually with the trendline drawing tool).

3e28IJs.png

wow, i am very sorry for coming across as a %$#%##. i must be tired or meds or..... something, as i was incapable of understanding your post#1.
( skipped right over Def UpBar = high > high[1] and low >= low[1]; )

watching video...
 
In the original post, I asked for a zigzag like line drawing indicator according to these rules. Upbar= Up direction means that when there's an upbar (as defined by the code I provided), the line of the zigzag should go in the up direction. Consider it an up leg if it's easier for you to understand it that way. And downbar = down direction, would be down leg. Since I'm discussing a line being drawn over price on the chart, and it only goes up and down (like a zigzag indicator), I've used terms up direction and down direction. Take a look at the image I posted and the video I posted, which may help in understanding the rules.

Here's an example of the same 2m chart I posted above with actual lines drawn over it as I would like the indicator to do (I did it manually with the trendline drawing tool).

3e28IJs.png

short answer - how to plot zigzag lines

create a variable that has price numbers for the peaks and valleys, and n/a's for all the other bars.
then plot it , using EnableApproximation()

once it starts plotting across the chart. it can't be turned off and on

https://tlc.thinkorswim.com/center/reference/thinkScript/Functions/Look---Feel/EnableApproximation
 
wow, i am very sorry for coming across as a %$#%##. i must be tired or meds or..... something, as i was incapable of understanding your post#1.
( skipped right over Def UpBar = high > high[1] and low >= low[1]; )

watching video...
No worries at all. I appreciate your input and attempt to help. You caused me to create several images that will help everyone else understand what I'm requesting as well.
 
short answer - how to plot zigzag lines

create a variable that has price numbers for the peaks and valleys, and n/a's for all the other bars.
then plot it , using EnableApproximation()

once it starts plotting across the chart. it can't be turned off and on

https://tlc.thinkorswim.com/center/reference/thinkScript/Functions/Look---Feel/EnableApproximation
Yes, this is essentially the challenge for me. I don't know enough coding to create this. As you and @BenTen have suggested, before I even started this thread, I tried to alter a zigzag indicator to suit my needs but I got tripped up in the code.
 
Yes, this is essentially the challenge for me. I don't know enough coding to create this. As you and @BenTen have suggested, before I even started this thread, I tried to alter a zigzag indicator to suit my needs but I got tripped up in the code.

See if this helps. It includes a price color option and zigzag option. The price color will follow an upbar or downbar (I used a different definition for each than was in the video and your code, but I think it seems to work) until it changes from one to the other. The inside or outside bars will therefore follow the upbar or downbar that preceded these.

The zigzag uses the price color logic.


Screenshot-2023-03-31-161409.png

Code:
#Gann Bar Counting Replica

input showpricecolor = yes;
input showzigzag     = yes;

def UpBar   =  high > high[1] and low > low[1];
def DownBar = low < low[1] and high < high[1];
def insidebar  = high < high[1] and low > low[1];
def outsidebar = high >= high[1] and low < low[-1];

#Price Color
def x = if UpBar then 1 else if DownBar then 0 else x[1];

AssignPriceColor(if !showpricecolor then color.current       
                 else if x > 0 then Color.CYAN else Color.MAGENTA);

#ZigZag
def sh  = if x[1] == 1 and x == 0 then 1 else 0;
def sl  = if x[1] == 0 and x == 1 then 1 else 0;
def sh1 = if (sh) then high else double.nan;
def sl1 = if (sl) then low else double.nan;

plot zz = if !showzigzag then double.nan       
          else if sh>sl then sh1 else sl1;
zz.EnableApproximation();
 
Last edited:
See if this helps. It includes a price color option and zigzag option. The price color will follow an upbar or downbar (I used a different definition for each than was in the video and your code, but I think it seems to work) until it changes from one to the other. The inside or outside bars will therefore follow the upbar or downbar that preceded these.

The zigzag uses the price color logic.
This is great and exactly what I was trying to figure out but couldn't; I appreciate your help. One thing I can try to figure out is how to get the lines to snap to and turn around at the swing high point of the price action, even though the leg count may not shift until the next bar or few bars later. For example, in your image, at 10:10 is the swing low in price action but leg doesn't shift till next candle and that's where zigzag line attaches to low of that 10:15 candle. I would like it to attach to the low of the 10:10 candle. I don't know how but will try to figure it out.

In the meantime, I created a lower study that shows the legs in case some traders don't want zigzags over their candles.
Code:
#Shimi's Price Action Legs, according to Gann bar counting rules.  Upbar starts and continues up leg.  Downbar starts and continues down leg.  Inside and outside bars continue current leg.

declare lower;

# Definitions
def UpBar = high > high[1] and low >= low[1];
def DownBar = low < low[1] and high <= high[1];
def Signal = if UpBar then 1 else if DownBar then -1 else signal[1] ;

# Calculate the cumulative total of the signal
def CumulativeSignal = CompoundValue(1, if BarNumber() == 1 then Signal else Sum(Signal, 1) + GetValue(CumulativeSignal, 1), 0);

# Plot the signal line
plot SignalLine = CumulativeSignal;
SignalLine.AssignValueColor(if CumulativeSignal > CumulativeSignal[1] then Color.green else if CumulativeSignal < CumulativeSignal[1] then Color.red else Color.white);

Here's a pic of what it looks like, with my manually drawn trendlines to mimic Gann legs in the upper area for comparison.


j0JCMF4.png
 

I've been trying to write an indicator that will draw lines over price action (similar to zigzag) using simple bar (candle) counting rules. Despite my efforts, I've been unable to do so. Can anyone help me out with the code? Leg counting rules are as follows:

1. Upbar is up direction
2. Downbar is down direction
3. Inside and Outside bars continue direction of the previous bar.

Here is the code I have for the actual candles:

Def UpBar = high > high[1] and low >= low[1];
Def DownBar = low < low[1] and high <= high[1];
def insidebar = high < high[1] and low > low[1];
def outsidebar= high >= high[1] and low <= low[1];

I can't figure out the rest (everything I've thrown together has errors and is not worth adding here). Btw, here's a nice video on bar counting (and the specific logic I've mentioned above)
.

I don't currently care about labeling the actual leg counts. I just want the line to follow the legs. Thanks in advance for your help.
Have you found any existing code like on TradingView etc? I watched the video and it aligns with how I trade trend reversals. It is definitely different than how pivots are calculated and is 100% mechanical.

I've defined the code for upbar and downbar in the original post (code is also in the video). Here's an image that may clarify for you: it's a 2m chart of NQ today (PST on the chart). The numbers mark each leg change according to the rules.

zrB9W56.png

@iAskQs this video walks through coding it you would have to adapt to TOS
 
Last edited by a moderator:
Coding the Indicator

• Down Bar: Low<Low [-1] and High<High [-1]
• Up Bar: High>High [-1] and Low>Low [-1]
• Inside Bar: High<High [-1] and Low>Low [-1]

• Everything else is an Outside Bar
○ High>=High [-1] and Low<=Low [-1]

Labels
Up Bar = Up | Down Bar = Down
Inside Bar = Same as last Bar

Outside Bar = Same as last Bar
 
This is great and exactly what I was trying to figure out but couldn't; I appreciate your help. One thing I can try to figure out is how to get the lines to snap to and turn around at the swing high point of the price action, even though the leg count may not shift until the next bar or few bars later. For example, in your image, at 10:10 is the swing low in price action but leg doesn't shift till next candle and that's where zigzag line attaches to low of that 10:15 candle. I would like it to attach to the low of the 10:10 candle. I don't know how but will try to figure it out.

In the meantime, I created a lower study that shows the legs in case some traders don't want zigzags over their candles.
Code:
#Shimi's Price Action Legs, according to Gann bar counting rules.  Upbar starts and continues up leg.  Downbar starts and continues down leg.  Inside and outside bars continue current leg.

declare lower;

# Definitions
def UpBar = high > high[1] and low >= low[1];
def DownBar = low < low[1] and high <= high[1];
def Signal = if UpBar then 1 else if DownBar then -1 else signal[1] ;

# Calculate the cumulative total of the signal
def CumulativeSignal = CompoundValue(1, if BarNumber() == 1 then Signal else Sum(Signal, 1) + GetValue(CumulativeSignal, 1), 0);

# Plot the signal line
plot SignalLine = CumulativeSignal;
SignalLine.AssignValueColor(if CumulativeSignal > CumulativeSignal[1] then Color.green else if CumulativeSignal < CumulativeSignal[1] then Color.red else Color.white);

Here's a pic of what it looks like, with my manually drawn trendlines to mimic Gann legs in the upper area for comparison.


j0JCMF4.png

This adjusts the zigzag but leaves the bar coloring the same as before.

Screenshot-2023-04-01-081350.png
Code:
#Gann Bar Counting Replica

input showpricecolor = yes;
input showzigzag     = yes;

def UpBar   =  high > high[1] and low > low[1];
def DownBar = low < low[1] and high < high[1];
def insidebar  = high < high[1] and low > low[1];
def outsidebar = high >= high[1] and low < low[-1];

#Price Color
def x = if UpBar then 1 else if DownBar then 0 else x[1];

AssignPriceColor(if !showpricecolor then color.current       
                 else if x > 0 then Color.CYAN else Color.MAGENTA);

#ZigZag
def sh  = if x[-1] == 0 and x == 1 then 1 else 0;
def sl  = if x[-1] == 1 and x == 0 then 1 else 0;
def sh1 = if (sh) then high else double.nan;
def sl1 = if (sl) then low else double.nan;

plot zz = if !showzigzag then double.nan       
          else if sh>sl then sh1 else sl1;
zz.EnableApproximation();
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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