SuPeR-RePaNoCHa #2TP#
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Xaviz

//#####©ÉÉÉÉ¶N###############################################
//####*..´´´´´´,,,»ëN########################################
//###ë..´´´´´´,,,,,,''%©#####################################
//###'´´´´´´,,,,,,,'''''?¶###################################
//##o´´´´´´,,,,,,,''''''''*©#################################
//##'´´´´´,,,,,,,'''''''^^^~±################################
//#±´´´´´,,,,,,,''''''''^í/;~*©####æ%;í»~~~~;==I±N###########
//#»´´´´,,,,,,'''''''''^;////;»¶X/í~~/~~~;=~~~~~~~~*¶########
//#'´´´,,,,,,''''''''^^;////;%I^~/~~/~~~=~~~;=?;~~~~;?ë######
//©´´,,,,,,,''''''''^^~/////X~/~~/~~/~~»í~~=~~~~~~~~~~^;É####
//¶´,,,,,,,''''''''^^^;///;%;~/~~;í~~»~í?~?~~~?I/~~~~?*=íÑ###
//N,,,,,,,'''''''^^^^^///;;o/~~;;~~;£=»í»;IX/=~~~~~~^^^^'*æ##
//#í,,,,,''''''''^^^^^;;;;;o~»~~~~íX//~/»~;í?IíI»~~^/*?'''=N#
//#%,,,'''''''''^^^^^^í;;;;£;~~~//»I»/£X/X/»í*&~~~^^^^'^*~'É#
//#©,,''''''''^^^^^^^^~;;;;&/~/////*X;í;o*í»~=*?*===^'''''*£#
//##&''''''''^^^^^^^^^^~;;;;X=í~~~»;;;/~;í»~»±;^^^^^';=''''É#
//##N^''''''^^^^^^^^^^~~~;;;;/£;~~/»~~»~~///o~~^^^^''''?^',æ#
//###Ñ''''^^^^^^^^^^^~~~~~;;;;;í*X*í»;~~IX?~~^^^^/?'''''=,=##
//####X'''^^^^^^^^^^~~~~~~~~;;íííííí~~í*=~~~~Ií^'''=''''^»©##
//#####£^^^^^^^^^^^~~~~~~~~~~~íííííí~~~~~*~^^^;/''''='',,N###
//######æ~^^^^^^^^~~~~~~~~~~~~~~íííí~~~~~^*^^^'=''''?',,§####
//########&^^^^^^~~~~~~~~~~~~~~~~~~~~~~~^^=^^''=''''?,íN#####
//#########N?^^~~~~~~~~~~~~~~~~~~~~~~~~^^^=^''^?''';í@#######
//###########N*~~~~~~~~~~~~~~~~~~~~~~~^^^*'''^='''/É#########
//##############@;~~~~~~~~~~~~~~~~~~~^^~='''~?'';É###########
//#################É=~~~~~~~~~~~~~~^^^*~'''*~?§##############
//#####################N§£I/~~~~~~»*?~»o§æN##################

//@version=4
strategy(title = "SuPeR-RePaNoCHa #2TP#", overlay = true, initial_capital = 10000, pyramiding = 100, currency = "USD",
   calc_on_order_fills = false, calc_on_every_tick = false, default_qty_type = strategy.fixed, default_qty_value = 1, commission_value = 0.03)

//study(title="SuPeR-RePaNoCHa #2TP#", overlay=true)

// ================================================================================================================================================================================
// VARIABLES
// ================================================================================================================================================================================

// Long/Short
var bool longCond = na, var bool shortCond = na, longCond := nz(longCond[1]), shortCond := nz(shortCond[1])
var bool XlongCond = na, var bool XshortCond = na, XlongCond := nz(XlongCond[1]), XshortCond := nz(XshortCond[1])
var int CondIni_long0 = 0, var int CondIni_short0 = 0, CondIni_long0 := nz(CondIni_long0[1]), CondIni_short0 := nz(CondIni_short0[1])
var int CondIni_long = 0, var int CondIni_short = 0, CondIni_long := nz(CondIni_long[1]), CondIni_short := nz(CondIni_short[1])
var int CondIniX0 = 0, var int CondIniX = 0, CondIniX0 := nz(CondIniX0[1]), CondIniX := nz(CondIniX[1])
var bool Final_longCondition0 = na, var bool Final_shortCondition0 = na, Final_longCondition0 := nz(Final_longCondition0[1]), Final_shortCondition0 := nz(Final_shortCondition0[1])
var bool Final_longCondition = na, var bool Final_shortCondition = na, Final_longCondition := nz(Final_longCondition[1]), Final_shortCondition := nz(Final_shortCondition[1])
var bool BT_Final_longCondition = na, var bool BT_Final_shortCondition = na, BT_Final_longCondition := nz(BT_Final_longCondition[1]), BT_Final_shortCondition := nz(BT_Final_shortCondition[1])
var float last_open_longCondition = na, var float last_open_shortCondition = na
var int last_longCondition0 = na, var int last_shortCondition0 = na
var int last_longCondition = na, var int last_shortCondition = na
var int last_XlongCondition0 = na, var int last_XshortCondition0 = na
var int last_XlongCondition = na, var int last_XshortCondition = na
var int nLongs = na, var int nShorts = na, nLongs := nz(nLongs[1]), nShorts := nz(nShorts[1])
// Xlong/Xshort
var bool Final_XlongCondition0 = na, var bool Final_XshortCondition0 = na, Final_XlongCondition0 := nz(Final_XlongCondition0[1]), Final_XshortCondition0 := nz(Final_XshortCondition0[1])
var bool Final_XlongCondition = na, var bool Final_XshortCondition = na, Final_XlongCondition := nz(Final_XlongCondition[1]), Final_XshortCondition := nz(Final_XshortCondition[1])
var int CondIni_Xlong0 = 0, var int CondIni_Xshort0 = 0, CondIni_Xlong0 := nz(CondIni_Xlong0[1]), CondIni_Xshort0 := nz(CondIni_Xshort0[1])
var int CondIni_Xlong = 0, var int CondIni_Xshort = 0, CondIni_Xlong := nz(CondIni_Xlong[1]), CondIni_Xshort := nz(CondIni_Xshort[1])
// Take profit
var bool long_tp = na, var bool short_tp = na
var bool long_tp2 = na, var bool short_tp2 = na
var bool long_tp_pump_last_minute = na, var bool short_tp_pump_last_minute = na
var bool long_tp_pump = na, var bool short_tp_pump = na
var int last_long_tp = na, var int last_short_tp = na
var int last_long_tp_pump = na, var int last_short_tp_pump = na
var int last_long_tp2 = na, var int last_short_tp2 = na
var bool Final_Long_tp = na, var bool Final_Short_tp = na, Final_Long_tp := nz(Final_Long_tp[1]), Final_Short_tp := nz(Final_Short_tp[1])
var bool Final_Long_tp2 = na, var bool Final_Short_tp2 = na, Final_Long_tp2 := nz(Final_Long_tp2[1]), Final_Short_tp2 := nz(Final_Short_tp2[1])
var bool Final_Long_tp_pump = na, var bool Final_Short_tp_pump = na
// Stop Loss
var int CondIni_long_sl = 0, var int CondIni_short_sl = 0
var bool Final_Long_sl0 = na, var bool Final_Short_sl0 = na, Final_Long_sl0 := nz(Final_Long_sl0[1]), Final_Short_sl0 := nz(Final_Short_sl0[1])
var bool Final_Long_sl = na, var bool Final_Short_sl = na, Final_Long_sl := nz(Final_Long_sl[1]), Final_Short_sl := nz(Final_Short_sl[1])
var int last_long_sl = na, var int last_short_sl = na
// Indicators
var bool JMA_longCond = na, var bool JMA_shortCond = na
var bool RF_longCond = na, var bool RF_shortCond = na
var bool ADX_longCond = na, var bool ADX_shortCond = na
var bool SAR_longCond = na, var bool SAR_shortCond = na
var bool RSI_longCond = na, var bool RSI_shortCond = na
var bool MACD_longCond = na, var bool MACD_shortCond = na
var bool VOL_longCond = na, var bool VOL_shortCond = na
var bool JMA_XlongCond = na, var bool JMA_XshortCond = na
var bool RF_XlongCond = na, var bool RF_XshortCond = na
var bool ADX_XlongCond = na, var bool ADX_XshortCond = na
var bool SAR_XlongCond = na, var bool SAR_XshortCond = na

// ================================================================================================================================================================================
// INITIAL SETTINGS
// ================================================================================================================================================================================

// Market side and source inputs 
Position = input("BOTH", "LONG / SHORT", options = ["BOTH","LONG","SHORT"])
src = hlc3
is_Long = Position == "SHORT" ? na : true
is_Short = Position == "LONG" ? na : true

// ================================================================================================================================================================================
// JURIK MOVING AVERAGE
// ================================================================================================================================================================================

// JMA inputs
Act_JMA = input(true, "JURIK MOVING AVERAGE")
length = input(22, title="JMA LENGTH", type=input.integer, minval = 0)
phase = input(22, title="JMA PHASE", type=input.integer, minval = 0)
power = input(2, title="JMA POWER", type=input.float, minval = 0, step = 0.5)

// JMA calculation
JMA(src)=>
    phaseRatio = phase < -100 ? 0.5 : phase > 100 ? 2.5 : phase / 100 + 1.5
    beta = 0.45 * (length - 1) / (0.45 * (length - 1) + 2)
    alpha = pow(beta, power)
    jma = 0.0
    e0 = 0.0
    e0 := (1 - alpha) * src + alpha * nz(e0[1])
    e1 = 0.0
    e1 := (src - e0) * (1 - beta) + beta * nz(e1[1])
    e2 = 0.0
    e2 := (e0 + phaseRatio * e1 - nz(jma[1])) * pow(1 - alpha, 2) + pow(alpha, 2) * nz(e2[1])
    jma := e2 + nz(jma[1])
    
// Defining JMA trend
JMA_Rising = JMA(src) > JMA(src)[1]
JMA_Falling = JMA(src) < JMA(src)[1]

// JMA Plotting
JMA_color = JMA_Rising ? color.green : JMA_Falling ? color.red : color.yellow
plot(Act_JMA ? JMA(src) : na, color=JMA_color, linewidth = 3, title= "JMA")

// ================================================================================================================================================================================
// RANGE FILTER
// ================================================================================================================================================================================

// Range Filter inputs
Act_RF = input(true, "RANGE FILTER")
per = input(defval=42, title="SAMPLING PERIOD", minval=1)
mult = input(defval=1.5, title="RANGE MULTIPLIER", minval=0.1, step = 0.1)

// Range Filter calculation
Range_filter(_src, _per, _mult)=>
    var float _upward = 0.0
    var float _downward = 0.0
    wper = (_per*2) - 1
    avrng = ema(abs(_src - _src[1]), _per)
    _smoothrng = ema(avrng, wper)*_mult
    _filt = _src
    _filt := _src > nz(_filt[1]) ? ((_src-_smoothrng) < nz(_filt[1]) ? nz(_filt[1]) : (_src-_smoothrng)) : ((_src+_smoothrng) > nz(_filt[1]) ? nz(_filt[1]) : (_src+_smoothrng))
    _upward := _filt > _filt[1] ? nz(_upward[1]) + 1 : _filt < _filt[1] ? 0 : nz(_upward[1])
    _downward := _filt < _filt[1] ? nz(_downward[1]) + 1 : _filt > _filt[1] ? 0 : nz(_downward[1])
    [_smoothrng,_filt,_upward,_downward]
    
// Defining variables for include in future conditions
[smoothrng, filt, upward, downward] = Range_filter(src, per, mult)

// Defining high and low bands
hband = filt + smoothrng
lband = filt - smoothrng

// Range Filter Plotting
filtcolor = upward > 0 ? color.lime : downward > 0 ? color.red : color.orange
filtplot = plot(Act_RF ? filt : na, color = filtcolor, linewidth = 3, title="Range Filter", editable = false)
hbandplot = plot(Act_RF ? hband : na, color = color.aqua, transp = 60, title = "High Target", editable = false)
lbandplot = plot(Act_RF ? lband : na, color = color.aqua, transp = 60, title = "Low Target", editable = false)
fill(hbandplot, filtplot, color = color.aqua, title = "High Target Range", editable = false)
fill(lbandplot, filtplot, color = color.aqua, title = "Low Target Range", editable = false)

// ================================================================================================================================================================================
// ADX
// ================================================================================================================================================================================

// ADX inputs
Act_ADX = input(true, "ORIGINAL AVERAGE DIRECTIONAL INDEX")
ADX_len = input(17, title="ADX LENGTH", type=input.integer, minval = 1)
th = input(17, title="ADX THRESHOLD", type=input.integer, minval = 0)

// ADX calculating
calcADX(_len)=>
    up = change(high)
	down = -change(low)
	plusDM = na(up) ? na : (up > down and up > 0 ? up : 0)
    minusDM = na(down) ? na : (down > up and down > 0 ? down : 0)
	truerange = rma(tr, _len)
	_plus = fixnan(100 * rma(plusDM, _len) / truerange)
	_minus = fixnan(100 * rma(minusDM, _len) / truerange)
	sum = _plus + _minus
	_adx = 100 * rma(abs(_plus - _minus) / (sum == 0 ? 1 : sum), _len)
    [_plus,_minus,_adx] 
 
 // Defining variables for include in future conditions   
[DIPlus,DIMinus,ADX] = calcADX(ADX_len)

// Plotting ADX bar colors
macol = DIPlus > DIMinus and ADX > th ? color.lime : DIPlus < DIMinus and ADX > th ? color.red : color.orange
barcolor(color = Act_ADX ? macol : na, title = "ADX")

// ================================================================================================================================================================================
// SAR
// ================================================================================================================================================================================

// SAR inputs
Act_SAR = input(true, "PARABOLIC SAR")
Sst = input (0.25, "SAR STAR", step=0.01, minval = 0.01)
Sinc = input (0.5, "SAR INC", step=0.01, minval = 0.01)
Smax = input (0.12, "SAR MAX", step=0.01, minval = 0.01)

// SAR calculation from TV
SAR = sar(Sst, Sinc, Smax)

// SAR Plotting
plot(Act_SAR ? SAR : na, color = macol, style = plot.style_cross, title = "SAR") 

// ================================================================================================================================================================================
// RSI VOLUME WEIGHTED
// ================================================================================================================================================================================

// RSI with volume inputs
Act_RSI = input(true, "RSI VOLUME WEIGHTED")
RSI_len = input(29, "RSI LENGHT", minval = 1)
RSI_obos = input(52,title="RSI CENTER LINE", type=input.integer, minval = 1)

// RSI with volume calculation
WiMA(src, length) => 
    var float MA_s=0.0
    MA_s:=(src + nz(MA_s[1] * (length-1)))/length
    MA_s
RSI_Volume(fv, length) =>	
	up=iff(fv>fv[1],abs(fv-fv[1])*volume,0)
	dn=iff(fv<fv[1],abs(fv-fv[1])*volume,0)
	upt=WiMA(up,length)
	dnt=WiMA(dn,length)
	100*(upt/(upt+dnt))

// Defining variable for include in conditions
RSI_V = RSI_Volume(src, RSI_len)

// ================================================================================================================================================================================
// MACD
// ================================================================================================================================================================================

// MACD inputs
Act_MACD = input(true, "MOVING AVERAGE CONVERGENCE / DIVERGENCE")
fast_length = input(8, title="MACD FAST LENGTH", type=input.integer, minval = 1)
slow_length = input(11, title="MACD SLOW LENGTH", type=input.integer, minval = 1)
signal_length = input(10, title="MACD SIGNAL SMOOTHING", type=input.integer, minval = 1, maxval = 50)

// MACD calculation from TV (histogram only)
[_,_,hist] = macd(src,fast_length,slow_length,signal_length)

// ================================================================================================================================================================================
// STRATEGY
// ================================================================================================================================================================================

// Volume inputs for entry conditions
Act_Vol = input(true, "VOLUME CONDITION")
volume_f = input(1.0, "VOLUME FACTOR", minval = 0, step = 0.1)
sma_length = input(35, "SMA VOLUME LENGTH", minval = 1)

// Confirmed/Unconfirmed options input
Act_not_conf = input(true, "UNCONFIRMED FIRST ENTRY 🤞")
Act_not_conf_X = input(false, "UNCONFIRMED XL/XS")
Act_X = input(false, "ACTIVATE XL/XS ON LONG&SHORT MODE")

// All indicators with long conditions and enable/disable option
JMA_longCond := (Act_JMA ? (JMA_Rising) : RF_longCond) 
RF_longCond := (Act_RF ? (high > hband and upward > 0) : ADX_longCond)
ADX_longCond := (Act_ADX ? (DIPlus > DIMinus and ADX > th) : SAR_longCond)
SAR_longCond := (Act_SAR ? (SAR < close) : RSI_longCond)
RSI_longCond := (Act_RSI ? (RSI_V > RSI_obos) : MACD_longCond)
MACD_longCond := (Act_MACD ? (hist > 0) : VOL_longCond)
VOL_longCond := (Act_Vol ? (volume > sma(volume,sma_length)*volume_f) : JMA_longCond)
// All indicators with short conditions and enable/disable option
JMA_shortCond := (Act_JMA ? (JMA_Falling) : RF_shortCond) 
RF_shortCond := (Act_RF ? (low < lband and downward > 0) : ADX_shortCond)
ADX_shortCond := (Act_ADX ? (DIPlus < DIMinus and ADX > th) : SAR_shortCond)
SAR_shortCond := (Act_SAR ? (SAR > close) : RSI_shortCond)
RSI_shortCond := (Act_RSI ? (RSI_V < RSI_obos) : MACD_shortCond)
MACD_shortCond := (Act_MACD ? (hist < 0) : VOL_shortCond)
VOL_shortCond := (Act_Vol ? (volume > sma(volume,sma_length)*volume_f) : JMA_shortCond)
// Defining long/short condition from indicators + volume
longCond := JMA_longCond and RF_longCond and ADX_longCond and SAR_longCond and RSI_longCond and MACD_longCond and VOL_longCond
shortCond := JMA_shortCond and RF_shortCond and ADX_shortCond and SAR_shortCond and RSI_shortCond and MACD_shortCond and VOL_shortCond

// All indicators with long closing conditions and enable/disable option
JMA_XlongCond := (Act_JMA ? (JMA_Falling) : RF_XlongCond) 
RF_XlongCond := (Act_RF ? (low < lband and downward > 0) : ADX_XlongCond)
ADX_XlongCond := (Act_ADX ? (DIPlus > DIMinus and ADX > th) : SAR_XlongCond)
SAR_XlongCond := (Act_SAR ? (SAR > close) : JMA_XlongCond)
// All indicators with short closing  conditions and enable/disable option
JMA_XshortCond := (Act_JMA ? (JMA_Rising) : RF_XshortCond) 
RF_XshortCond := (Act_RF ? (high > hband and upward > 0) : ADX_XshortCond)
ADX_XshortCond := (Act_ADX ? (DIPlus < DIMinus and ADX > th) : SAR_XshortCond)
SAR_XshortCond := (Act_SAR ? (SAR < close) : JMA_XshortCond)
// Defining the closing long/short condition from indicators
XlongCond := JMA_XlongCond and RF_XlongCond and ADX_XlongCond and SAR_XlongCond
XshortCond := JMA_XshortCond and RF_XshortCond and ADX_XshortCond and SAR_XshortCond

// Avoiding unconfirmed long/short simultaneity
CondIni_long0 := longCond ? 1 : shortCond ? -1 : nz(CondIni_long0[1])
CondIni_short0 := longCond ? 1 : shortCond ? -1 : nz(CondIni_short0[1])
// Unconfirmed long/short conditions
longCondition0 = (longCond and nz(CondIni_long0[1]) == -1)
shortCondition0 = (shortCond and nz(CondIni_short0[1]) == 1)

// Avoiding confirmed long/short simultaneity
CondIni_long := longCond[1] ? 1 : shortCond[1] ? -1 : nz(CondIni_long[1])
CondIni_short := longCond[1] ? 1 : shortCond[1] ? -1 : nz(CondIni_short[1])
// Confirmed long/short conditions
longCondition = (longCond[1] and nz(CondIni_long[1]) == -1)
shortCondition = (shortCond[1] and nz(CondIni_short[1]) == 1)

// Avoiding unconfirmed long/short closing simultaneity
CondIniX0 := XlongCond ? 1 : XshortCond ? -1 : nz(CondIniX0[1])
// Unconfirmed long/short closing conditions
XlongCondition0 = XlongCond and nz(CondIniX0[1]) == -1
XshortCondition0 = XshortCond and nz(CondIniX0[1]) == 1

// Avoiding confirmed long/short closing simultaneity
CondIniX := XlongCond[1] ? 1 : XshortCond[1] ? -1 : nz(CondIniX[1])
// Confirmed long/short closing conditions
XlongCondition = XlongCond[1] and nz(CondIniX[1]) == -1
XshortCondition = XshortCond[1] and nz(CondIniX[1]) == 1

// ================================================================================================================================================================================
// POSITION PRICE
// ================================================================================================================================================================================

// Last opened long/short price on unconfirmed/confirmed conditions
last_open_longCondition := longCondition0 and not longCondition ? max(SAR[1],hband,close[1]) : longCondition ? close[1] : nz(last_open_longCondition[1])
last_open_shortCondition := shortCondition0 and not shortCondition ? min(SAR[1],lband,close[1]) : shortCondition ? close[1] : nz(last_open_shortCondition[1])

// Check if your last position was an unconfirmed long or a short
last_longCondition0 := longCondition0 ? time : nz(last_longCondition0[1])
last_shortCondition0 := shortCondition0 ? time : nz(last_shortCondition0[1])
in_longCondition0 = last_longCondition0 > last_shortCondition0
in_shortCondition0 = last_shortCondition0 > last_longCondition0

// Check if your last position was a confirmed long or a short
last_longCondition := longCondition ? time : nz(last_longCondition[1])
last_shortCondition := shortCondition ? time : nz(last_shortCondition[1])
in_longCondition = last_longCondition > last_shortCondition
in_shortCondition = last_shortCondition > last_longCondition

// Check if your last position was an unconfirmed Xlong or a Xshort
last_XlongCondition0 := XlongCondition0 ? time : nz(last_XlongCondition0[1])
last_XshortCondition0 := XshortCondition0 ? time : nz(last_XshortCondition0[1])
in_longConditionX0 = last_longCondition > last_XlongCondition0
in_shortConditionX0 = last_shortCondition > last_XshortCondition0

// Check if your last position closing was a confirmed Xlong or Xshort
last_XlongCondition := XlongCondition ? time : nz(last_XlongCondition[1])
last_XshortCondition := XshortCondition ? time : nz(last_XshortCondition[1])
in_longConditionX = last_longCondition > last_XlongCondition
in_shortConditionX = last_shortCondition > last_XshortCondition

// Longs Counter
if longCondition
    nLongs := nLongs + 1
    nShorts := na
    
// Shorts Counter
if shortCondition
    nLongs := na
    nShorts := nShorts + 1
    
// ================================================================================================================================================================================
// TAKE PROFIT 1
// ================================================================================================================================================================================

// First take profit input
tp = input(0.8, "TAKE PROFIT 1 %", type = input.float, step = 0.1) 

// First TP Conditions
long_tp := (is_Long and high > (last_open_longCondition*(1+(tp/100))) and not (shortCondition0 and Act_not_conf) and in_longCondition)
short_tp := (is_Short and low < (last_open_shortCondition*(1-(tp/100))) and not (longCondition0 and Act_not_conf) and in_shortCondition)

// Get the time of the last tp close
last_long_tp := long_tp ? time : nz(last_long_tp[1])
last_short_tp := short_tp ? time : nz(last_short_tp[1])

// Final Take profit condition (never after the stop loss)
Final_Long_tp := (long_tp and last_longCondition > nz(last_long_tp[1]) and last_longCondition > nz(last_long_sl[1]))
Final_Short_tp := (short_tp and last_shortCondition > nz(last_short_tp[1]) and last_shortCondition > nz(last_short_sl[1]))

// ================================================================================================================================================================================
// TAKE PROFIT 2
// ================================================================================================================================================================================

// Second take profit input
Act_tp2 = input(true, "ACTIVATE TAKE PROFIT 2")
tp2 = input(1.8, "TAKE PROFIT 2 %", type = input.float, step = 0.1) 

// Second TP Conditions
long_tp2 := (Act_tp2 and is_Long and high > (last_open_longCondition*(1+(tp2/100))) and not (shortCondition0 and Act_not_conf) and in_longCondition)
short_tp2 := (Act_tp2 and is_Short and low < (last_open_shortCondition*(1-(tp2/100))) and not (longCondition0 and Act_not_conf) and in_shortCondition)

// Get the time of the last tp2 close
last_long_tp2 := long_tp2 ? time : nz(last_long_tp2[1])
last_short_tp2 := short_tp2 ? time : nz(last_short_tp2[1])

// Final second Take profit condition (never after the stop loss)
Final_Long_tp2 := (long_tp2 and last_longCondition > nz(last_long_tp2[1]) and last_longCondition > nz(last_long_sl[1]))
Final_Short_tp2 := (short_tp2 and last_shortCondition > nz(last_short_tp2[1]) and last_shortCondition > nz(last_short_sl[1]))
   
// ================================================================================================================================================================================
// TAKE PROFIT FOR PUMPS
// ================================================================================================================================================================================

// Take profit on pumps input
tp_pump = input(1.0, "TAKE PROFIT FOR PUMPS % ", type = input.float, step = 0.1)

// Second TP Conditions
long_tp_pump := is_Long and longCondition0 and not longCondition and high > last_open_longCondition*(1+(tp_pump/100)) and Act_not_conf
short_tp_pump := is_Short and shortCondition0 and not shortCondition and low < last_open_shortCondition*(1-(tp_pump/100)) and Act_not_conf

// Get the time of the last take profit on pumps close
last_long_tp_pump := long_tp_pump ? time : nz(last_long_tp_pump[1])
last_short_tp_pump := short_tp_pump ? time : nz(last_short_tp_pump[1])

// Final Take profit on pumps condition (never after the stop loss)
Final_Long_tp_pump := (long_tp_pump and last_longCondition > nz(last_long_tp_pump[1]) and last_longCondition > nz(last_long_sl[1]))
Final_Short_tp_pump := (short_tp_pump and last_shortCondition > nz(last_short_tp_pump[1]) and last_shortCondition > nz(last_short_sl[1]))

// ================================================================================================================================================================================
// STOP LOSS
// ================================================================================================================================================================================

// Stop Loss input
Act_sl = input(false, "ACTIVATE STOP LOSS")
sl = input(4.0, "STOP LOSS %", type = input.float, step = 0.1)

// Stop Loss conditions
long_sl = Act_sl and is_Long and low <= ((1-(sl/100))*last_open_longCondition) and not (open < ((1-(sl/100))*last_open_longCondition)) 
short_sl = Act_sl and is_Short and high >= ((1+(sl/100))*last_open_shortCondition) and not (open > ((1+(sl/100))*last_open_shortCondition))

// Avoiding simultaneity with Stop loss and other signals
Final_Long_sl0 := Position == "BOTH" ? long_sl and nz(CondIni_long_sl[1]) == -1 and not Final_Long_tp and not shortCondition and not (shortCondition0 and Act_not_conf) : 
   long_sl and nz(CondIni_long_sl[1]) == -1 and not Final_Long_tp
Final_Short_sl0 := Position == "BOTH" ? short_sl and nz(CondIni_short_sl[1]) == -1 and not Final_Short_tp and not longCondition and not (longCondition0 and Act_not_conf) : 
   short_sl and nz(CondIni_short_sl[1]) == -1 and not Final_Short_tp

// Get the time of the last sl close
last_long_sl := Final_Long_sl ? time : nz(last_long_sl[1])
last_short_sl := Final_Short_sl ? time : nz(last_short_sl[1])

// Final Stop Loss condition
Final_Long_sl := Final_Long_sl0 and last_longCondition > nz(last_long_tp[1]) and last_longCondition > nz(last_long_sl[1])
Final_Short_sl := Final_Short_sl0 and last_shortCondition > nz(last_short_tp[1]) and last_shortCondition > nz(last_short_sl[1])

// Final SL Counter
CondIni_long_sl := Final_Long_tp or Final_Long_sl or Final_shortCondition0 or Final_shortCondition or Final_XlongCondition ? 1 : 
   Final_longCondition0 or Final_longCondition ? -1 : nz(CondIni_long_sl[1])
CondIni_short_sl := Final_Short_tp or Final_Short_sl or Final_longCondition0 or Final_longCondition or Final_XshortCondition ? 1 : 
   Final_shortCondition0 or Final_shortCondition ? -1 : nz(CondIni_short_sl[1])

// ================================================================================================================================================================================
// SIGNALS PLOTTING
// ================================================================================================================================================================================

// Final unconfirmed long & short conditions
Final_longCondition0 := is_Long and  longCondition0 and not longCondition and nz(CondIni_long0[1]) == -1 and Act_not_conf
Final_shortCondition0 := is_Short and shortCondition0 and not shortCondition and nz(CondIni_short0[1]) == 1 and Act_not_conf
// Final unconfirmed long & short triangles
plotshape(Final_longCondition0, title = "Not_Conf Long Signal", text = "🤞", style=shape.triangleup, location=location.belowbar, color = color.blue, transp = 0, size=size.tiny)
plotshape(Final_shortCondition0, title = "Not_Conf Short Signal", text = "🤞", style=shape.triangledown, location=location.abovebar, color = #FF0000, transp = 0, size=size.tiny)

// Final confirmed long & short conditions
Final_longCondition := is_Long and longCondition and not Final_shortCondition0 //and not (Final_longCondition0[1] and not Final_Long_ts_pump[1])
Final_shortCondition := is_Short and shortCondition and not Final_longCondition0 //and not (Final_shortCondition0[1] and not Final_Short_ts_pump[1])
// Final confirmed long & short triangles
plotshape(Final_longCondition, title = "Long Signal", style=shape.triangleup, location=location.belowbar, color = color.blue, transp = 0, size=size.tiny)
plotshape(Final_shortCondition, title = "Short Signal", style=shape.triangledown, location=location.abovebar, color = #FF0000, transp = 0, size=size.tiny)

// Avoiding simultaneity and ordering unconfirmed Xlong & Xshort with other signals
CondIni_Xlong0 := Final_Long_tp or Final_Long_tp2 or Final_Short_tp or Final_Long_sl or Final_XlongCondition0 or Final_shortCondition0 or Final_shortCondition ? 1 : 
   Final_longCondition0 or Final_longCondition ? -1 : nz(CondIni_Xlong0[1])
CondIni_Xshort0 := Final_Short_tp or Final_Short_tp2 or Final_Short_tp or Final_Short_sl or Final_XshortCondition0 or Final_longCondition0 or Final_longCondition ? 1 : 
   Final_shortCondition0 or Final_shortCondition ? -1 : nz(CondIni_Xshort0[1])

// Final unconfirmed Xlong & Xshort conditions
Final_XlongCondition0 := (Position == "SHORT" ? na : Position == "BOTH" and Act_X == true ? (XlongCondition0 and last_longCondition > last_XlongCondition0[1]) : Position == "LONG" ?
   ((shortCondition0 and last_longCondition > last_shortCondition0[1]) or (XlongCondition0 and last_longCondition > last_XlongCondition0[1])) : na)
   and CondIni_Xlong0 == -1 and not Final_longCondition0 and not Final_shortCondition0 and not Final_longCondition and not Final_shortCondition
Final_XshortCondition0 := (Position == "LONG" ? na : Position == "BOTH" and Act_X == true ? (XshortCondition0 and last_shortCondition > last_XshortCondition0[1]) : Position == "SHORT" ?
   ((longCondition0 and last_shortCondition > last_longCondition0[1]) or (XshortCondition0 and last_shortCondition > last_XshortCondition0[1])) : na)
   and CondIni_Xshort0 == -1 and not Final_longCondition0 and not Final_shortCondition0 and not Final_longCondition and not Final_shortCondition
   
// Avoiding simultaneity and ordering confirmed Xlong & Xshort with other signals
CondIni_Xlong := Final_Long_tp or Final_Short_tp or Final_Long_sl or Final_XlongCondition or Final_shortCondition0 or Final_shortCondition ? 1 : 
   Final_longCondition0 or Final_longCondition ? -1 : nz(CondIni_Xlong[1])
CondIni_Xshort := Final_Long_tp or Final_Short_tp or Final_Short_sl or Final_XshortCondition or Final_longCondition0 or Final_longCondition ? 1 : 
   Final_shortCondition0 or Final_shortCondition ? -1 : nz(CondIni_Xshort[1])

// Final confirmed Xlong & Xshort conditions
Final_XlongCondition := (Position == "SHORT" ? na : Position == "BOTH" and Act_X == true ? (XlongCondition and last_longCondition > last_XlongCondition[1]) : Position == "LONG" ?
   ((shortCondition and last_longCondition > last_shortCondition[1]) or (XlongCondition and last_longCondition > last_XlongCondition[1])) : na)
   and CondIni_Xlong == -1 and not Final_longCondition0 and not Final_shortCondition0
Final_XshortCondition := (Position == "LONG" ? na : Position == "BOTH" and Act_X == true ? (XshortCondition and last_shortCondition > last_XshortCondition[1]) : Position == "SHORT" ?
   ((longCondition and last_shortCondition > last_longCondition[1]) or (XshortCondition and last_shortCondition > last_XshortCondition[1])) : na)
   and CondIni_Xshort == -1 and not Final_longCondition0 and not Final_shortCondition0

// Final Confirmed or unconfirmed Xlong & Xshort conditions   
Final_XL = ((Final_XlongCondition0 and Act_not_conf_X) or (Final_XlongCondition and not Act_not_conf_X))
Final_XS = ((Final_XshortCondition0 and Act_not_conf_X) or (Final_XshortCondition and not Act_not_conf_X))

// Final Confirmed or unconfirmed Xlong & Xshort triangles  
plotshape(Final_XL, title = "XL Signal", text = "XL", style=shape.triangledown, location=location.abovebar, color = color.orange, transp = 0, size=size.tiny)
plotshape(Final_XS, title = "XS Signal", text = "XS", style=shape.triangleup, location=location.belowbar, color = color.aqua, transp = 0, size=size.tiny)

// TP triangles
plotshape(Final_Long_tp and not Final_Long_tp2, text ="TP", title="Take Profit Long", style=shape.triangledown, location=location.abovebar, color = color.red, editable = false, transp = 0) 
plotshape(Final_Short_tp and not Final_Short_tp2, text ="TP", title="Take Profit Short", style=shape.triangleup, location=location.belowbar, color = color.lime, editable = false, transp = 0) 

// TP crosses
ltp = iff(Final_Long_tp, last_open_longCondition*(1+(tp/100)), na), plot(ltp, style = plot.style_cross, linewidth=3, color = color.white, editable = false)
stp = iff(Final_Short_tp, last_open_shortCondition*(1-(tp/100)), na), plot(stp, style = plot.style_cross, linewidth=3, color = color.white, editable = false)

// TP2 triangles
plotshape(Final_Long_tp2 and not Final_Long_tp, text ="TP2", title="Take Profit Long 2", style=shape.triangledown, location=location.abovebar, color = color.red, editable = false, transp = 0) 
plotshape(Final_Short_tp2 and not Final_Short_tp, text ="TP2", title="Take Profit Short 2", style=shape.triangleup, location=location.belowbar, color = color.lime, editable = false, transp = 0) 

// TP2 crosses
ltp2 = iff(Final_Long_tp2, last_open_longCondition*(1+(tp2/100)), na), plot(ltp2, style = plot.style_cross, linewidth=3, color = color.white, editable = false)
stp2 = iff(Final_Short_tp2, last_open_shortCondition*(1-(tp2/100)), na), plot(stp2, style = plot.style_cross, linewidth=3, color = color.white, editable = false)

// TP & TP2 flags on condition of simultaneity
plotshape(Final_Long_tp and Final_Long_tp2, title="TP & TP2 Long", style=shape.flag, location=location.abovebar, color = color.red, editable = false, transp = 0, size=size.tiny) 
plotshape(Final_Short_tp and Final_Short_tp2, title="TP & TP2 Short", style=shape.flag, location=location.belowbar, color = color.green, editable = false, transp = 0, size=size.tiny) 

// TP on Pumps triangles
plotshape(Final_Long_tp_pump and Final_longCondition0, text ="PUMP", title="Take Profit Long Pump", style=shape.triangledown, 
   location=location.abovebar, color = color.red, editable = false, transp = 0) 
plotshape(Final_Short_tp_pump and Final_shortCondition0, text ="PUMP", title="Take Profit Short Pump", style=shape.triangleup, 
   location=location.belowbar, color = color.lime, editable = false, transp = 0) 
   
//TP on pumps crosses
ltp_pump = iff(Final_Long_tp_pump and Final_longCondition0, last_open_longCondition*(1+(tp_pump/100)), na), plot(ltp_pump, style = plot.style_cross, linewidth=3, color = color.white, editable = false)
stp_pump = iff(Final_Short_tp_pump and Final_shortCondition0, last_open_shortCondition*(1-(tp_pump/100)), na), plot(stp_pump, style = plot.style_cross, linewidth=3, color = color.white, editable = false)

// Stop Loss triangles
plotshape(Final_Long_sl, text ="SL", title="Stop Loss Long", style=shape.triangledown, location=location.abovebar, color = color.fuchsia, editable = false, transp = 0) 
plotshape(Final_Short_sl, text ="SL", title="Stop Loss Short", style=shape.triangleup, location=location.belowbar, color = color.fuchsia, editable = false, transp = 0) 

// Stop Loss crosses
lsl = iff(Final_Long_sl, (1-(sl/100))*last_open_longCondition, na), plot(lsl, style = plot.style_cross, linewidth=3, color = color.white, editable = false)
ssl = iff(Final_Short_sl, (1+(sl/100))*last_open_shortCondition, na), plot(ssl, style = plot.style_cross, linewidth=3, color = color.white, editable = false)

// TP Levels
plot(is_Long and in_longCondition0 and not longCondition0 and last_longCondition > nz(last_long_tp[1]) and last_longCondition > nz(last_long_sl[1]) ? 
   (last_open_longCondition*(1+(tp/100))) : na, "Long Take Profit", color = color.green, style=3, linewidth=1, editable = false)
plot(is_Short and in_shortCondition0 and not shortCondition0 and last_shortCondition > nz(last_short_tp[1]) and last_shortCondition > nz(last_short_sl[1]) ? 
   (last_open_shortCondition*(1-(tp/100))) : na, "Short Take Profit", color =color.red , style=3, linewidth=1, editable = false)

// TP2 Levels
plot(Act_tp2 and is_Long and in_longCondition0 and not longCondition0 and last_longCondition > nz(last_long_tp2[1]) and last_longCondition > nz(last_long_sl[1]) ? 
   (last_open_longCondition*(1+(tp2/100))) : na, "Long Take Profit 2", color = color.green, style=3, linewidth=1, editable = false)
plot(Act_tp2 and is_Short and in_shortCondition0 and not shortCondition0 and last_shortCondition > nz(last_short_tp2[1]) and last_shortCondition > nz(last_short_sl[1]) ? 
   (last_open_shortCondition*(1-(tp2/100))) : na, "Short Take Profit 2", color =color.red , style=3, linewidth=1, editable = false)
   
// Weekend
Weekend = true
W_color = Weekend and (dayofweek == dayofweek.sunday or dayofweek == dayofweek.saturday) ? color.teal : na
bgcolor(W_color, title = "WEEKEND")

// ================================================================================================================================================================================
// RE-ENTRY CONDITIONS
// ================================================================================================================================================================================

// Re-entry on long after tp, sl or Xlong
if Final_Long_tp or Final_Long_sl or Final_XlongCondition
    CondIni_long := -1
    CondIni_long0 := -1
// Re-entry on short after tp, sl or Xshort
if Final_Short_tp or Final_Short_sl or Final_XshortCondition
    CondIni_short := 1
    CondIni_short0 := 1
// Re-entry on unconfirmed long after a confirmed long
if Final_longCondition
    CondIni_long0 := 1
// Re-entry on unconfirmed short after a confirmed short
if Final_shortCondition
    CondIni_short0 := -1

// ================================================================================================================================================================================
// BACKTEST
// ================================================================================================================================================================================

// Backtest input
Act_BT = input(true, "BACKTEST 💹")
// Time period input
testStartYear = input(2019, "BACKTEST START YEAR", minval = 1980, maxval = 2222) 
testStartMonth = input(06, "BACKTEST START MONTH", minval = 1, maxval = 12)
testStartDay = input(01, "BACKTEST START DAY", minval = 1, maxval = 31)
testPeriodStart = timestamp(testStartYear,testStartMonth,testStartDay,0,0)
testStopYear = input(2222, "BACKTEST STOP YEAR", minval=1980, maxval = 2222)
testStopMonth = input(12, "BACKTEST STOP MONTH", minval=1, maxval=12)
testStopDay = input(31, "BACKTEST STOP DAY", minval=1, maxval=31)
testPeriodStop = timestamp(testStopYear, testStopMonth, testStopDay, 0, 0)
testPeriod = time >= testPeriodStart and time <= testPeriodStop ? true : false
// Choosing between contracts or $
contracts_or_cash = input("CONTRACTS", "CONTRACTS ₿ / CASH $", options = ["CONTRACTS","CASH"])
cc_factor = (contracts_or_cash == "CASH") ? open : 1
// Entering the quantity of each entry to close on its take profit
quantity_cash_1TP = input(0.5, "QUANTITY 1ST TP", type = input.float) / cc_factor
quantity_cash_2TP = input(0.5, "QUANTITY 2ND TP", type = input.float) / cc_factor
Act_Lev = input(true, "ACTIVATE LEVERAGE BY VOLUME")
var int qty_Lev = na
qty_Lev := Act_Lev ? ceil(volume/sma(volume,sma_length)) : 1

// Defining new final unconfirmed conditions to use on the TV backtest
BT_Final_longCondition := Position == "SHORT" ? na : ((longCond and not in_longCondition) or (longCond and Final_Long_tp) or (longCond and Final_Long_sl)) or
   (longCond and not longCondition and (last_long_tp > nz(last_longCondition))) or (longCond and not longCondition and (last_long_sl > nz(last_longCondition))) or
   (longCond and Act_X == true and not longCondition and (last_XlongCondition0 > nz(last_longCondition)))
BT_Final_shortCondition := Position == "LONG" ? na : ((shortCond and not in_shortCondition) or (shortCond and Final_Short_tp) or (shortCond and Final_Short_sl)) or 
   (shortCond and not shortCondition and (last_short_tp > nz(last_shortCondition))) or (shortCond and not shortCondition and (last_short_sl > nz(last_shortCondition))) or
   (shortCond and Act_X == true and not shortCondition and (last_XshortCondition0 > nz(last_shortCondition)))
   
// Entering long positions
if (BT_Final_longCondition)
    strategy.entry("long1", strategy.long, qty = quantity_cash_1TP*qty_Lev, when = Act_BT and testPeriod)
    strategy.entry("long2", strategy.long, qty = quantity_cash_2TP*qty_Lev, when = Act_BT and testPeriod and Act_tp2)
// Entering short positions
if (BT_Final_shortCondition) 
    strategy.entry("short1", strategy.short, qty = quantity_cash_1TP*qty_Lev, when = Act_BT and testPeriod)
    strategy.entry("short2", strategy.short, qty = quantity_cash_2TP*qty_Lev, when = Act_BT and testPeriod and Act_tp2)
    
// Closing positions with first TP
strategy.exit("Tpl", "long1", profit = (abs((last_open_longCondition*(1+(tp/100)))-last_open_longCondition)/syminfo.mintick), 
   loss = Act_sl ? (abs((last_open_longCondition*(1-(sl/100)))-last_open_longCondition)/syminfo.mintick) : na)
strategy.exit("Tps", "short1", profit = (abs((last_open_shortCondition*(1-(tp/100)))-last_open_shortCondition)/syminfo.mintick),
   loss = Act_sl ? (abs((last_open_shortCondition*(1+(sl/100)))-last_open_shortCondition)/syminfo.mintick) : na)
// Closing positions with second TP   
strategy.exit("Tpl2", "long2", profit = (abs((last_open_longCondition*(1+(tp2/100)))-last_open_longCondition)/syminfo.mintick), 
   loss = Act_sl ? (abs((last_open_longCondition*(1-(sl/100)))-last_open_longCondition)/syminfo.mintick) : na)
strategy.exit("Tps2", "short2", profit = (abs((last_open_shortCondition*(1-(tp2/100)))-last_open_shortCondition)/syminfo.mintick),
   loss = Act_sl ? (abs((last_open_shortCondition*(1+(sl/100)))-last_open_shortCondition)/syminfo.mintick) : na)
   
// Closing all positions with Xlong/Xshort
strategy.close_all(when = Final_XlongCondition0 or Final_XshortCondition0)

// ================================================================================================================================================================================
// ALERTS
// ================================================================================================================================================================================

//alertcondition(Final_longCondition0, title="Not_Conf Long Alert", message = "NC LONG") 
//alertcondition(Final_shortCondition0, title="Not_Conf Short Alert", message = "NC SHORT")
//alertcondition(Final_longCondition, title="Long Alert", message = "LONG") 
//alertcondition(Final_shortCondition, title="Short Alert", message = "SHORT")
//alertcondition(Final_Long_tp, title="Take Profit on Longs Alert", message = "LONG TP")
//alertcondition(Final_Short_tp, title="Take Profit on Shorts Alert", message = "SHORT TP")
//alertcondition(Final_Long_tp2, title="Take Profit2 on Longs Alert", message = "LONG TP2")
//alertcondition(Final_Short_tp2, title="Take Profit2 on Shorts Alert", message = "SHORT TP2")
//alertcondition(Final_XlongCondition or Final_Long_sl or (Final_Long_tp_pump and Act_not_conf), title="XLong/PUMP/Stop-Loss on Longs Alert", message = "XLONG/PUMP/STOP-LOSS")
//alertcondition(Final_XshortCondition or Final_Short_sl or (Final_Short_tp_pump and Act_not_conf), title="XShort/PUMP/Stop-Loss on Shorts Alert", message = "XSHORT/PUMP/STOP-LOSS")

// BTC BINANCE FUTURES

alertcondition(Final_longCondition0, title="BTC NC Long Alert", 
   message = "NC LONG | e=BINANCEFUTURES a=BINANCE s=BTCUSDT c=order b=long | delay=1 | e=BINANCEFUTURES a=BINANCE s=BTCUSDT c=position b=short t=market ro=1 | delay=1 | e=BINANCEFUTURES a=BINANCE s=BTCUSDT b=long q=25% t=market")
alertcondition(Final_shortCondition0, title="BTC NC Short Alert", 
   message = "NC SHORT | e=BINANCEFUTURES a=BINANCE s=BTCUSDT c=order b=short | delay=1 | e=BINANCEFUTURES a=BINANCE s=BTCUSDT c=position b=long t=market ro=1 | delay=1 | e=BINANCEFUTURES a=BINANCE s=BTCUSDT b=short q=25% t=market")
alertcondition(Final_longCondition, title="BTC Long Alert", 
   message = "LONG | e=BINANCEFUTURES a=BINANCE s=BTCUSDT c=order b=long | delay=1 | e=BINANCEFUTURES a=BINANCE s=BTCUSDT c=position b=short t=market ro=1 | delay=1 | e=BINANCEFUTURES a=BINANCE s=BTCUSDT b=long q=100% t=market | delay=1 | e=BINANCEFUTURES a=BINANCE s=BTCUSDT c=position b=long p=0.80% q=50% t=limit ro=1 | delay=1 | e=BINANCEFUTURES a=BINANCE s=BTCUSDT c=position b=long p=1.8% q=50% t=limit ro=1")
alertcondition(Final_shortCondition, title="BTC Short Alert", 
   message = "SHORT | e=BINANCEFUTURES a=BINANCE s=BTCUSDT c=order b=short | delay=1 | e=BINANCEFUTURES a=BINANCE s=BTCUSDT c=position b=long t=market ro=1 | delay=1 | e=BINANCEFUTURES a=BINANCE s=BTCUSDT b=short q=100% t=market | delay=1 | e=BINANCEFUTURES a=BINANCE s=BTCUSDT c=position b=short p=-0.80% q=50% t=limit ro=1 | delay=1 | e=BINANCEFUTURES a=BINANCE s=BTCUSDT c=position b=short p=-1.8% q=50% t=limit ro=1")
//alertcondition(Final_Long_tp, title="BTC Take Profit on Longs Alert", 
//   message = "LONG TP | e=BINANCEFUTURES a=BINANCE s=BTCUSDT c=position q=50% t=market ro=1")
//alertcondition(Final_Short_tp, title="BTC Take Profit on Shorts Alert", 
//   message = "SHORT TP | e=BINANCEFUTURES a=BINANCE s=BTCUSDT c=position q=50% t=market ro=1")
//alertcondition(Final_Long_tp2, title="BTC Take Profit on Longs Alert", 
//   message = "LONG TP2 | e=BINANCEFUTURES a=BINANCE s=BTCUSDT c=position b=long q=100% t=market ro=1")
//alertcondition(Final_Short_tp2, title="BTC Take Profit on Shorts Alert", 
//   message = "SHORT TP2 | e=BINANCEFUTURES a=BINANCE s=BTCUSDT c=position b=short q=100% t=market ro=1")
alertcondition(Final_XL or Final_Long_sl or (Final_Long_tp_pump and Act_not_conf), title="BTC XLong/PUMP/Stop-Loss on Longs Alert", 
   message = "XLONG/PUMP/STOP-LOSS | e=BINANCEFUTURES a=BINANCE s=BTCUSDT c=order | delay=1 | e=BINANCEFUTURES a=BINANCE s=BTCUSDT c=position b=long t=market ro=1")
alertcondition(Final_XS or Final_Short_sl or (Final_Short_tp_pump and Act_not_conf), title="BTC XShort/PUMP/Stop-Loss on Shorts Alert", 
   message = "XSHORT/PUMP/STOP-LOSS | e=BINANCEFUTURES a=BINANCE s=BTCUSDT c=order | delay=1 | e=BINANCEFUTURES a=BINANCE s=BTCUSDT c=position b=short t=market ro=1")

// by Xaviz