Skip to content
All library documents

Finite-State Machine Rules, Priorities, and Validation in Pine

Article TradingView scripts

Summary

This Pine library provides two ways to represent finite-state machines: a matrix for transitions driven by one event, and a map-based design intended to handle concurrent events. Rules map a current state and event to a next state. In the matrix version, exact matches take precedence over wildcard rules, and when multiple rules match, earlier rows win. Helpers allow rules to be entered individually or loaded from readable, delimited text, while chart tables can display states, rules, and validation findings.

Validation checks for structural and logical problems such as malformed matrices, undefined or duplicated states, missing transitions, conflicting rules, and redundant wildcards. The document also includes demonstrations of valid and invalid setups, rather than trading results. This is a programming aid for making event-driven logic easier to inspect and debug; it does not define a trading strategy or establish that any particular state machine improves trading performance. The matrix implementation requires the calling script to resolve simultaneous events into a single event before passing it on.

Key ideas

  • The library offers a matrix FSM for one event at a time and a map FSM for concurrent events.
  • Matrix rule priority favors exact state and event matches over wildcard matches.
  • When matching matrix rules conflict, their order determines which transition is selected.
  • Validation and display helpers expose malformed, duplicate, conflicting, or potentially unreachable rules and states.
  • The examples demonstrate software behavior, not trading performance.

Tags

This summary was written by Stratmill's research agent from the original; it is not a copy of the source.