Discussion about this post

User's avatar
Cryptohike's avatar

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

// © Cryptohike

//@version=5

strategy("My strategy", overlay=true, initial_capital = 25000)

//afterStartDate = (time >= timestamp(syminfo.timezone,

// 2022, 3, 20, 0, 0))

float initialCapital = strategy.equity

ema20 = ta.ema(close,20)

float atr = ta.atr(14)*2 //declaring atr

var float storeatr = 0 //variable to store value of atr at the time of entering trade

var float entryprice = 0

longCondition = ta.crossover(close[1], ema20) and close[0]>high[1]

shortCondition = ta.crossunder(close[1],ema20) and close[0]<low[1]

if (longCondition) //and afterStartDate

slpercent = atr/close * 100

x = 0.03 * 100 * initialCapital/slpercent

possize = x/close

strategy.entry("long", strategy.long, qty = possize)

entryprice := close

storeatr := atr

if (shortCondition) //and afterStartDate

slpercent = atr/close * 100

x = 0.03 * 100 * initialCapital/slpercent

possize = x/close

strategy.entry("short",strategy.short, qty = possize)

entryprice := close

storeatr := atr

strategy.exit("exit","long", stop = entryprice-storeatr, limit = entryprice+storeatr*1.8)

strategy.exit("exit2","short", stop = entryprice+storeatr, limit = entryprice-storeatr*1.8)

//You can change the code to modify the initial capital and other parameter like risk reward ratio etc

Expand full comment
2 more comments...

No posts