Learn to build a hierarchical market-structure model in Python using the Directional Change algorithm with ATR thresholds to group turning points into multiple significance levels.
Key Takeaways
- ATR-based Directional Change reliably extracts alternating turning points from raw OHLC data without fixed percentage thresholds.
- Recursively upgrading surrounded extremes produces a multi-scale hierarchy of market structure.
- Most first-level extremes are noise; significance emerges at higher levels that require more hindsight to confirm.
- The ATR lookback is a practical parameter to set by common sense rather than heavy optimization.
What the video covers
- Introduces a method to group market turning points into levels of significance, creating a hierarchy that quantifies market structure at multiple scales.
- Uses the Directional Change algorithm, reimplemented with an ATR-based confirmation threshold, to find alternating tops and bottoms directly from candlestick data.
- Tracks the highest high since the last bottom (pending top) and the lowest low since the last top (pending bottom), confirming an extreme when price reverses past the ATR threshold.
- Stores each local extreme in a data class containing type, price, index, timestamp, and confirmation details, since local extremes are only identifiable in hindsight.
- Computes a rolling Average True Range over a lookback window (24 hours of one-minute Bitcoin data in the example), updating the sum incrementally for efficiency.
- Builds higher levels recursively: when a top is surrounded by lower tops (or a bottom by higher bottoms), the surrounding extreme is upgraded to the next level.
- Explains that the first-level extremes are mostly noise, while higher-level extremes are more pronounced and represent significant market structure.
- Provides a HierarchicalExtremes class that takes the number of levels and the ATR lookback, loops through data calling update on each bar, and collects extremes per level.
- Handles edge cases such as two consecutive tops without an intervening bottom by upgrading the lowest bottom between them.
- Notes that the lookback should be chosen with common sense rather than optimized, and suggests future videos may apply the structure to trading signals or entry rules.
Chapters
- 00:00Introduction to Hierarchical Market Structure
- 00:37Finding Alternating Turning Points with Directional Change
- 02:01Confirming Extremes with ATR Thresholds
- 03:46Data Class and Extreme Tracking
- 04:00Building the Hierarchical Extremes Class
- 05:36Computing the Rolling Average True Range
- 07:13Recursive Upgrading of Surrounded Extremes
- 11:12Handling Edge Cases and Closing Remarks
Full Transcript — Download SRT & Markdown
Speaker A
in this video I'll show a method to group market turning points into different levels of significance, allowing us to create a hierarchy of turning points so we can organize and quantify market structure at multiple scales.
Speaker A
alternating turning points in the market I will use the directional change algorithm to identify them I covered directional change in my previous video but I'll explain it here as I've reimplemented the algorithm in a different way for this video directional
Speaker A
To start, we need to find alternating turning points in the market. I will use the Directional Change algorithm to identify them. I covered Directional Change in my previous video, but I'll explain it here as I've reimplemented the algorithm in a different way for this video.
Speaker A
top to do this we keep track of the highest price scen since the bottom drawn with an orange line This highest price is the pending Next Top when the next candle comes in we check if it had a high that was higher than the pending
Speaker A
Directional Change always finds alternating extremes from top to bottom and bottom to top. The white lines connect each confirmed extreme.
Speaker A
continue again no new high and not low enough to confirm on this next candle we did get a higher high so we update the pending High the market continues making higher highs for the next few bars so we
Speaker A
At this point in time, the last confirmed extreme was a bottom, so we are currently in an upward move, looking for the next top.
Speaker A
procedure but symmetrically for the lows we keep track of the lowest price since the top this will be our pending bottom we update it whenever a lower price is found if a new low isn't found we check if the high of the candle is above the
Speaker A
To do this, we keep track of the highest price seen since the bottom, drawn with an orange line. This highest price is the pending next top.
Speaker A
change algorithm but now I'll go over the rest of the directional change code I made this data class to store all the information about a local extreme we store the type the price the index and the time stamp of the extreme because we
Speaker A
When the next candle comes in, we check if it had a high that was higher than the pending top. Here it did not, so we now check if the low of the candle was below the confirmation threshold, drawn in blue. The confirmation threshold is the highest price minus the Average True Range. The low wasn't low enough to confirm, so we continue.
Speaker A
true range here I'm using one minute Bitcoin data so I opted for a 24-hour look back I wouldn't optimize the look back or think about it too much just use common sense and pick a reasonably long look back for your data to use the class
Speaker A
Again, no new high and not low enough to confirm. On this next candle, we did get a higher high, so we update the pending high. The market continues making higher highs for the next few bars, so we continue to update the pending high as they come in.
Speaker A
we have a full look back window we compute the sum of average true range values then on every update after that we add the newest true range value and subtract the oldest true range value to maintain the sum then we divide the sum
Speaker A
Finally, we don't make a new high, and we find a low that goes below the confirmation threshold. We can now confirm the top.
Speaker A
creating an instance of that data class and appending it to the list of extremes with this ATR directional change we can find small small scale tops and bottoms directly from Candlestick data most of these extremes are insignificant and
Speaker A
From here, we do the same procedure but symmetrically for the lows. We keep track of the lowest price since the top. This will be our pending bottom, and we update it whenever a lower price is found. If a new low isn't found, we check if the high of the candle is above the confirmation threshold, the lowest price plus the Average True Range. Here the high is above the threshold, so we can confirm the bottom.
Speaker A
bottoms these topss and bottoms are more pronounced than others a new level or scale of extremes if we find them all and connect to the dots we get this there can be situations where we have two tops in a row without a bottom
Speaker A
Now we set up for the next top and start again.
Speaker A
do it again upgrade the tops surrounded by lower tops and upgrade the bottom surrounded by higher bottoms and we could go again and again but I think you get the point we're recursively creating different scales of Market structure
Speaker A
The code on the left is the heart of the Directional Change algorithm. Now I'll go over the rest of the Directional Change code.
Speaker A
pass it the number of levels or scales to track and the ATR look back just like the directional change class we just Loop through the data and call the update function on each new bar under the hood the hierarchical extremes class
Speaker A
I made this data class to store all the information about a local extreme. We store the type, the price, the index, and the timestamp of the extreme, because we can only identify local tops and bottoms with hindsight. I also store the index and timestamp of when the extreme is confirmed.
Speaker A
length of extremes before and after the update to see if we have a new extreme confirmed these functions are called whenever a new extreme at a given level is confirmed we pass the level of the newly confirmed extreme then the index
Speaker A
I implemented the Directional Change algorithm using this class. We have to pick a lookback for the Average True Range. Here I'm using one-minute Bitcoin data, so I opted for a 24-hour lookback. I wouldn't optimize the lookback or think about it too much. Just use common sense and pick a reasonably long lookback for your data.
Speaker A
it could potentially add a level one top it is called every time a new local top is confirmed this function is recursive this conditional return will break the recursive Loop once the specif number of levels is reached first we get the newly
Speaker A
To use the class, we loop through all the data, and on each new bar call the class's update function. The update function starts by computing the current Average True Range value. If we have less data than the ATR lookback, we do nothing.
Speaker A
is less than four we don't yet have three tops to work with we then get the previous top the previous top is the one that could be upgraded the newly confirmed top needs to be lower than the previous next we find the previous
Speaker A
The first time we have a full lookback window, we compute the sum of True Range values. Then on every update after that, we add the newest True Range value and subtract the oldest to maintain the sum. Then we divide the sum by the window to get the Average True Range.
Speaker A
to deal with the case of equal prices most of the time this Loop will only have one iteration this prior top cannot be greater than the one we're considering upgrading we only check tops after the previous extreme at the next
Speaker A
Then we get to the main part of the algorithm, what I showed during the visualization. When new extremes are confirmed, I call this new extreme function. It's just a wrapper for creating an instance of that data class and appending it to the list of extremes.
Speaker A
we've made it here in the code we can now upgrade this top we copy the local extreme from the previous level and we update the confirmation attributes as confirming an extreme at a higher level takes more hindsight there can be times
Speaker A
With this ATR Directional Change, we can find small-scale tops and bottoms directly from candlestick data. Most of these extremes are insignificant and possibly just noise. But let's hide the price and just look at the extremes.
Speaker A
upgrade then append that upgraded bottom first and call the new bottom function then after checking for that situation we append the upgraded top and recursively call this function again to check at the next level the new bottom function is nearly identical to what we
Speaker A
So we can take it a step further. What if we found a top surrounded by two lower tops, or a bottom surrounded by two higher bottoms? These tops and bottoms are more pronounced than others, a new level or scale of extremes. If we find them all and connect the dots, we get this.
Speaker A
additional parameter for the type one for Tops negative one for bottoms and they made a helper function that flips the comparison depending on the type we're dealing with here's the algorithm running tracking three levels the white is level zero the directional change
Speaker A
There can be situations where we have two tops in a row without a bottom between them. So if we encounter a situation like this, I'll upgrade the lowest bottom between the two tops.
Speaker A
lines are drawn quite a while after the actual top or bottom happens I got this idea from the chapter on Market structure from Larry Williams book long-term secrets to short-term trading my implementation deviates quite a bit from what the book describes but the
Speaker A
Now we can zoom out and hide the previous level of extremes. We are left with something very similar, and we can just do it again: upgrade the tops surrounded by lower tops and upgrade the bottoms surrounded by higher bottoms. We could go again and again, but I think you get the point. We're recursively creating different scales of market structure.
Speaker A
or entry rules and at the very least it looks cool I have a few ideas for future videos that may use this so you should subscribe like the video and comment several times to boost my engagement that's it for this one thanks for
Speaker A
I'll refer to the tops and bottoms produced by Directional Change as level zero, and levels one, two, three, and so on will be the tops and bottoms found from the prior level.
Topics:directional changemarket structurealgorithmic tradingaverage true rangepythonturning pointshierarchical extremesbitcointechnical analysisswing detection











