// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/ // © Chart0bserver // // READ THIS CAREFULLY!!! ----------------// // This code is provided for educational purposes only. The results of this strategy should not be considered investment advice. // The user of this script acknolwedges that it can cause serious financial loss when used as a trading tool // For more information: please watch the video: https://youtu.be/M1Cj8HdopxY?si=uaQz5ibbWlfHVjIl //@version=6 strategy("Exit Strategy with 3 Trailing Stops", "Exit Strategy", overlay=true, pyramiding=1, default_qty_type=strategy.percent_of_equity, default_qty_value=100, process_orders_on_close=true) // Inputs barIndex1 = input.int(0, title="Bar Index of your First Entry", group="Where did you open your trade", tooltip="Bar Index can be found in the Data Window") drop1 = input.float(4, title="1st trailing stop loss (%)", minval=0, group="Trailing Stop") / 100 firstExitPerc = input.int(30, title="% of position to sell", minval=0, group="Trailing Stop") drop2 = input.float(7.5, title="2nd trailing stop loss (%)", minval=0, group="Trailing Stop") / 100 secondExitPerc = input.int(70, title="% of remaining position to sell", minval=0, group="Trailing Stop") drop3 = input.float(11, title="3rd trailing stop loss (%)", minval=0, group="Trailing Stop") / 100 thirdExitPerc = input.int(100, title="% of remaining position to sell", minval=100, maxval=100, group="Trailing Stop") // Track Entry Price and Highest Price Since Entry var float entryPrice = na var float highestPrice = na var bool sl1_triggered = false var bool sl2_triggered = false var bool sl3_triggered = false // Entry Condition: Enter the bar_index of yor opening candle in the config -----------------------------------------------------------------------------------// if (bar_index == barIndex1) strategy.entry("Long", strategy.long) sl1_triggered := false sl2_triggered := false sl3_triggered := false if (strategy.position_size > 0) entryPrice := na(entryPrice) ? strategy.position_avg_price : entryPrice // Set entry price on first bar of position highestPrice := na(highestPrice) ? high : math.max(highestPrice, high) // Initialize or update highest price else entryPrice := na // Reset when no position is open highestPrice := na // Reset when no position is open // Trailing stop loss orders level1 = ta.ema(highestPrice[1] * (1 - drop1), 10) level1Trigger = (ta.crossunder(close, level1)) level2 = ta.ema(highestPrice[1] * (1 - drop2), 10) level2Trigger = (ta.crossunder(close, level2)) level3 = ta.ema(highestPrice[1] * (1 - drop3), 10) level3Trigger = (ta.crossunder(close, level3)) firstComment = str.tostring(firstExitPerc) + "%" secondComment = str.tostring(secondExitPerc) + "%" thirdComment = str.tostring(thirdExitPerc) + "%" // Partial Exits Based on Price Drops for highest price since this position was opened if level3Trigger and not sl3_triggered // strategy.close("Long", qty_percent=thirdExitPerc, comment=thirdComment) strategy.close_all(comment=thirdComment) sl1_triggered := true sl2_triggered := true sl3_triggered := true else if level2Trigger and not sl2_triggered strategy.close("Long", qty_percent=secondExitPerc, comment=secondComment) sl1_triggered := true sl2_triggered := true else if level1Trigger and not sl1_triggered strategy.close("Long", qty_percent=firstExitPerc, comment=firstComment) sl1_triggered := true fillColor1 = color.new(color.green, 90) fillColor2 = color.new(color.yellow, 90) fillColor3 = color.new(color.orange, 90) fillColor4 = color.new(color.red, 90) // Debugging: Plot Entry Price, Highest Price, Drop Levels, and Profit Target Level Dynamically plot(entryPrice, color=color.gray, title="Entry Price", linewidth=1, style=plot.style_linebr) p1 = plot(highestPrice, color=color.green, title="Highest Price Since Entry", linewidth=2, style=plot.style_linebr) p2 = plot(level1, color=color.yellow, title="Level 1 Exit", linewidth=1, style=plot.style_linebr) p3 = plot(level2, color=color.orange, title="Level 2 Exit", linewidth=1, style=plot.style_linebr) p4 = plot(level3, color=color.red, title="Level 3 Exit", linewidth=1, style=plot.style_linebr) p5 = plot(strategy.position_avg_price, color=color.gray, title="Entry price", linewidth = 1, style=plot.style_linebr) plotchar(strategy.position_size, "Position Size", " ") plotchar(strategy.opentrades, "Open Trades", " ") plotchar(bar_index, "Bar Index", " ") fill(p1, p2, fillColor1) fill(p2, p3, fillColor2) fill(p3, p4, fillColor3) fill(p4, p5, fillColor4)