I’m struggling with the Pinescript as below. Instead of calculating the take profit/stop loss level based on the high/low of entry candle, it calculates the level based on the high/low of the next qualify candle. Can someone help me on this. Thank you.
//@version=5
strategy("EMA5 & EMA100 strategy", overlay=true)
// Calculate EMAs using ta.ema function
ema100 = ta.ema(close, 100)
ema5 = ta.ema(close, 5)
// Determine trend
trend = na(ema100[1]) ? ta.crossover(ema100, ema5) : ema5 > ema100 ? 1 : -1
// Filter signals
buySignal = trend == 1 and ema100 < close and close < ema5 and close < open
sellSignal = trend == -1 and ema100 > close and close > ema5 and close > open
// Enter positions
if buySignal
strategy.entry("Buy", strategy.long, stop=ta.highest(1)) // No stop argument needed for limit orders
var float entrybuy_low = na
var float entrybuy_high = na
if buySignal == true
entrybuy_low :=low
entrybuy_high :=high
TP = entrybuy_high + (entrybuy_high - entrybuy_low) // Take profit = entry high + distance to entry low (1:1 ratio)
SL = entrybuy_low // Stop loss = entry low - distance to entry high (1:1 ratio)
strategy.exit("Buy TP", "Buy", limit=TP, stop=SL) // Set limit order exits
if sellSignal
strategy.entry("Sell", strategy.short,stop=ta.lowest(1)) // No stop argument needed for limit orders
var float entrysell_low = na
var float entrysell_high = na
if sellSignal == true
entrysell_low :=low
entrysell_high :=high
TP1 = entrysell_low - (entrysell_high - entrysell_low) // Take profit = entry low - distance to entry high (1:1 ratio)
SL1 = entrysell_high // Stop loss = entry high + distance to entry low (1:1 ratio)
strategy.exit("Sell TP", "Sell", limit=TP1, stop=SL1) // Set limit order exits
// Display information
plotshape(buySignal, color=color.green, style=shape.arrowup, text="Buy", location=location.bottom)
plotshape(sellSignal, color=color.red, style=shape.arrowdown, text="Sell", location=location.top)
plot(ta.ema(close, 100), color=color.blue, linewidth=2)
plot(ta.ema(close, 5), color=color.orange, linewidth=2)
Expecting someone can help me to solve this problem
New contributor
Hiếu Vũ is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.