Strategy · Pine Script v5 · On the chart

5 EMA Strategy: Power of Stocks

by might-tree

Calculates a 5-period Exponential Moving Average (EMA) to identify short and long trade setups based on price distance from the average.

View on GitHub

GitHub stars4on the repository
Pine versionv5runs as is
Lines of code156
Settings0inputs
LicenceNone statedfile: MPL-2.0
Last update3 years ago2 Aug 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 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

156 lines of Pine Script v5 from Strategies/5EMA.pine, published under MPL-2.0 (file header). Keep the author's credit and licence lines when you reuse it.

5EMA.pine
//Run the following Script on PINE EDITOR or any platform supporting Pine Script

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © might-tree

//@version=5
strategy("5 EMA Strategy: Power of Stocks", overlay=true, margin_long=100, margin_short=100)

Eusl = false // Enable Extra SL 
usl = 10 // Value to set SL number of points above-high or below-low
risk_reward_ratio = 5
show_sell = true
show_buy = false
candle_close_buy_sell = false

ema5 = ta.ema(close,5)
pema5 = plot(ema5, '5 Ema', color=color.new(#882626, 0), linewidth=2)
   
var bool Short  = na
var bool Long  = na
var shortC = 0
var sslhitC = 0
var starhitC = 0
var float ssl = na
var float starl = na
var float star = na
var float sellat = na
var float alert_shorthigh = na
var float alert_shortlow = na
var line lssl = na
var line lstar = na
var line lsell = na
var label lssllbl = na
var label lstarlbl = na
var label lselllbl = na
var longC = 0
var lslhitC = 0
var ltarhitC = 0
var float lsl = na
var float ltarl = na
var float ltar = na
var float buyat = na
var float alert_longhigh = na
var float alert_longlow = na
var line llsl = na
var line lltar = na
var line lbuy = na
var label llsllbl = na
var label lltarlbl = na
var label lbuylbl = na

ShortWC = low[1] > ema5[1] and low[1] > low and shortC == 0 and close<close[1]
ShortWOC = low[1] > ema5[1] and low[1] > low and shortC == 0
Short := candle_close_buy_sell ? ShortWC : ShortWOC
sslhit = high > ssl and shortC > 0 and sslhitC == 0
starhit = low < star and shortC > 0 and starhitC == 0
LongWC =  high[1] < ema5[1] and high[1] < high and longC == 0 and close>close[1]
LongWOC =  high[1] < ema5[1] and high[1] < high and longC == 0
Long := candle_close_buy_sell ? LongWC : LongWOC
lslhit = low < lsl and longC > 0 and lslhitC == 0
ltarhit = high > ltar and longC > 0 and ltarhitC == 0

if Short and show_sell
    shortC := shortC + 1
    sslhitC := 0
    starhitC := 0
    alert_shorthigh := high[1]
    if Eusl
        ssl := high[1] + usl
        starl := candle_close_buy_sell ? ((high[1] - close) + usl) * risk_reward_ratio : ((high[1] - low[1]) + usl) * risk_reward_ratio
    else
        ssl := high[1]
        starl := candle_close_buy_sell ? (high[1] - close) * risk_reward_ratio : (high[1] - low[1]) * risk_reward_ratio
    star := candle_close_buy_sell ? close - starl : low[1] - starl
    sellat := candle_close_buy_sell ? close : low[1]
    lssl := line.new(bar_index, ssl, bar_index, ssl, color=color.new(#fc2d01,45), style=line.style_dashed)
    lstar := line.new(bar_index, star, bar_index, star, color=color.new(color.green,45), style=line.style_dashed)
    lsell := line.new(bar_index, sellat, bar_index, sellat, color=color.new(color.orange,45), style=line.style_dashed)
    lssllbl := label.new(bar_index, ssl, style=label.style_none, text='Stop Loss - Short' + ' (' + str.tostring(ssl) + ')', textcolor=color.new(#fc2d01,35), color=color.new(#fc2d01,35))
    lstarlbl := label.new(bar_index, star, style=label.style_none, text='Target - Short' + ' (' + str.tostring(star) + ')', textcolor=color.new(color.green,35), color=color.new(color.green,35))
    lselllbl := label.new(bar_index, sellat, style=label.style_none, text='Sell at' + ' (' + str.tostring(sellat) + ')', textcolor=color.new(color.orange,35), color=color.new(color.orange,35))

if sslhit == false and starhit == false and shortC > 0
    line.set_x2(lssl, bar_index)
    line.set_x2(lstar, bar_index)
    line.set_x2(lsell, bar_index)
    sslhitC := 0
    starhitC := 0
else
    if sslhit
        shortC := 0
        sslhitC := sslhitC + 1
    else
        if starhit
            shortC := 0
            starhitC := starhitC + 1

if Long and show_buy
    longC := longC + 1
    lslhitC := 0
    ltarhitC := 0
    alert_longlow := low[1]
    if Eusl
        lsl := low[1] - usl
        ltarl := candle_close_buy_sell ? ((close - low[1]) + usl) * risk_reward_ratio : ((high[1] - low[1]) + usl) * risk_reward_ratio
    else
        lsl := low[1]
        ltarl := candle_close_buy_sell ? (close - low[1]) * risk_reward_ratio : (high[1] - low[1]) * risk_reward_ratio
    ltar := candle_close_buy_sell ? close + ltarl : high[1] + ltarl
    buyat := candle_close_buy_sell ? close : high[1]
    llsl := line.new(bar_index, lsl, bar_index, lsl, color=color.new(#fc2d01,45), style=line.style_dotted)
    lltar := line.new(bar_index, ltar, bar_index, ltar, color=color.new(color.green,45), style=line.style_dotted)
    lbuy := line.new(bar_index, buyat, bar_index, buyat, color=color.new(color.orange,45), style=line.style_dotted)
    llsllbl := label.new(bar_index, lsl, style=label.style_none, text='Stop Loss - Long' + ' (' + str.tostring(lsl) + ')', textcolor=color.new(#fc2d01,35), color=color.new(#fc2d01,35))
    lltarlbl := label.new(bar_index, ltar, style=label.style_none, text='Target - Long' + ' (' + str.tostring(ltar) + ')', textcolor=color.new(color.green,35), color=color.new(color.green,35))
    lbuylbl := label.new(bar_index, buyat, style=label.style_none, text='Buy at' + ' (' + str.tostring(buyat) + ')', textcolor=color.new(color.orange,35), color=color.new(color.orange,35))

if lslhit == false and ltarhit == false and longC > 0
    line.set_x2(llsl, bar_index)
    line.set_x2(lltar, bar_index)
    line.set_x2(lbuy, bar_index)
    lslhitC := 0
    ltarhitC := 0
else
    if lslhit
        longC := 0
        lslhitC := lslhitC + 1
    else
        if ltarhit
            longC := 0
            ltarhitC := ltarhitC + 1

plotshape(show_sell and Short, title='Sell', location=location.abovebar, offset=0, color=color.new(#e74c3c, 45), style=shape.arrowdown, size=size.normal, text='Sell', textcolor=color.new(#e74c3c, 55))
plotshape(show_sell and sslhit, title='SL Hit - Short', location=location.abovebar, offset=0, color=color.new(#fc2d01, 25), style=shape.arrowdown, size=size.normal, text='SL Hit - Short', textcolor=color.new(#fc2d01, 25))
plotshape(show_sell and starhit, title='Target Hit - Short', location=location.belowbar, offset=0, color=color.new(color.green, 45), style=shape.arrowup, size=size.normal, text='Target Hit - Short', textcolor=color.new(color.green, 55))
plotshape(show_buy and Long, title='Buy', location=location.belowbar, offset=0, color=color.new(#2ecc71, 45), style=shape.arrowup, size=size.normal, text='Buy', textcolor=color.new(#2ecc71, 55))
plotshape(show_buy and lslhit, title='SL Hit - Long', location=location.belowbar, offset=0, color=color.new(#fc2d01, 25), style=shape.arrowdown, size=size.normal, text='SL Hit - Long', textcolor=color.new(#fc2d01, 25))
plotshape(show_buy and ltarhit, title='Target Hit - Long', location=location.abovebar, offset=0, color=color.new(color.green, 45), style=shape.arrowup, size=size.normal, text='Target Hit - Long', textcolor=color.new(color.green, 55))

if show_sell and Short
    alert("Go Short@ " + str.tostring(sellat) + " : SL@ " + str.tostring(ssl) + " : Target@ " + str.tostring(star) + " ", alert.freq_once_per_bar )

if show_sell and sslhit
    alert("Short Stop-loss hit ", alert.freq_once_per_bar )

if show_sell and starhit
    alert("Short Target hit ", alert.freq_once_per_bar )

if show_buy and Long
    alert("Go Long@ " + str.tostring(buyat) + " : SL@ " + str.tostring(lsl) + " : Target@ " + str.tostring(ltar) + " ", alert.freq_once_per_bar )

if show_buy and lslhit
    alert("Long Stop-loss hit ", alert.freq_once_per_bar )

if show_buy and ltarhit
    alert("Long Target hit ", alert.freq_once_per_bar )

Overview

5 EMA Strategy: Power of Stocks triggers entries when price breaks the high or low of an alert candle that formed completely outside the 5 EMA. The strategy projects entry, target, and stop loss lines directly on the price chart using a customizable risk-reward ratio and optional point buffer.

Details

Published by

might-tree on GitHub

Type

Strategy, Pine Script v5

Draws

On the price chart

Category

Moving averages, Breakout, Trend

Markets

Stocks, Indices

Timeframes

M1-M5, M15-H1

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.

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