[SCL] True Market Structure

See market structure at a glance with Higher Highs and Lower Lows. Bullish / Bearish /Ranging market bias is automatically derived. Optionally get alerted for breaks in market structure. Uses true Local Highs/Lows instead of simply the highest/lowest "pivot" for x bars. Can be useful as a support for learning market structure or for alerts for a change in structure while you're not at the computer.
版本注释: Fixed some broken logic. It confirms Highs and Lows better now. This is why you keep the debug handy, kids.
版本注释: Updated comments to match the fixed functions.
Note to the Pinescripters: I was having problems accessing past states of variables within functions using , which is why you'll see the workaround with variable_1, passing in a hardcoded .
版本注释: Fixed a case where we could confirm fractals out of sequence.
Added a new setting: "Use Short Label Text"; off by default. This makes the label text much shorter and people will probably prefer it after they get used to the indicator.
Removed the setting to display market bias in labels, because it's superseded by the new setting.

Note about rogue wicks
If we get a candle with a high wick but a normal close, after a Local High but before that Local High is confirmed by the appropriate close, the high wick will never be counted as a Local High, even if it ends up coming in sequence after a Local Low.
Basically, candidates for Local Highs are only accepted AFTER the previous Local High is confirmed (or on the very same bar), and I'm probably going to keep it that way. Not only would it be extremely time-consuming to code and test doing it differently, I'm not convinced it would be the right thing to do.
Same thing goes for Lows.
This case is quite rare (I don't even have a screenshot handy). Also, note that forcing confirmations with a low number could change this behaviour and confirm the wick.
For any other cases where you think the script isn't picking out Highs and Lows correctly, please do drop a comment with a screenshot of the chart and your current settings.
版本注释: Fixed the spelling on the short label text. Hat tip to @crossc for letting me know
版本注释: Simplified the display mode inputs. There's only one colour scheme now, which is according to market bias. I think that's more useful than just red/green.

Added a minimalist mode, because having all the labels displayed when using the indicator regularly cluttered up the chart a bit. This mode only shows the most recent High and Low (the code that deletes old labels might be interesting if you need to script with labels printed in arbitrary locations). The defaults are the same as before.
版本注释: Added a new display option. You can show only those breaks in market structure that are reversals, or you can show both reversal and continuation breaks.

For fun: find an asset that trends nicely and hide everything except reversal breaks.
版本注释: Attempting to fix a user-reported issue with Bearish Breaks.
版本注释: + Added a label to show the current market bias, with three settings: Short Text, Full Text, and Off. The label is coloured according to the bias.

+ Added an option to fill the background with the market bias colour.

With the range of visualisations now available, you can see market structure in as much or as little detail as you want, with the best chance of avoiding visual conflict with whatever other indicators you might be using.

+ Fixed the market bias to be set properly on breaks. Needed now it's more visible!

+ Increased the maximum number of labels shown to 100. If you want to do historical analysis, you can increase this number up to the system limit of 500 manually.

+ Added lines that show where price needs to close in order to flip market bias to bullish or bearish. You *could* also add lines to show the close needed to flip to ranging. If people want this feature I might add it.
NOTE: This feature repaints in the sense that it extends the line back in time to the High or Low that set that price.
Importantly, it can change intra-candle. A candle close is needed to confirm a Local High/Low, and while a candle is open it uses the current price as the "working close". So if we are in the very first candle of a changed market bias, the most recent Local High/Low is actually waiting for the current candle to close in order to be confirmed. If the current candle then reverses, the line will revert back to using the previously confirmed High/Low. Although this looks jarring when it happens, it's technically correct. Coding it to always use a High/Low that's confirmed on close instead is probably not worth the effort and would be less useful most of the time. The general principle is the same as for most indicators - for the current candle, Things Can Change until it's closed.

If you have questions, please read the full description of the settings in the top part of the script first.
版本注释: New feature: Option to display the break of the line that shows where a close would need to be in order to break the current market structure. Thanks to user @Meshidorin for the suggestion.
Updated the colours so that you won't get compilation warnings from TradingView because of the impending deprecation of "transp".
版本注释: Thanks to @dalescher for a fix to the price on the label for the price needed for a bullish or bearish break
版本注释: Minor update: Fixed the alert text for Breaks.

本着真正的TradingView精神,该脚本的作者将其开源发布,因此交易者可以理解和验证它。为作者加油! 您可以免费使用它,但是在发布中重复使用此代码受网站规则的约束。您可以收藏它以在图表上使用。



Tops! Thanks for making and sharing openly. Was using it to try and identify "contraction zone".
+1 回复
@Things, Thank you. Interesting use case that I hadn't considered. But I guess if you get a big range and inside that you get highs and lows you could still consider that as ranging behaviour and not truly bullish or bearish market structure. Bit tricky to formalise it.
+1 回复
Things SimpleCryptoLife
@SimpleCryptoLife, Indeed. Was working on consecutive LH,HL etc and/or a fractal type arrangement i.e. LH < LH and HL > HL . Admittedly is difficult to do, and your ranging condition worked somewhat.
@Things, I wonder if simply multiple EMAs tightening would be an easier way to look for contraction zones. It's quite simple to code if someone hasn't done it already.
Things SimpleCryptoLife
@SimpleCryptoLife, Would be easier I guess, haven't seen it, or remember it being done. What attracted me to this approach is that it's pure price action, and there's less subjectiveness around MA lengths, MA types etc etc etc. Come to think of it, the lag of the averages would make this approach useless. Where price action doesn't lag.
@Things, I see what you mean. I'm working on something that includes MA squeezes, so I think it can have a place, but it is different. For pure price action it might work to use market structure on multiple timeframes. The more timeframes that are ranging simultaneously the stronger the contraction zone, or something. Just thinking out loud.
+1 回复
i found your true market structure very useful to learn and spot HH, HL, LH and LL. Is it clever to change the number of bars counted from default of 20 to lower figures like 10 or even 5. or is it better to leave them at default of 20?
SimpleCryptoLife traders_tool
@traders_tool, Great question. The default is pretty good. If you make it lower, like 10 or 5, it will confirm more highs and lows. This is because it stops waiting for the strict rules to be followed and just goes ahead. This approach brings it closer to other market structure indicators on TradingView, which all (last time I checked) use some variation of Williams Fractals instead of enforcing the rules of closing beyond the high/low, and H only after L. The unique point of this indicator is that it does follow those rules. However, if you want it to confirm highs and lows faster, you could try lowering the Force Confirmation bars and see if you like the result.
I added the option to control the Market Bias background transparency.
It just takes one new line of code & modification of two other lines. The author's code structure made it simple - thank you!
Insert at line 100:
int inputBackgroundTransparency = input(type=input.integer, minval=0, maxval=100, defval=80, title="Background Transparency")
Then, in the "Background Fill" section around line 514, modify the lines to:
color backgroundColourTran =,inputBackgroundTransparency)
bgcolor(backgroundColourTran) // Actually do the fill. Remember it will usually be delayed until the Local High/Low is confirmed.