Strategy · Pine Script v5 · On the chart

All Moving Average Strategy

by hayashi19· code credited to HayashiHitsuji

This strategy builds trades from two configurable moving averages, each selectable from eleven types with its own source, length, offset and ALMA settings.

View on GitHub

GitHub stars1on the repository
Pine versionv5runs as is
Lines of code120
Settings29inputs
LicenceNone statedfile: MPL-2.0
Last update4 years ago10 Jan 2022

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

120 lines of Pine Script v5 from strategy/STARTEGY All Moving Average (林19).pine, published under MPL-2.0 (file header). Keep the author's credit and licence lines when you reuse it.

STARTEGY All Moving Average (林19).pine
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © HayashiHitsuji

//@version=5
strategy(title = "STARTEGY: All Moving Average (林19)", shorttitle = "All MA (林19)", overlay = true, format = format.price, pyramiding = 0, default_qty_type = strategy.cash, default_qty_value = 100, currency = currency.USD, slippage = 1, process_orders_on_close = true, initial_capital = 100)

// MA 1 INPUT
a = input(false, "MA 1 =======================================================")
ma_1_type   = input.session('SMA', "MA 1 Type", options = ['SMA','EMA','ALMA','HMA','RMA','SWMA','VWMA','WMA', "LSMA", "TEMA", "DEMA"], inline = "MA 1")
ma_1_src    = input.source(close, "MA 1 Source", inline = "MA 1")

ma_1_lenght = input.int(9, "MA 1 Lenght", inline = "Extra MA 1")
ma_1_offset = input.int(0, "MA 1 Offset", inline = "Extra MA 1")
ma_1_sigma  = input.float(0.85, "MA 1 Sigma", inline = "Extra MA 1")
ma_1_floor  = input.int(6, "MA 1 Floor", inline = "Extra MA 1")

// MA 1 CALCULATION
tema11      = ta.ema(ma_1_src, ma_1_lenght)
tema12      = ta.ema(tema11, ma_1_lenght)
tema13      = ta.ema(tema12, ma_1_lenght)
tema_1      = 3 * (tema11 - tema12) + tema13

dema11      = ta.ema(ma_1_src, ma_1_lenght)
dema12      = ta.ema(dema11, ma_1_lenght)
dema_1      = 2 * dema11 - dema12

ma_1        = ma_1_type == 'SMA' ? ta.sma(ma_1_src, ma_1_lenght) :
                 ma_1_type == 'EMA' ? ta.ema(ma_1_src, ma_1_lenght) :
                     ma_1_type == 'ALMA' ? ta.alma(ma_1_src, ma_1_lenght, ma_1_offset, ma_1_floor) :
                         ma_1_type == 'HMA' ? ta.hma(ma_1_src, ma_1_lenght) :
                             ma_1_type == 'RMA' ? ta.rma(ma_1_src, ma_1_lenght) :
                                 ma_1_type == 'SWMA' ? ta.swma(ma_1_src) :
                                     ma_1_type == 'VWMA' ? ta.vwma(ma_1_src, ma_1_lenght) :
                                         ma_1_type == 'WMA' ? ta.wma(ma_1_src, ma_1_lenght) :
                                             ma_1_type == 'LSMA' ? ta.vwma(ma_1_src, ma_1_lenght) :
                                                 ma_1_type == 'TEMA' ? tema_1 :
                                                     ma_1_type == 'DEMA' ? dema_1 : na

// MA 2 INPUT
b = input(false, "MA 2 =======================================================")
ma_2_type   = input.session('SMA', "MA 2 Type", options = ['SMA','EMA','ALMA','HMA','RMA','SWMA','VWMA','WMA', "LSMA", "TEMA", "DEMA"], inline = "MA 2")
ma_2_src    = input.source(close, "MA 2 Source", inline = "MA 2")

ma_2_lenght = input.int(50, "MA 2 Lenght", inline = "Extra MA 2")
ma_2_offset = input.int(0, "MA 2 Offset", inline = "Extra MA 2")
ma_2_sigma  = input.float(0.85, "MA 2 Sigma", inline = "Extra MA 2")
ma_2_floor  = input.int(6, "MA 2 Floor", inline = "Extra MA 2")

// MA 2 CALCULATION
tema21      = ta.ema(ma_2_src, ma_2_lenght)
tema22      = ta.ema(tema21, ma_2_lenght)
tema23      = ta.ema(tema22, ma_2_lenght)
tema_2      = 3 * (tema21 - tema22) + tema23

dema21      = ta.ema(ma_2_src, ma_2_lenght)
dema22      = ta.ema(dema21, ma_2_lenght)
dema_2      = 2 * dema21 - dema22

ma_2        = ma_2_type == 'SMA' ? ta.sma(ma_2_src, ma_2_lenght) :
                 ma_2_type == 'EMA' ? ta.ema(ma_2_src, ma_2_lenght) :
                     ma_2_type == 'ALMA' ? ta.alma(ma_2_src, ma_2_lenght, ma_2_offset, ma_2_floor) :
                         ma_2_type == 'HMA' ? ta.hma(ma_2_src, ma_2_lenght) :
                             ma_2_type == 'RMA' ? ta.rma(ma_2_src, ma_2_lenght) :
                                 ma_2_type == 'SWMA' ? ta.swma(ma_2_src) :
                                     ma_2_type == 'VWMA' ? ta.vwma(ma_2_src, ma_2_lenght) :
                                         ma_2_type == 'WMA' ? ta.wma(ma_2_src, ma_2_lenght) :
                                             ma_1_type == 'LSMA' ? ta.vwma(ma_2_src, ma_2_lenght) :
                                                 ma_1_type == 'TEMA' ? tema_2 :
                                                     ma_1_type == 'DEMA' ? dema_2 : na
                                                     
// RSI
d = input(false, "RSI ========================================================")
rsi_src     = input.source(ohlc4, "RSI Source", inline = "RSI")
rsi_lenght  = input.int(14, "RSI", inline = "RSI")

rsi_tp_long = input.int(70, "RSI TP Long", inline = "RSI TP SL")
rsi_tp_short = input.int(30, "RSI TP Short", inline = "RSI TP SL")
rsi_sl_long = input.int(30, "RSI SL Long", inline = "RSI TP SL")
rsi_sl_short = input.int(70, "RSI SL Short", inline = "RSI TP SL")
rsi         = ta.rsi(rsi_src, rsi_lenght)

// VOLATILITY
e = input(false, "Volatility =================================================")
atr_lenght  = input.int(50, "ATR", inline = "ATR")
atr_comp    = input.int(11, "ATR Compare", inline = "ATR")
atr = ta.atr(atr_lenght)

// Show Plot
c = input(false, "Startegy Extra Setting =====================================")
plot    = input.bool(true, "Show Indicator")
buy_sell= input.bool(true, "Show Buy & Sell")
rsi_stop    = input.bool(true, "Use RSI stoploss takeprofit?")
atr_comfirm = input.bool(true, "Use ATR comfirmation?")

p1      = plot(plot ? ma_1 : na, color = color.aqua)
p2      = plot(plot ? ma_2 : na, color = color.fuchsia)
fill_color = ma_1 > ma_2 ? color.green : color.red
fill(p1, p2, color = color.new(fill_color, 85))

// Strategy Algo
long_entry  = ta.crossover(ma_1, ma_2) and (atr_comfirm ? atr[0] > atr[atr_comp] : 0 == 0)
short_entry = ta.crossunder(ma_1, ma_2) and (atr_comfirm ? atr[0] > atr[atr_comp] : 0 == 0)

long_tp     = ta.crossover(rsi, rsi_tp_long)
short_tp    = ta.crossunder(rsi, rsi_tp_short)

long_sl     = ta.crossunder(rsi, rsi_sl_long)
short_sl    = ta.crossover(rsi, rsi_sl_short)

// Strategy Execution
strategy.entry("enter long", strategy.long, when = long_entry)
strategy.entry("enter short", strategy.short, when = short_entry)

strategy.close("enter long", when = rsi_stop ? long_tp : na, qty_percent = 100, comment = "take profit slong")
strategy.close("enter short", when = rsi_stop ? short_tp : na, qty_percent = 100, comment = "take profit short")

strategy.close("enter long", when = rsi_stop ? long_sl : na, qty_percent = 100, comment = "stop loss slong")
strategy.close("enter short", when = rsi_stop ? short_sl : na, qty_percent = 100, comment = "stop loss short")

bgcolor(buy_sell ? color.new(long_entry ? color.green : short_entry ? color.red : na, 85) : na)

Overview

Optional modules add RSI-based take profit and stop levels and an ATR comparison as confirmation, and it can show the indicator lines and buy and sell markers. Orders use a fixed cash amount with slippage, no pyramiding and execution on bar close. Much of the entry logic was not shown, so details are not confirmed.

Details

Published by

hayashi19 on GitHub

Code credit

HayashiHitsuji, as written in the script's header

Type

Strategy, Pine Script v5

Draws

On the price chart

Category

Moving averages, Trend

Markets

Any symbol

Timeframes

Any timeframe

Risk Notes

  • The strategy enters trades without a stop loss

Settings

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

SettingTypeDefaultVariable
MA 1 =======================================================boolfalsea
MA 1 TypesessionSMAma_1_type
MA 1 Sourcesourceclosema_1_src
MA 1 Lenghtint9ma_1_lenght
MA 1 Offsetint0ma_1_offset
MA 1 Sigmafloat0.85ma_1_sigma
MA 1 Floorint6ma_1_floor
MA 2 =======================================================boolfalseb
MA 2 TypesessionSMAma_2_type
MA 2 Sourcesourceclosema_2_src
MA 2 Lenghtint50ma_2_lenght
MA 2 Offsetint0ma_2_offset
Show all 29 settings
SettingTypeDefaultVariable
MA 2 Sigmafloat0.85ma_2_sigma
MA 2 Floorint6ma_2_floor
RSI ========================================================boolfalsed
RSI Sourcesourceohlc4rsi_src
RSIint14rsi_lenght
RSI TP Longint70rsi_tp_long
RSI TP Shortint30rsi_tp_short
RSI SL Longint30rsi_sl_long
RSI SL Shortint70rsi_sl_short
Volatility =================================================boolfalsee
ATRint50atr_lenght
ATR Compareint11atr_comp
Startegy Extra Setting =====================================boolfalsec
Show Indicatorbooltrueplot
Show Buy & Sellbooltruebuy_sell
Use RSI stoploss takeprofit?booltruersi_stop
Use ATR comfirmation?booltrueatr_comfirm
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

Doge SMA

by hackingthemarkets
474 stars

Doge SMA is a long-only moving-average crossover strategy drawn on the price chart. It calculates a fast and a slow simple moving average of close and enters a long position when the fast average crosses above the slow average, but only inside a user-defined s

Strategy v4 TrendMoving averages
Updated 4 years ago · None stated GitHub repository of Doge SMA

This strategy calculates and plots a fast Exponential Moving Average (EMA) and a slow EMA directly on the main price chart. It enters long trades when the fast EMA crosses above the slow EMA and exits when the fast EMA crosses below the slow EMA. Inputs allow

Strategy v6 TrendMoving averages
Updated yesterday · AGPL-3.0 GitHub repository of Forven S01508 — Simple EMA20/50 Cross
330 stars

2 EMA/SMA + RSI Strategy [Alorse] combines three configurable moving averages with a short-period Relative Strength Index filter. It plots fast, slow, and trend moving averages on the price chart. Long entries trigger when the fast moving average is above the

Strategy v4 TrendOscillators & momentum
Updated 2 months ago · None stated GitHub repository of 2 EMA/SMA + RSI Strategy [Alorse]

Acrypto - Weighted Strategy

by AlbertoCuadra
318 stars

This strategy plots on the price chart and opens long or short positions when a weighted score from MACD, Stochastic RSI, RSI, Supertrend and a moving-average crossover exceeds a user-set trigger. Each component has its own weight and candle delay. Entries can

Strategy v5 TrendOscillators & momentum
Updated 3 years ago · Custom licence GitHub repository of Acrypto - Weighted Strategy
257 stars

This strategy calculates dynamic support and resistance levels using pivot highs and lows alongside a custom smoothed trend filter and a 3-period EMA. It identifies entry opportunities when price breaks out relative to the trend filter and short-term EMA cross

Strategy v5 TrendSupport & resistance Repaints
Updated 6 weeks ago · None stated GitHub repository of 30s - ES 1m Strategy Candle Bar

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