Indicator · Pine Script v4 · On the chart

20 EMA + 50 EMA + Breakout

by krosenfeld7

Calculates short and long Exponential Moving Averages alongside pivot high and low levels to identify breakout signals.

View on GitHub

GitHub stars5on the repository
Pine versionv4older, still runs
Lines of code69
Settings5inputs
LicenceApache-2.0
Last update3 years ago12 May 2023

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 indicator, select all of the template text and paste. Press Save and give it a name.
  4. 4Add it to the chartPress Add to chart, then adjust the settings with the gear icon next to its name.

Source Code

69 lines of Pine Script v4 from 20_EMA+50_EMA_Breakout/20_EMA+50_EMA_Breakout.txt, published under Apache-2.0. Keep the author's credit and licence lines when you reuse it.

20_EMA+50_EMA_Breakout.txt
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © krosenfeld7

//@version=4
study("20 EMA + 50 EMA + Breakout", shorttitle="20 EMA + 50 EMA + Breakout", overlay=true)

lookback = input(title="Pivot Lookback", type=input.integer, minval=1, defval=7)
// 20 period EMA
len1 = input(20, minval=1, title="Short EMA Length")
src1 = input(close, title="Short EMA Source")
ema20 = ema(src1, len1)
plot(ema20, title="Short EMA", color=color.white, linewidth=3)

// 50 period EMA
len2 = input(50, minval=1, title="Long EMA Length")
src2 = input(close, title="Long EMA Source")
ema50 = ema(src2, len2)
plot(ema50, title="Long EMA", color=color.blue, linewidth=3)

emaLong = ema20 > ema50
emaShort = ema50 > ema20

pivotHigh = pivothigh(lookback, lookback)
pivotLow = pivotlow(lookback, lookback)

var float pivotHighPrice = na
var float pivotLowPrice = na

if not na(pivotHigh)
    pivotHighPrice := pivotHigh
if not na(pivotLow)
    pivotLowPrice := pivotLow

lowestCloseAbove50 = true
lowestCloseBelow20 = false

for i = 0 to barssince(pivotHigh)
    if close[i] < ema50[i]
        lowestCloseAbove50 := false
    if close[i] < ema20[i]
        lowestCloseBelow20 := true
    
bullishBreakoutValid = close > pivotHighPrice

tempLong = emaLong and lowestCloseAbove50 and lowestCloseBelow20 and bullishBreakoutValid and close > open
long = tempLong ? 1 : 0

highestCloseBelow50 = true
highestCloseAbove20 = false

for i = 0 to barssince(pivotLow)
    if close[i] > ema50[i]
        highestCloseBelow50 := false
    if close[i] > ema20[i]
        highestCloseAbove20 := true
        
bearishBreakoutValid = close < pivotLowPrice

tempShort = emaShort and highestCloseBelow50 and highestCloseAbove20 and bearishBreakoutValid and close < open
short = tempShort ? 1 : 0

if long
    label.new(x = bar_index, y = na, text = "Long", yloc = yloc.abovebar, color = color.green)
if short
    label.new(x = bar_index, y = na, text = "Short", yloc = yloc.abovebar, color = color.red)

// Alerts
alertcondition(long, title = "Long Signal", message = "Long @ {{timenow}}: {{ticker}} -> {{interval}}: {{close}}")
alertcondition(short, title = "Short Signal", message = "Short @ {{timenow}}: {{ticker}} -> {{interval}}: {{close}}")

Overview

The indicator plots a 20 EMA and a 50 EMA on the main price chart. It evaluates trend direction based on EMA crossover alignment, checks for price pullbacks relative to the EMAs, and confirms breakouts beyond recent pivot levels. Labels and alerts are triggered when valid bullish or bearish breakout setups occur.

Details

Published by

krosenfeld7 on GitHub

Type

Indicator, Pine Script v4

Draws

On the price chart

Category

Moving averages, Trend, Breakout, Support & resistance

Markets

Any symbol

Timeframes

Any timeframe

Risk Notes

No repainting, lookahead or stacked entries were found in its code or README. That says nothing about how well it works: test it first.

Settings

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

SettingTypeDefaultVariable
Pivot Lookbackint7lookback
Short EMA Lengthint20len1
Short EMA Sourcesourceclosesrc1
Long EMA Lengthint50len2
Long EMA Sourcesourceclosesrc2
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

Sideway Zones Live

by dearvn
257 stars

Sideway Zones Live is an overlay indicator that estimates inner and outer sideways bands from recent highs and lows with a rational quadratic kernel. It tracks swing pivots over a loopback period and counts bars that remain between the outer high and low. Afte

Indicator v5 TrendSupport & resistance
Updated 6 weeks ago · None stated GitHub repository of Sideway Zones Live

Ichimoku Cloud

by harryguiacorn
219 stars

This indicator plots Ichimoku Cloud elements on the price chart, including Tenkan-sen, Kijun-sen, Senkou Span A and B lines with optional Chikou Span. It signals trend changes via Tenkan-sen and Kijun-sen crosses and places arrows at Kijun-sen direction shifts

Indicator v5 TrendMoving averages
Updated 1 year ago · None stated GitHub repository of Ichimoku Cloud

MA Envelope (MAE)

by mihakralj
99 stars

The MA Envelope indicator calculates upper and lower channel bands shifted by a user-defined percentage around a baseline moving average. It supports Simple Moving Average, Exponential Moving Average, and Weighted Moving Average calculation types. The script p

Indicator v6 Moving averagesSupport & resistance
Updated 4 weeks ago · Apache-2.0 GitHub repository of MA Envelope (MAE)

More indicators

Trade Your Charts With a Regulated Broker

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

Compare brokers