"Short the second push above a swing high." It's one sentence, and a surprisingly hard one to backtest. The rule has to find the swing, count each push through it, keep the count through the pullbacks in between, and forget it the moment the swing is taken out. The State Machine node keeps a pattern like that in one place: a few named states, the rules that move between them, and the numbers it remembers along the way.
- Takes
- Its own bars, plus up to six market values you declare
- Gives
- The current state, the numbers it keeps, and the events its rules fire
- Reach for it
- A pattern has steps: it arms, counts, and starts over
How it works
A machine is always in exactly one state, starting in the first one you list. Transitions move it from one state to another when their conditions hold, and as they go they can write registers, the numbers the machine remembers. That's the whole model. Everything else is detail.
Each transition has a clock. Bar close rules are checked once, when a bar closes. Intrabar rules watch the trades inside the bar, and their first condition is always a price break: "price breaks above" or "breaks below" a level that's fixed for the bar. Within one state, rules are tried in the order they're listed and the first whose conditions all hold wins, so the order is the priority. At most one bar-close rule and one intrabar rule fire per bar.
Conditions are short formulas over the machine's own bars: high[0] is the signal bar's high (the last completed bar), low[1] the low of the bar before, up to 50 bars back. They can also read registers, params (named numbers you tune) and inputs (values wired in from the canvas). Actions look like attempts = attempts + 1.
The Second entry preset
You rarely start from nothing. A new State Machine starts as the Second entry (swing high) preset, and its twin, Second entry (swing low), is one click away under Start from. Here's the swing-high version, rule by rule:
| From → To | Clock | What happens |
|---|---|---|
| Locate → Armed | Bar close | A bar's high comes in below the bar before it. That earlier high becomes the swing, and also the initialSwing, which never moves again. |
| Armed → Broke | Intrabar | Price breaks above the signal bar's high. Add 1 to attempts and fire the Attempt event. |
| Broke → Pullback | Bar close | A bar closes with a higher high than the one before. |
| Pullback → Armed | Bar close | A lower low and a lower high, still under the swing. The swing moves to the previous bar's high, and the count is kept. |
| Pullback → Locate | Bar close | A high at or above the swing. It becomes the new swing and attempts go back to 0. |
The count survives a pullback because the re-arm rule never touches it. Only taking out the swing resets it. That's the part a pile of variables gets wrong.
Events, and why “the second” is a number
Every transition can fire a named event, and each event gets its own output. An event is true for one bar (from a bar-close rule) or one trade (from an intrabar rule). When an event carries a register, it hands over that register's value instead of true. Attempt carries attempts, so on the second push its output is 2.
That's why "the second attempt" is a Comparison: Attempt Equal a Constant of 2. No counter on the canvas. When the event isn't firing, the engine publishes 0, and the comparison reads false.
Building and editing one
Edit machine opens the editor, which shows the same machine two ways: a Diagram of cards and arrows (drag from one card to another to add a transition, or onto empty canvas to add a state) and a Table with a row per transition. The In words panel reads every rule back as a sentence, and a live preview runs the machine on real sample bars, with a band for the state, a line per register and a dot per event. Mirror turns a swing-high machine into its swing-low twin.
Inputs bring market data in: an indicator, a Price History, Event Day, a price, a Since or Capture output, a bar-close Crossover, or a modifier over one of those. Arithmetic belongs in the formulas, not in a Math node in front.
Why variables can't do this
Set Variable and Get Variable look like they could count attempts. They can count bars on the Bar close clock, but a count of attempts needs the re-arm between them, which is a second variable switching the first on and off, and with Parallel on (the default) every variable starts over from its Initial Value each month of the backtest. On the Intrabar clock it's worse: a counter climbs on every trade print inside one bar. A machine is computed from the bars themselves, judged once per bar, with its states and registers in one node, which is what lets it count across trades, days and months.
Warmup and settling
A backtest runs as monthly pieces side by side, and a month can't know what state the machine was in when it began. So the engine works it out.
The Warmup (500 bars by default) is how much history those copies get, and the builder warns when it's shorter than one session. One design never settles at all: a machine that counts and never resets, because two copies that started at different bars hold different counts forever. The preset settles because taking out the swing resets its count.
Example: short the second push
You trade ES 5m and want to fade the second push above a swing high. Drop a State Machine onto the canvas; it arrives as Second entry (swing high), on a 500-bar warmup.
Wire its Attempt output and a Constant of 2 into a Comparison set to Equal. AND that with a Time Window of 09:30 to 15:55 on the Intrabar clock (the event happens inside a bar, so the window should judge the same moment) through a Logic node, and send the AND into a short Entry. On the Bracket Order, measure both rows from the Signal bar: the stop 10 points above its high, the target 1.5 points below its low. Both are fixed when the trade fills.
The first push above the signal bar fires Attempt with 1, and nothing trades. The pullback re-arms the machine with the count kept. The second push fires Attempt with 2, and the entry sells on that trade. If price takes out the swing instead, the machine goes back to Locate and the count starts over.
Settings
A machine's settings are its table, edited in the editor rather than on the node.
Outputs
Besides State, the node grows an output per register (the preset's swing, initialSwing and attempts, each unset until something writes it) and one per event (Attempt).
Use something else when
When it doesn't behave
The preview shows events, but the backtest never trades
The machine probably never settles. If it counts something and nothing ever resets the count, two copies started at different bars never agree, and the backtest publishes nothing. Give the count a way back to 0, like the preset's invalidation rule. A short Warmup is a milder version of the same problem: the copies agree later, and the events before that are lost at the start of each month.
It trades every attempt, not only the second
Attempt carries the count, so it reads 1, 2, 3, and any of those counts as true in an AND. Compare it first: Attempt Equal 2.
My exit fires right after the entry
That's NOT of an event in an exit. An event is true for one bar or one trade, so its NOT is true nearly all the time. Exit on the machine's State instead, compared with the state you mean. The builder warns about this one.
The builder won't let me wire an input
Inputs take market data directly. A Math node or a condition is refused (put the arithmetic in the formulas), and so are another machine, an intrabar Crossover, an external data source, and the five price-action nodes that carry memory across months: Swing Points, Market Structure, Order Block, Fair Value Gap and Multi-Bar Formation.
Going further
Keep filters out of the machine. A trend filter, a time window or a distance check belongs on the canvas, ANDed with the event: the machine describes the pattern, the canvas decides when it may trade, and the machine keeps advancing when a filter says no.
Each param is one sweep row, inside its own bounds. The states and rules never are, on purpose: a sweep tunes a machine, it doesn't write a new one per run. A third entry, or trading with the push instead of against it, is a table edit you make before you look at results.
After a run, the chart draws the machine's state as a band with its events marked, and the trade panel shows the path it took to each trade, with Show on diagram lighting the route.