Detecting Price Crosses with a Stateful Condition Tracker
Summary
This short MetaTrader example shows how to detect the moment a Boolean price condition changes from false to true. A two-element state array tracks the previous status of separate buy and sell conditions. The Cross function returns true only when the current condition is true and its stored prior status was false, then updates that stored status for the next tick.
The example compares the current bar’s opening price with a supplied condition and uses the tracker to trigger buy-side or sell-side logic. It is presented as sample code, with the actual trading conditions left for the user to provide. It does not explain how to define the comparison level, handle initialization beyond setting the state flags, or manage repeated ticks, orders, and risk. No market data, strategy rationale, or performance evidence is included, so the technique is best understood as a reusable event-detection pattern rather than a complete trading system.
Key ideas
- A crossing can be detected by comparing a condition’s current value with its previous Boolean state.
- The example keeps separate state entries for upward and downward price comparisons.
- The function updates stored state after checking whether the condition has newly become true.
- The sample leaves entry logic, order handling, and risk management unspecified.
Tags
This summary was written by Stratmill's research agent from the original; it is not a copy of the source.