//@version=5 strategy("Improved 5-Min Candle Strategy with Custom Stop Loss", overlay=true) // User-defined input parameters stopLossBuffer = input.float(250.0, title="Stop Loss Buffer (points)") customStopLoss = input.float(550.0, title="Custom Stop Loss (points)") takeProfit1Points = input.float(150.0, title="Take Profit 1 (points)") takeProfit2Points = input.float(250.0, title="Take Profit 2 (points)") takeProfit3Points = input.float(400.0, title="Take Profit 3 (points)") startDate = input.time(timestamp("2024-08-01 00:00"), title="Start Date") // Input options to show/hide lines showStopLossLines = input.bool(true, title="Show Stop Loss Lines") showTakeProfitLines = input.bool(true, title="Show Take Profit Lines") // Input parameters for take profit percentages tp1Percentage = input.float(30.0, title="TP1 Percentage", minval=0.0, maxval=100.0) / 100 tp2Percentage = input.float(50.0, title="TP2 Percentage", minval=0.0, maxval=100.0) / 100 tp3Percentage = 1.0 - tp1Percentage - tp2Percentage // Define strategy settings var float longStopLoss = na var float longTakeProfit1 = na var float longTakeProfit2 = na var float longTakeProfit3 = na var float shortStopLoss = na var float shortTakeProfit1 = na var float shortTakeProfit2 = na var float shortTakeProfit3 = na // Track if a trade was executed on the current candle var bool tradeExecuted = false // Ensure the current bar's timestamp is after the start date if (time >= startDate) // Reset tradeExecuted at the beginning of a new candle if (na(time[1]) or time[1] != time) tradeExecuted := false // Check for a red candle followed by a green candle (Long Entry) isRedCandle = close[1] < open[1] isGreenCandle = close > open previousCandleRange = high[1] - low[1] if (isRedCandle and isGreenCandle and not tradeExecuted) if (previousCandleRange > 200 * 0.01) longStopLoss := close - customStopLoss * 0.01 else longStopLoss := low[1] - stopLossBuffer * 0.01 longTakeProfit1 := close + takeProfit1Points * 0.01 longTakeProfit2 := close + takeProfit2Points * 0.01 longTakeProfit3 := close + takeProfit3Points * 0.01 strategy.entry("Long", strategy.long) strategy.exit("Take Profit 1 Long", "Long", limit=longTakeProfit1, qty=strategy.position_size * tp1Percentage) strategy.exit("Take Profit 2 Long", "Long", limit=longTakeProfit2, qty=strategy.position_size * tp2Percentage) strategy.exit("Take Profit 3 Long", "Long", limit=longTakeProfit3, qty=strategy.position_size * tp3Percentage) strategy.exit("Stop Loss Long", "Long", stop=longStopLoss) tradeExecuted := true // Check for a green candle followed by a red candle (Short Entry) isGreenCandlePrev = close[1] > open[1] isRedCandleCurr = close < open if (isGreenCandlePrev and isRedCandleCurr and not tradeExecuted) if (previousCandleRange > 500 * 0.01) shortStopLoss := close + customStopLoss * 0.01 else shortStopLoss := high[1] + stopLossBuffer * 0.01 shortTakeProfit1 := close - takeProfit1Points * 0.01 shortTakeProfit2 := close - takeProfit2Points * 0.01 shortTakeProfit3 := close - takeProfit3Points * 0.01 strategy.entry("Short", strategy.short) strategy.exit("Take Profit 1 Short", "Short", limit=shortTakeProfit1, qty=strategy.position_size * tp1Percentage) strategy.exit("Take Profit 2 Short", "Short", limit=shortTakeProfit2, qty=strategy.position_size * tp2Percentage) strategy.exit("Take Profit 3 Short", "Short", limit=shortTakeProfit3, qty=strategy.position_size * tp3Percentage) strategy.exit("Stop Loss Short", "Short", stop=shortStopLoss) tradeExecuted := true // Plot stop loss and take profit levels if enabled plot(showStopLossLines ? longStopLoss : na, color=color.red, title="Long Stop Loss", style=plot.style_linebr) plot(showStopLossLines ? shortStopLoss : na, color=color.red, title="Short Stop Loss", style=plot.style_linebr) plot(showTakeProfitLines ? longTakeProfit1 : na, color=color.green, title="Long Take Profit 1", style=plot.style_linebr) plot(showTakeProfitLines ? longTakeProfit2 : na, color=color.green, title="Long Take Profit 2", style=plot.style_linebr) plot(showTakeProfitLines ? longTakeProfit3 : na, color=color.green, title="Long Take Profit 3", style=plot.style_linebr) plot(showTakeProfitLines ? shortTakeProfit1 : na, color=color.green, title="Short Take Profit 1", style=plot.style_linebr) plot(showTakeProfitLines ? shortTakeProfit2 : na, color=color.green, title="Short Take Profit 2", style=plot.style_linebr) plot(showTakeProfitLines ? shortTakeProfit3 : na, color=color.green, title="Short Take Profit 3", style=plot.style_linebr)