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.
 
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.
Yes I have done this with @yungtrader the Swing video shown is not what you want for this.
In Java Thinkscript you have to use several Compound values going backwards not counting highs or lows but counting where there are NOT highs and lows.
May I ask WHY and HOW you plan to use this. Your eyes can already scan and see highs and lows. In the past we used this to characterize different Volume behaviors in penny stocks as we believed those to be trickling down from large FED actions and not because we thought there was any pattern behind speculation
 

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

That is an efficient use of Zig Zag but you can't retrieve data from that

Here are examples of some of the data retrieved from that zigzag displayed in bubbles: Price at ZigZag, Price Change, Bar Count Change.

Screenshot-2023-04-01-091743.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();

#Test Data
input test = yes;
def p1 = if !IsNaN(zz)  then zz else p1[1];
def p2 = if p1 != p1[1] then p1[1] else p2[1];
def p3 = if p2 != p2[1] then p2[1] else p3[1];
def chg = AbsValue(p1 - p2);
AddLabel(Test, p1 + " " + p2[1]);
def b1 = if !IsNaN(zz)  then barnumber() else b1[1];
def b2 = if b1 != b1[1] then b1[1] else b2[1];
def b3 = if b2 != b2[1] then b2[1] else b3[1];
def bchg = AbsValue(b1 - b2);
AddLabel(test, b1 + " " + b2[1]);
plot pp = if !test then double.nan else barnumber();
pp.SetPaintingStrategy(PaintingStrategy.VALUES_ABOVE);

input showbubbles = yes;
AddChartBubble(showbubbles and zz, if sh then high else low, (if sh then high else low) + "\n" + chg +"\n"+bchg, Color.WHITE, if sh then yes else no);
 
Here are examples of some of the data retrieved from that zigzag displayed in bubbles: Price at ZigZag, Price Change, Bar Count Change.

Okay good.
I have heard some people will time legs amongst correlated assets or will notice how many legs particular events will take such as "Fed meeting might have 2 legs" that's what I'm curious about.
If they may want to run this multi time frame.
 
This adjusts the zigzag but leaves the bar coloring the same as before.
Great work and thank you. I've adjusted the bar definitions according to my understanding of the leg counting rules. Here's the adjusted version.

Code:
#Gann Bar Counting Replica v2
# Created by SleepyZ
# v2 Shimi adjusted definitions of bar types

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();
 
Last edited:
mod note:
The candle painting portion of this study does not repaint.
https://usethinkscript.com/threads/leg-counting-indicator.15026/page-2#post-122951
It paints up and down bars accurately.

The zigzag script employs the use of "future bars" to enhance its accuracy. The script gathers information from the future then retroactively repaints its calculations.

You can identify future bar calculations. They use [-1] in the code.
The pivots in this script are determined:
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;


The zigzags look so good, because it uses future bars to determine the pivots - that is the repainting at work!
@MatthewA we would never make an MTF of a repainting indicator.
Repainting a Repainter is contra-indicated.
 
Last edited:
Great work and thank you. I've adjusted the bar definitions according to my understanding of the leg counting rules. Here's the adjusted version.

Code:
#Gann Bar Counting Replica v2
# Created by SleepyZ
# v2 Shimi adjusted definitions of bar types

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();
Oh I see you want to have the equal low and a HH as an up bar and equal high bar with a LL as a down bar. I added this to the original script that @SleepyZ posted as I like the ability to turn off the cloud.
 
Also in this example you can see the difference between Gann and a traditional Zig-Zag. The actual high pivot was discounted as the pivot bar was on outside bar. Not sure what happened at the lower yellow level as the inside bar should be discounted and the bar to the left should have been marked. The color looks correct but not the plot lines Let me know @iAskQs as I just started to look at the Gann Bar Counting system.

TzWNg1Pl_o.png
 
Last edited:
Also in this example you can see the difference between Gann and a traditional Zig-Zag. The actual high pivot was discounted as the pivot bar was on outside bar. Not sure what happened at the lower yellow level as the inside bar should be discounted and the bar to the left should have been marked. The color looks correct but not the plot lines Let me know @iAskQs as I just started to look at the Gann Bar Counting system.

TzWNg1Pl_t.jpg
Current version of the code, the color bars accurately show leg counting rules and zigzag anchors at last bar in upleg there in your picture, which is accurate. It just looks a bit weird because it's not the actual highest price of the price action in your image because of the outside bar.
 
iAsk - thanks for starting this thread. I just watched Quant's Gann Counting video last night and came here to search for a solution.

Are you still using the v2 code posted above or have you improved it? I noticed in the video that reverting to the previous bar type after an inside/outside bar, he draws (albeit manually) the pivot on the bar that actually had the pivot high/low. Have you figured out a way to do this automatically?

1688689494388.png
 
iAsk - thanks for starting this thread. I just watched Quant's Gann Counting video last night and came here to search for a solution.

Are you still using the v2 code posted above or have you improved it? I noticed in the video that reverting to the previous bar type after an inside/outside bar, he draws (albeit manually) the pivot on the bar that actually had the pivot high/low. Have you figured out a way to do this automatically?

View attachment 19074
V2 is the most current version. It accurately draws on the pivot high/low according to Gann leg counting rules instead of the actual high/low, when they are different. In the image you provided, the leg pivots and actual hi/low seem to coincide, so there's no difference.
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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