Added a Profit/Loss to the study
#//Original Script > @DonovanWall
#// Actual Version > @guikroth
#https://fr.tradingview.com/script/vkyFo1sQ/
#// Settings for 5min chart, BTCUSDC. For Other coin, change the paremeters
#study(title="Range Filter 5min", overlay=true)
# Converted by Sam4Cok @ 07/2022
#https://usethinkscript.com/threads/range-filter-buy-sell-5-min-for-thinkorswim.12020/
input ShowTarget = yes;
input BarColor = yes;
input ShowBubble = yes;
input src = close; #, title="Source")
input RFLength = 100; #, minval=1, title="Sampling Period")
input RangeMulti = 3.0;#, minval=0.1, title="Range Multiplier")
def na = Double.NaN;
########## Colors ########
DefineGlobalColor("lime" , CreateColor(0,230,118));
DefineGlobalColor("red" , CreateColor(247,12,24));
DefineGlobalColor("aqua" , CreateColor(124,252,0));
DefineGlobalColor("maroon" , CreateColor(132,0,0));
DefineGlobalColor("green" , CreateColor(22,96,69));
DefineGlobalColor("orange" , CreateColor(255,152,0));
DefineGlobalColor("fuchsia" , CreateColor(140,5,79));;
DefineGlobalColor("white" , CreateColor(255,255,255));;
############
script nz {
input data = 0;
input data1 = 0;
def ret_val = if isNaN(data) then data1 else data;
plot return = ret_val;
}
#// Smooth Average Range
#smoothrng(x, t, m)
script smoothrng {
input src = close;
input per = 100;
input mult = 3;
def wper = per * 2 - 1;
def avrng = ExpAverage(AbsValue(src - src[1]), per);
def smoothrng = ExpAverage(avrng, wper) * mult;
plot result = smoothrng;
}
#// Range Filter
#rngfilt(x, r) =>
script rngfilt {
input x = close;
input r = 0;
def rngfilt = if x > nz(rngfilt[1]) then if x - r < nz(rngfilt[1]) then nz(rngfilt[1]) else x - r else
if x + r > nz(rngfilt[1]) then nz(rngfilt[1]) else x + r;
plot result = rngfilt;
}
#########
def smrng = smoothrng(src, RFLength, RangeMulti);
def filt = rngfilt(src, smrng);
#// Filter Direction
def upward = if filt > filt[1] then nz(upward[1]) + 1 else if filt < filt[1] then 0 else nz(upward[1]);
def downward = if filt < filt[1] then nz(downward[1]) + 1 else if filt > filt[1] then 0 else nz(downward[1]);
#// Colors & plot
def filtcolor = if upward > 0 then 1 else if downward > 0 then -1 else 0;
plot filtplot = nz(filt, close); #"Range Filter"
filtplot.AssignValueColor( if filtcolor > 0 then GlobalColor("lime") else
if filtcolor < 0 then GlobalColor("red") else GlobalColor("orange"));
filtplot.SetLineWeight(2);
filtplot.HideTitle();
#// Target Bands
def hband = filt + smrng;
def lband = filt - smrng;
#// Target
def hbandplot = hband;
def lbandplot = lband;
#// Break Outs
def longCond = src > filt and src > src[1] and upward > 0 or
src > filt and src < src[1] and upward > 0;
def shortCond = src < filt and src < src[1] and downward > 0 or
src < filt and src > src[1] and downward > 0;
def CondIni = if longCond then 1 else if !shortCond then -1 else CondIni[1];
def longCondition = filtcolor > 0 and filtcolor[1] < 0;
def shortCondition = filtcolor < 0 and filtcolor[1] > 0;
#//Alerts & Fills
Addcloud(if ShowTarget then hbandplot else na, filtplot, GlobalColor("aqua"), GlobalColor("aqua"), no);
Addcloud(if ShowTarget then lbandplot else na, filtplot, GlobalColor("white"), GlobalColor("white"), no);
#AssignPriceColor(if BarColor then if src > filt and src > src[1] and upward > 0 then GlobalColor("lime") else
# if src > filt and src < src[1] and upward > 0 then GlobalColor("green") else
# if src < filt and src < src[1] and downward > 0 then GlobalColor("red") else
# if src < filt and src > src[1] and downward > 0 then GlobalColor("maroon") else
# GlobalColor("orange") else Color.CURRENT);
AddChartBubble(if ShowBubble then longCondition else na, low, "Buy", GlobalColor("green"), no);
AddChartBubble(if ShowBubble then shortCondition else na, high, "Sell", GlobalColor("red"), yes);
AddLabel(yes, if filtcolor > 0 then "RangeFilter-Bullish " else
if filtcolor < 0 then "RangeFilter-Bearish" else "",
if filtcolor > 0 then Color.green else
if filtcolor < 0 then Color.red else color.black);
Alert(filtcolor[1] < 0 and filtcolor > 0, "RangeFilter-Bullish ", Alert.Bar, Sound.ring);
Alert(filtcolor[1] > 0 and filtcolor < 0, "RangeFilter-Bearish ", Alert.Bar, Sound.ring);
############################################
## Define BuySignal and SellSignal above
## or uncomment them out below and set
## them to your buy/sell conditions
##
## If using stops, define them below
############################################
###------------------------------------------------------------------------------------------
input showSignals = yes;
input showLabels = yes;
input showBubbles = yes;
input useStops = no;
input useAlerts = no;
###------------------------------------------------------------------------------------------
############################################
## Create Signals -
## FILL IN THIS SECTION IF NOT DEFINED ABOVE
##
############################################
def BuySignal = longcondition; # insert condition to create long position
def SellSignal = shortcondition; # insert condition to create short position
def BuyStop = if !useStops then 0 else if 0<0 then 1 else 0 ; # insert condition to stop in place of the 0<0
def SellStop = if !useStops then 0 else if 0<0 then 1 else 0 ; # insert condition to stop in place of the 0>0
#######################################
## Maintain the position of trades
#######################################
def CurrentPosition; # holds whether flat = 0 long = 1 short = -1
if (BarNumber()==1) OR isNaN(CurrentPosition[1]) {
CurrentPosition = 0;
}else{
if CurrentPosition[1] == 0 { # FLAT
if (BuySignal) {
CurrentPosition = 1;
} else if (SellSignal){
CurrentPosition = -1;
} else {
CurrentPosition = CurrentPosition[1];
}
} else if CurrentPosition[1] == 1 { # LONG
if (SellSignal){
CurrentPosition = -1;
} else if (BuyStop and useStops){
CurrentPosition = 0;
} else {
CurrentPosition = CurrentPosition[1];
}
} else if CurrentPosition[1] == -1 { # SHORT
if (BuySignal){
CurrentPosition = 1;
} else if (SellStop and useStops){
CurrentPosition = 0;
} else {
CurrentPosition = CurrentPosition[1];
}
} else {
CurrentPosition = CurrentPosition[1];
}
}
def isLong = if CurrentPosition == 1 then 1 else 0;
def isShort = if CurrentPosition == -1 then 1 else 0;
def isFlat = if CurrentPosition == 0 then 1 else 0;
# If not already long and get a BuySignal
Plot BuySig = if (!isLong[1] and BuySignal and showSignals) then 1 else 0;
BuySig.AssignValueColor(color.cyan);
BuySig.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
BuySig.SetLineWeight(5);
Alert(BuySig and useAlerts, "Buy Signal",Alert.bar,sound.Ding);
Alert(BuySig and useAlerts, "Buy Signal",Alert.bar,sound.Ding);
# If not already short and get a SellSignal
Plot SellSig = if (!isShort[1] and SellSignal and showSignals) then 1 else 0;
SellSig.AssignValueColor(color.cyan);
SellSig.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
SellSig.SetLineWeight(5);
Alert(SellSig and useAlerts, "Sell Signal",Alert.bar,sound.Ding);
Alert(SellSig and useAlerts, "Sell Signal",Alert.bar,sound.Ding);
# If long and get a BuyStop
Plot BuyStpSig = if (BuyStop and isLong[1] and showSignals and useStops) then 1 else 0;
BuyStpSig.AssignValueColor(color.light_gray);
BuyStpSig.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
BuyStpSig.SetLineWeight(3);
# If short and get a SellStop
Plot SellStpSig = if (SellStop and isShort[1] and showSignals and useStops) then 1 else 0;
SellStpSig.AssignValueColor(color.light_gray);
SellStpSig.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
SellStpSig.SetLineWeight(3);
#######################################
## Orders
#######################################
def isOrder = if CurrentPosition == CurrentPosition[1] then 0 else 1; # Position changed so it's a new order
# If there is an order, then the price is the next days close
def orderPrice = if (isOrder and (BuySignal or SellSignal)) then close else orderPrice[1];
#######################################
## Price and Profit
#######################################
def profitLoss;
if (!isOrder or orderPRice[1]==0){
profitLoss = 0;
} else if ((isOrder and isLong[1]) and (SellSig or BuyStpSig)){
profitLoss = close - orderPrice[1];
} else if ((isOrder and isShort[1]) and (BuySig or SellStpSig)) {
profitLoss = orderPrice[1] - close;
} else {
profitLoss = 0;
}
# Total Profit or Loss
def profitLossSum = compoundValue(1, if isNaN(isOrder) or barnumber()==1 then 0 else if isOrder then profitLossSum[1] + profitLoss else profitLossSum[1], 0);
# How many trades won or lost
def profitWinners = compoundValue(1, if isNaN(profitWinners[1]) or barnumber()==1 then 0 else if isOrder and profitLoss > 0 then profitWinners[1] + 1 else profitWinners[1], 0);
def profitLosers = compoundValue(1, if isNaN(profitLosers[1]) or barnumber()==1 then 0 else if isOrder and profitLoss < 0 then profitLosers[1] + 1 else profitLosers[1], 0);
def profitPush = compoundValue(1, if isNaN(profitPush[1]) or barnumber()==1 then 0 else if isOrder and profitLoss == 0 then profitPush[1] + 1 else profitPush[1], 0);
def orderCount = (profitWinners + profitLosers + profitPush) - 1;
# Current Open Trade Profit or Loss
def TradePL = If isLong then Round(((close - orderprice)/TickSize())*TickValue()) else if isShort then Round(((orderPrice - close)/TickSize())*TickValue()) else 0;
# Convert to actual dollars based on Tick Value for bubbles
def dollarProfitLoss = if orderPRice[1]==0 or isNaN(orderPrice[1]) then 0 else round((profitLoss/Ticksize())*Tickvalue());
# Closed Orders dollar P/L
def dollarPLSum = round((profitLossSum/Ticksize())*Tickvalue());
# Split profits or losses by long and short trades
def profitLong = compoundValue(1, if isNan(profitLong[1]) or barnumber()==1 then 0 else if isOrder and isLong[1] then profitLong[1]+dollarProfitLoss else profitLong[1],0);
def profitShort = compoundValue(1, if isNan(profitShort[1]) or barnumber()==1 then 0 else if isOrder and isShort[1] then profitShort[1]+dollarProfitLoss else profitShort[1],0);
def countLong = compoundValue(1, if isNaN(countLong[1]) or barnumber()==1 then 0 else if isOrder and isLong[1] then countLong[1]+1 else countLong[1],0);
def countShort = compoundValue(1, if isNaN(countShort[1]) or barnumber()==1 then 0 else if isOrder and isShort[1] then countShort[1]+1 else countShort[1],0);
# What was the biggest winning and losing trade
def biggestWin = compoundValue(1, if isNaN(biggestWin[1]) or barnumber()==1 then 0 else if isOrder and (dollarProfitLoss > 0) and (dollarProfitLoss > biggestWin[1]) then dollarProfitLoss else biggestWin[1], 0);
def biggestLoss = compoundValue(1, if isNaN(biggestLoss[1]) or barnumber()==1 then 0 else if isOrder and (dollarProfitLoss < 0) and (dollarProfitLoss < biggestLoss[1]) then dollarProfitLoss else biggestLoss[1], 0);
# What percent were winners
def PCTWin = round((profitWinners/orderCount)*100,2);
# Average trade
def avgTrade = round((dollarPLSum/orderCount),2);
#######################################
## Create Labels
#######################################
AddLabel(showLabels, GetSymbol()+" Tick Size: "+TickSize()+" Value: "+TickValue(), color.white);
AddLabel(showLabels, "Closed Orders: " + orderCount + " P/L: " + AsDollars(dollarPLSum), if dollarPLSum > 0 then Color.GREEN else if dollarPLSum< 0 then Color.RED else Color.GRAY);
AddLabel(if !IsNan(orderPrice) and showLabels then 1 else 0, "Closed+Open P/L: "+ AsDollars(TradePL+dollarPLSum), if ((TradePL+dollarPLSum) > 0) then color.green else if ((TradePL+dollarPLSum) < 0) then color.red else color.gray);
AddLabel(showLabels, "Avg per Trade: "+ AsDollars(avgTrade), if avgTrade > 0 then Color.Green else if avgTrade < 0 then Color.RED else Color.GRAY);
AddLabel(showLabels, "Winners: "+ PCTWin +"%",if PCTWin > 50 then color.green else if PCTWin > 40 then color.yellow else color.gray);
AddLabel(showLabels, "MaxUp: "+ AsDollars(biggestWin) +" MaxDown: "+AsDollars(biggestLoss), color.white);
AddLabel(showLabels, "Long Profit: " +AsDollars(profitLong), if profitLong > 0 then color.green else if profitLong < 0 then color.red else color.gray);
AddLabel(showLabels, "Short Profit: " +AsDollars(profitShort), if profitShort > 0 then color.green else if profitShort < 0 then color.red else color.gray);
AddLabel(if !IsNan(CurrentPosition) and showLabels then 1 else 0, "Open: "+ (If isLong then "Bought" else "Sold") + " @ "+orderPrice, color.white);
AddLabel(if !IsNan(orderPrice) and showLabels then 1 else 0, "Open Trade P/L: "+ AsDollars(TradePL), if (TradePL > 0) then color.green else if (TradePl < 0) then color.red else color.gray);
#######################################
## Chart Bubbles for Profit/Loss
#######################################
AddChartBubble(showSignals and showBubbles and isOrder and isLong[1], low, "$"+dollarProfitLoss, if dollarProfitLoss == 0 then Color.LIGHT_GRAY else if dollarProfitLoss > 0 then Color.GREEN else color.Red, 0);
AddChartBubble(showSignals and showBubbles and isOrder and isShort[1], high, "$"+dollarProfitLoss, if dollarProfitLoss == 0 then Color.LIGHT_GRAY else if dollarProfitLoss > 0 then Color.GREEN else color.Red, 1);
#AssignPriceColor(if CurrentPosition == 1 then color.green else if CurrentPosition == -1 then color.red else color.gray);
#### END
#//Original Script > @DonovanWall
#// Actual Version > @guikroth
#https://fr.tradingview.com/script/vkyFo1sQ/
#// Settings for 5min chart, BTCUSDC. For Other coin, change the paremeters
#study(title="Range Filter 5min", overlay=true)
# Converted by Sam4Cok @ 07/2022
#https://usethinkscript.com/threads/range-filter-buy-sell-5-min-for-thinkorswim.12020/
input ShowTarget = yes;
input BarColor = yes;
input ShowBubble = yes;
input src = close; #, title="Source")
input RFLength = 100; #, minval=1, title="Sampling Period")
input RangeMulti = 3.0;#, minval=0.1, title="Range Multiplier")
def na = Double.NaN;
########## Colors ########
DefineGlobalColor("lime" , CreateColor(0,230,118));
DefineGlobalColor("red" , CreateColor(247,12,24));
DefineGlobalColor("aqua" , CreateColor(124,252,0));
DefineGlobalColor("maroon" , CreateColor(132,0,0));
DefineGlobalColor("green" , CreateColor(22,96,69));
DefineGlobalColor("orange" , CreateColor(255,152,0));
DefineGlobalColor("fuchsia" , CreateColor(140,5,79));;
DefineGlobalColor("white" , CreateColor(255,255,255));;
############
script nz {
input data = 0;
input data1 = 0;
def ret_val = if isNaN(data) then data1 else data;
plot return = ret_val;
}
#// Smooth Average Range
#smoothrng(x, t, m)
script smoothrng {
input src = close;
input per = 100;
input mult = 3;
def wper = per * 2 - 1;
def avrng = ExpAverage(AbsValue(src - src[1]), per);
def smoothrng = ExpAverage(avrng, wper) * mult;
plot result = smoothrng;
}
#// Range Filter
#rngfilt(x, r) =>
script rngfilt {
input x = close;
input r = 0;
def rngfilt = if x > nz(rngfilt[1]) then if x - r < nz(rngfilt[1]) then nz(rngfilt[1]) else x - r else
if x + r > nz(rngfilt[1]) then nz(rngfilt[1]) else x + r;
plot result = rngfilt;
}
#########
def smrng = smoothrng(src, RFLength, RangeMulti);
def filt = rngfilt(src, smrng);
#// Filter Direction
def upward = if filt > filt[1] then nz(upward[1]) + 1 else if filt < filt[1] then 0 else nz(upward[1]);
def downward = if filt < filt[1] then nz(downward[1]) + 1 else if filt > filt[1] then 0 else nz(downward[1]);
#// Colors & plot
def filtcolor = if upward > 0 then 1 else if downward > 0 then -1 else 0;
plot filtplot = nz(filt, close); #"Range Filter"
filtplot.AssignValueColor( if filtcolor > 0 then GlobalColor("lime") else
if filtcolor < 0 then GlobalColor("red") else GlobalColor("orange"));
filtplot.SetLineWeight(2);
filtplot.HideTitle();
#// Target Bands
def hband = filt + smrng;
def lband = filt - smrng;
#// Target
def hbandplot = hband;
def lbandplot = lband;
#// Break Outs
def longCond = src > filt and src > src[1] and upward > 0 or
src > filt and src < src[1] and upward > 0;
def shortCond = src < filt and src < src[1] and downward > 0 or
src < filt and src > src[1] and downward > 0;
def CondIni = if longCond then 1 else if !shortCond then -1 else CondIni[1];
def longCondition = filtcolor > 0 and filtcolor[1] < 0;
def shortCondition = filtcolor < 0 and filtcolor[1] > 0;
#//Alerts & Fills
Addcloud(if ShowTarget then hbandplot else na, filtplot, GlobalColor("aqua"), GlobalColor("aqua"), no);
Addcloud(if ShowTarget then lbandplot else na, filtplot, GlobalColor("white"), GlobalColor("white"), no);
#AssignPriceColor(if BarColor then if src > filt and src > src[1] and upward > 0 then GlobalColor("lime") else
# if src > filt and src < src[1] and upward > 0 then GlobalColor("green") else
# if src < filt and src < src[1] and downward > 0 then GlobalColor("red") else
# if src < filt and src > src[1] and downward > 0 then GlobalColor("maroon") else
# GlobalColor("orange") else Color.CURRENT);
AddChartBubble(if ShowBubble then longCondition else na, low, "Buy", GlobalColor("green"), no);
AddChartBubble(if ShowBubble then shortCondition else na, high, "Sell", GlobalColor("red"), yes);
AddLabel(yes, if filtcolor > 0 then "RangeFilter-Bullish " else
if filtcolor < 0 then "RangeFilter-Bearish" else "",
if filtcolor > 0 then Color.green else
if filtcolor < 0 then Color.red else color.black);
Alert(filtcolor[1] < 0 and filtcolor > 0, "RangeFilter-Bullish ", Alert.Bar, Sound.ring);
Alert(filtcolor[1] > 0 and filtcolor < 0, "RangeFilter-Bearish ", Alert.Bar, Sound.ring);
############################################
## Define BuySignal and SellSignal above
## or uncomment them out below and set
## them to your buy/sell conditions
##
## If using stops, define them below
############################################
###------------------------------------------------------------------------------------------
input showSignals = yes;
input showLabels = yes;
input showBubbles = yes;
input useStops = no;
input useAlerts = no;
###------------------------------------------------------------------------------------------
############################################
## Create Signals -
## FILL IN THIS SECTION IF NOT DEFINED ABOVE
##
############################################
def BuySignal = longcondition; # insert condition to create long position
def SellSignal = shortcondition; # insert condition to create short position
def BuyStop = if !useStops then 0 else if 0<0 then 1 else 0 ; # insert condition to stop in place of the 0<0
def SellStop = if !useStops then 0 else if 0<0 then 1 else 0 ; # insert condition to stop in place of the 0>0
#######################################
## Maintain the position of trades
#######################################
def CurrentPosition; # holds whether flat = 0 long = 1 short = -1
if (BarNumber()==1) OR isNaN(CurrentPosition[1]) {
CurrentPosition = 0;
}else{
if CurrentPosition[1] == 0 { # FLAT
if (BuySignal) {
CurrentPosition = 1;
} else if (SellSignal){
CurrentPosition = -1;
} else {
CurrentPosition = CurrentPosition[1];
}
} else if CurrentPosition[1] == 1 { # LONG
if (SellSignal){
CurrentPosition = -1;
} else if (BuyStop and useStops){
CurrentPosition = 0;
} else {
CurrentPosition = CurrentPosition[1];
}
} else if CurrentPosition[1] == -1 { # SHORT
if (BuySignal){
CurrentPosition = 1;
} else if (SellStop and useStops){
CurrentPosition = 0;
} else {
CurrentPosition = CurrentPosition[1];
}
} else {
CurrentPosition = CurrentPosition[1];
}
}
def isLong = if CurrentPosition == 1 then 1 else 0;
def isShort = if CurrentPosition == -1 then 1 else 0;
def isFlat = if CurrentPosition == 0 then 1 else 0;
# If not already long and get a BuySignal
Plot BuySig = if (!isLong[1] and BuySignal and showSignals) then 1 else 0;
BuySig.AssignValueColor(color.cyan);
BuySig.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
BuySig.SetLineWeight(5);
Alert(BuySig and useAlerts, "Buy Signal",Alert.bar,sound.Ding);
Alert(BuySig and useAlerts, "Buy Signal",Alert.bar,sound.Ding);
# If not already short and get a SellSignal
Plot SellSig = if (!isShort[1] and SellSignal and showSignals) then 1 else 0;
SellSig.AssignValueColor(color.cyan);
SellSig.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
SellSig.SetLineWeight(5);
Alert(SellSig and useAlerts, "Sell Signal",Alert.bar,sound.Ding);
Alert(SellSig and useAlerts, "Sell Signal",Alert.bar,sound.Ding);
# If long and get a BuyStop
Plot BuyStpSig = if (BuyStop and isLong[1] and showSignals and useStops) then 1 else 0;
BuyStpSig.AssignValueColor(color.light_gray);
BuyStpSig.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
BuyStpSig.SetLineWeight(3);
# If short and get a SellStop
Plot SellStpSig = if (SellStop and isShort[1] and showSignals and useStops) then 1 else 0;
SellStpSig.AssignValueColor(color.light_gray);
SellStpSig.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
SellStpSig.SetLineWeight(3);
#######################################
## Orders
#######################################
def isOrder = if CurrentPosition == CurrentPosition[1] then 0 else 1; # Position changed so it's a new order
# If there is an order, then the price is the next days close
def orderPrice = if (isOrder and (BuySignal or SellSignal)) then close else orderPrice[1];
#######################################
## Price and Profit
#######################################
def profitLoss;
if (!isOrder or orderPRice[1]==0){
profitLoss = 0;
} else if ((isOrder and isLong[1]) and (SellSig or BuyStpSig)){
profitLoss = close - orderPrice[1];
} else if ((isOrder and isShort[1]) and (BuySig or SellStpSig)) {
profitLoss = orderPrice[1] - close;
} else {
profitLoss = 0;
}
# Total Profit or Loss
def profitLossSum = compoundValue(1, if isNaN(isOrder) or barnumber()==1 then 0 else if isOrder then profitLossSum[1] + profitLoss else profitLossSum[1], 0);
# How many trades won or lost
def profitWinners = compoundValue(1, if isNaN(profitWinners[1]) or barnumber()==1 then 0 else if isOrder and profitLoss > 0 then profitWinners[1] + 1 else profitWinners[1], 0);
def profitLosers = compoundValue(1, if isNaN(profitLosers[1]) or barnumber()==1 then 0 else if isOrder and profitLoss < 0 then profitLosers[1] + 1 else profitLosers[1], 0);
def profitPush = compoundValue(1, if isNaN(profitPush[1]) or barnumber()==1 then 0 else if isOrder and profitLoss == 0 then profitPush[1] + 1 else profitPush[1], 0);
def orderCount = (profitWinners + profitLosers + profitPush) - 1;
# Current Open Trade Profit or Loss
def TradePL = If isLong then Round(((close - orderprice)/TickSize())*TickValue()) else if isShort then Round(((orderPrice - close)/TickSize())*TickValue()) else 0;
# Convert to actual dollars based on Tick Value for bubbles
def dollarProfitLoss = if orderPRice[1]==0 or isNaN(orderPrice[1]) then 0 else round((profitLoss/Ticksize())*Tickvalue());
# Closed Orders dollar P/L
def dollarPLSum = round((profitLossSum/Ticksize())*Tickvalue());
# Split profits or losses by long and short trades
def profitLong = compoundValue(1, if isNan(profitLong[1]) or barnumber()==1 then 0 else if isOrder and isLong[1] then profitLong[1]+dollarProfitLoss else profitLong[1],0);
def profitShort = compoundValue(1, if isNan(profitShort[1]) or barnumber()==1 then 0 else if isOrder and isShort[1] then profitShort[1]+dollarProfitLoss else profitShort[1],0);
def countLong = compoundValue(1, if isNaN(countLong[1]) or barnumber()==1 then 0 else if isOrder and isLong[1] then countLong[1]+1 else countLong[1],0);
def countShort = compoundValue(1, if isNaN(countShort[1]) or barnumber()==1 then 0 else if isOrder and isShort[1] then countShort[1]+1 else countShort[1],0);
# What was the biggest winning and losing trade
def biggestWin = compoundValue(1, if isNaN(biggestWin[1]) or barnumber()==1 then 0 else if isOrder and (dollarProfitLoss > 0) and (dollarProfitLoss > biggestWin[1]) then dollarProfitLoss else biggestWin[1], 0);
def biggestLoss = compoundValue(1, if isNaN(biggestLoss[1]) or barnumber()==1 then 0 else if isOrder and (dollarProfitLoss < 0) and (dollarProfitLoss < biggestLoss[1]) then dollarProfitLoss else biggestLoss[1], 0);
# What percent were winners
def PCTWin = round((profitWinners/orderCount)*100,2);
# Average trade
def avgTrade = round((dollarPLSum/orderCount),2);
#######################################
## Create Labels
#######################################
AddLabel(showLabels, GetSymbol()+" Tick Size: "+TickSize()+" Value: "+TickValue(), color.white);
AddLabel(showLabels, "Closed Orders: " + orderCount + " P/L: " + AsDollars(dollarPLSum), if dollarPLSum > 0 then Color.GREEN else if dollarPLSum< 0 then Color.RED else Color.GRAY);
AddLabel(if !IsNan(orderPrice) and showLabels then 1 else 0, "Closed+Open P/L: "+ AsDollars(TradePL+dollarPLSum), if ((TradePL+dollarPLSum) > 0) then color.green else if ((TradePL+dollarPLSum) < 0) then color.red else color.gray);
AddLabel(showLabels, "Avg per Trade: "+ AsDollars(avgTrade), if avgTrade > 0 then Color.Green else if avgTrade < 0 then Color.RED else Color.GRAY);
AddLabel(showLabels, "Winners: "+ PCTWin +"%",if PCTWin > 50 then color.green else if PCTWin > 40 then color.yellow else color.gray);
AddLabel(showLabels, "MaxUp: "+ AsDollars(biggestWin) +" MaxDown: "+AsDollars(biggestLoss), color.white);
AddLabel(showLabels, "Long Profit: " +AsDollars(profitLong), if profitLong > 0 then color.green else if profitLong < 0 then color.red else color.gray);
AddLabel(showLabels, "Short Profit: " +AsDollars(profitShort), if profitShort > 0 then color.green else if profitShort < 0 then color.red else color.gray);
AddLabel(if !IsNan(CurrentPosition) and showLabels then 1 else 0, "Open: "+ (If isLong then "Bought" else "Sold") + " @ "+orderPrice, color.white);
AddLabel(if !IsNan(orderPrice) and showLabels then 1 else 0, "Open Trade P/L: "+ AsDollars(TradePL), if (TradePL > 0) then color.green else if (TradePl < 0) then color.red else color.gray);
#######################################
## Chart Bubbles for Profit/Loss
#######################################
AddChartBubble(showSignals and showBubbles and isOrder and isLong[1], low, "$"+dollarProfitLoss, if dollarProfitLoss == 0 then Color.LIGHT_GRAY else if dollarProfitLoss > 0 then Color.GREEN else color.Red, 0);
AddChartBubble(showSignals and showBubbles and isOrder and isShort[1], high, "$"+dollarProfitLoss, if dollarProfitLoss == 0 then Color.LIGHT_GRAY else if dollarProfitLoss > 0 then Color.GREEN else color.Red, 1);
#AssignPriceColor(if CurrentPosition == 1 then color.green else if CurrentPosition == -1 then color.red else color.gray);
#### END