Moving Average

MA calculates the average price of an asset over a specific number of bars.

Role in the strategy

SlotAvailableRole
Entry PointYesEnters at the moving average price.
Entry RuleYesFilters entries by the MA direction, bar position, or proposed entry price relative to the MA.
Exit PointYesExits at the moving average price.
Exit RuleYesTriggers 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:
RuleLong signal is 1 whenShort signal is 1 when
Moving Average risesMA rose on the previous barMA fell on the previous bar
Moving Average fallsMA fell on the previous barMA rose on the previous bar
The bar opens above Moving Averagebar open is above the previous bar's MAbar open is below the previous bar's MA
The bar opens below Moving Averagebar open is below the previous bar's MAbar open is above the previous bar's MA
The bar opens above Moving Average after opening below itbar open rose above the MA after opening below itbar open fell below the MA after opening above it
The bar opens below Moving Average after opening above itbar open fell below the MA after opening above itbar open rose above the MA after opening below it
The bar closes above Moving Averagebar close is above the MA of that barbar close is below the MA of that bar
The bar closes below Moving Averagebar close is below the MA of that barbar close is above the MA of that bar
The bar closes above Moving Average after closing below itbar close rose above the MA after closing below itbar close fell below the MA after closing above it
The bar closes below Moving Average after closing above itbar close fell below the MA after closing above itbar close rose above the MA after closing below it
Moving Average changes its direction upwardMA formed a valley (down then up)MA formed a peak (up then down)
Moving Average changes its direction downwardMA 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

ParameterTypeDefaultRangePurpose
MA methodselectSimpleSimple, Exponential, Weighted, SmoothedSmoothing method of the average
Apply toselectCloseOpen, High, Low, Close, Median, Typical, WeightedPrice series the MA is calculated from
Periodnumber141 to 1000Number of bars of the average
Shiftnumber00 to 1000Bars 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.