change tp2 to close all which mean close all = (tp2 - tp1), so when change the value in…
- User
change tp2 to close all which mean close all = (tp2 - tp1), so when change the value in the screen of tp1 , tp2 change auto
// This work is licensed under a Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0) // © veryfid
//@version=5 strategy("Demo GPT - Boom Hunter Pro 1.022", overlay=false, commission_type=strategy.commission.percent, commission_value=0.1, slippage=3)
// Declare errorLabel variable var label errorLabel = na
// Start and End Date Inputs startDate = input.time(defval=timestamp("01 Jan 2018"), title="Start Date", group="Backtest Period") endDate = input.time(defval=timestamp("31 Dec 2069"), title="End Date", group="Backtest Period") inDateRange = (time >= startDate) and (time <= endDate)
// Input Settings theme = 'Dark' showfib = input.bool(defval=false, title='Show Fibonacci Lines?', group='Main Settings', inline='main2') showentry = input.bool(defval=true, title='Show Entry and Exit Points?', group='Main Settings') square = input.bool(true, title='Square Line?', group='Main Settings') showdboom = input.bool(true, title='Show Downward Boom Line?', group='Main Settings') showlongs = input.bool(true, title='Show Long Entries?', group='Main Settings') showshorts = input.bool(true, title='Show Short Entries?', group='Main Settings')
// Take Profit and Stop Loss Inputs tp1_perc = input.float(defval=1.5, title="Take Profit 1 (%)", minval=0.0, step=0.1, group="Trade Settings") tp2_perc = input.float(defval=3.0, title="Take Profit 2 (%)", minval=0.0, step=0.1, group="Trade Settings")
sl_perc = input.float(defval=5.0, title="Stop Loss (%)", minval=0.0, step=0.1, group="Trade Settings")
// Take Profit Percentages for Position Size tp1_qty_perc = input.float(defval=25.0, title="TP1 Close Position (%)", minval=0.0, maxval=100.0, step=1.0, group="Trade Settings") tp2_qty_perc = input.float(defval=75.0, title="TP2 Close Position (%)", minval=0.0, maxval=100.0, step=1.0, group="Trade Settings")
// Ensure total TP percentages add up to 100% total_tp_qty = tp1_qty_perc + tp2_qty_perc
// Check if total percentages add up to 100% if total_tp_qty != 100 // Display a message on the chart to inform the user if not na(errorLabel) label.delete(errorLabel) errorLabel := label.new(bar_index, high, text="Total TP Close Position (%) does not add up to 100%", style=label.style_label_down, color=color.red, textcolor=color.white) else // Remove the error label if percentages are correct if not na(errorLabel) label.delete(errorLabel) errorLabel := na
// Quotient Inputs LPPeriod = input.int(6, title='Quotient | LPPeriod', inline='quotient', group='EOT 1 (Main Oscillator)') K1 = input.float(0.0, title='K1', inline='quotient', group='EOT 1 (Main Oscillator)') K2 = 0.3 esize = 60 ey = 50 trigno = input.int(2, 'Trigger Length', group='EOT 1 (Main Oscillator)', inline='quotient2') osccol = input.color(color.blue, title='Oscillator Color:', group='EOT 1 (Main Oscillator)', inline='q2') trigcol = input.color(color.white, title='Trigger Color:', group='EOT 1 (Main Oscillator)', inline='q2')
// EOT 2 Inputs LPPeriod2 = input.int(27, title='LPPeriod2', group='EOT 2 (Red Wave)', inline='q2') K12 = input.float(0.8, title='K1', group='EOT 2 (Red Wave)', inline='q2') K22 = input.float(0.3, title='K2', group='EOT 2 (Red Wave)', inline='q2') esize3 = 60 ey3 = 50 osccol2 = input.color(color.red, title='Line Color:', inline='quotient22', group='EOT 2 (Red Wave)') osccol22 = input.color(color.red, title='Fill Color:', inline='quotient22', group='EOT 2 (Red Wave)')
// EOT 3 Inputs LPPeriod3 = input.int(11, title='LPPeriod3', group='EOT 3 (Yellow Line)', inline='q2') K13_input = input.float(0.99, title='K1', group='EOT 3 (Yellow Line)', inline='q2') esize2 = 60 ey2 = 50 osccol3 = input.color(color.yellow, title='Line Color:', group='EOT 3 (Yellow Line)', inline='quotient3')
// Declare K13 and K33 float K13 = na float K33 = na
// Assign K13 and K33 based on 'square' input if square K13 := 0.9999 K33 := -0.9999 else K13 := K13_input K33 := -K13_input
// LSMAWT Inputs n1 = input.int(9, 'WT | master', inline='lsma', group='LSMA Wave Trend Settings') n2 = input.int(6, 'time 1', inline='lsma', group='LSMA Wave Trend Settings') n3 = input.int(3, '2', inline='lsma', group='LSMA Wave Trend Settings') n4 = input.int(21, 'LSMA | 1', inline='LSMA', group='LSMA Wave Trend Settings') n5 = input.int(0, '2', inline='LSMA', group='LSMA Wave Trend Settings') smalen = 2 lsmaline = input.int(200, title='LSMA Long', group='LSMA Wave Trend Settings', inline='LSMA')
// Initialize variables var float alpha1 = na var float HP = 0.0 var float a1 = na var float b1 = na var float c1 = na var float c2 = na var float c3 = na var float Filt = 0.0 var float Peak = 0.0 var float X = na var float Quotient1 = na var float Quotient2 = na pi = 2 * math.asin(1)
// Highpass filter cyclic components alpha1 := (math.cos(.707 * 2 * pi / 100) + math.sin(.707 * 2 * pi / 100) - 1) / math.cos(.707 * 2 * pi / 100) HP := (1 - alpha1 / 2) * (1 - alpha1 / 2) * (close - 2 * nz(close[1]) + nz(close[2])) + 2 * (1 - alpha1) * nz(HP[1]) - (1 - alpha1) * (1 - alpha1) * nz(HP[2])
// SuperSmoother Filter a1 := math.exp(-1.414 * pi / LPPeriod) b1 := 2 * a1 * math.cos(1.414 * pi / LPPeriod) c2 := b1 c3 := -a1 * a1 c1 := 1 - c2 - c3 Filt := c1 * (HP + nz(HP[1])) / 2 + c2 * nz(Filt[1]) + c3 * nz(Filt[2])
// Fast Attack - Slow Decay Algorithm Peak := .991 * nz(Peak[1]) if math.abs(Filt) > Peak Peak := math.abs(Filt) if Peak != 0.0 X := Filt / Peak else X := 0.0
Quotient1 := (X + K1) / (K1 * X + 1) Quotient2 := (X + K2) / (K2 * X + 1)
// EOT 2 Calculations var float alpha1222 = na var float HP2 = 0.0 var float a12 = na var float b12 = na var float c12 = na var float c22_var = na var float c32 = na var float Filt2 = 0.0 var float Peak2 = 0.0 var float X2 = na var float Quotient3 = na var float Quotient4 = na
alpha1222 := (math.cos(.707 * 2 * pi / 100) + math.sin(.707 * 2 * pi / 100) - 1) / math.cos(.707 * 2 * pi / 100) HP2 := (1 - alpha1222 / 2) * (1 - alpha1222 / 2) * (close - 2 * nz(close[1]) + nz(close[2])) + 2 * (1 - alpha1222) * nz(HP2[1]) - (1 - alpha1222) * (1 - alpha1222) * nz(HP2[2])
// SuperSmoother Filter a12 := math.exp(-1.414 * pi / LPPeriod2) b12 := 2 * a12 * math.cos(1.414 * pi / LPPeriod2) c22_var := b12 c32 := -a12 * a12 c12 := 1 - c22_var - c32 Filt2 := c12 * (HP2 + nz(HP2[1])) / 2 + c22_var * nz(Filt2[1]) + c32 * nz(Filt2[2])
// Fast Attack - Slow Decay Algorithm Peak2 := .991 * nz(Peak2[1]) if math.abs(Filt2) > Peak2 Peak2 := math.abs(Filt2) if Peak2 != 0.0 X2 := Filt2 / Peak2 else X2 := 0.0
Quotient3 := (X2 + K12) / (K12 * X2 + 1) Quotient4 := (X2 + K22) / (K22 * X2 + 1)
// EOT 3 Calculations var float alpha1333 = na var float HP3 = 0.0 var float a13 = na var float b13 = na var float c13 = na var float c33_var = na var float c333 = na var float Filt3 = 0.0 var float Peak3 = 0.0 var float X3 = na var float Quotient5 = na var float Quotient6 = na
alpha1333 := (math.cos(.707 * 2 * pi / 100) + math.sin(.707 * 2 * pi / 100) - 1) / math.cos(.707 * 2 * pi / 100) HP3 := (1 - alpha1333 / 3) * (1 - alpha1333 / 2) * (close - 2 * nz(close[1]) + nz(close[2])) + 2 * (1 - alpha1333) * nz(HP3[1]) - (1 - alpha1333) * (1 - alpha1333) * nz(HP3[2])
// SuperSmoother Filter a13 := math.exp(-1.414 * pi / LPPeriod3) b13 := 2 * a13 * math.cos(1.414 * pi / LPPeriod3) c33_var := b13 c333 := -a13 * a13 c13 := 1 - c33_var - c333 Filt3 := c13 * (HP3 + nz(HP3[1])) / 2 + c33_var * nz(Filt3[1]) + c333 * nz(Filt3[2])
// Fast Attack - Slow Decay Algorithm Peak3 := .991 * nz(Peak3[1]) if math.abs(Filt3) > Peak3 Peak3 := math.abs(Filt3) if Peak3 != 0.0 X3 := Filt3 / Peak3 else X3 := 0.0
Quotient5 := (X3 + K13) / (K13 * X3 + 1) Quotient6 := (X3 + K33) / (K33 * X3 + 1)
// LSMAWT Calculations tci(src) => ta.ema((src - ta.ema(src, n1)) / (0.025 * ta.ema(math.abs(src - ta.ema(src, n1)), n1)), n2) + 50
mf(src) => 100.0 - 100.0 / (1.0 + math.sum(volume * (ta.change(src) <= 0 ? 0 : src), n3) / math.sum(volume * (ta.change(src) >= 0 ? 0 : src), n3))
tradition(src) => math.avg(tci(src), mf(src), ta.rsi(src, n3))
src5 = hlc3 wt1 = tradition(src5) wt2 = ta.sma(wt1, 6) wt3 = ta.linreg(wt1, n4, n5) wt4 = ta.ema((wt1 - wt2) * 2 + 50, n3) trig = ta.sma(wt3, smalen)
// Quotient Calculations q1 = Quotient1 * esize + ey q2 = Quotient2 * esize + ey q3 = Quotient3 * esize + ey q4 = Quotient4 * esize + ey q5 = Quotient5 * esize2 + ey2 q6 = Quotient6 * esize2 + ey2 trigger = ta.sma(q1, trigno)
// Entry and Exit Conditions sma200 = ta.sma(close, 200) line1 = -0.9 over = ta.cross(Quotient5, Quotient6) and (Quotient5 > 0.5) over3 = ta.cross(Quotient3, Quotient4) and (Quotient3 > 0) crossover = ta.crossover(q1, trigger) crossunder= ta.crossunder(q1, trigger)
// Additional Conditions warn = ta.crossover(Quotient3, -0.9) warn2 = ta.crossover(Quotient1, -0.9) warn3 = ta.crossunder(Quotient1, 0.9) enter3 = (Quotient3 <= -0.9) and crossover and (ta.barssince(warn2) <= 7) and (q1 <= 20) and (ta.barssince(ta.crossover(q1, 20)) <= 21) enter5 = (ta.barssince((q1 <= 0) and crossunder) <= 5) and crossover enter6 = (ta.barssince((q1 <= 20) and crossunder) <= 11) and crossover enter7 = (Quotient3 <= -0.9) and crossover
// Short Conditions senter3 = (Quotient3 >= -0.9) and crossunder and (ta.barssince(warn3) <= 7) and (q1 >= 99) and (ta.barssince(ta.crossover(q1, 80)) <= 21)
// Trade Execution Logic if inDateRange // Long Entry Conditions longCondition = showlongs and (enter6 and (q1 <= 60) or enter7 or enter5 or enter3) if longCondition and strategy.position_size <= 0 if strategy.position_size < 0 strategy.close(id="Short Entry", comment="Close Short") strategy.entry(id="Long Entry", direction=strategy.long, comment="Long Entry") if showentry label.new(bar_index, low, "Buy", style=label.style_label_up, color=color.new(color.green, 0), textcolor=color.white, size=size.tiny)
// Calculate Take Profit and Stop Loss Prices tp1_price = close * (1 + tp1_perc / 100) tp2_price = close * (1 + tp2_perc / 100) sl_price = close * (1 - sl_perc / 100) // Set Take Profit and Stop Loss Orders strategy.exit(id="TP1", from_entry="Long Entry", qty_percent=tp1_qty_perc, limit=tp1_price, stop=sl_price, comment="Take Profit 1") strategy.exit(id="TP2", from_entry="Long Entry", qty_percent=tp2_qty_perc, limit=tp2_price, stop=sl_price, comment="Take Profit 2") // Short Entry Conditions shortCondition = showshorts and senter3 if shortCondition and strategy.position_size >= 0 if strategy.position_size > 0 strategy.close(id="Long Entry", comment="Close Long") strategy.entry(id="Short Entry", direction=strategy.short, comment="Short Entry") if showentry label.new(bar_index, high, "Sell", style=label.style_label_down, color=color.new(color.red, 0), textcolor=color.white, size=size.tiny) // Calculate Take Profit and Stop Loss Prices for Short Position tp1_price = close * (1 - tp1_perc / 100) tp2_price = close * (1 - tp2_perc / 100) sl_price = close * (1 + sl_perc / 100) // Set Take Profit and Stop Loss Orders strategy.exit(id="TP1_Short", from_entry="Short Entry", qty_percent=tp1_qty_perc, limit=tp1_price, stop=sl_price, comment="Take Profit 1 Short") strategy.exit(id="TP2_Short", from_entry="Short Entry", qty_percent=tp2_qty_perc, limit=tp2_price, stop=sl_price, comment="Take Profit 2 Short") // Exit Long Position if strategy.position_size > 0 and (over or over3) strategy.close(id="Long Entry", comment="Exit Long") if showentry label.new(bar_index, high, "Sell", style=label.style_label_down, color=color.new(color.red, 0), textcolor=color.white, size=size.tiny) // Exit Short Position if strategy.position_size < 0 and (over or over3) strategy.close(id="Short Entry", comment="Exit Short") if showentry label.new(bar_index, low, "Buy", style=label.style_label_up, color=color.new(color.green, 0), textcolor=color.white, size=size.tiny)// Note: All plotting functions have been removed as per your request. The labels indicate entry and exit points.
- Assistant
To implement your desired changes—making
TP2automatically adjust based onTP1and ensuring thatTP2effectively closes the remaining position—you can modify your Pine Script as follows:-
Remove the Separate Input for
TP2 Close Position (%): Since you wantTP2to always close the remaining portion of the position afterTP1is closed, you don't need a separate input forTP2. Instead,TP2will automatically be set to100% - TP1. -
Update the Take Profit Logic: Adjust the
strategy.exitfunctions to reflect this change, ensuring thatTP2always closes the remaining position. -
Remove Validation for Total Percentages: Since
TP2is now derived directly fromTP1, you no longer need to validate thatTP1andTP2add up to100%.
Below is the modified version of your script with these changes implemented:
// This work is licensed under a Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0) // © veryfid //@version=5 strategy("Demo GPT - Boom Hunter Pro 1.022", overlay=false, commission_type=strategy.commission.percent, commission_value=0.1, slippage=3) // Declare errorLabel variable var label errorLabel = na // Start and End Date Inputs startDate = input.time(defval=timestamp("01 Jan 2018"), title="Start Date", group="Backtest Period") endDate = input.time(defval=timestamp("31 Dec 2069"), title="End Date", group="Backtest Period") inDateRange = (time >= startDate) and (time <= endDate) // Input Settings theme = 'Dark' showfib = input.bool(defval=false, title='Show Fibonacci Lines?', group='Main Settings', inline='main2') showentry = input.bool(defval=true, title='Show Entry and Exit Points?', group='Main Settings') square = input.bool(true, title='Square Line?', group='Main Settings') showdboom = input.bool(true, title='Show Downward Boom Line?', group='Main Settings') showlongs = input.bool(true, title='Show Long Entries?', group='Main Settings') showshorts = input.bool(true, title='Show Short Entries?', group='Main Settings') // Take Profit and Stop Loss Inputs tp1_perc = input.float(defval=1.5, title="Take Profit 1 (%)", minval=0.0, step=0.1, group="Trade Settings") tp2_perc = input.float(defval=3.0, title="Take Profit 2 (%)", minval=0.0, step=0.1, group="Trade Settings") sl_perc = input.float(defval=5.0, title="Stop Loss (%)", minval=0.0, step=0.1, group="Trade Settings") // Take Profit Percentages for Position Size tp1_qty_perc = input.float(defval=25.0, title="TP1 Close Position (%)", minval=0.0, maxval=100.0, step=1.0, group="Trade Settings") tp2_qty_perc = 100.0 - tp1_qty_perc // Automatically set TP2 to close the remaining position // Remove the following lines as they are no longer needed // Ensure total TP percentages add up to 100% // total_tp_qty = tp1_qty_perc + tp2_qty_perc // Check if total percentages add up to 100% // if total_tp_qty != 100 // if not na(errorLabel) // label.delete(errorLabel) // errorLabel := label.new(bar_index, high, text="Total TP Close Position (%) does not add up to 100%", style=label.style_label_down, color=color.red, textcolor=color.white) // else // if not na(errorLabel) // label.delete(errorLabel) // errorLabel := na // Quotient Inputs LPPeriod = input.int(6, title='Quotient | LPPeriod', inline='quotient', group='EOT 1 (Main Oscillator)') K1 = input.float(0.0, title='K1', inline='quotient', group='EOT 1 (Main Oscillator)') K2 = 0.3 esize = 60 ey = 50 trigno = input.int(2, 'Trigger Length', group='EOT 1 (Main Oscillator)', inline='quotient2') osccol = input.color(color.blue, title='Oscillator Color:', group='EOT 1 (Main Oscillator)', inline='q2') trigcol = input.color(color.white, title='Trigger Color:', group='EOT 1 (Main Oscillator)', inline='q2') // EOT 2 Inputs LPPeriod2 = input.int(27, title='LPPeriod2', group='EOT 2 (Red Wave)', inline='q2') K12 = input.float(0.8, title='K1', group='EOT 2 (Red Wave)', inline='q2') K22 = input.float(0.3, title='K2', group='EOT 2 (Red Wave)', inline='q2') esize3 = 60 ey3 = 50 osccol2 = input.color(color.red, title='Line Color:', inline='quotient22', group='EOT 2 (Red Wave)') osccol22 = input.color(color.red, title='Fill Color:', inline='quotient22', group='EOT 2 (Red Wave)') // EOT 3 Inputs LPPeriod3 = input.int(11, title='LPPeriod3', group='EOT 3 (Yellow Line)', inline='q2') K13_input = input.float(0.99, title='K1', group='EOT 3 (Yellow Line)', inline='q2') esize2 = 60 ey2 = 50 osccol3 = input.color(color.yellow, title='Line Color:', group='EOT 3 (Yellow Line)', inline='quotient3') // Declare K13 and K33 float K13 = na float K33 = na // Assign K13 and K33 based on 'square' input if square K13 := 0.9999 K33 := -0.9999 else K13 := K13_input K33 := -K13_input // LSMAWT Inputs n1 = input.int(9, 'WT | master', inline='lsma', group='LSMA Wave Trend Settings') n2 = input.int(6, 'time 1', inline='lsma', group='LSMA Wave Trend Settings') n3 = input.int(3, '2', inline='lsma', group='LSMA Wave Trend Settings') n4 = input.int(21, 'LSMA | 1', inline='LSMA', group='LSMA Wave Trend Settings') n5 = input.int(0, '2', inline='LSMA', group='LSMA Wave Trend Settings') smalen = 2 lsmaline = input.int(200, title='LSMA Long', group='LSMA Wave Trend Settings', inline='LSMA') // Initialize variables var float alpha1 = na var float HP = 0.0 var float a1 = na var float b1 = na var float c1 = na var float c2 = na var float c3 = na var float Filt = 0.0 var float Peak = 0.0 var float X = na var float Quotient1 = na var float Quotient2 = na pi = 2 * math.asin(1) // Highpass filter cyclic components alpha1 := (math.cos(.707 * 2 * pi / 100) + math.sin(.707 * 2 * pi / 100) - 1) / math.cos(.707 * 2 * pi / 100) HP := (1 - alpha1 / 2) * (1 - alpha1 / 2) * (close - 2 * nz(close[1]) + nz(close[2])) + 2 * (1 - alpha1) * nz(HP[1]) - (1 - alpha1) * (1 - alpha1) * nz(HP[2]) // SuperSmoother Filter a1 := math.exp(-1.414 * pi / LPPeriod) b1 := 2 * a1 * math.cos(1.414 * pi / LPPeriod) c2 := b1 c3 := -a1 * a1 c1 := 1 - c2 - c3 Filt := c1 * (HP + nz(HP[1])) / 2 + c2 * nz(Filt[1]) + c3 * nz(Filt[2]) // Fast Attack - Slow Decay Algorithm Peak := .991 * nz(Peak[1]) if math.abs(Filt) > Peak Peak := math.abs(Filt) if Peak != 0.0 X := Filt / Peak else X := 0.0 Quotient1 := (X + K1) / (K1 * X + 1) Quotient2 := (X + K2) / (K2 * X + 1) // EOT 2 Calculations var float alpha1222 = na var float HP2 = 0.0 var float a12 = na var float b12 = na var float c12 = na var float c22_var = na var float c32 = na var float Filt2 = 0.0 var float Peak2 = 0.0 var float X2 = na var float Quotient3 = na var float Quotient4 = na alpha1222 := (math.cos(.707 * 2 * pi / 100) + math.sin(.707 * 2 * pi / 100) - 1) / math.cos(.707 * 2 * pi / 100) HP2 := (1 - alpha1222 / 2) * (1 - alpha1222 / 2) * (close - 2 * nz(close[1]) + nz(close[2])) + 2 * (1 - alpha1222) * nz(HP2[1]) - (1 - alpha1222) * (1 - alpha1222) * nz(HP2[2]) // SuperSmoother Filter a12 := math.exp(-1.414 * pi / LPPeriod2) b12 := 2 * a12 * math.cos(1.414 * pi / LPPeriod2) c22_var := b12 c32 := -a12 * a12 c12 := 1 - c22_var - c32 Filt2 := c12 * (HP2 + nz(HP2[1])) / 2 + c22_var * nz(Filt2[1]) + c32 * nz(Filt2[2]) // Fast Attack - Slow Decay Algorithm Peak2 := .991 * nz(Peak2[1]) if math.abs(Filt2) > Peak2 Peak2 := math.abs(Filt2) if Peak2 != 0.0 X2 := Filt2 / Peak2 else X2 := 0.0 Quotient3 := (X2 + K12) / (K12 * X2 + 1) Quotient4 := (X2 + K22) / (K22 * X2 + 1) // EOT 3 Calculations var float alpha1333 = na var float HP3 = 0.0 var float a13 = na var float b13 = na var float c13 = na var float c33_var = na var float c333 = na var float Filt3 = 0.0 var float Peak3 = 0.0 var float X3 = na var float Quotient5 = na var float Quotient6 = na alpha1333 := (math.cos(.707 * 2 * pi / 100) + math.sin(.707 * 2 * pi / 100) - 1) / math.cos(.707 * 2 * pi / 100) HP3 := (1 - alpha1333 / 3) * (1 - alpha1333 / 2) * (close - 2 * nz(close[1]) + nz(close[2])) + 2 * (1 - alpha1333) * nz(HP3[1]) - (1 - alpha1333) * (1 - alpha1333) * nz(HP3[2]) // SuperSmoother Filter a13 := math.exp(-1.414 * pi / LPPeriod3) b13 := 2 * a13 * math.cos(1.414 * pi / LPPeriod3) c33_var := b13 c333 := -a13 * a13 c13 := 1 - c33_var - c333 Filt3 := c13 * (HP3 + nz(HP3[1])) / 2 + c33_var * nz(Filt3[1]) + c333 * nz(Filt3[2]) // Fast Attack - Slow Decay Algorithm Peak3 := .991 * nz(Peak3[1]) if math.abs(Filt3) > Peak3 Peak3 := math.abs(Filt3) if Peak3 != 0.0 X3 := Filt3 / Peak3 else X3 := 0.0 Quotient5 := (X3 + K13) / (K13 * X3 + 1) Quotient6 := (X3 + K33) / (K33 * X3 + 1) // LSMAWT Calculations tci(src) => ta.ema((src - ta.ema(src, n1)) / (0.025 * ta.ema(math.abs(src - ta.ema(src, n1)), n1)), n2) + 50 mf(src) => 100.0 - 100.0 / (1.0 + math.sum(volume * (ta.change(src) <= 0 ? 0 : src), n3) / math.sum(volume * (ta.change(src) >= 0 ? 0 : src), n3)) tradition(src) => math.avg(tci(src), mf(src), ta.rsi(src, n3)) src5 = hlc3 wt1 = tradition(src5) wt2 = ta.sma(wt1, 6) wt3 = ta.linreg(wt1, n4, n5) wt4 = ta.ema((wt1 - wt2) * 2 + 50, n3) trig = ta.sma(wt3, smalen) // Quotient Calculations q1 = Quotient1 * esize + ey q2 = Quotient2 * esize + ey q3 = Quotient3 * esize + ey q4 = Quotient4 * esize + ey q5 = Quotient5 * esize2 + ey2 q6 = Quotient6 * esize2 + ey2 trigger = ta.sma(q1, trigno) // Entry and Exit Conditions sma200 = ta.sma(close, 200) line1 = -0.9 over = ta.cross(Quotient5, Quotient6) and (Quotient5 > 0.5) over3 = ta.cross(Quotient3, Quotient4) and (Quotient3 > 0) crossover = ta.crossover(q1, trigger) crossunder= ta.crossunder(q1, trigger) // Additional Conditions warn = ta.crossover(Quotient3, -0.9) warn2 = ta.crossover(Quotient1, -0.9) warn3 = ta.crossunder(Quotient1, 0.9) enter3 = (Quotient3 <= -0.9) and crossover and (ta.barssince(warn2) <= 7) and (q1 <= 20) and (ta.barssince(ta.crossover(q1, 20)) <= 21) enter5 = (ta.barssince((q1 <= 0) and crossunder) <= 5) and crossover enter6 = (ta.barssince((q1 <= 20) and crossunder) <= 11) and crossover enter7 = (Quotient3 <= -0.9) and crossover // Short Conditions senter3 = (Quotient3 >= -0.9) and crossunder and (ta.barssince(warn3) <= 7) and (q1 >= 99) and (ta.barssince(ta.crossover(q1, 80)) <= 21) // Trade Execution Logic if inDateRange // Long Entry Conditions longCondition = showlongs and (enter6 and (q1 <= 60) or enter7 or enter5 or enter3) if longCondition and strategy.position_size <= 0 if strategy.position_size < 0 strategy.close(id="Short Entry", comment="Close Short") strategy.entry(id="Long Entry", direction=strategy.long, comment="Long Entry") if showentry label.new(bar_index, low, "Buy", style=label.style_label_up, color=color.new(color.green, 0), textcolor=color.white, size=size.tiny) // Calculate Take Profit and Stop Loss Prices tp1_price = close * (1 + tp1_perc / 100) tp2_price = close * (1 + tp2_perc / 100) sl_price = close * (1 - sl_perc / 100) // Set Take Profit and Stop Loss Orders strategy.exit(id="TP1", from_entry="Long Entry", qty_percent=tp1_qty_perc, limit=tp1_price, stop=sl_price, comment="Take Profit 1") strategy.exit(id="TP2", from_entry="Long Entry", qty_percent=tp2_qty_perc, limit=tp2_price, stop=sl_price, comment="Take Profit 2") // Short Entry Conditions shortCondition = showshorts and senter3 if shortCondition and strategy.position_size >= 0 if strategy.position_size > 0 strategy.close(id="Long Entry", comment="Close Long") strategy.entry(id="Short Entry", direction=strategy.short, comment="Short Entry") if showentry label.new(bar_index, high, "Sell", style=label.style_label_down, color=color.new(color.red, 0), textcolor=color.white, size=size.tiny) // Calculate Take Profit and Stop Loss Prices for Short Position tp1_price = close * (1 - tp1_perc / 100) tp2_price = close * (1 - tp2_perc / 100) sl_price = close * (1 + sl_perc / 100) // Set Take Profit and Stop Loss Orders strategy.exit(id="TP1_Short", from_entry="Short Entry", qty_percent=tp1_qty_perc, limit=tp1_price, stop=sl_price, comment="Take Profit 1 Short") strategy.exit(id="TP2_Short", from_entry="Short Entry", qty_percent=tp2_qty_perc, limit=tp2_price, stop=sl_price, comment="Take Profit 2 Short") // Exit Long Position if strategy.position_size > 0 and (over or over3) strategy.close(id="Long Entry", comment="Exit Long") if showentry label.new(bar_index, high, "Sell", style=label.style_label_down, color=color.new(color.red, 0), textcolor=color.white, size=size.tiny) // Exit Short Position if strategy.position_size < 0 and (over or over3) strategy.close(id="Short Entry", comment="Exit Short") if showentry label.new(bar_index, low, "Buy", style=label.style_label_up, color=color.new(color.green, 0), textcolor=color.white, size=size.tiny) // Note: All plotting functions have been removed as per your request. The labels indicate entry and exit points.Summary of Changes
-
Automated
TP2 Close Position (%):// Take Profit Percentages for Position Size tp1_qty_perc = input.float(defval=25.0, title="TP1 Close Position (%)", minval=0.0, maxval=100.0, step=1.0, group="Trade Settings") tp2_qty_perc = 100.0 - tp1_qty_perc // Automatically set TP2 to close the remaining position -
Removed Validation for Total Percentages: The section that checks if
tp1_qty_perc + tp2_qty_percequals 100% has been removed sincetp2_qty_percis now automatically derived. -
Ensured
TP2Closes the Remaining Position: By settingtp2_qty_percto100.0 - tp1_qty_perc, you ensure that after taking profit atTP1, the remaining position is entirely closed atTP2.
Additional Recommendations
-
Dynamic Take Profit Levels: If you want
TP2to be dynamically calculated based onTP1_perc(e.g.,TP2_perc = TP1_perc + some_increment), you can adjust the logic accordingly. However, based on your description, settingTP2_qty_percto100% - TP1_qty_percachieves the "close all" functionality. -
User Interface Messaging: Since the automatic calculation ensures that
TP1andTP2always sum to 100%, there's no longer a need to display error messages regarding percentage totals. This simplifies the user interface and reduces potential confusion. -
Testing: After implementing these changes, thoroughly backtest the strategy to ensure that the take profit orders execute as expected and that positions are closed appropriately.
Feel free to reach out if you need further modifications or have any additional questions!
-