OPEN-SOURCE SCRIPT

Trend Signals with TP & SL Kang

460
//version=5
strategy("Buy/Sell with SL & TP", overlay=true, initial_capital=100000, default_qty_type=strategy.percent_of_equity, default_qty_value=10)

//===== Inputs =====
fastLen = input.int(9, "Fast MA Length")
slowLen = input.int(21, "Slow MA Length")
stopLossP = input.float(0.5, "Stop Loss %", step=0.1)
takeProfP = input.float(1.0, "Take Profit %", step=0.1)

//===== Indicators =====
fastMA = ta.ema(close, fastLen)
slowMA = ta.ema(close, slowLen)

plot(fastMA, color=color.new(color.green, 0))
plot(slowMA, color=color.new(color.red, 0))

//===== Conditions =====
longCondition = ta.crossover(fastMA, slowMA)
shortCondition = ta.crossunder(fastMA, slowMA)

//===== Entry Logic =====
if (longCondition)
strategy.entry("Long", strategy.long)
if (shortCondition)
strategy.entry("Short", strategy.short)

//===== Exit Logic =====
if (strategy.position_size > 0)
strategy.exit("Long Exit", "Long", stop=strategy.position_avg_price * (1 - stopLossP/100), limit=strategy.position_avg_price * (1 + takeProfP/100))
if (strategy.position_size < 0)
strategy.exit("Short Exit", "Short", stop=strategy.position_avg_price * (1 + stopLossP/100), limit=strategy.position_avg_price * (1 - takeProfP/100))

Disclaimer

The information and publications are not meant to be, and do not constitute, financial, investment, trading, or other types of advice or recommendations supplied or endorsed by TradingView. Read more in the Terms of Use.