'TradingView Pine Script - How do I get this code to exit an entry when a Take Profit/Stoploss is hit or on an opposite entry?
How do I get this strategy to exit a trade when either the Take Profit or Stop Loss is hit or when an opposite entry occurs? Opposite entry would be if in a "long" (buy) but then a "sell" (short) is triggered then the "long" (buy) trade would close and the "sell" (short) would open.
Currently it looks like it is only exiting the strategy when the long take profit or stop loss is hit or when in a long and the opposite entry "sell" (short) is triggered.
//@version=5
strategy(title="Strategy Testing 2", overlay=true)
// Get User Inputs
len = input.int(defval=5, minval=1, title="ADX and DI Length", group="ADX and DI")
adxThreshhold = input.int(title="ADX Threshhold",defval=35, group="ADX and DI")
emaFilter = input.bool(title="EMA Filter?", defval=true, group="EMA")
emaInput = input.int(title="EMA",defval=29,minval=5, maxval=52,tooltip="EMA Length",group="EMA")
takeProfit = input.float(title="Take Profit",defval=1.5,minval=.5,maxval=3.5,step=.01,group="Take Profit and Stop Loss")
stopLoss = input.float(title="Stop Loss", defval=.75, minval=.15,maxval=2.25,step=.01,group="Take Profit and Stop Loss")
timeZone = input.session(title="Time Session", defval="0930-1600")
// Converting take profit to percentage calculation
takeProfitPercentage = takeProfit * .01
stopLossPercentage = stopLoss * .01
// In Session
inSession(sess) => na(time(timeframe.period, sess)) == false
inSessionFilter = inSession(timeZone)
// ADX Value
TrueRange = math.max(math.max(high-low, math.abs(high-nz(close[1]))), math.abs(low-nz(close[1])))
DirectionalMovementPlus = high-nz(high[1]) > nz(low[1])-low ? math.max(high-nz(high[1]), 0): 0
DirectionalMovementMinus = nz(low[1])-low > high-nz(high[1]) ? math.max(nz(low[1])-low, 0): 0
SmoothedTrueRange = 0.0
SmoothedTrueRange := nz(SmoothedTrueRange[1]) - (nz(SmoothedTrueRange[1])/len) + TrueRange
SmoothedDirectionalMovementPlus = 0.0
SmoothedDirectionalMovementPlus := nz(SmoothedDirectionalMovementPlus[1]) - (nz(SmoothedDirectionalMovementPlus[1])/len) + DirectionalMovementPlus
SmoothedDirectionalMovementMinus = 0.0
SmoothedDirectionalMovementMinus := nz(SmoothedDirectionalMovementMinus[1]) - (nz(SmoothedDirectionalMovementMinus[1])/len) + DirectionalMovementMinus
DIPlus = SmoothedDirectionalMovementPlus / SmoothedTrueRange * 100
DIMinus = SmoothedDirectionalMovementMinus / SmoothedTrueRange * 100
DX = math.abs(DIPlus-DIMinus) / (DIPlus+DIMinus)*100
ADX = ta.sma(DX, len)
ADXBuySignal = DIPlus > adxThreshhold
ADXSellSignal = DIMinus > adxThreshhold
ADXTotal = ADX > adxThreshhold
// EMA Value
ema = ta.ema(close,emaInput)
emaabove = not emaFilter or low > ema or low[1] > ema[1]
emabelow = not emaFilter or high < ema or high[1] < ema[1]
// BUY ORDER
// Enter Buy Order
longCondition = inSessionFilter and ADXBuySignal and ADXTotal and emaabove
if longCondition
strategy.entry(id="Long", direction=strategy.long)
//Buy Take Profit and Stop Loss
longTakeProfit = strategy.position_avg_price*(1+takeProfitPercentage)
longStopLoss = strategy.position_avg_price*(1-stopLossPercentage)
// Manage Buy Exit Order
if strategy.position_avg_price>0
strategy.exit(id = "Long Exit",from_entry="Long", limit=longTakeProfit,stop=longStopLoss, when=strategy.position_size > 0)
// SELL ORDER
// Enter Sell Order
shortCondition = inSessionFilter and ADXSellSignal and ADXTotal and emabelow
if shortCondition
strategy.entry(id="Sell", direction=strategy.short)
//Short Take Profit and Stop Loss
shortTakeProfit = strategy.position_avg_price*(1-takeProfitPercentage)
shortStopLoss = strategy.position_avg_price*(1+stopLossPercentage)
// Manage Sell Exit Order
if strategy.position_avg_price<0
strategy.exit(id = "Short Exit",from_entry="Sell", limit=shortTakeProfit,stop=shortStopLoss, when=strategy.position_size < 0)
// Draw Take Profit and Stops
plot(strategy.position_avg_price != 0 ? longTakeProfit : na, color=color.green, style=plot.style_linebr, title="Long Take Profit")
plot(strategy.position_avg_price != 0 ? longStopLoss : na, color=color.red, style=plot.style_linebr, title="Long Stop Loss")
plot(strategy.position_avg_price != 0 ? shortTakeProfit : na, color=color.green, style=plot.style_linebr, title="Short Take Profit")
plot(strategy.position_avg_price != 0 ? shortStopLoss : na, color=color.red, style=plot.style_linebr, title="Short Stop Loss")
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
