Strategy · Pine Script v5 · On the chart

Verge EMA Crossover + Trailing Stop

by kritarth2121· code credited to pranit123

This strategy goes long when a 13-period EMA crosses above a 34-period EMA and short on the opposite cross, closing each side on the reverse cross.

View on GitHub

Repaints

GitHub stars1on the repository
Pine versionv5runs as is
Lines of code75
Settings4inputs
LicenceNone statedfile: MPL-2.0
Last update2 years ago10 Dec 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

75 lines of Pine Script v5 from EMA + TRAILING STOPLOSS.txt, published under MPL-2.0 (file header). Keep the author's credit and licence lines when you reuse it.

EMA + TRAILING STOPLOSS.txt
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © pranit123

//@version=5
strategy("Verge EMA CROSSOVER + TRAILING STOP", overlay = true, initial_capital = 30000,max_labels_count=500,max_lines_count=500,max_boxes_count = 500,calc_on_every_tick = true)

/// USER INPUT 

EMA_L1 = input.int(title = "ema1", defval = 13,minval = 1, maxval = 200, step = 1)
EMA_L2= input.int(title = "ema2", defval = 34,minval = 1, maxval = 200, step = 1)

trailing_Stop_b = input.float(title = "TSL_B", defval = 0.02, minval = 0.01, step = 0.01, maxval = 100.00)
trailing_Stop_s = input.float(title = "TSL_S", defval = 0.02, minval = 0.01, step = 0.01, maxval = 100.00)


/// trading indicators 

EMA1 = ta.ema(close,EMA_L1)
EMA2 = ta.ema(close,EMA_L2)


/// buy and sell rules 

BUY = ta.crossover(EMA1,EMA2)
SELL = ta.crossunder(EMA1,EMA2)

BUYEXIT = ta.crossunder(EMA1,EMA2)
SELLEXIT = ta.crossover(EMA1,EMA2)





////// trailing stoploss 

longstopprice = 0.0 
shortstopprice = 0.0

longstopprice := if (strategy.position_size > 0 )
    stopvalue = close * (1 - trailing_Stop_b)
    math.max(stopvalue,longstopprice[1])
else 
    0
    

shortstopprice := if (strategy.position_size < 0 )
    stopvalue = close * (1 + trailing_Stop_s)
    math.min(stopvalue, shortstopprice[1])
else 
    999999
    
    
/// strategy 
if time(timeframe.period, "0915-1435:23456")
    strategy.entry("long", when = BUY, direction = strategy.long, comment = "B")
    strategy.close("long", when = BUYEXIT, comment = "BE")
    strategy.exit("long", from_entry = "long", stop = longstopprice, comment = "trailexitB") 

    strategy.entry("short", when = SELL, direction = strategy.short,comment = "S" )
    strategy.close("short", when = SELLEXIT, comment = "SE")
    strategy.exit("short", from_entry = "short", stop = shortstopprice, comment = "trailexitS") 


plot(strategy.position_size > 0 ? longstopprice : na, style = plot.style_circles, title = "TSLB", color = color.new(color.green,0))
plot(strategy.position_size < 0 ? shortstopprice : na, style = plot.style_circles, title = "TSLB", color = color.new(color.red,0))


plot(EMA1, color = color.new(color.lime,0))
plot(EMA2,color = color.new(color.purple,0))



if(hour==15)
    label.new(bar_index,high,'CLOSED',textcolor = color.white)
    strategy.close_all()

Overview

A percentage trailing stop, set separately for longs and shorts, ratchets behind the close and is plotted as circles. Orders are limited to a weekday window from 9:15 to 14:35, and all positions are closed once the 15:00 hour begins. It recalculates on every tick, so live bars can change signals.

Details

Published by

kritarth2121 on GitHub

Code credit

pranit123, as written in the script's header

Type

Strategy, Pine Script v5

Draws

On the price chart

Category

Moving averages, Trend, Sessions & time

Markets

Any symbol

Timeframes

M15-H1

Risk Notes

  • Signals can change on the live bar or after it closes (repainting)

Settings

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

SettingTypeDefaultVariable
ema1int13EMA_L1
ema2int34EMA_L2
TSL_Bfloat0.02trailing_Stop_b
TSL_Sfloat0.02trailing_Stop_s
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

Donald - Test Logic Futures is an overlay strategy designed for intraday futures trading. It combines kernel regression functions with exponential moving averages to evaluate price retracements and entry timing. Trades are filtered by session execution windows

Strategy v5 TrendMoving averages Repaints
Updated 6 weeks ago · None stated GitHub repository of Donald - Test Logic Futures

Andean Scalping

by hasnocool
106 stars

Andean Scalping is a Pine Script v5 strategy template designed for backtesting trading signals. It features session and date-range filtering, directional trade toggles for long and short entries, and optional trend filtering tools including higher-timeframe EM

Strategy v5 TrendMoving averages
Updated 2 years ago · GPL-3.0 GitHub repository of Andean Scalping

This strategy plots a 90-period simple moving average from the daily and four-hour charts plus a daily session VWAP on the price chart. It enters long when VWAP is above the daily average and the four-hour VWAP crosses above its average, and short in the oppos

Strategy v5 TrendMoving averages Repaints
Updated 3 years ago · MIT GitHub repository of 1D Bullish Buy 4H Bearish Sell Session
4 stars

This strategy trades directional momentum breakouts for crude oil on the price chart. It enters long or short positions when a fast exponential moving average crosses a slow exponential moving average, provided that current Average True Range exceeds its movin

Strategy v6 TrendMoving averages
Updated 2 months ago · MIT GitHub repository of CL Crude Oil Momentum [trugurpala]

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