sma strategy

 avatar
user_6299896
plain_text
2 months ago
956 B
4
Indexable
Never
//@version=5
strategy("SMA Strategy", overlay = true, initial_capital = 1000, max_bars_back = 2000)

//Trading
len1 = input(10, title="MA length 1")
len2 = input(20, title="MA length 2")

tp = input(0.5, title="Take Profit Percentage")/100
sl = input(0.2, title="Stop Loss Percentage")/100

quant = input(1, title="Quantity")

var bool buy_tr = na
var bool sell_tr = na

//Buy and Sell signals
ema1 = ta.sma(close, len1)
ema2 = ta.sma(close, len2)
plot(ema1, color = color.red)
plot(ema2, color = color.blue)
buy_tr := ta.crossover(ema1,ema2)
sell_tr := ta.crossunder(ema1,ema2)

//Trading
if buy_tr and strategy.opentrades==0 and barstate.isconfirmed
    strategy.entry("Buy", strategy.long, qty = quant)
    strategy.exit("Buy", limit = close*(1+tp), stop=close*(1-sl))

if sell_tr and strategy.opentrades==0 and barstate.isconfirmed
    strategy.entry("Sell", strategy.short, qty = quant)
    strategy.exit("Sell", stop=close*(1+sl), limit=close*(1-tp))