//@version=5
strategy("US30", overlay=true, default_qty_type=strategy.percent_of_equity, default_qty_value=100)
// Define Moving Average
length = 50
ma = ta.sma(close, length)
// Entry Conditions
longCondition = ta.crossover(close, ma)
shortCondition = ta.crossunder(close, ma)
// Define 100 pips SL/TP for US30 (1 pip = 1 point)
sl_pips = 100
tp_pips = 100
// Get current price for SL/TP calculation
long_sl = close - sl_pips
long_tp = close + tp_pips
short_sl = close + sl_pips
short_tp = close - tp_pips
if (longCondition)
strategy.entry("Long Trade", strategy.long)
strategy.exit("Long TP/SL", from_entry="Long Trade", limit=long_tp, stop=long_sl)
if (shortCondition)
strategy.entry("Short Trade", strategy.short)
strategy.exit("Short TP/SL", from_entry="Short Trade", limit=short_tp, stop=short_sl)
// Plot Moving Average
plot(ma, color=color.blue, title="50 SMA")
The issue is that the previous trade is being closed prematurely whenever a new entry signal is generated, instead of remaining open until it reaches its designated stop loss or take profit level. Ideally, the strategy should allow each trade to run its full course and only exit when the price hits either the stop loss or take profit, rather than closing early due to a new signal.
Hedging on Tradingview strategies is not supported. So, you cannot have an open long and short positions at the same time. Therefore, the opposite direction trade closes the other one.