Working on a custom P&L study that's used in conjunction with a strategy (see full script here). I'm attempting to get the lowest and highest P&L for each round trip triggered by the strategy (not the Total P&L value, but the return for each individual trade - i.e.
After some debugging it seems when using either
I'm thinking there may be a problem deeper in my code, but I can't seem to figure out where. Here's the logic that handles the w/l ratios, counts & trade return calculations. If anyone needs to dig deeper than the snippet below, the entire script can be found in this post. Any suggestions on how to resolve this using the current logic or possibly another different approach to calculating the difference between the entry and exit price of a strategy order?
$1.50[exitPrice] - $1.00[entryPrice] = .50). The code as is working fine to determine the difference between the
entryPrice. The problem is when I store the return value in a temp variable and then try to use either
LowestAll()to find the highest and lowest trade returns, it breaks all of my win/loss ratios.
def highestReturn = HighestAll(entryReturnWin); def lowestReturn = LowestAll(entryReturnLoss);
LowestAll()on any of the variables declared using the
isExitBarcondition, it breaks all the win/loss logic due to a divide by zero error and instead of returning the highest/lowest returns it just returns 0 for either. The odd thing is that if I use
Lowesteverything works fine with the caveat that I can only check for the highest/lowest return for a maximum of 2000 bars in the past (which will potentially return the incorrect lowest value if the chart is greater than 2k bars).
#Breaks all the win/loss calculations and returns 0 def highestReturn = HighestAll(entryReturnWin); def lowestReturn = LowestAll(entryReturnLoss); #Does not break the win/loss calculations but will not return lowest value if it's older than 2k bars def highestReturn = Highest(entryReturnWin, 1999); def lowestReturn = Lowest(entryReturnLoss, 1999);
#Win/Loss ratios def entryBarsTemp = if hasEntry then bn else nan; def entryBarNum = if hasEntry and IsNaN(entryBarsTemp) then bn else entryBarNum; def isEntryBar = entryBarNum != entryBarNum; def entryBarPL = if isEntryBar then FPL else entryBarPL; def exitBarsTemp = if !hasEntry and bn > entryBarsTemp then bn else nan; def exitBarNum = if !hasEntry and !IsNaN(exitBarsTemp) then bn else exitBarNum; def isExitBar = exitBarNum != exitBarNum; def exitBarPL = if isExitBar then FPL else exitBarPL; def isWin = if isExitBar and exitBarPL > entryBarPL then 1 else 0; def isLoss = if isExitBar and exitBarPL < entryBarPL then 1 else 0; def entryReturn = if isExitBar then exitBarPL - exitBarPL else 0; def entryReturnWin = if entryReturn > 0 then entryReturn else 0; def entryReturnLoss = if entryReturn < 0 then entryReturn else 0; def entryFPLWins = if isWin then FPL else entryFPLWins; def entryFPLLosses = if isLoss then FPL else entryFPLLosses; def entryFPLAll = if isLoss or isWin then FPL else entryFPLAll; def winCount = CompoundValue(1, if isWin then winCount + 1 else winCount, 0); def lossCount = CompoundValue(1, if isLoss then lossCount + 1 else lossCount, 0); def winRate = winCount / lossCount; def winLossRatio = winCount / entryCount; #Breaks all the win/loss calculations and returns 0 def highestReturn = HighestAll(entryReturnWin); def lowestReturn = LowestAll(entryReturnLoss); def avgReturn = TotalSum(entryReturn)/entryCount; def avgWin = TotalSum(entryReturnWin)/winCount; def avgLoss = TotalSum(entryReturnLoss)/lossCount;