// 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
// 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)
// 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
// 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
// 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)
code
// 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="close all")
// 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.
Certainly! Below is your updated Pine Script strategy with the requested modifications:
Close Condition Changed to EMA Cross:
Replaced the fixed amount close condition with an exit based on the crossover of two EMAs (ema1 and ema2).
EMA Parameters Made Adjustable on the Screen:
Added input fields to adjust the lengths of ema1 and ema2 directly from the strategy settings panel.
ema1 and ema2 are calculated using the ta.ema function.
Exit Conditions:
exit_long: Triggers when ema1 crosses belowema2.
exit_short: Triggers when ema1 crosses aboveema2.
Removed TP2 Exit Orders:
The fixed amount-based exit orders (TP2 and TP2_Short) have been removed to prevent closing positions based solely on take profit percentages.
Implemented EMA-Based Exit Logic:
Added conditional statements to close long and short positions when the corresponding EMA cross occurs.
Updated the strategy.close comments to reflect that the exit is triggered by an EMA cross for clarity.
Optional EMA Plots:
If you wish to visualize the EMAs on your chart, you can uncomment the plot lines and set overlay=true in the strategy declaration.
Additional Notes
Overlay Setting:
Currently, overlay=false is set, which means indicators like EMAs won't be plotted on the price chart. If you decide to plot EMAs, change it to overlay=true in the strategy declaration:
pinescript
strategy("Demo GPT - Boom Hunter Pro 1.022", overlay=true, ...)
Customizing EMA Colors:
Feel free to adjust the colors or styles of the EMA plots to better suit your preferences by modifying the plot function parameters.
Testing and Optimization:
After implementing these changes, it's advisable to backtest the strategy to ensure that the new exit conditions align with your trading objectives and perform as expected.
Let me know if you need any further modifications or assistance!