Strategy · Pine Script v6 · On the chart

MA Stack Array

by pineforge-4pass· code credited to PineForge contributors 2026

This strategy calculates three simple moving averages with lengths 10, 20 and 50.

View on GitHub

GitHub stars1on the repository
Pine versionv6current
Lines of code46
Settings3inputs
LicenceApache-2.0
Last update5 days ago23 Oct 2026

Add It to Your TradingView Chart

  1. 1Copy the scriptPress Copy script above, or the copy button on the code below.
  2. 2Open the Pine EditorOpen a TradingView chart and choose Pine Editor at the bottom of the screen.
  3. 3Paste over the templateCreate a new strategy, select all of the template text and paste. Press Save and give it a name.
  4. 4Add it to the chartPress Add to chart. The trades and results appear in the Strategy Tester tab; adjust the settings with the gear icon.

Source Code

46 lines of Pine Script v6 from validation/ta-triple-sma-stack-latch-01/strategy.pine, published under Apache-2.0. Keep the author's credit and licence lines when you reuse it.

strategy.pine
// This Pine Script® code is licensed under Apache-2.0
// SPDX-License-Identifier: Apache-2.0
// © PineForge contributors 2026
//
// PF probe 27 — 3-MA stack with var-bool transition latch
// Purpose: parity test for triple-SMA stack with `var bool prevBull / prevBear`
// latch suppressing duplicate same-direction entries.
//
//@version=6
strategy("MA Stack Array", overlay=true, initial_capital=1000000, process_orders_on_close=false, pyramiding=1, commission_type=strategy.commission.percent, commission_value=0, slippage=0, default_qty_type=strategy.fixed, default_qty_value=1)

// === Inputs ===
ma1Len = input.int(10, "MA 1 Length", minval=2)
ma2Len = input.int(20, "MA 2 Length", minval=2)
ma3Len = input.int(50, "MA 3 Length", minval=2)

// === Calculate MAs ===
ma1 = ta.sma(close, ma1Len)
ma2 = ta.sma(close, ma2Len)
ma3 = ta.sma(close, ma3Len)

// Check stack order directly (no array crash risk)
bullStack = ma1 > ma2 and ma2 > ma3
bearStack = ma1 < ma2 and ma2 < ma3

// === Track transitions ===
var bool prevBull = false
var bool prevBear = false

if bullStack and not prevBull
    strategy.entry("Long", strategy.long)
if bearStack and not prevBear
    strategy.entry("Short", strategy.short)

prevBull := bullStack
prevBear := bearStack

// === Exit when stack breaks ===
if strategy.position_size > 0 and not bullStack
    strategy.close("Long")
if strategy.position_size < 0 and not bearStack
    strategy.close("Short")

plot(ma1, "MA 10", color=color.blue)
plot(ma2, "MA 20", color=color.green)
plot(ma3, "MA 50", color=color.red)

Overview

MA Stack Array identifies a bull stack when the shortest exceeds the middle which exceeds the longest, and a bear stack in the opposite direction. A var-bool latch tracks prior states to suppress duplicate entries on the same side. The strategy enters long on a new bull stack and short on a new bear stack, then exits when the stack order breaks. Inputs control the three MA lengths. The three MAs draw as colored plots on the price chart. It has no stop loss and exits on stack break.

Details

Published by

pineforge-4pass on GitHub

Code credit

PineForge contributors 2026, as written in the script's header

Type

Strategy, Pine Script v6

Draws

On the price chart

Category

Moving averages, Trend, Breakout

Markets

Any symbol

Timeframes

Any timeframe

Risk Notes

  • The strategy enters trades without a stop loss

Topics

algorithmic-tradingbacktestingparitypine-scriptpinescriptquantitative-financetest-corpustradingview

Settings

The inputs you can change from the gear icon on the chart. Defaults are the author's.

SettingTypeDefaultVariable
MA 1 Lengthint10ma1Len
MA 2 Lengthint20ma2Len
MA 3 Lengthint50ma3Len
Third-party code. ForexR did not write, test or endorse this script, and the summary above was generated automatically from its code and README. Indicators describe the past, and a strategy's backtest can look better than live trading, especially when a script repaints. Check it in the Strategy Tester and on a demo account before you rely on it.

Similar Pine Scripts

257 stars

This strategy plots running pivot highs as red R circles and pivot lows as blue S circles directly on the price chart. It computes an EMA 3 and a Cto trend line from close prices, then generates long signals on upward EMA-Cto crossovers combined with price pos

Strategy v6 TrendMoving averages Repaints
Updated 6 weeks ago · None stated GitHub repository of Best Chart 1m Strategy Candle Bar

12/26-IT Strategy

by hasnocool
106 stars

This strategy executes trades based on crossovers between a 12-period and a 26-period exponential moving average (EMA). Long positions can be filtered by pivot resistance breakouts and RSI boundary ranges. For trade management, it provides two configurable par

Strategy v5 TrendMoving averages
Updated 2 years ago · GPL-3.0 GitHub repository of 12/26-IT Strategy

均线突破交易系统

by tangly1024
76 stars

This strategy trades long breakouts of a fast and a slow moving average on the price chart. It opens a long position when price crosses above either average, using a percentage of equity as size. It reduces the position by a set share when the high reaches the

Strategy v5 TrendMoving averages
Updated 4 months ago · None stated GitHub repository of 均线突破交易系统
43 stars

This strategy executes trades on short-term cryptocurrency charts using a combination of pivot support and resistance levels and a multi-pole smoothed trend filter. Entries trigger when price crosses the smoothed trend line and confirms above or below a short-

Strategy v5 TrendBreakout Repaints
Updated 2 years ago · None stated GitHub repository of SOL 1m - Strategy Candle Bar

ADA4H Optuna Strategy

by skywalker0803r
42 stars

This strategy trades longs and shorts on the price chart. Long entries require a close above the prior Donchian high, a close above a long-term SMA, and RSI above a threshold, with size set as a percentage of equity. Long exits use a stop at the prior Donchian

Strategy v5 TrendOscillators & momentum
Updated 2 months ago · None stated GitHub repository of ADA4H Optuna Strategy
16 stars

This strategy plots Donchian channels, trailing stops, and profit targets directly on the price chart. It enters breakout trades using a 55-bar Donchian channel and sizes positions according to an ATR-based risk percentage per unit. The system adds units as pr

Strategy v6 TrendBreakout
Updated 11 months ago · None stated GitHub repository of Seykota Alt 10: Profit Targets

More strategys

Trade Your Charts With a Regulated Broker

Compare brokers that connect to TradingView, with spreads, minimum deposits and leverage side by side.

Compare brokers