Moving Average
MA calculates the average price of an asset over a specific number of bars.
Role in the strategy
| Slot | Available | Role |
|---|---|---|
| Entry Point | Yes | Enters at the moving average price. |
| Entry Rule | Yes | Filters entries by the MA direction, bar position, or proposed entry price relative to the MA. |
| Exit Point | Yes | Exits at the moving average price. |
| Exit Rule | Yes | Triggers exits by the MA direction or the bar position to the MA. |
As an entry point the moving average is the price level of the pending entry orders; as an exit point it is the closing price level. As a rule it is an entry filter (AND) or an exit trigger (OR). See Indicators for the slot semantics.
As an exit point it places a resting order the bar reaches, so it allows no exit rules: Exit Rules are available only behind Bar Open or Bar Close.
Logic rules
The logic line depends on the slot:
- Entry Point: Enter at Moving Average.
- Exit Point: Exit at Moving Average.
- Entry Rule / Exit Rule:
| Rule | Long signal is 1 when | Short signal is 1 when |
|---|---|---|
| Moving Average rises | MA rose on the previous bar | MA fell on the previous bar |
| Moving Average falls | MA fell on the previous bar | MA rose on the previous bar |
| The bar opens above Moving Average | bar open is above the previous bar's MA | bar open is below the previous bar's MA |
| The bar opens below Moving Average | bar open is below the previous bar's MA | bar open is above the previous bar's MA |
| The bar opens above Moving Average after opening below it | bar open rose above the MA after opening below it | bar open fell below the MA after opening above it |
| The bar opens below Moving Average after opening above it | bar open fell below the MA after opening above it | bar open rose above the MA after opening below it |
| The bar closes above Moving Average | bar close is above the MA of that bar | bar close is below the MA of that bar |
| The bar closes below Moving Average | bar close is below the MA of that bar | bar close is above the MA of that bar |
| The bar closes above Moving Average after closing below it | bar close rose above the MA after closing below it | bar close fell below the MA after closing above it |
| The bar closes below Moving Average after closing above it | bar close fell below the MA after closing above it | bar close rose above the MA after closing below it |
| Moving Average changes its direction upward | MA formed a valley (down then up) | MA formed a peak (up then down) |
| Moving Average changes its direction downward | MA formed a peak (up then down) | MA formed a valley (down then up) |
The four The bar closes rules are exit-only (Exit Rules): they compare the close of the bar the rule reads with the MA of that same bar. Behind a Bar Open exit the read bar is the last completed bar (prev = 1); behind a Bar Close exit it is the bar being closed (prev = 0). The open family keeps its own reference — the bar open at the execution moment against the MA read one bar back.
Entry-only rules add The position opens above Moving Average and The position opens below Moving Average. They compare the Entry Point's proposed order price with the raw MA line; the short comparison reverses the inequality.
Parameters
| Parameter | Type | Default | Range | Purpose |
|---|---|---|---|---|
| MA method | select | Simple | Simple, Exponential, Weighted, Smoothed | Smoothing method of the average |
| Apply to | select | Close | Open, High, Low, Close, Median, Typical, Weighted | Price series the MA is calculated from |
| Period | number | 14 | 1 to 1000 | Number of bars of the average |
| Shift | number | 0 | 0 to 1000 | Bars the line is shifted forward |
Calculation
The moving average is calculated on the selected price series (tech.movingAverage in engine/indicator-engine/tech.ts) with the selected method, then shifted forward by the Shift value. The line is drawn on the main price chart as Moving Average.
As an entry or exit point, a second, undisplayed component holds the order price: the previous bar's MA value adjusted for the gap to the bar open (singlePrice), so the backtester places the pending order there.
Backtesting
- Rules are evaluated on the previous (closed) bar with the standard dead zone when the governing point executes at the bar open (a Bar Open entry, or a point evaluated at the bar opening). When the point executes at the bar close (a Bar Close exit), its rules read the current bar instead: the backtester evaluates them on the completed bar, and the exported EA re-evaluates them at the Bar Close event (the near-final bar). A The bar closes rule always reads the close and the MA of the same bar, so the open-price relaxation never shortens it.
- Bar Open + Moving Average exit runs the OpenIndicator scenario; Moving Average entry runs IndicatorOpen, IndicatorClose, or IndicatorIndicator depending on the exit point. See the scenario table in the Indicators index.
- With "Enter at Moving Average" both directions signal every bar, so an entry is attempted on every bar the rules allow. When both directions are allowed at the same price level, the bar is ambiguous and no order is placed; the entry rule combination decides the direction.
- Position-price rules use a strict half-point dead zone and the MA value at the slot's effective read offset. They filter the proposed order price (including any Entry Point gap adjustment), not the bar open or eventual fill.
- The order price is the previous bar's MA value, so the fill is the MA level reached during the bar; the intrabar interpolation method resolves fills when the level is crossed.