0% found this document useful (0 votes)
1K views32 pages

Profit Pulse Pro

Easier to follow with trend ribbon. More accurate trading with advanced buy and sell signals. No more repaints thanks to AI-supported coding.

Uploaded by

moleno56
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1K views32 pages

Profit Pulse Pro

Easier to follow with trend ribbon. More accurate trading with advanced buy and sell signals. No more repaints thanks to AI-supported coding.

Uploaded by

moleno56
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 32

//@version=5

strategy('Profit Pulse Pro', overlay=true, default_qty_type=strategy.cash,


default_qty_value=10000, initial_capital=10000, currency='USD',
commission_type=strategy.commission.percent, commission_value=0.03,
max_bars_back=3000, max_lines_count=100, calc_on_every_tick=false,
max_labels_count=100, process_orders_on_close =true)

var initialcapital = strategy.equity

//Truncate Function
truncate(number, decimals) =>
factor = math.pow(10, decimals)
int(number * factor) / factor

//
===================================================================================
====
// Trade Time
daysback = input(14, title='Backtest Days, For Defining Best TF, Max: 20 Days')
millisecondinxdays = 1000 * 60 * 60 * 24 * daysback
leftbar = timenow - time < millisecondinxdays
bool backtest = leftbar
//
===================================================================================
====
et1 = 'Supertrend'
bool longside = input.bool(true, title='Long Side', group='Strategy Option')
bool shortside = input.bool(true, title='Short Side', group='Strategy Option')
filter1 = 'Filter with Atr'
filter2 = 'Filter with RSI'
filter3 = 'Atr or RSI'
filter4 = 'Atr and RSI'
filter5 = 'No Filtering'
// filter6 = 'Entry Only in sideways market(By ATR or RSI)'
// filter7 = 'Entry Only in sideways market(By ATR and RSI)'
typefilter = input.string(filter5, title='Sideways Filtering Input',
options=[filter1, filter2, filter3, filter4, filter5], group='Strategy Options')
withsl = input.bool(false, title='Use Dynamic SL?', group='Strategy Options')

RSI = truncate(ta.rsi(close, input.int(14, group='RSI Filterring')), 2)


toplimitrsi = input.float(55.5, title='TOP Limit', group='RSI Filterring')
botlimitrsi = input.float(45.5, title='BOT Limit', group='RSI Filterring')

// TP SL Option
ex1 = 'TP & SL by Percentage Price'
ex2 = 'TP & SL by atr Value'
et = input.string(ex2, title='TP SL Type', options=[ex1, ex2], group='Strategy
Options')

ST = input.bool(true, title='Show Supertrend?', group='Supertrend Indicator')


period = input.int(15, group='Supertrend Indicator')
mult = input.int(5, group='Supertrend Indicator')
atrLen = input.int(13, minval=1, title='atr Length', group='Sideways Filtering
Input')
atrMaType = input.string('EMA', options=['SMA', 'EMA'], group='Sideways Filtering
Input', title='atr Moving Average Type')
atrMaLen = input.int(13, minval=1, title='atr MA Length', group='Sideways Filtering
Input')
//adxLen = input(5, minval = 1, maxval = 50, title = "ADX SMOOTHing",
group='Sideways Filtering Input')
//diLen = input(14, minval = 1, title = "DI Length", group='Sideways Filtering
Input')
//adxLim = input(22, minval = 1, title = "ADX Limit", group='Sideways Filtering
Input')
//SMOOTH = input(3, minval = 1, maxval = 5, title = "SMOOTHing Factor",
group='Sideways Filtering Input')
//lag = input(8, minval = 0, maxval = 15, title = "Lag", group='Sideways Filtering
Input')

round_num = close < 1 ? 5 : close < 99 ? 3 : 2

TP1 = input.float(1.5, title='TP1 Atr Multiplier', group='TP/SL by ATR')


TP2 = input.int(3, title='TP2 Atr Multiplier', group='TP/SL by ATR')
TP3 = input.int(5, title='TP3 Atr Multiplier', group='TP/SL by ATR')
TP4 = input.float(title='TP 4', minval=0.0, step=0.1, defval=9, group='TP/SL by
ATR')
SL = input.int(4, title='SL Atr Multiplier', group='TP/SL by ATR')
ptp1 = input.float(2, title='Take Profit 1 (%)', minval=0.0, step=0.1, group='TP/SL
Percentage Price') * 0.01
ptp2 = input.float(4, title='Take Profit 2 (%)', minval=0.0, step=0.1, group='TP/SL
Percentage Price') * 0.01
ptp3 = input.float(6, title='Take Profit 3 (%)', minval=0.0, step=0.1, group='TP/SL
Percentage Price') * 0.01
ptp4 = input.float(12, title='Take Profit 4 (%)', minval=0.0, step=0.1,
group='TP/SL Percentage Price') * 0.01
psl = input.float(3, title='StopLoss (%)', minval=0.0, step=0.1) * 0.01

qtytp1 = input.int(30, title='QTY TP 1', group='Qty for TP')


qtytp2 = input.int(30, title='QTY TP 2', group='Qty for TP')
qtytp3 = input.int(30, title='QTY TP 3', group='Qty for TP')
qtytp4 = input.int(10, title='QTY TP 4', group='Qty for TP')
Qtytp1 = qtytp1 / 100
Qtytp2 = qtytp2 / 100
Qtytp3 = qtytp3 / 100
Qtytp4 = qtytp4 / 100
///

//===================================================================
//===================================================================
//Timeframe
//Supertrend Indicator

src = hl2
atr2 = ta.sma(ta.tr, period)
atr = request.security(syminfo.tickerid, '', ta.atr(period))
up = src - mult * atr
up1 = nz(up[1], up)
up := close[1] > up1 ? math.max(up, up1) : up
dn = src + mult * atr
dn1 = nz(dn[1], dn)
dn := close[1] < dn1 ? math.min(dn, dn1) : dn
trend = 1
trend := nz(trend[1], trend)
trend := trend == -1 and close > dn1 ? 1 : trend == 1 and close < up1 ? -1 : trend
st = trend == 1 ? up : dn

//filtering
atra = request.security(syminfo.tickerid, '', ta.atr(atrLen))
atrMa = atrMaType == 'EM' ? ta.ema(atra, atrMaLen) : ta.sma(atra, atrMaLen)
updm = ta.change(high)
downdm = -ta.change(low)
plusdm = na(updm) ? na : updm > downdm and updm > 0 ? updm : 0
minusdm = na(downdm) ? na : downdm > updm and downdm > 0 ? downdm : 0
//trur = rma(tr, diLen)
//plus = fixnan(100 * rma(plusdm, diLen) / trur)
//minus = fixnan(100 * rma(minusdm, diLen) / trur)
//sum = plus + minus
//adx = 100 * rma(abs(plus - minus) / (sum == 0 ? 1 : sum), adxLen)

cndSidwayss1 = atra >= atrMa


cndSidwayss2 = RSI > toplimitrsi or RSI < botlimitrsi
cndSidways = cndSidwayss1 or cndSidwayss2
cndSidways1 = cndSidwayss1 and cndSidwayss2
Sidwayss1 = atra <= atrMa
Sidwayss2 = RSI < toplimitrsi and RSI > botlimitrsi
Sidways = Sidwayss1 or Sidwayss2
Sidways1 = Sidwayss1 and Sidwayss2

bool trendType = typefilter == filter1 ? cndSidwayss1 : typefilter == filter2 ?


cndSidwayss2 : typefilter == filter3 ? cndSidways : typefilter == filter4 ?
cndSidways1 : typefilter == filter5 ? RSI > 0 : na

// Entry FIX
buysignal1 = ta.crossover(close, st)
sellsignal1 = ta.crossunder(close, st)

bool buy = buysignal1 and longside


bool sell = sellsignal1 and shortside

//Tp & Sl by atr Mult


cb = ta.valuewhen(buy, close, 0)
ratr = ta.valuewhen(buy, ta.atr(14), 0)

tpb1 = cb + ratr * TP1


tpb2 = cb + ratr * TP2
tpb3 = cb + ratr * TP3
tpb4 = cb + ratr * TP4
slb = cb - ratr * SL //withsl?up:valuewhen(buy, up,0)

cs = ta.valuewhen(sell, close, 0)
ratrs = ta.valuewhen(sell, ta.atr(14), 0)
tps1 = cs - ratrs * TP1
tps2 = cs - ratrs * TP2
tps3 = cs - ratrs * TP3
tps4 = cs - ratrs * TP4
sls = cs + ratrs * SL //withsl?dn:valuewhen(sell, dn, 0)

Ptpb1 = cb * (1 + ptp1)
Ptpb2 = cb * (1 + ptp2)
Ptpb3 = cb * (1 + ptp3)
Ptpb4 = et == ex1 ? cb * (1 + ptp4) : tpb4
Pslb = cb * (1 - psl)

Ptps1 = cs * (1 - ptp1)
Ptps2 = cs * (1 - ptp2)
Ptps3 = cs * (1 - ptp3)
Ptps4 = et == ex1 ? cs * (1 - ptp4) : tps4
Psls = cs * (1 + psl)

//Variable Fix TP SL
float tpb1t = et == ex1 ? Ptpb1 : tpb1
float tpb2t = et == ex1 ? Ptpb2 : tpb2
float tpb3t = et == ex1 ? Ptpb3 : tpb3
float tpb4t = Ptpb4
float slbt = et == ex1 ? Pslb : slb
float tps1t = et == ex1 ? Ptps1 : tps1
float tps2t = et == ex1 ? Ptps2 : tps2
float tps3t = et == ex1 ? Ptps3 : tps3
float tps4t = Ptps4
float slst = et == ex1 ? Psls : sls

//===================================================================

slbm = et == ex1 ? Psls : slb


slsm = et == ex1 ? Psls : sls
slbmessage = '","stopLossPriority":"price","stopLossPrice":"' + str.tostring(slbm)
slsmessage = '","stopLossPriority":"price","stopLossPrice":"' + str.tostring(slsm)

//signal provider
tpb1tsp = et == ex1 ? ptp1 * 100 : truncate((tpb1 - cb) * 100 / cb, 2)
tpb2tsp = et == ex1 ? ptp2 * 100 : truncate((tpb2 - cb) * 100 / cb, 2)
tpb3tsp = et == ex1 ? ptp3 * 100 : truncate((tpb3 - cb) * 100 / cb, 2)
tpb4tsp = ptp4 * 100
slbtsp = truncate((cb - slb) * 100 / cb, 2)

tps1tsp = et == ex1 ? ptp1 * 100 : truncate((cs - tps1) * 100 / cs, 2)


tps2tsp = et == ex1 ? ptp2 * 100 : truncate((cs - tps2) * 100 / cs, 2)
tps3tsp = et == ex1 ? ptp3 * 100 : truncate((cs - tps3) * 100 / cs, 2)
tps4tsp = ptp4 * 100
slstsp = truncate((sls - cs) * 100 / cs, 2)

slbmsp = et == ex1 ? psl * 100 : slbtsp


slsmsp = et == ex1 ? psl * 100 : slstsp
slbmessagesp = '","stopLossPercentage":"-' + str.tostring(slbmsp)
slsmessagesp = '","stopLossPercentage":"-' + str.tostring(slsmsp)

//===================================================================
//MTF Analysis
//===================================================================
//===================================================================

//Source MTF
tf1 = input.timeframe('15', group='MTF Analysis')
tf2 = input.timeframe('30', group='MTF Analysis')
tf3 = input.timeframe('45', group='MTF Analysis')
tf4 = input.timeframe('60', group='MTF Analysis')

hl2A = request.security(syminfo.tickerid, tf1, hl2)


hl2B = request.security(syminfo.tickerid, tf2, hl2)
hl2C = request.security(syminfo.tickerid, tf3, hl2)
hl2D = request.security(syminfo.tickerid, tf4, hl2)
trA = request.security(syminfo.tickerid, tf1, ta.tr)
trB = request.security(syminfo.tickerid, tf2, ta.tr)
trC = request.security(syminfo.tickerid, tf3, ta.tr)
trD = request.security(syminfo.tickerid, tf4, ta.tr)

highA = request.security(syminfo.tickerid, tf1, high)


highB = request.security(syminfo.tickerid, tf2, high)
highC = request.security(syminfo.tickerid, tf3, high)
highD = request.security(syminfo.tickerid, tf4, high)

lowA = request.security(syminfo.tickerid, tf1, low)


lowB = request.security(syminfo.tickerid, tf2, low)
lowC = request.security(syminfo.tickerid, tf3, low)
lowD = request.security(syminfo.tickerid, tf4, low)

closeA = request.security(syminfo.tickerid, tf1, close)


closeB = request.security(syminfo.tickerid, tf2, close)
closeC = request.security(syminfo.tickerid, tf3, close)
closeD = request.security(syminfo.tickerid, tf4, close)

//===================================================================
//===================================================================
//Timeframe A
//Supertrend Indicator

srcA = hl2A
atr2A = ta.sma(trA, period)
atrA = request.security(syminfo.tickerid, tf1, ta.atr(period))
upA = srcA - mult * atrA
up1A = nz(upA[1], upA)
upA := closeA[1] > up1A ? math.max(upA, up1A) : upA
dnA = srcA + mult * atrA
dn1A = nz(dnA[1], dnA)
dnA := closeA[1] < dn1A ? math.min(dnA, dn1A) : dnA
trendA = 1
trendA := nz(trendA[1], trendA)
trendA := trendA == -1 and closeA > dn1A ? 1 : trendA == 1 and closeA < up1A ? -1 :
trendA
stA = trendA == 1 ? upA : dnA

//filtering
atraA = request.security(syminfo.tickerid, tf1, ta.atr(atrLen))
atrMaA = atrMaType == 'EMA' ? ta.ema(atraA, atrMaLen) : ta.sma(atraA, atrMaLen)
updmA = ta.change(highA)
downdmA = -ta.change(lowA)
plusdmA = na(updmA) ? na : updmA > downdmA and updmA > 0 ? updmA : 0
minusdmA = na(downdmA) ? na : downdmA > updmA and downdmA > 0 ? downdmA : 0
//trurA = rma(trA, diLen)
//plusA = fixnan(100 * rma(plusdmA, diLen) / trurA)
//minusA = fixnan(100 * rma(minusdmA, diLen) / trurA)
//sumA = plusA + minusA
//adxA = 100 * rma(abs(plusA - minusA) / (sumA == 0 ? 1 : sumA), adxLen)

cndSidwayss1A = atraA >= atrMaA


cndSidwayss2A = RSI > toplimitrsi or RSI < botlimitrsi
cndSidwaysA = cndSidwayss1A or cndSidwayss2A
cndSidways1A = cndSidwayss1A and cndSidwayss2A
Sidwayss1A = atraA <= atrMaA
Sidwayss2A = RSI < toplimitrsi and RSI > botlimitrsi
SidwaysA = Sidwayss1A or Sidwayss2A
Sidways1A = Sidwayss1A and Sidwayss2A

trendTypeA = typefilter == filter1 ? cndSidwayss1A : typefilter == filter2 ?


cndSidwayss2A : typefilter == filter3 ? cndSidwaysA : typefilter == filter4 ?
cndSidways1A : typefilter == filter5 ? RSI > 0 : na

// Entry FIX
buysignal1A = ta.crossover(closeA, stA) and backtest and trendTypeA
sellsignal1A = ta.crossunder(closeA, stA) and backtest and trendTypeA

buyA = buysignal1A and longside


sellA = sellsignal1A and shortside

//Tp & Sl by atr Mult


cbA = ta.valuewhen(buyA, closeA, 0)
ratrA = ta.valuewhen(buyA, request.security(syminfo.tickerid, tf1, ta.atr(14)), 0)

tpb1A = cbA + ratrA * TP1


tpb2A = cbA + ratrA * TP2
tpb3A = cbA + ratrA * TP3
tpb4A = cbA + ratrA * TP4
slbA = cbA - ratrA * SL //withsl?upA:valuewhen(buyA, upA,0)

csA = ta.valuewhen(sellA, closeA, 0)


ratrsA = ta.valuewhen(sellA, request.security(syminfo.tickerid, tf1, ta.atr(14)),
0)
tps1A = csA - ratrsA * TP1
tps2A = csA - ratrsA * TP2
tps3A = csA - ratrsA * TP3
tps4A = csA - ratrsA * TP4
slsA = csA + ratrsA * SL //withsl?dnA:valuewhen(sellA, dnA, 0)

Ptpb1A = cbA * (1 + ptp1)


Ptpb2A = cbA * (1 + ptp2)
Ptpb3A = cbA * (1 + ptp3)
Ptpb4A = et == ex1 ? cbA * (1 + ptp4) : tpb4A
PslbA = cbA * (1 - psl)

Ptps1A = csA * (1 - ptp1)


Ptps2A = csA * (1 - ptp2)
Ptps3A = csA * (1 - ptp3)
Ptps4A = et == ex1 ? csA * (1 - ptp4) : tps4A
PslsA = csA * (1 + psl)

//Variable Fix TP SL
tpb1tA = et == ex1 ? Ptpb1A : tpb1A
tpb2tA = et == ex1 ? Ptpb2A : tpb2A
tpb3tA = et == ex1 ? Ptpb3A : tpb3A
tpb4tA = Ptpb4A
slbtA = et == ex1 ? PslbA : slbA
tps1tA = et == ex1 ? Ptps1A : tps1A
tps2tA = et == ex1 ? Ptps2A : tps2A
tps3tA = et == ex1 ? Ptps3A : tps3A
tps4tA = Ptps4A
slstA = et == ex1 ? PslsA : slsA

sinceentrybA = int(math.max(1, nz(ta.barssince(buyA))))


openedbA = ta.barssince(buyA) >= 0 and trendA[1] == 1 and longside
highestbarbA = ta.highest(sinceentrybA)
lowestbarbA = ta.lowest(sinceentrybA)
stillopenb1A = ta.barssince(buyA) == 1 ? true : highestbarbA[1] < tpb1tA and
lowestbarbA[1] > slbtA
stillopenb2A = ta.barssince(buyA) == 1 ? true : highestbarbA[1] < tpb2tA and
lowestbarbA[1] > slbtA
stillopenb3A = ta.barssince(buyA) == 1 ? true : highestbarbA[1] < tpb3tA and
lowestbarbA[1] > slbtA
stillopenb4A = ta.barssince(buyA) == 1 ? true : highestbarbA[1] < tpb4tA and
lowestbarbA[1] > slbtA

hittpb1A = ta.cross(highA, tpb1tA) and stillopenb1A and openedbA


plotshape(hittpb1A)
hittpb2A = ta.cross(highA, tpb2tA) and stillopenb2A and openedbA
hittpb3A = ta.cross(highA, tpb3tA) and stillopenb3A and openedbA
hittpb4A = ta.cross(highA, tpb4tA) and stillopenb4A and openedbA
hitslbA = lowA < slbtA and stillopenb4A and openedbA
winreversebA = closeA > cbA and backtest and ta.crossunder(closeA, stA) and
stillopenb4A and trendA == -1 and openedbA
losereversebA = closeA < cbA and backtest and ta.crossunder(closeA, stA) and
stillopenb4A and trendA == -1 and openedbA

sinceentrysA = int(math.max(1, nz(ta.barssince(sellA))))


openedsA = ta.barssince(sellA) >= 0 and trendA[1] == -1 and shortside
highestbarsA = ta.highest(sinceentrysA)
lowestbarsA = ta.lowest(sinceentrysA)
stillopens1A = ta.barssince(sellA) == 1 ? true : highestbarsA[1] < slstA and
lowestbarsA[1] > tps1tA
stillopens2A = ta.barssince(sellA) == 1 ? true : highestbarsA[1] < slstA and
lowestbarsA[1] > tps2tA
stillopens3A = ta.barssince(sellA) == 1 ? true : highestbarsA[1] < slstA and
lowestbarsA[1] > tps3tA
stillopens4A = ta.barssince(sellA) == 1 ? true : highestbarsA[1] < slstA and
lowestbarsA[1] > tps4tA

hittps1A = ta.cross(lowA, tps1tA) and stillopens1A and openedsA


hittps2A = ta.cross(lowA, tps2tA) and stillopens2A and openedsA
hittps3A = ta.cross(lowA, tps3tA) and stillopens3A and openedsA
hittps4A = ta.cross(lowA, tps4tA) and stillopens4A and openedsA
hitslsA = highA > slstA and stillopens4A and openedsA
winreversesA = closeA <= csA and backtest and ta.crossover(closeA, stA) and
stillopens4A and trendA == 1 and openedsA
losereversesA = closeA >= csA and backtest and ta.crossover(closeA, stA) and
stillopens4A and trendA == 1 and openedsA

//============================================
cumbuyA = ta.cum(buyA ? 1 : 0)
cumsellA = ta.cum(sellA ? 1 : 0)
cumsignalA = cumbuyA + cumsellA

//net profit
sisaqtybA = stillopenb1A ? 1 : stillopenb2A ? 1 - Qtytp1 : stillopenb3A ? 1 -
(Qtytp1 + Qtytp2) : 1 - (Qtytp1 + Qtytp2 + Qtytp3)
sisaqtysA = stillopens1A ? 1 : stillopens2A ? 1 - Qtytp1 : stillopens3A ? 1 -
(Qtytp1 + Qtytp2) : 1 - (Qtytp1 + Qtytp2 + Qtytp3)

ptpb1A = (tpb1tA - cbA) * 100 / cbA * Qtytp1 //-0.06


ptpb2A = (tpb2tA - cbA) * 100 / cbA * Qtytp2 //-0.06
ptpb3A = (tpb3tA - cbA) * 100 / cbA * Qtytp3 //-0.06
ptpb4A = (tpb4tA - cbA) * 100 / cbA * Qtytp4 //-0.06
pslbA = (closeA - cbA) * 100 / cbA * sisaqtybA //-0.06
prbA = (closeA - cbA) * 100 / cbA * sisaqtybA //-0.06

ptps1A = (csA - tps1tA) * 100 / csA * Qtytp1 //-0.06


ptps2A = (csA - tps2tA) * 100 / csA * Qtytp2 //-0.06
ptps3A = (csA - tps3tA) * 100 / csA * Qtytp3 //-0.06
ptps4A = (csA - tps4tA) * 100 / csA * Qtytp4 //-0.06
pslsA = (csA - closeA) * 100 / csA * sisaqtysA //-0.06
prsA = (csA - closeA) * 100 / csA * sisaqtysA //-0.06

prtpb1A = ta.cum(hittpb1A ? ptpb1A : 0)


prtpb2A = ta.cum(hittpb2A ? ptpb2A : 0)
prtpb3A = ta.cum(hittpb3A ? ptpb3A : 0)
prtpb4A = ta.cum(hittpb4A ? ptpb4A : 0)
prslbA = ta.cum(hitslbA ? pslbA : 0)
prwrbA = ta.cum(winreversebA ? prbA : 0)
prlrbA = ta.cum(losereversebA ? prbA : 0)

prtps1A = ta.cum(hittps1A ? ptps1A : 0)


prtps2A = ta.cum(hittps2A ? ptps2A : 0)
prtps3A = ta.cum(hittps3A ? ptps3A : 0)
prtps4A = ta.cum(hittps4A ? ptps4A : 0)
prslsA = ta.cum(hitslsA ? pslsA : 0)
prwrsA = ta.cum(winreversesA ? prsA : 0)
prlrsA = ta.cum(losereversesA ? prsA : 0)

netcapitalgainA = prtpb1A + prtpb2A + prtpb3A + prtpb4A + prslbA + prtps1A +


prtps2A + prtps3A + prtps4A + prslsA + prwrbA + prlrbA + prwrsA + prlrsA

///winrate
wintpb1A = ta.cum(hittpb1A ? 1 : 0)
wintpb2A = ta.cum(hittpb2A ? 1 : 0)
wintpb3A = ta.cum(hittpb3A ? 1 : 0)
wintpb4A = ta.cum(hittpb4A ? 1 : 0)
wrslbA = ta.cum(hitslbA ? 1 : 0)

wintps1A = ta.cum(hittps1A ? 1 : 0)
wintps2A = ta.cum(hittps2A ? 1 : 0)
wintps3A = ta.cum(hittps3A ? 1 : 0)
wintps4A = ta.cum(hittps4A ? 1 : 0)
wrslsA = ta.cum(hitslsA ? 1 : 0)

cumtp1A = wintpb1A + wintps1A


cumtp2A = wintpb2A + wintps2A
cumtp3A = wintpb3A + wintps3A
cumtp4A = wintpb4A + wintps4A
cumslA = wrslbA + wrslsA
cumpositionA = cumtp1A + cumtp2A + cumtp3A + cumtp4A + cumslA
//+cumwinreverseA//+cumlosereverseA

wrtp1A = truncate(cumtp1A * 100 / cumsignalA, 0)


wrtp2A = truncate(cumtp2A * 100 / cumsignalA, 0)
wrtp3A = truncate(cumtp3A * 100 / cumsignalA, 0)
wrtp4A = truncate(cumtp4A * 100 / cumsignalA, 0)
wrslA = truncate(cumslA * 100 / cumsignalA, 0)

winallA = cumtp1A + cumtp2A + cumtp3A + cumtp4A //+cumwinreverseA


wrallA = winallA * 100 / cumpositionA

//===================================================================
//===================================================================
//===================================================================
//===================================================================
//Timeframe B
//Supertrend Indicator

srcB = hl2B
atr2B = ta.sma(trB, period)
atrB = request.security(syminfo.tickerid, tf2, ta.atr(period))
upB = srcB - mult * atrB
up1B = nz(upB[1], upB)
upB := closeB[1] > up1B ? math.max(upB, up1B) : upB
dnB = srcB + mult * atrB
dn1B = nz(dnB[1], dnB)
dnB := closeB[1] < dn1B ? math.min(dnB, dn1B) : dnB
trendB = 1
trendB := nz(trendB[1], trendB)
trendB := trendB == -1 and closeB > dn1B ? 1 : trendB == 1 and closeB < up1B ? -1 :
trendB
stB = trendB == 1 ? upB : dnB

//filtering
atraB = request.security(syminfo.tickerid, tf2, ta.atr(atrLen))
atrMaB = atrMaType == 'EMB' ? ta.ema(atraB, atrMaLen) : ta.sma(atraB, atrMaLen)
updmB = ta.change(highB)
downdmB = -ta.change(lowB)
plusdmB = na(updmB) ? na : updmB > downdmB and updmB > 0 ? updmB : 0
minusdmB = na(downdmB) ? na : downdmB > updmB and downdmB > 0 ? downdmB : 0
//trurB = rma(trB, diLen)
//plusB = fixnan(100 * rma(plusdmB, diLen) / trurB)
//minusB = fixnan(100 * rma(minusdmB, diLen) / trurB)
//sumB = plusB + minusB
//adxB = 100 * rma(abs(plusB - minusB) / (sumB == 0 ? 1 : sumB), adxLen)

cndSidwayss1B = atraB >= atrMaB


cndSidwayss2B = RSI > toplimitrsi or RSI < botlimitrsi
cndSidwaysB = cndSidwayss1B or cndSidwayss2B
cndSidways1B = cndSidwayss1B and cndSidwayss2B
Sidwayss1B = atraB <= atrMaB
Sidwayss2B = RSI < toplimitrsi and RSI > botlimitrsi
SidwaysB = Sidwayss1B or Sidwayss2B
Sidways1B = Sidwayss1B and Sidwayss2B

trendTypeB = typefilter == filter1 ? cndSidwayss1B : typefilter == filter2 ?


cndSidwayss2B : typefilter == filter3 ? cndSidwaysB : typefilter == filter4 ?
cndSidways1B : typefilter == filter5 ? RSI > 0 : na

// Entry FIX
buysignal1B = ta.crossover(closeB, stB) and backtest and trendTypeB
sellsignal1B = ta.crossunder(closeB, stB) and backtest and trendTypeB
buyB = buysignal1B and longside
sellB = sellsignal1B and shortside

//Tp & Sl by atr Mult


cbB = ta.valuewhen(buyB, closeB, 0)
ratrB = ta.valuewhen(buyB, request.security(syminfo.tickerid, tf2, ta.atr(14)), 0)

tpb1B = cbB + ratrB * TP1


tpb2B = cbB + ratrB * TP2
tpb3B = cbB + ratrB * TP3
tpb4B = cbB + ratrB * TP4
slbB = cbB - ratrB * SL //withsl?upB:valuewhen(buyB, upB,0)

csB = ta.valuewhen(sellB, closeB, 0)


ratrsB = ta.valuewhen(sellB, request.security(syminfo.tickerid, tf2, ta.atr(14)),
0)
tps1B = csB - ratrsB * TP1
tps2B = csB - ratrsB * TP2
tps3B = csB - ratrsB * TP3
tps4B = csB - ratrsB * TP4
slsB = csB + ratrsB * SL //withsl?dnB:valuewhen(sellB, dnB, 0)

Ptpb1B = cbB * (1 + ptp1)


Ptpb2B = cbB * (1 + ptp2)
Ptpb3B = cbB * (1 + ptp3)
Ptpb4B = et == ex1 ? cbB * (1 + ptp4) : tpb4B
PslbB = cbB * (1 - psl)

Ptps1B = csB * (1 - ptp1)


Ptps2B = csB * (1 - ptp2)
Ptps3B = csB * (1 - ptp3)
Ptps4B = et == ex1 ? csB * (1 - ptp4) : tps4B
PslsB = csB * (1 + psl)

//Variable Fix TP SL
tpb1tB = et == ex1 ? Ptpb1B : tpb1B
tpb2tB = et == ex1 ? Ptpb2B : tpb2B
tpb3tB = et == ex1 ? Ptpb3B : tpb3B
tpb4tB = Ptpb4B
slbtB = et == ex1 ? PslbB : slbB
tps1tB = et == ex1 ? Ptps1B : tps1B
tps2tB = et == ex1 ? Ptps2B : tps2B
tps3tB = et == ex1 ? Ptps3B : tps3B
tps4tB = Ptps4B
slstB = et == ex1 ? PslsB : slsB

sinceentrybB = int(math.max(1, nz(ta.barssince(buyB))))


openedbB = ta.barssince(buyB) >= 0 and trendB[1] == 1 and longside
highestbarbB = ta.highest(sinceentrybB)
lowestbarbB = ta.lowest(sinceentrybB)
stillopenb1B = ta.barssince(buyB) == 1 ? true : highestbarbB[1] < tpb1tB and
lowestbarbB[1] > slbtB
stillopenb2B = ta.barssince(buyB) == 1 ? true : highestbarbB[1] < tpb2tB and
lowestbarbB[1] > slbtB
stillopenb3B = ta.barssince(buyB) == 1 ? true : highestbarbB[1] < tpb3tB and
lowestbarbB[1] > slbtB
stillopenb4B = ta.barssince(buyB) == 1 ? true : highestbarbB[1] < tpb4tB and
lowestbarbB[1] > slbtB

hittpb1B = ta.cross(highB, tpb1tB) and stillopenb1B and openedbB


plotshape(hittpb1B)
hittpb2B = ta.cross(highB, tpb2tB) and stillopenb2B and openedbB
hittpb3B = ta.cross(highB, tpb3tB) and stillopenb3B and openedbB
hittpb4B = ta.cross(highB, tpb4tB) and stillopenb4B and openedbB
hitslbB = lowB < slbtB and stillopenb4B and openedbB
winreversebB = closeB > cbB and backtest and ta.crossunder(closeB, stB) and
stillopenb4B and trendB == -1 and openedbB
losereversebB = closeB < cbB and backtest and ta.crossunder(closeB, stB) and
stillopenb4B and trendB == -1 and openedbB

sinceentrysB = int(math.max(1, nz(ta.barssince(sellB))))


openedsB = ta.barssince(sellB) >= 0 and trendB[1] == -1 and shortside
highestbarsB = ta.highest(sinceentrysB)
lowestbarsB = ta.lowest(sinceentrysB)
stillopens1B = ta.barssince(sellB) == 1 ? true : highestbarsB[1] < slstB and
lowestbarsB[1] > tps1tB
stillopens2B = ta.barssince(sellB) == 1 ? true : highestbarsB[1] < slstB and
lowestbarsB[1] > tps2tB
stillopens3B = ta.barssince(sellB) == 1 ? true : highestbarsB[1] < slstB and
lowestbarsB[1] > tps3tB
stillopens4B = ta.barssince(sellB) == 1 ? true : highestbarsB[1] < slstB and
lowestbarsB[1] > tps4tB

hittps1B = ta.cross(lowB, tps1tB) and stillopens1B and openedsB


hittps2B = ta.cross(lowB, tps2tB) and stillopens2B and openedsB
hittps3B = ta.cross(lowB, tps3tB) and stillopens3B and openedsB
hittps4B = ta.cross(lowB, tps4tB) and stillopens4B and openedsB
hitslsB = highB > slstB and stillopens4B and openedsB
winreversesB = closeB <= csB and backtest and ta.crossover(closeB, stB) and
stillopens4B and trendB == 1 and openedsB
losereversesB = closeB >= csB and backtest and ta.crossover(closeB, stB) and
stillopens4B and trendB == 1 and openedsB

//============================================
cumbuyB = ta.cum(buyB ? 1 : 0)
cumsellB = ta.cum(sellB ? 1 : 0)
cumsignalB = cumbuyB + cumsellB

//net profit
sisaqtybB = stillopenb1B ? 1 : stillopenb2B ? 1 - Qtytp1 : stillopenb3B ? 1 -
(Qtytp1 + Qtytp2) : 1 - (Qtytp1 + Qtytp2 + Qtytp3)
sisaqtysB = stillopens1B ? 1 : stillopens2B ? 1 - Qtytp1 : stillopens3B ? 1 -
(Qtytp1 + Qtytp2) : 1 - (Qtytp1 + Qtytp2 + Qtytp3)

ptpb1B = (tpb1tB - cbB) * 100 / cbB * Qtytp1 //-0.06


ptpb2B = (tpb2tB - cbB) * 100 / cbB * Qtytp2 //-0.06
ptpb3B = (tpb3tB - cbB) * 100 / cbB * Qtytp3 //-0.06
ptpb4B = (tpb4tB - cbB) * 100 / cbB * Qtytp4 //-0.06
pslbB = (closeB - cbB) * 100 / cbB * sisaqtybB //-0.06
prbB = (closeB - cbB) * 100 / cbB * sisaqtybB //-0.06

ptps1B = (csB - tps1tB) * 100 / csB * Qtytp1 //-0.06


ptps2B = (csB - tps2tB) * 100 / csB * Qtytp2 //-0.06
ptps3B = (csB - tps3tB) * 100 / csB * Qtytp3 //-0.06
ptps4B = (csB - tps4tB) * 100 / csB * Qtytp4 //-0.06
pslsB = (csB - close) * 100 / csB * sisaqtysB //-0.06
prsB = (csB - closeB) * 100 / csB * sisaqtysB //-0.06

prtpb1B = ta.cum(hittpb1B ? ptpb1B : 0)


prtpb2B = ta.cum(hittpb2B ? ptpb2B : 0)
prtpb3B = ta.cum(hittpb3B ? ptpb3B : 0)
prtpb4B = ta.cum(hittpb4B ? ptpb4B : 0)
prslbB = ta.cum(hitslbB ? pslbB : 0)
prwrbB = ta.cum(winreversebB ? prbB : 0)
prlrbB = ta.cum(losereversebB ? prbB : 0)

prtps1B = ta.cum(hittps1B ? ptps1B : 0)


prtps2B = ta.cum(hittps2B ? ptps2B : 0)
prtps3B = ta.cum(hittps3B ? ptps3B : 0)
prtps4B = ta.cum(hittps4B ? ptps4B : 0)
prslsB = ta.cum(hitslsB ? pslsB : 0)
prwrsB = ta.cum(winreversesB ? prsB : 0)
prlrsB = ta.cum(losereversesB ? prsB : 0)

netcapitalgainB = prtpb1B + prtpb2B + prtpb3B + prtpb4B + prslbB + prtps1B +


prtps2B + prtps3B + prtps4B + prslsB + prwrbB + prlrbB + prwrsB + prlrsB

///winrate
wintpb1B = ta.cum(hittpb1B ? 1 : 0)
wintpb2B = ta.cum(hittpb2B ? 1 : 0)
wintpb3B = ta.cum(hittpb3B ? 1 : 0)
wintpb4B = ta.cum(hittpb4B ? 1 : 0)
wrslbB = ta.cum(hitslbB ? 1 : 0)

wintps1B = ta.cum(hittps1B ? 1 : 0)
wintps2B = ta.cum(hittps2B ? 1 : 0)
wintps3B = ta.cum(hittps3B ? 1 : 0)
wintps4B = ta.cum(hittps4B ? 1 : 0)
wrslsB = ta.cum(hitslsB ? 1 : 0)

cumtp1B = wintpb1B + wintps1B


cumtp2B = wintpb2B + wintps2B
cumtp3B = wintpb3B + wintps3B
cumtp4B = wintpb4B + wintps4B
cumslB = wrslbB + wrslsB
cumpositionB = cumtp1B + cumtp2B + cumtp3B + cumtp4B + cumslB
//+cumwinreverseB//+cumlosereverseB

wrtp1B = truncate(cumtp1B * 100 / cumsignalB, 0)


wrtp2B = truncate(cumtp2B * 100 / cumsignalB, 0)
wrtp3B = truncate(cumtp3B * 100 / cumsignalB, 0)
wrtp4B = truncate(cumtp4B * 100 / cumsignalB, 0)
wrslB = truncate(cumslB * 100 / cumsignalB, 0)

winallB = cumtp1B + cumtp2B + cumtp3B + cumtp4B //+cumwinreverseB


wrallB = winallB * 100 / cumpositionB

//===================================================================
//===================================================================
//===================================================================
//===================================================================
//Timeframe C
//Supertrend Indicator
srcC = hl2C
atr2C = ta.sma(trC, period)
atrC = request.security(syminfo.tickerid, tf3, ta.atr(period))
upC = srcC - mult * atrC
up1C = nz(upC[1], upC)
upC := closeC[1] > up1C ? math.max(upC, up1C) : upC
dnC = srcC + mult * atrC
dn1C = nz(dnC[1], dnC)
dnC := closeC[1] < dn1C ? math.min(dnC, dn1C) : dnC
trendC = 1
trendC := nz(trendC[1], trendC)
trendC := trendC == -1 and closeC > dn1C ? 1 : trendC == 1 and closeC < up1C ? -1 :
trendC
stC = trendC == 1 ? upC : dnC

//filtering
atraC = request.security(syminfo.tickerid, tf3, ta.atr(atrLen))
atrMaC = atrMaType == 'EMC' ? ta.ema(atraC, atrMaLen) : ta.sma(atraC, atrMaLen)
updmC = ta.change(highC)
downdmC = -ta.change(lowC)
plusdmC = na(updmC) ? na : updmC > downdmC and updmC > 0 ? updmC : 0
minusdmC = na(downdmC) ? na : downdmC > updmC and downdmC > 0 ? downdmC : 0
//trurC = rma(trC, diLen)
//plusC = fixnan(100 * rma(plusdmC, diLen) / trurC)
//minusC = fixnan(100 * rma(minusdmC, diLen) / trurC)
//sumC = plusC + minusC
//adxC = 100 * rma(abs(plusC - minusC) / (sumC == 0 ? 1 : sumC), adxLen)

cndSidwayss1C = atraC >= atrMaC


cndSidwayss2C = RSI > toplimitrsi or RSI < botlimitrsi
cndSidwaysC = cndSidwayss1C or cndSidwayss2C
cndSidways1C = cndSidwayss1C and cndSidwayss2C
Sidwayss1C = atraC <= atrMaC
Sidwayss2C = RSI < toplimitrsi and RSI > botlimitrsi
SidwaysC = Sidwayss1C or Sidwayss2C
Sidways1C = Sidwayss1C and Sidwayss2C

trendTypeC = typefilter == filter1 ? cndSidwayss1C : typefilter == filter2 ?


cndSidwayss2C : typefilter == filter3 ? cndSidwaysC : typefilter == filter4 ?
cndSidways1C : typefilter == filter5 ? RSI > 0 : na

// Entry FIX
buysignal1C = ta.crossover(closeC, stC) and backtest and trendTypeC
sellsignal1C = ta.crossunder(closeC, stC) and backtest and trendTypeC

buyC = buysignal1C and longside


sellC = sellsignal1C and shortside

//Tp & Sl by atr Mult


cbC = ta.valuewhen(buyC, closeC, 0)
ratrC = ta.valuewhen(buyC, request.security(syminfo.tickerid, tf3, ta.atr(14)), 0)

tpb1C = cbC + ratrC * TP1


tpb2C = cbC + ratrC * TP2
tpb3C = cbC + ratrC * TP3
tpb4C = cbC + ratrC * TP4
slbC = cbC - ratrC * SL //withsl?upC:valuewhen(buyC, upC,0)
csC = ta.valuewhen(sellC, closeC, 0)
ratrsC = ta.valuewhen(sellC, request.security(syminfo.tickerid, tf3, ta.atr(14)),
0)
tps1C = csC - ratrsC * TP1
tps2C = csC - ratrsC * TP2
tps3C = csC - ratrsC * TP3
tps4C = csC - ratrsC * TP4
slsC = csC + ratrsC * SL //withsl?dnC:valuewhen(sellC, dnC, 0)

Ptpb1C = cbC * (1 + ptp1)


Ptpb2C = cbC * (1 + ptp2)
Ptpb3C = cbC * (1 + ptp3)
Ptpb4C = et == ex1 ? cbC * (1 + ptp4) : tpb4C
PslbC = cbC * (1 - psl)

Ptps1C = csC * (1 - ptp1)


Ptps2C = csC * (1 - ptp2)
Ptps3C = csC * (1 - ptp3)
Ptps4C = et == ex1 ? csC * (1 - ptp4) : tps4C
PslsC = csC * (1 + psl)

//Variable Fix TP SL
tpb1tC = et == ex1 ? Ptpb1C : tpb1C
tpb2tC = et == ex1 ? Ptpb2C : tpb2C
tpb3tC = et == ex1 ? Ptpb3C : tpb3C
tpb4tC = Ptpb4C
slbtC = et == ex1 ? PslbC : slbC
tps1tC = et == ex1 ? Ptps1C : tps1C
tps2tC = et == ex1 ? Ptps2C : tps2C
tps3tC = et == ex1 ? Ptps3C : tps3C
tps4tC = Ptps4C
slstC = et == ex1 ? PslsC : slsC

sinceentrybC = int(math.max(1, nz(ta.barssince(buyC))))


openedbC = ta.barssince(buyC) >= 0 and trendC[1] == 1 and longside
highestbarbC = ta.highest(sinceentrybC)
lowestbarbC = ta.lowest(sinceentrybC)
stillopenb1C = ta.barssince(buyC) == 1 ? true : highestbarbC[1] < tpb1tC and
lowestbarbC[1] > slbtC
stillopenb2C = ta.barssince(buyC) == 1 ? true : highestbarbC[1] < tpb2tC and
lowestbarbC[1] > slbtC
stillopenb3C = ta.barssince(buyC) == 1 ? true : highestbarbC[1] < tpb3tC and
lowestbarbC[1] > slbtC
stillopenb4C = ta.barssince(buyC) == 1 ? true : highestbarbC[1] < tpb4tC and
lowestbarbC[1] > slbtC

hittpb1C = ta.cross(highC, tpb1tC) and stillopenb1C and openedbC


plotshape(hittpb1C)
hittpb2C = ta.cross(highC, tpb2tC) and stillopenb2C and openedbC
hittpb3C = ta.cross(highC, tpb3tC) and stillopenb3C and openedbC
hittpb4C = ta.cross(highC, tpb4tC) and stillopenb4C and openedbC
hitslbC = lowC < slbtC and stillopenb4C and openedbC
winreversebC = closeC > cbC and backtest and ta.crossunder(closeC, stC) and
stillopenb4C and trendC == -1 and openedbC
losereversebC = closeC < cbC and backtest and ta.crossunder(closeC, stC) and
stillopenb4C and trendC == -1 and openedbC
sinceentrysC = int(math.max(1, nz(ta.barssince(sellC))))
openedsC = ta.barssince(sellC) >= 0 and trendC[1] == -1 and shortside
highestbarsC = ta.highest(sinceentrysC)
lowestbarsC = ta.lowest(sinceentrysC)
stillopens1C = ta.barssince(sellC) == 1 ? true : highestbarsC[1] < slstC and
lowestbarsC[1] > tps1tC
stillopens2C = ta.barssince(sellC) == 1 ? true : highestbarsC[1] < slstC and
lowestbarsC[1] > tps2tC
stillopens3C = ta.barssince(sellC) == 1 ? true : highestbarsC[1] < slstC and
lowestbarsC[1] > tps3tC
stillopens4C = ta.barssince(sellC) == 1 ? true : highestbarsC[1] < slstC and
lowestbarsC[1] > tps4tC

hittps1C = ta.cross(lowC, tps1tC) and stillopens1C and openedsC


hittps2C = ta.cross(lowC, tps2tC) and stillopens2C and openedsC
hittps3C = ta.cross(lowC, tps3tC) and stillopens3C and openedsC
hittps4C = ta.cross(lowC, tps4tC) and stillopens4C and openedsC
hitslsC = highC > slstC and stillopens4C and openedsC
winreversesC = closeC <= csC and backtest and ta.crossover(closeC, stC) and
stillopens4C and trendC == 1 and openedsC
losereversesC = closeC >= csC and backtest and ta.crossover(closeC, stC) and
stillopens4C and trendC == 1 and openedsC

//============================================
cumbuyC = ta.cum(buyC ? 1 : 0)
cumsellC = ta.cum(sellC ? 1 : 0)
cumsignalC = cumbuyC + cumsellC

//net profit
sisaqtybC = stillopenb1C ? 1 : stillopenb2C ? 1 - Qtytp1 : stillopenb3C ? 1 -
(Qtytp1 + Qtytp2) : 1 - (Qtytp1 + Qtytp2 + Qtytp3)
sisaqtysC = stillopens1C ? 1 : stillopens2C ? 1 - Qtytp1 : stillopens3C ? 1 -
(Qtytp1 + Qtytp2) : 1 - (Qtytp1 + Qtytp2 + Qtytp3)

ptpb1C = (tpb1tC - cbC) * 100 / cbC * Qtytp1 //-0.06


ptpb2C = (tpb2tC - cbC) * 100 / cbC * Qtytp2 //-0.06
ptpb3C = (tpb3tC - cbC) * 100 / cbC * Qtytp3 //-0.06
ptpb4C = (tpb4tC - cbC) * 100 / cbC * Qtytp4 //-0.06
pslbC = (closeC - cbC) * 100 / cbC * sisaqtybC //-0.06
prbC = (closeC - cbC) * 100 / cbC * sisaqtybC //-0.06

ptps1C = (csC - tps1tC) * 100 / csC * Qtytp1 //-0.06


ptps2C = (csC - tps2tC) * 100 / csC * Qtytp2 //-0.06
ptps3C = (csC - tps3tC) * 100 / csC * Qtytp3 //-0.06
ptps4C = (csC - tps4tC) * 100 / csC * Qtytp4 //-0.06
pslsC = (csC - closeC) * 100 / csC * sisaqtysC //-0.06
prsC = (csC - closeC) * 100 / csC * sisaqtysC //-0.06

prtpb1C = ta.cum(hittpb1C ? ptpb1C : 0)


prtpb2C = ta.cum(hittpb2C ? ptpb2C : 0)
prtpb3C = ta.cum(hittpb3C ? ptpb3C : 0)
prtpb4C = ta.cum(hittpb4C ? ptpb4C : 0)
prslbC = ta.cum(hitslbC ? pslbC : 0)
prwrbC = ta.cum(winreversebC ? prbC : 0)
prlrbC = ta.cum(losereversebC ? prbC : 0)

prtps1C = ta.cum(hittps1C ? ptps1C : 0)


prtps2C = ta.cum(hittps2C ? ptps2C : 0)
prtps3C = ta.cum(hittps3C ? ptps3C : 0)
prtps4C = ta.cum(hittps4C ? ptps4C : 0)
prslsC = ta.cum(hitslsC ? pslsC : 0)
prwrsC = ta.cum(winreversesC ? prsC : 0)
prlrsC = ta.cum(losereversesC ? prsC : 0)

netcapitalgainC = prtpb1C + prtpb2C + prtpb3C + prtpb4C + prslbC + prtps1C +


prtps2C + prtps3C + prtps4C + prslsC + prwrbC + prlrbC + prwrsC + prlrsC

///winrate
wintpb1C = ta.cum(hittpb1C ? 1 : 0)
wintpb2C = ta.cum(hittpb2C ? 1 : 0)
wintpb3C = ta.cum(hittpb3C ? 1 : 0)
wintpb4C = ta.cum(hittpb4C ? 1 : 0)
wrslbC = ta.cum(hitslbC ? 1 : 0)

wintps1C = ta.cum(hittps1C ? 1 : 0)
wintps2C = ta.cum(hittps2C ? 1 : 0)
wintps3C = ta.cum(hittps3C ? 1 : 0)
wintps4C = ta.cum(hittps4C ? 1 : 0)
wrslsC = ta.cum(hitslsC ? 1 : 0)

cumtp1C = wintpb1C + wintps1C


cumtp2C = wintpb2C + wintps2C
cumtp3C = wintpb3C + wintps3C
cumtp4C = wintpb4C + wintps4C
cumslC = wrslbC + wrslsC
cumpositionC = cumtp1C + cumtp2C + cumtp3C + cumtp4C + cumslC
//+cumwinreverseC//+cumlosereverseC

wrtp1C = truncate(cumtp1C * 100 / cumsignalC, 0)


wrtp2C = truncate(cumtp2C * 100 / cumsignalC, 0)
wrtp3C = truncate(cumtp3C * 100 / cumsignalC, 0)
wrtp4C = truncate(cumtp4C * 100 / cumsignalC, 0)
wrslC = truncate(cumslC * 100 / cumsignalC, 0)

winallC = cumtp1C + cumtp2C + cumtp3C + cumtp4C //+cumwinreverseC


wrallC = winallC * 100 / cumpositionC

//===================================================================
//===================================================================
//===================================================================
//===================================================================
//Timeframe D
//Supertrend Indicator

srcD = hl2D
atr2D = ta.sma(trD, period)
atrD = request.security(syminfo.tickerid, tf4, ta.atr(period))
upD = srcD - mult * atrD
up1D = nz(upD[1], upD)
upD := closeD[1] > up1D ? math.max(upD, up1D) : upD
dnD = srcD + mult * atrD
dn1D = nz(dnD[1], dnD)
dnD := closeD[1] < dn1D ? math.min(dnD, dn1D) : dnD
trendD = 1
trendD := nz(trendD[1], trendD)
trendD := trendD == -1 and closeD > dn1D ? 1 : trendD == 1 and closeD < up1D ? -1 :
trendD
stD = trendD == 1 ? upD : dnD

//filtering
atraD = request.security(syminfo.tickerid, tf4, ta.atr(atrLen))
atrMaD = atrMaType == 'EMD' ? ta.ema(atraD, atrMaLen) : ta.sma(atraD, atrMaLen)
updmD = ta.change(highD)
downdmD = -ta.change(lowD)
plusdmD = na(updmD) ? na : updmD > downdmD and updmD > 0 ? updmD : 0
minusdmD = na(downdmD) ? na : downdmD > updmD and downdmD > 0 ? downdmD : 0
//trurD = rma(trD, diLen)
//plusD = fixnan(100 * rma(plusdmD, diLen) / trurD)
//minusD = fixnan(100 * rma(minusdmD, diLen) / trurD)
//sumD = plusD + minusD
//adxD = 100 * rma(abs(plusD - minusD) / (sumD == 0 ? 1 : sumD), adxLen)

cndSidwayss1D = atraD >= atrMaD


cndSidwayss2D = RSI > toplimitrsi or RSI < botlimitrsi
cndSidwaysD = cndSidwayss1D or cndSidwayss2D
cndSidways1D = cndSidwayss1D and cndSidwayss2D
Sidwayss1D = atraD <= atrMaD
Sidwayss2D = RSI < toplimitrsi and RSI > botlimitrsi
SidwaysD = Sidwayss1D or Sidwayss2D
Sidways1D = Sidwayss1D and Sidwayss2D

trendTypeD = typefilter == filter1 ? cndSidwayss1D : typefilter == filter2 ?


cndSidwayss2D : typefilter == filter3 ? cndSidwaysD : typefilter == filter4 ?
cndSidways1D : typefilter == filter5 ? RSI > 0: na

// Entry FIX
buysignal1D = ta.crossover(closeD, stD) and backtest and trendTypeD
sellsignal1D = ta.crossunder(closeD, stD) and backtest and trendTypeD

buyD = buysignal1D and longside


sellD = sellsignal1D and shortside

//Tp & Sl by atr Mult


cbD = ta.valuewhen(buyD, closeD, 0)
ratrD = ta.valuewhen(buyD, request.security(syminfo.tickerid, tf4, ta.atr(14)), 0)

tpb1D = cbD + ratrD * TP1


tpb2D = cbD + ratrD * TP2
tpb3D = cbD + ratrD * TP3
tpb4D = cbD + ratrD * TP4
slbD = cbD - ratrD * SL //withsl?upD:valuewhen(buyD, upD,0)

csD = ta.valuewhen(sellD, closeD, 0)


ratrsD = ta.valuewhen(sellD, request.security(syminfo.tickerid, tf4, ta.atr(14)),
0)
tps1D = csD - ratrsD * TP1
tps2D = csD - ratrsD * TP2
tps3D = csD - ratrsD * TP3
tps4D = csD - ratrsD * TP4
slsD = csD + ratrsD * SL //withsl?dnD:valuewhen(sellD, dnD, 0)

Ptpb1D = cbD * (1 + ptp1)


Ptpb2D = cbD * (1 + ptp2)
Ptpb3D = cbD * (1 + ptp3)
Ptpb4D = et == ex1 ? cbD * (1 + ptp4) : tpb4D
PslbD = cbD * (1 - psl)

Ptps1D = csD * (1 - ptp1)


Ptps2D = csD * (1 - ptp2)
Ptps3D = csD * (1 - ptp3)
Ptps4D = et == ex1 ? csD * (1 - ptp4) : tps4D
PslsD = csD * (1 + psl)

//Variable Fix TP SL
tpb1tD = et == ex1 ? Ptpb1D : tpb1D
tpb2tD = et == ex1 ? Ptpb2D : tpb2D
tpb3tD = et == ex1 ? Ptpb3D : tpb3D
tpb4tD = Ptpb4D
slbtD = et == ex1 ? PslbD : slbD
tps1tD = et == ex1 ? Ptps1D : tps1D
tps2tD = et == ex1 ? Ptps2D : tps2D
tps3tD = et == ex1 ? Ptps3D : tps3D
tps4tD = Ptps4D
slstD = et == ex1 ? PslsD : slsD

sinceentrybD = int(math.max(1, nz(ta.barssince(buyD))))


openedbD = ta.barssince(buyD) >= 0 and trendD[1] == 1 and longside
highestbarbD = ta.highest(sinceentrybD)
lowestbarbD = ta.lowest(sinceentrybD)
stillopenb1D = ta.barssince(buyD) == 1 ? true : highestbarbD[1] < tpb1tD and
lowestbarbD[1] > slbtD
stillopenb2D = ta.barssince(buyD) == 1 ? true : highestbarbD[1] < tpb2tD and
lowestbarbD[1] > slbtD
stillopenb3D = ta.barssince(buyD) == 1 ? true : highestbarbD[1] < tpb3tD and
lowestbarbD[1] > slbtD
stillopenb4D = ta.barssince(buyD) == 1 ? true : highestbarbD[1] < tpb4tD and
lowestbarbD[1] > slbtD

hittpb1D = ta.cross(highD, tpb1tD) and stillopenb1D and openedbD


plotshape(hittpb1D)
hittpb2D = ta.cross(highD, tpb2tD) and stillopenb2D and openedbD
hittpb3D = ta.cross(highD, tpb3tD) and stillopenb3D and openedbD
hittpb4D = ta.cross(highD, tpb4tD) and stillopenb4D and openedbD
hitslbD = lowD < slbtD and stillopenb4D and openedbD
winreversebD = closeD > cbD and backtest and ta.crossunder(closeD, stD) and
stillopenb4D and trendD == -1 and openedbD
losereversebD = closeD < cbD and backtest and ta.crossunder(closeD, stD) and
stillopenb4D and trendD == -1 and openedbD

sinceentrysD = int(math.max(1, nz(ta.barssince(sellD))))


openedsD = ta.barssince(sellD) >= 0 and trendD[1] == -1 and shortside
highestbarsD = ta.highest(sinceentrysD)
lowestbarsD = ta.lowest(sinceentrysD)
stillopens1D = ta.barssince(sellD) == 1 ? true : highestbarsD[1] < slstD and
lowestbarsD[1] > tps1tD
stillopens2D = ta.barssince(sellD) == 1 ? true : highestbarsD[1] < slstD and
lowestbarsD[1] > tps2tD
stillopens3D = ta.barssince(sellD) == 1 ? true : highestbarsD[1] < slstD and
lowestbarsD[1] > tps3tD
stillopens4D = ta.barssince(sellD) == 1 ? true : highestbarsD[1] < slstD and
lowestbarsD[1] > tps4tD
hittps1D = ta.cross(lowD, tps1tD) and stillopens1D and openedsD
hittps2D = ta.cross(lowD, tps2tD) and stillopens2D and openedsD
hittps3D = ta.cross(lowD, tps3tD) and stillopens3D and openedsD
hittps4D = ta.cross(lowD, tps4tD) and stillopens4D and openedsD
hitslsD = highD > slstD and stillopens4D and openedsD
winreversesD = closeD <= csD and backtest and ta.crossover(closeD, stD) and
stillopens4D and trendD == 1 and openedsD
losereversesD = closeD >= csD and backtest and ta.crossover(closeD, stD) and
stillopens4D and trendD == 1 and openedsD

//============================================
cumbuyD = ta.cum(buyD ? 1 : 0)
cumsellD = ta.cum(sellD ? 1 : 0)
cumsignalD = cumbuyD + cumsellD

//net profit
sisaqtybD = stillopenb1D ? 1 : stillopenb2D ? 1 - Qtytp1 : stillopenb3D ? 1 -
(Qtytp1 + Qtytp2) : 1 - (Qtytp1 + Qtytp2 + Qtytp3)
sisaqtysD = stillopens1D ? 1 : stillopens2D ? 1 - Qtytp1 : stillopens3D ? 1 -
(Qtytp1 + Qtytp2) : 1 - (Qtytp1 + Qtytp2 + Qtytp3)

ptpb1D = (tpb1tD - cbD) * 100 / cbD * Qtytp1 //-0.06


ptpb2D = (tpb2tD - cbD) * 100 / cbD * Qtytp2 //-0.06
ptpb3D = (tpb3tD - cbD) * 100 / cbD * Qtytp3 //-0.06
ptpb4D = (tpb4tD - cbD) * 100 / cbD * Qtytp4 //-0.06
pslbD = (closeD - cbD) * 100 / cbD * sisaqtybD //-0.06
prbD = (closeD - cbD) * 100 / cbD * sisaqtybD //-0.06

ptps1D = (csD - tps1tD) * 100 / csD * Qtytp1 //-0.06


ptps2D = (csD - tps2tD) * 100 / csD * Qtytp2 //-0.06
ptps3D = (csD - tps3tD) * 100 / csD * Qtytp3 //-0.06
ptps4D = (csD - tps4tD) * 100 / csD * Qtytp4 //-0.06
pslsD = (csD - closeD) * 100 / csD * sisaqtysD //-0.06
prsD = (csD - closeD) * 100 / csD * sisaqtysD //-0.06

prtpb1D = ta.cum(hittpb1D ? ptpb1D : 0)


prtpb2D = ta.cum(hittpb2D ? ptpb2D : 0)
prtpb3D = ta.cum(hittpb3D ? ptpb3D : 0)
prtpb4D = ta.cum(hittpb4D ? ptpb4D : 0)
prslbD = ta.cum(hitslbD ? pslbD : 0)
prwrbD = ta.cum(winreversebD ? prbD : 0)
prlrbD = ta.cum(losereversebD ? prbD : 0)

prtps1D = ta.cum(hittps1D ? ptps1D : 0)


prtps2D = ta.cum(hittps2D ? ptps2D : 0)
prtps3D = ta.cum(hittps3D ? ptps3D : 0)
prtps4D = ta.cum(hittps4D ? ptps4D : 0)
prslsD = ta.cum(hitslsD ? pslsD : 0)
prwrsD = ta.cum(winreversesD ? prsD : 0)
prlrsD = ta.cum(losereversesD ? prsD : 0)

netcapitalgainD = prtpb1D + prtpb2D + prtpb3D + prtpb4D + prslbD + prtps1D +


prtps2D + prtps3D + prtps4D + prslsD + prwrbD + prlrbD + prwrsD + prlrsD

///winrate
wintpb1D = ta.cum(hittpb1D ? 1 : 0)
wintpb2D = ta.cum(hittpb2D ? 1 : 0)
wintpb3D = ta.cum(hittpb3D ? 1 : 0)
wintpb4D = ta.cum(hittpb4D ? 1 : 0)
wrslbD = ta.cum(hitslbD ? 1 : 0)

wintps1D = ta.cum(hittps1D ? 1 : 0)
wintps2D = ta.cum(hittps2D ? 1 : 0)
wintps3D = ta.cum(hittps3D ? 1 : 0)
wintps4D = ta.cum(hittps4D ? 1 : 0)
wrslsD = ta.cum(hitslsD ? 1 : 0)

cumtp1D = wintpb1D + wintps1D


cumtp2D = wintpb2D + wintps2D
cumtp3D = wintpb3D + wintps3D
cumtp4D = wintpb4D + wintps4D
cumslD = wrslbD + wrslsD
cumpositionD = cumtp1D + cumtp2D + cumtp3D + cumtp4D + cumslD
//+cumwinreverseD//+cumlosereverseD

wrtp1D = truncate(cumtp1D * 100 / cumsignalD, 0)


wrtp2D = truncate(cumtp2D * 100 / cumsignalD, 0)
wrtp3D = truncate(cumtp3D * 100 / cumsignalD, 0)
wrtp4D = truncate(cumtp4D * 100 / cumsignalD, 0)
wrslD = truncate(cumslD * 100 / cumsignalD, 0)

winallD = cumtp1D + cumtp2D + cumtp3D + cumtp4D //+cumwinreverseD


wrallD = winallD * 100 / cumpositionD

//===================================================================
//===================================================================

//
===================================================================================
====
//Trend Panel

smoothKsrsi = input.int(3, 'K', minval=1)


smoothDsrsi = input.int(3, 'D', minval=1)
lengthRSIsrsi = input.int(14, 'RSI Length', minval=1)
lengthStochsrsi = input.int(14, 'Stochastic Length', minval=1)
srcsrsi = input(close, title='RSI Source')
rsi1srsi = ta.rsi(srcsrsi, lengthRSIsrsi)
ksrsi = truncate(ta.sma(ta.stoch(rsi1srsi, rsi1srsi, rsi1srsi, lengthStochsrsi),
smoothKsrsi), round_num)
dsrsi = ta.sma(ksrsi, smoothDsrsi)

fast_lengthm = input(title='Fast Length', defval=12)


slow_lengthm = input(title='Slow Length', defval=26)
srcm = input(title='Source', defval=close)
signal_lengthm = input.int(title='Signal Smoothing', minval=1, maxval=50, defval=9)
sma_sourcem = input.string(title='Oscillator MA Type', defval='EMA',
options=['SMA', 'EMA'])
sma_signalm = input.string(title='Signal Line MA Type', defval='EMA',
options=['SMA', 'EMA'])

fast_mam = sma_sourcem == 'SMA' ? ta.sma(srcm, fast_lengthm) : ta.ema(srcm,


fast_lengthm)
slow_mam = sma_sourcem == 'SMA' ? ta.sma(srcm, slow_lengthm) : ta.ema(srcm,
slow_lengthm)
macd = fast_mam - slow_mam
signalm = sma_signalm == 'SMA' ? ta.sma(macd, signal_lengthm) : ta.ema(macd,
signal_lengthm)
histm = macd - signalm

trenddir = RSI > 55 ? 'Bullish' : RSI < 45 ? 'Bearish' : 'Sideways'

//Tele Notif
ep = trend == 1 ? cb : cs
winrate = truncate(strategy.wintrades / strategy.closedtrades * 100, 0)
capitalgain = 100 * strategy.netprofit / initialcapital
closedtrades = strategy.closedtrades

//Gathers User Inputs


//Dashboard
sideentry = strategy.position_size > 0 ? '🟢 LONG' : strategy.position_size < 0 ? '🔴
SHORT' : 'No Position'
epp = strategy.position_size > 0 ? cb : strategy.position_size < 0 ? cs : na
tp1p = truncate(strategy.position_size > 0 ? tpb1t : strategy.position_size < 0 ?
tps1t : na, round_num)
tp2p = truncate(strategy.position_size > 0 ? tpb2t : strategy.position_size < 0 ?
tps2t : na, round_num)
tp3p = truncate(strategy.position_size > 0 ? tpb3t : strategy.position_size < 0 ?
tps3t : na, round_num)
tp4p = truncate(strategy.position_size > 0 ? tpb4t : strategy.position_size < 0 ?
tps4t : na, round_num)
slp = truncate(strategy.position_size > 0 ? slbt : strategy.position_size < 0 ?
slst : na, round_num)
dashDist = input.int(5, 'Dashboard Distance', group='Dashboard Settings')
dashColor = input.color(color.new(#000000, 0), 'Dashboard Color', inline='Dash
Line', group='Dashboard Settings')
dashTextColor = input.color(color.new(#ffffff, 0), 'Text Color', inline='Dash
Line', group='Dashboard Settings')

sinceentryb = int(math.max(1, nz(ta.barssince(buy and backtest and trendType))))


highestbarb = ta.highest(high[1], sinceentryb)
lowestbarb = ta.lowest(low[1], sinceentryb)
sinceentrys = int(math.max(1, nz(ta.barssince(sell and backtest and trendType))))
highestbars = ta.highest(high[1], sinceentrys)
lowestbars = ta.lowest(low[1], sinceentrys)

hittp1 = strategy.position_size > 0 ? highestbars > tpb1t : strategy.position_size


< 0 ? lowestbars < tps1t : na
hittp1t = hittp1 ? ' ✅' : na
hittp2 = strategy.position_size > 0 ? highestbars > tpb2t : strategy.position_size
< 0 ? lowestbars < tps2t : na
hittp2t = hittp2 ? ' ✅' : na
hittp3 = strategy.position_size > 0 ? highestbars > tpb3t : strategy.position_size
< 0 ? lowestbars < tps3t : na
hittp3t = hittp3 ? ' ✅' : na
hittp4 = strategy.position_size > 0 ? highestbars > tpb4t : strategy.position_size
< 0 ? lowestbars < tps4t : na
hittp4t = hittp4 ? ' ✅' : na

// Counter for cancelled trades


var int cancelledTrades = 0
// Variables to track the current and previous position
var string currentPos = "none"
var string prevPos = "none"
var int longEntryCount = 0
var int shortEntryCount = 0

//
if buy and backtest and trendType and longside
strategy.entry('Buy', direction=strategy.long, comment='LONG')
longEntryCount := longEntryCount + 1

if sell and backtest and trendType and shortside


strategy.entry('Sell', direction=strategy.short, comment='SHORT')
shortEntryCount := shortEntryCount + 1

strategy.exit('TP 1', 'Buy', qty_percent=qtytp1, limit=tpb1t, comment_profit = "TP


1")
strategy.exit('TP 2', 'Buy', qty_percent=qtytp2, limit=tpb2t, comment_profit = "TP
2")
strategy.exit('TP 3', 'Buy', qty_percent=qtytp3, limit=tpb3t, comment_profit = "TP
3")
strategy.exit('TP 4', 'Buy', qty_percent=qtytp4, limit=tpb4t, comment_profit = "TP
4")

slbclose = sellsignal1 or low < slbt


if slbclose
strategy.close('Buy', comment='SL')

strategy.exit('TP 1', 'Sell', qty_percent=qtytp1, limit=tps1t, comment_profit = "TP


1")
strategy.exit('TP 2', 'Sell', qty_percent=qtytp2, limit=tps2t, comment_profit = "TP
2")
strategy.exit('TP 3', 'Sell', qty_percent=qtytp3, limit=tps3t, comment_profit = "TP
3")
strategy.exit('TP 4', 'Sell', qty_percent=qtytp4, limit=tps4t, comment_profit = "TP
4")

slsclose = buysignal1 or high > slst


if slsclose
strategy.close('Sell', comment='SL')

// Initialize a variable to store the last line objects for each type of line
var line lineTpb1t = na
var line lineTpb2t = na
var line lineTpb3t = na
var line lineTpb4t = na
var line lineTps1t = na
var line lineTps2t = na
var line lineTps3t = na
var line lineTps4t = na
var line lineSlbt = na
var line lineSlst = na

// Function to delete a line


deleteLine(line l) =>
if not na(l)
line.delete(l)
// Variable to keep track of the last strategy
var bool lastStrategyLong = na

// Check for strategy change


strategyChanged = (buy and not lastStrategyLong) or (sell and lastStrategyLong)

if strategyChanged
// Delete all lines from previous strategy
deleteLine(lineTpb1t)
deleteLine(lineTpb2t)
deleteLine(lineTpb3t)
deleteLine(lineTpb4t)
deleteLine(lineTps1t)
deleteLine(lineTps2t)
deleteLine(lineTps3t)
deleteLine(lineTps4t)
deleteLine(lineSlbt)
deleteLine(lineSlst)

if buy and backtest and trendType and longside


// Update the strategy tracker
lastStrategyLong := true

// Create lines for Buy strategy


lineTpb1t := line.new(bar_index[1], tpb1t, last_bar_index, tpb1t, width=2,
color=color.blue, style=line.style_dotted)
lineTpb2t := line.new(bar_index[1], tpb2t, last_bar_index, tpb2t, width=2,
color=color.blue, style=line.style_dotted)
lineTpb3t := line.new(bar_index[1], tpb3t, last_bar_index, tpb3t, width=2,
color=color.blue, style=line.style_dotted)
lineTpb4t := line.new(bar_index[1], tpb4t, last_bar_index, tpb4t, width=2,
color=color.blue, style=line.style_dotted)
lineSlbt := line.new(bar_index[1], slbt, last_bar_index, slbt, width=2,
color=color.red, style=line.style_dotted)

if sell and backtest and trendType and shortside


// Update the strategy tracker
lastStrategyLong := false

// Create lines for Sell strategy


lineTps1t := line.new(bar_index[1], tps1t, last_bar_index, tps1t, width=2,
color=color.blue, style=line.style_dotted)
lineTps2t := line.new(bar_index[1], tps2t, last_bar_index, tps2t, width=2,
color=color.blue, style=line.style_dotted)
lineTps3t := line.new(bar_index[1], tps3t, last_bar_index, tps3t, width=2,
color=color.blue, style=line.style_dotted)
lineTps4t := line.new(bar_index[1], tps4t, last_bar_index, tps4t, width=2,
color=color.blue, style=line.style_dotted)
lineSlst := line.new(bar_index[1], slst, last_bar_index, slst, width=2,
color=color.red, style=line.style_dotted)

//
besttf = math.max(netcapitalgainA, netcapitalgainB, netcapitalgainC,
netcapitalgainD)
besttft = besttf == netcapitalgainA ? tf1 : besttf == netcapitalgainB ? tf2 :
besttf == netcapitalgainC ? tf3 : besttf == netcapitalgainD ? tf4 : na
accesstf = timeframe.period
wrtp1 = accesstf == str.tostring(8) ? besttf == netcapitalgainA ? wrtp1A : besttf
== netcapitalgainB ? wrtp1B : besttf == netcapitalgainC ? wrtp1C : besttf ==
netcapitalgainD ? wrtp1D : na : na
wrtp2 = accesstf == str.tostring(8) ? besttf == netcapitalgainA ? wrtp2A : besttf
== netcapitalgainB ? wrtp2B : besttf == netcapitalgainC ? wrtp2C : besttf ==
netcapitalgainD ? wrtp2D : na : na
wrtp3 = accesstf == str.tostring(8) ? besttf == netcapitalgainA ? wrtp3A : besttf
== netcapitalgainB ? wrtp3B : besttf == netcapitalgainC ? wrtp3C : besttf ==
netcapitalgainD ? wrtp3D : na : na
wrtp4 = accesstf == str.tostring(8) ? besttf == netcapitalgainA ? wrtp4A : besttf
== netcapitalgainB ? wrtp4B : besttf == netcapitalgainC ? wrtp4C : besttf ==
netcapitalgainD ? wrtp4D : na : na
wrsl = accesstf == str.tostring(8) ? besttf == netcapitalgainA ? wrslA : besttf ==
netcapitalgainB ? wrslB : besttf == netcapitalgainC ? wrslC : besttf ==
netcapitalgainD ? wrslD : na : na
Besttf = accesstf == str.tostring(8) ? besttft : 'NaN'

mtfanalysis = accesstf == str.tostring(8) ? str.tostring(Besttf) : 'NaN'

epp2 = (epp + slp) / 2

//PLOTTING
bodyMiddle = plot(ST ? (open + close) / 2 : na, display=display.none)
uptrend = plot(ST ? trend < 0 ? na : up : na, 'Up trend',
color=color.new(color.green, 0), style=plot.style_linebr, linewidth=2)
downtrend = plot(ST ? trend < 0 ? dn : na : na, 'Down trend',
color=color.new(color.red, 0), style=plot.style_linebr, linewidth=2)

///////////////////////////////////////////////////////////////////////////////////
//////////////////

Dashboard1= true

//Start dashboard
import jdehorty/EconomicCalendar/1 as calendar

// ---- Table Settings Start ----//


max = 160 //Maximum Length
min = 10 //Minimum Length

i_s_Y1 = input.string( 'top', 'Table Position 1', inline = '0', options =


['top', 'middle', 'bottom'],group='Style Settings')
i_s_X1 = input.string( 'right', '', inline = '0', options =
['left', 'center', 'right'],group='Style Settings')

i_s_Y2 = input.string( 'bottom', 'Table Position 2', inline = '0', options =


['top', 'middle', 'bottom'],group='Style Settings')
i_s_X2 = input.string( 'right', '', inline = '0', options =
['left', 'center', 'right'],group='Style Settings')

heading_size = input.session('Small',"Heading
Size" ,options=["Tiny","Small","Normal","Large"] ,group='Style Settings')
sheading_size = input.session('Small',"Text
Size" ,options=["Tiny","Small","Normal","Large"] ,group='Style Settings')
text_size = input.session('Normal',"Data
Size" ,options=["Tiny","Small","Normal","Large"] ,group='Style Settings')
heading_size2 = input.session('Normal',"Heading
Size2" ,options=["Tiny","Small","Normal","Large"] ,group='Style Settings')
sheading_size2 = input.session('Normal',"Text
Size2" ,options=["Tiny","Small","Normal","Large"] ,group='Style Settings')
text_size2 = input.session('Normal',"Data
Size2" ,options=["Tiny","Small","Normal","Large"] ,group='Style Settings')

bg_color = input.color(color.rgb(67,70,81), "Background Color", group='Table Color


Settings')
Header_col = input.color(color.gray, "Headers Color", group='Table Color Settings')
headingColor = input.color(color.white, "Headings text Color", group='Table Color
Settings')
perColor = input.color(color.red, "Performance Color", group='Table Color
Settings')
tpslColor = input.color(color.green, "TP Color", group='Table Color Settings')
ssslColor = input.color(color.red, "SL Color", group='Table Color Settings')
overallColor = input.color(color.yellow, "Trades Color", group='Table Color
Settings')
intervalColor = input.color(color.orange, "Interval Color", group='Table Color
Settings')

// ==================
// ==== Settings ====
// ==================

//------Seting Color Calender Economi------

color1 = color.rgb(234, 51, 41)


color2 = color.orange
color3 = color.rgb(255, 230, 0)
color4 = color.rgb(97, 203, 74)
color5 = color.rgb(0, 225, 255)
color6 = color.rgb(214, 0, 252)
color7 = color.silver

var table_text_size = text_size == 'Normal' ? size.normal :


text_size == 'Tiny' ? size.tiny :
text_size == 'Small' ? size.small :
text_size == 'Normal' ? size.normal : size.large

var htable_text_size = heading_size == 'Normal' ? size.normal :


text_size == 'Tiny' ? size.tiny :
text_size == 'Small' ? size.small :
text_size == 'Normal' ? size.normal : size.large

var shtable_text_size = sheading_size == 'Normal' ? size.normal :


text_size == 'Tiny' ? size.tiny :
text_size == 'Small' ? size.small :
text_size == 'Normal' ? size.normal : size.large

var table_text_size2 = text_size2 == 'Normal' ? size.normal :


text_size == 'Tiny' ? size.tiny :
text_size == 'Small' ? size.small :
text_size == 'Normal' ? size.normal : size.large

var htable_text_size2 = heading_size2 == 'Normal' ? size.normal :


text_size == 'Tiny' ? size.tiny :
text_size == 'Small' ? size.small :
text_size == 'Normal' ? size.normal : size.large

var shtable_text_size2 = sheading_size2 == 'Normal' ? size.normal :


text_size == 'Tiny' ? size.tiny :
text_size == 'Small' ? size.small :
text_size == 'Normal' ? size.normal : size.large

var t = table.new(i_s_Y1 + "_" + i_s_X1,15,math.abs(max-min)+2,


frame_color =color.new(#2b2b2b, 0),
frame_width =1,
border_color =color.new(#2b2b2b, 0),
border_width =1)
//---- Table Position & Size code end ----//

exitStats() =>
tp1Count = 0
tp2Count = 0
tp3Count = 0
tp4Count = 0
slCount = 0

for i = 0 to strategy.closedtrades - 1
switch strategy.closedtrades.exit_comment(i)
"TP 1" => tp1Count += 1
"TP 2" => tp2Count += 1
"TP 3" => tp3Count += 1
"TP 4" => tp4Count += 1
"SL" => slCount += 1
[tp1Count, tp2Count, tp3Count, tp4Count, slCount]

[tp1Count, tp2Count, tp3Count, tp4Count, slCount] = exitStats()

var slongPosition = false


var sshortPosition = false

var stp1CountStored = 0
var stp2CountStored = 0
var stp3CountStored = 0
var stp4CountStored = 0
var sslCountStored = 0

// Function to update TP and SL counts


sexitStats() =>
stp1Count = 0
stp2Count = 0
stp3Count = 0
stp4Count = 0
sslCount = 0
for i = 0 to strategy.closedtrades - 1
switch strategy.closedtrades.exit_comment(i)
"TP 1" => stp1Count += 1
"TP 2" => stp2Count += 1
"TP 3" => stp3Count += 1
"TP 4" => stp4Count += 1
"SL" => sslCount += 1
[stp1Count, stp2Count, stp3Count, stp4Count, sslCount]

// Your entry conditions (example)


longCondition = buy and backtest and trendType and longside
shortCondition = sell and backtest and trendType and shortside

// Update TP and SL counts


[stp1Count, stp2Count, stp3Count, stp4Count, sslCount] = sexitStats()

if (longCondition)
if (sshortPosition and stp1Count == stp1CountStored and stp2Count ==
stp2CountStored and stp3Count == stp3CountStored and stp4Count == stp4CountStored
and sslCount == sslCountStored)
cancelledTrades += 1
slongPosition := true
sshortPosition := false
// Store the current counters
stp1CountStored := stp1Count
stp2CountStored := stp2Count
stp3CountStored := stp3Count
stp4CountStored := stp4Count
sslCountStored := sslCount
else if (shortCondition)
if (slongPosition and stp1Count == stp1CountStored and stp2Count ==
stp2CountStored and stp3Count == stp3CountStored and stp4Count == stp4CountStored
and sslCount == sslCountStored)
cancelledTrades += 1
slongPosition := false
sshortPosition := true
// Store the current counters
stp1CountStored := stp1Count
stp2CountStored := stp2Count
stp3CountStored := stp3Count
stp4CountStored := stp4Count
sslCountStored := sslCount

//---- Table Column & Rows code start ----//


if Dashboard1
//---- Table Main Column Headers code start ----//
table.cell(t,1,1,"Performance
Analysis",text_color=headingColor,text_size=htable_text_size,bgcolor=Header_col)

table.cell(t,1,2,'Net
Profit',text_color=headingColor,text_size=shtable_text_size,bgcolor=bg_color)

table.cell(t,1,3,'Realized',text_color=headingColor,text_size=shtable_text_size,bgc
olor=bg_color)

table.cell(t,1,4,'Winrate',text_color=headingColor,text_size=shtable_text_size,bgco
lor=bg_color)
table.cell(t,1,5,'',text_color=headingColor,text_size=table_text_size,bgcolor=bg_co
lor)

table.cell(t,1,6,'TP/SL Hit
Report',text_color=headingColor,text_size=htable_text_size,bgcolor=Header_col)

table.cell(t,1,7,'Targets',text_color=headingColor,text_size=shtable_text_size,bgco
lor=bg_color)

table.cell(t,1,8,'No. of
Hits',text_color=headingColor,text_size=shtable_text_size,bgcolor=bg_color)

table.cell(t,1,9,'',text_color=headingColor,text_size=table_text_size,bgcolor=bg_co
lor)

table.cell(t,1,10,'Overall
Report',text_color=headingColor,text_size=htable_text_size,bgcolor=Header_col)

table.cell(t,1,12,'Long
Trades',text_color=headingColor,text_size=shtable_text_size,bgcolor=bg_color)

table.cell(t,1,13,'Short
Trades',text_color=headingColor,text_size=shtable_text_size,bgcolor=bg_color)

table.cell(t,1,14,'',text_color=headingColor,text_size=table_text_size,bgcolor=bg_c
olor)

table.cell(t,1,15,'Interval',text_color=headingColor,text_size=htable_text_size,bgc
olor=Header_col)

table.cell(t,1,16,'Start',text_color=headingColor,text_size=shtable_text_size,bgcol
or=bg_color)

table.cell(t,1,17,'End',text_color=headingColor,text_size=shtable_text_size,bgcolor
=bg_color)

table.cell(t,1,11,'Total Closed
Trades',text_color=headingColor,text_size=shtable_text_size,bgcolor=bg_color)

table.cell(t,2,7,'TP1',text_color=headingColor,text_size=shtable_text_size,bgcolor=
bg_color)

table.cell(t,3,7,'TP2',text_color=headingColor,text_size=shtable_text_size,bgcolor=
bg_color)

table.cell(t,4,7,'TP3',text_color=headingColor,text_size=shtable_text_size,bgcolor=
bg_color)
table.cell(t,5,7,'TP4',text_color=headingColor,text_size=shtable_text_size,bgcolor=
bg_color)

table.cell(t,6,7,'SL',text_color=headingColor,text_size=shtable_text_size,bgcolor=b
g_color)

table.cell(t,7,7,'❌',text_color=headingColor,text_size=shtable_text_size,bgcolor=bg
_color)

table.cell(t,4,12,
str.tostring(longEntryCount),text_color=overallColor,text_size=table_text_size,bgco
lor=bg_color)

table.cell(t,4,13,
str.tostring(shortEntryCount) ,text_color=overallColor,text_size=table_text_size,bg
color=bg_color)

table.cell(t,4,11, str.tostring(longEntryCount +
shortEntryCount),text_color=overallColor,text_size=table_text_size,bgcolor=bg_color
)

table.cell(t,7,8,
str.tostring(cancelledTrades),text_color=ssslColor,text_size=table_text_size,bgcolo
r=bg_color)

table.cell(t,2,8,
str.tostring(tp1Count) ,text_color=tpslColor,text_size=table_text_size,bgcolor=bg_c
olor)
table.cell(t,3,8,
str.tostring(tp2Count) ,text_color=tpslColor,text_size=table_text_size,bgcolor=bg_c
olor)
table.cell(t,4,8,
str.tostring(tp3Count) ,text_color=tpslColor,text_size=table_text_size,bgcolor=bg_c
olor)
table.cell(t,5,8,
str.tostring(tp4Count) ,text_color=tpslColor,text_size=table_text_size,bgcolor=bg_c
olor)
table.cell(t,6,8,
str.tostring(slCount) ,text_color=ssslColor,text_size=table_text_size,bgcolor=bg_co
lor)

// Function to determine color based on value


colorBasedOnValue(value) =>
value >= 0 ? color.green : color.red

// Calculate and truncate values


winTradesPercent = math.round(strategy.wintrades / closedtrades * 100, 2)
netProfit = math.round(strategy.netprofit, 2)
capitalGain = math.round(capitalgain, 2)

// Determine colors
colorWinTrades = colorBasedOnValue(winTradesPercent)
colorNetProfit = colorBasedOnValue(netProfit)
colorCapitalGain = colorBasedOnValue(capitalGain)

// Create or update your table here...


// ...

// Update table cells with values and colors


table.cell(t, 4, 4, str.tostring(winTradesPercent) + " %",
text_color=colorWinTrades, text_size=table_text_size, bgcolor=bg_color)
table.cell(t, 4, 2, str.tostring(netProfit) + " " + syminfo.currency,
text_color=colorNetProfit, text_size=table_text_size, bgcolor=bg_color)
table.cell(t, 4, 3, str.tostring(capitalGain) + " %", text_color=colorCapitalGain,
text_size=table_text_size, bgcolor=bg_color)

///////////////////////

// Input for backtest days with a maximum of 20 days

// Calculating the start of the time window


start_window = timenow - millisecondinxdays

// Variable to store the date of the most recent trade within the time window
var string recentTradeDate = na

if (time >= start_window and time <= timenow)


recentTradeDate := str.tostring(year(timenow)) + "-" +
str.tostring(month(timenow)) + "-" + str.tostring(dayofmonth(timenow))

string startDate = str.format_time(start_window, "dd-MM-yyyy")

// Function to format date as ddMMyyyy


formatDate(d) =>
string dayStr = dayofmonth(d) < 10 ? "0" + str.tostring(dayofmonth(d)) :
str.tostring(dayofmonth(d))
string monthStr = month(d) < 10 ? "0" + str.tostring(month(d)) :
str.tostring(month(d))
string yearStr = str.tostring(year(d))
dayStr + '-' + monthStr + '-' + yearStr

// Display the entry date of the most recent trade


if Dashboard1 and strategy.opentrades > 0
trade_entry_time = strategy.opentrades.entry_time(strategy.opentrades - 1)
trade_date_str = formatDate(trade_entry_time)

table.cell(t,4,16,
startDate,text_color=intervalColor,text_size=table_text_size,bgcolor=bg_color)
table.cell(t,4,17,
trade_date_str,text_color=intervalColor,text_size=table_text_size,bgcolor=bg_color)

table.merge_cells(t,1,1,7,1)
table.merge_cells(t,1,5,7,5)
table.merge_cells(t,1,6,7,6)
table.merge_cells(t,1,9,7,9)
table.merge_cells(t,1,10,7,10)
table.merge_cells(t,1,15,7,15)
table.merge_cells(t,1,14,7,14)
table.merge_cells(t,1,2,3,2)
table.merge_cells(t,1,3,3,3)
table.merge_cells(t,1,4,3,4)
table.merge_cells(t,1,11,3,11)
table.merge_cells(t,1,12,3,12)
table.merge_cells(t,1,15,7,15)
table.merge_cells(t,1,16,3,16)
table.merge_cells(t,4,2,7,2)
table.merge_cells(t,4,3,7,3)
table.merge_cells(t,4,4,7,4)
table.merge_cells(t,4,11,7,11)
table.merge_cells(t,4,12,7,12)
table.merge_cells(t,1,15,7,15)
table.merge_cells(t,1,13,3,13)
table.merge_cells(t,4,13,7,13)
table.merge_cells(t,4,16,7,16)
table.merge_cells(t,1,17,3,17)
table.merge_cells(t,4,17,7,17)

var t2 = table.new(i_s_Y2 + "_" + i_s_X2,15,math.abs(max-min)+2,


frame_color =color.new(#2b2b2b, 0),
frame_width =1,
border_color =color.new(#2b2b2b, 0),
border_width =1)

if Dashboard1

table.cell(t2,1,1,'Current
Position',text_color=headingColor,text_size=htable_text_size2,bgcolor=Header_col)

table.cell(t2,1,2,'Coin',text_color=headingColor,text_size=shtable_text_size2,bgcol
or=bg_color)

table.cell(t2,1,3,'Position',text_color=headingColor,text_size=shtable_text_size2,b
gcolor=bg_color)
table.cell(t2,1,4,'Entry
range',text_color=headingColor,text_size=shtable_text_size2,bgcolor=bg_color)
table.cell(t2,1,5,'Take Profit
1',text_color=headingColor,text_size=shtable_text_size2,bgcolor=bg_color)
table.cell(t2,1,6,'Take Profit
2',text_color=headingColor,text_size=shtable_text_size2,bgcolor=bg_color)
table.cell(t2,1,7,'Take Profit
3',text_color=headingColor,text_size=shtable_text_size2,bgcolor=bg_color)
table.cell(t2,1,8,'Take Profit
4',text_color=headingColor,text_size=shtable_text_size2,bgcolor=bg_color)

table.cell(t2,1,9,'Stoploss',text_color=headingColor,text_size=shtable_text_size2,b
gcolor=bg_color)

table.cell(t2,4,2,syminfo.ticker,text_color=headingColor,text_size=shtable_text_siz
e2,bgcolor=bg_color)

table.cell(t2,4,3,str.tostring(sideentry),text_color=headingColor,text_size=shtable
_text_size2,bgcolor=bg_color)
table.cell(t2,4,4,str.tostring(truncate(epp, round_num)) + ' - ' +
str.tostring(truncate(epp2,
round_num)),text_color=headingColor,text_size=shtable_text_size2,bgcolor=bg_color)
table.cell(t2,4,5,str.tostring(truncate(tp1p, round_num)) +
str.tostring(hittp1t) ,text_color=headingColor,text_size=shtable_text_size2,bgcolor
=bg_color)
table.cell(t2,4,6,str.tostring(truncate(tp2p, round_num)) +
str.tostring(hittp2t),text_color=headingColor,text_size=shtable_text_size2,bgcolor=
bg_color)
table.cell(t2,4,7,str.tostring(truncate(tp3p, round_num)) +
str.tostring(hittp3t),text_color=headingColor,text_size=shtable_text_size2,bgcolor=
bg_color)
table.cell(t2,4,8,str.tostring(truncate(tp4p, round_num)) +
str.tostring(hittp4t) ,text_color=headingColor,text_size=shtable_text_size2,bgcolor
=bg_color)
table.cell(t2,4,9,str.tostring(truncate(slp,
round_num)),text_color=headingColor,text_size=shtable_text_size2,bgcolor=bg_color)
//
table.cell(t2,4,10,'',text_color=headingColor,text_size=table_text_size2,bgcolor=bg
_color)

table.merge_cells(t2,1,1,7,1)

table.merge_cells(t2,1,2,3,2)
table.merge_cells(t2,1,3,3,3)
table.merge_cells(t2,1,4,3,4)
table.merge_cells(t2,1,5,3,5)
table.merge_cells(t2,1,6,3,6)
table.merge_cells(t2,1,7,3,7)
table.merge_cells(t2,1,8,3,8)
table.merge_cells(t2,1,9,3,9)

table.merge_cells(t2,4,2,7,2)
table.merge_cells(t2,4,3,7,3)
table.merge_cells(t2,4,4,7,4)
table.merge_cells(t2,4,5,7,5)
table.merge_cells(t2,4,6,7,6)
table.merge_cells(t2,4,7,7,7)
table.merge_cells(t2,4,8,7,8)
table.merge_cells(t2,4,9,7,9)

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy