You can copy paste the code or refer the screenshot.
Please make sure everything is in order as seen in the screen shot below. Copy pasting can lead to codes being crunched up so please be careful with the spaces.
Contact us at "[email protected]" if there are any questions

 

Long Side


//@version=4
strategy("Trailingstop", overlay=true)
if close >sma(close,50)
    strategy.entry("long",strategy.long)

 

//Trailing stop loss
Trailperc=0.20
price_stop=0.0

if (strategy.position_size > 0)
    stopValue = close * (1 - Trailperc)
    price_stop:=max(stopValue, price_stop[1])
else
    price_stop:=0

if (strategy.position_size > 0)
    strategy.exit(id="stoploss", stop=price_stop)

Short Side 

//@version=4
strategy("Trailingstop", overlay=true)
if close <sma(close,50)
    strategy.entry("short",strategy.short)

 

//Trailing stop loss
Trailperc=0.20
price_stop_short=10000000.0

if (strategy.position_size < 0)
    stopValue = close * (1 + Trailperc)
    price_stop_short:=min(stopValue, price_stop_short[1])
else
    price_stop_short:=10000000000

if (strategy.position_size < 0)
    strategy.exit(id="stoploss", stop=price_stop_short)