Market Structure Indicator
A \real-time market structure analysis engine\ for TradingView that automatically detects swing structure, Break of Structure (BOS), Market Structure Shift (MSS), and HH/HL/LH/LL trend classification directly on the chart. What started as a simple BOS detector evolved into a \state-aware structural system\ capable of maintaining market context across bars, separating major structure from internal movement, and invalidating failed shifts.
Problem and context
I wanted the chart to remember what I was seeing Market structure is easy to describe on paper. A trader looks at a chart, identifies a swing high, finds a swing low, watches price break an important level, and then decides whether that movement represents a continuation or a shift in direction. The problem appears when you try to make a computer do the same thing. A human can look several swings back and understand that a new high is part of a larger trend. An indicator has no such intuition. It has to be explicitly told \what matters, what should be remembered, and what qualifies as a structural event\. That was the starting point for the Market Structure Engine. I wanted to build a TradingView indicator that could take the visual process of manually reading structure and turn it into \consistent, executable rules\. Instead of repeatedly marking swing points and structural breaks by hand, the chart would maintain its own structural context as new price data arrived. Manual structure breaks down when the market gets noisy Manual market structure analysis works reasonably well when a chart is clean. Once price becomes more active, the process becomes considerably harder. A trader has to identify meaningful swing points, compare them against previous structure, distinguish a major movement from an internal fluctuation, and determine whether a break actually changed the market structure. That creates a problem of \consistency\. Two traders can look at the same chart and mark different structural points. Even the same trader can classify the same market differently after several hours of rapid price movement. The engine was designed to remove as much of that ambiguity as possible. It focuses on automatically identifying: \Break of Structure (BOS)\ \Market Structure Shift (MSS)\ \Higher High / Higher Low\ \Lower High / Lower Low\ \Main structure vs. internal structure\ \Failed structure shifts / fakeouts\ The objective was not to create another indicator that simply adds more labels to a chart. It was to build a system that could \maintain a structured memory of price action\ and make the same classification rules consistently.
Outcome
Turning internal state into something a trader can read The engine's internal logic is ultimately represented visually on the TradingView chart. BOS Green structural lines identify confirmed breaks of previous structure. MSS White structure-shift lines indicate potential changes in structural direction. HH / HL / LH / LL Confirmed pivots are classified according to their relationship with the previously stored structural points. Main Structure The larger structural path of the market. Internal Structure The smaller swings occurring inside that larger structure. FAKE ❌ A previously triggered shift that failed its validation conditions is visually invalidated rather than being left on the chart as a successful signal. The objective is to make the chart communicate \structural information without requiring the trader to manually reconstruct what happened several bars ago\. THE RESULT From a BOS detector to a structural engine The project eventually became much larger than the first version I started with. The current implementation combines \BOS, MSS, trend classification, main/internal structure, and fakeout validation\ inside one Pine Script indicator. It can now: Detect confirmed swing highs and lows Classify HH/HL/LH/LL Maintain separate main and internal structure Detect Break of Structure events Detect Market Structure Shifts Track structural state across bars Validate failed shifts Render the resulting structure directly on the chart The most meaningful result isn't simply the number of features. It's that the indicator now maintains enough context to \interpret new price events relative to previously confirmed structure\. \WHAT I'M LEARNING\ \The difficult part isn't drawing the signal\ Building this engine changed how I think about technical indicators. At first, the problem appeared visual: identify a pivot, draw a line, add a label. But the deeper I went, the more obvious it became that the real problem was \state management\. The engine has to know what happened before the current bar, which structural point is still relevant, whether a new pivot is major or internal, whether a break actually represents a shift, and whether that shift survived long enough to remain valid. That means \indicator logic and state management are inseparable\. I also learned the importance of keeping a verified baseline. Several later changes introduced regressions that weren't obvious when looking only at the newest version. Comparing against earlier working implementations made those problems much easier to isolate. The biggest lesson wasn't how to detect BOS or draw HH/HL labels. It was learning to think about \state, transitions, validation, and failure conditions\ as first-class parts of an indicator. \WHAT I WOULD IMPROVE\ \The engine isn't finished\ There are several areas I would address before considering this a mature trading tool. Alerts Add alertcondition() support so confirmed BOS and MSS events can trigger \TradingView notifications\ without requiring the user to watch the chart continuously. Historical validation Test the fakeout-protection logic across a larger set of historical market conditions and tune the \bar-distance threshold\ based on actual behavior rather than visual assumptions. Refactoring Move repeated line and label rendering into reusable functions so the BOS, main structure, internal structure, and MSS sections share a cleaner implementation. Systematic testing Create a more structured testing process across trending, ranging, volatile, and rapidly reversing markets to understand where the classification performs well and where the model needs additional rules. These improvements would move the project from a \working indicator into a more rigorously validated trading system\. \THE BIGGER TAKEAWAY\ \Turning visual reasoning into executable rules\ The Market Structure Engine started with a relatively simple question: Can I automate the structural markings I normally make manually on a chart? The answer turned out to be more complicated than simply detecting highs and lows. To make the system useful, I had to model \memory, relationships, hierarchy, state transitions, and invalidation\. A swing needs context. A break needs a reference point. A shift needs validation. And a signal needs to know what happened before it. That's what makes this project valuable to me as an engineering exercise. I wasn't just building another TradingView overlay. I was taking something that traders normally interpret visually and attempting to turn that interpretation into \a repeatable, state-aware system of rules\.
Technologies: Pine Script v6, TradingView, Market Structure, Fibonacci