Learn NinjaScript: Event-Based Logic and the First Touch
How to detect events in NinjaScript — price touches, session transitions, and threshold crosses — using fired-this-bar flags and once-per-session flags for single-fire logic.
NinjaScript tutorials from someone who’s been writing it since NT6.5. Start at the top if you’re new — or jump to whatever you’re stuck on.

How to detect events in NinjaScript — price touches, session transitions, and threshold crosses — using fired-this-bar flags and once-per-session flags for single-fire logic.

A line says “trend is up.” A background gradient that deepens in color as trend strengthens says “trend is up, and getting stronger for three bars now.” Backgrounds are an underused signal channel — cheap to implement, expressive when done well, and invisible until the reader hovers to notice them. BackBrush paints the current bar’s…

Signals need to be visible on the chart. Arrows for entries, triangles for reversals, dots for bar-by-bar markers, diamonds for key levels. NinjaScript’s Draw.* family ships a handful of stock marker types that cover most needs — the mechanics are simple, the UX details are what separate a polished indicator from a sloppy one. This…

Zones and ranges carry more information than lines. A Fair Value Gap is a box that tells you where price left unfilled orders. An Opening Range is a box bounded by the first session hour’s high and low. A Supply/Demand zone is a rectangle with a story — price rejected from this area, and traders…

Some indicators belong on the price panel — moving averages, VWAP, pivots, trend lines. Some belong in a sub-panel — RSI, MACD, oscillators, anything whose scale is unrelated to price. A few indicators want both: an overlay line on the price panel plus a histogram or secondary metric in a sub-panel below. NinjaScript’s IsOverlay property…

A single-bar signal is easy. “Close above SMA” or “RSI crossed 30” fits in one if block. Multi-bar signals — “setup bar, then a confirmation bar within three bars, then a trigger bar” — turn into nested conditions that are fragile, hard to test, and almost impossible to debug when they misfire. A state machine…

“I have a jittery calculation and I want to smooth it.” The answer is almost always “call one of NinjaTrader’s built-in moving-average indicators on your existing Series.” NinjaScript ships SMA, EMA, WMA, HMA, TEMA, DEMA, VWMA, and several others as indicator objects you instantiate and call the same way you’d call SMA on Close. This…

“Three consecutive higher closes.” “Five bars without a lower low.” Pattern rules like these depend on a run counter — a small int that tracks how many bars in a row share a direction. The logic looks trivial but the corner cases are real: what counts as up, what to do with doji bars, how…

“How many bars since the last cross?” “How many bars has price been above the SMA?” Bars since a condition was met is one of the most-reused small patterns in NinjaScript — a single int field that increments every bar and resets to zero whenever the trigger fires. This post covers the pattern, the corner…

The previous post built a Random Forest — many decision trees, each grown independently on its own slice of the data, their votes averaged. This post keeps the trees but changes how they are built and combined. Gradient boosting grows trees in sequence: each new tree is fitted to the errors the ensemble still makes,…
The Account Copier mirrors one leader account to every follower in lockstep, on the same machine, across your LAN, or through the cloud relay. Free on one account.
Notifications