Ok, I've done some more fiddling and now would like to store the maximum profit/loss percentage. Here's my code. I calculate a profit/loss percentage (PLPercent) and then use the variable MPL to save whatever the maximum profit/loss is.
My problem is it's not always saving the highest PLPercent value. Sometimes it does, but other times it drops back down to the current PLPercent. Is that because the previous bar has a range, and my code may be picking up something different than what I expect?
The last line accounts for long or short positions.
Code:
def PLPercent = if qty>0 then
(qty * close - qty * GetAveragePrice()) / (qty * GetAveragePrice()) else
(qty * GetAveragePrice() - qty * close) / (qty * GetAveragePrice());
AddLabel(qty<>0, "P/L: " + AsPercent(PLPercent),
if PLPercent==0 then Color.LIGHT_GRAY else if PLPercent>0 then Color.GREEN else Color.RED);
def MPL = if GetQuantity()==0 then 0 else if GetQuantity()[1]==0 then PLPercent else (if GetQuantity()>0 then Max(PLPercent, MPL[1]) else Min(PLPercent, MPL[1]));