Extended Floating Profit & Loss - Backtesting data utility

M

Max911

New member
@eddielee39 Where's the script error that doesn't count trades when 'entry bar' is the next bar to the last 'exit bar'?
Also I don't know, if that script bug doesn't count the trade of the last 'exit bar' or the trade of new 'entry bar'.

def nan = Double.NaN;
def bn = if !IsNaN(close) and !IsNaN(close[1]) and !IsNaN(close[-1]) then BarNumber() else bn[1];
def entry = EntryPrice();
def entryPrice = if !IsNaN(entry) then entry else entryPrice[1];
def hasEntry = !IsNaN(entry);
def isNewEntry = entryPrice != entryPrice[1];
def highFPL = HighestAll(FPL);
def lowFPL = LowestAll(FPL);
def entryBarsTemp = if hasEntry then bn else nan;
def entryBarNum = if hasEntry and isNewEntry then bn else entryBarNum[1];
def isEntryBar = entryBarNum != entryBarNum[1];
def entryBarPL = if isEntryBar then FPL else entryBarPL[1];
def exitBarsTemp = if !hasEntry and bn > entryBarsTemp[1] then bn else nan;
def exitBarNum = if !hasEntry and !IsNaN(exitBarsTemp[1]) then bn else exitBarNum[1];
def isExitBar = exitBarNum != exitBarNum[1];
def exitBarPL = if isExitBar then FPL else exitBarPL[1];
def entryReturn = if isExitBar then exitBarPL - exitBarPL[1] else entryReturn[1];
def isWin = if isExitBar and entryReturn >= 0 then 1 else 0;
def isLoss = if isExitBar and entryReturn < 0 then 1 else 0;
def entryReturnWin = if isWin then entryReturn else entryReturnWin[1];
def entryReturnLoss = if isLoss then entryReturn else entryReturnLoss[1];
def entryFPLWins = if isWin then entryReturn else 0;
def entryFPLLosses = if isLoss then entryReturn else 0;
def entryFPLAll = if isLoss or isWin then entryReturn else 0;
 
M

Max911

New member
Be careful with the original script, since it doesn't count the entries when they occur in the bar following the last exit.
Anyone have any idea where the error might be? I can't find it.

def nan = Double.NaN;
def bn = if !IsNaN(close) and !IsNaN(close[1]) and !IsNaN(close[-1]) then BarNumber() else bn[1];
def entry = EntryPrice();

def entryPrice = if !IsNaN(entry) then entry else entryPrice[1];
def hasEntry = !IsNaN(entry);
def isNewEntry = entryPrice != entryPrice[1];

def entryBarsTemp = if hasEntry then bn else nan;
def entryBarNum = if hasEntry and isNewEntry then bn else entryBarNum[1];
def isEntryBar = entryBarNum != entryBarNum[1];

def exitBarsTemp = if !hasEntry and bn > entryBarsTemp[1] then bn else nan;
def exitBarNum = if !hasEntry and !IsNaN(exitBarsTemp[1]) then bn else exitBarNum[1];
def isExitBar = exitBarNum != exitBarNum[1];
 
Z

Zara2

New member
OP's script is very useful, thank you very much, and as I started using it, I've also noticed discrepancies in the counting of trades.

I can confirm this script is not counting the exits if the next entry occurs on the bar following the exit bar, but this only results in wrong number of trades, not their P/L.

See image1:



There are 6 trades in total, but the script only counts 4 trades, missing the 2 back-to-back losing trades around the cross-hair. However, their P/L are included in the next trade's FPL, which is a winner, but is now a smaller winner after the two losses are included. As a result, the final FPL is always correct, which I made sure by running the strategy with and without the floating P/L script.

When the debug mode is turned on, you can see it more clearly. See image2:



The exits for trades at Bar 22 and Bar 24 are not plotted, and we only get the exit plotted at Bar 31.

Even though the exits are not counted, they, and the resulting P/L, ARE still tracked by the script. But I don't know how the codes should be corrected.

During testing, I've also noticed another issue of this script regarding Peak and Trough and therefore the calculation of max drawdowns. They seem off a lot of the times. The above image also illustrates how it's happening. The cross-hair marked the peak P/L at 261.8 (not plotted by the script) before the Trough plotted by the "FPL: -2870.5765" bubble. The bubble states that Peak is 0 and the Drawdown is 0%. I think the drawdown should be from 261.8 to -2870.5765

Another example image3:



Here the peak of 251.1 is plotted by the yellow bubble, but the trough of -990 at the cross-hair is not plotted, resulting in the floating labels not showing PeaktoVally DD (=NaN).

I may be misunderstanding the definition of PeaktoVally, though, so please let me know if the script is in fact accurate.

Edit: Is it possible to add counters for: Max consecutive winning trades, Max consecutive losing trades, and Avg. bars in winning/losing trades?
 
Last edited:
M

Max911

New member
I can confirm this script is not counting the exits if the next entry occurs on the bar following the exit bar, but this only results in wrong number of trades, not their P/L.
Ok, it doesn't affect the P/L but it does affect the rest of the ratios, since by miscounting the trades, the WinRate, WinLoss and AvgReturn ratios are wrong. As it's written, this script is useless ... We've to fix that error when counting trades, how? I don't know...
 
Z

Zara2

New member
I'm able to make the script recognize more exits by changing this line:
Code:
def exitBarNum = if !hasEntry and !IsNaN(exitBarsTemp[1]) then bn else exitBarNum[1];

To:
Code:
def exitBarNum = if !hasEntry and !IsNaN(exitBarsTemp) then bn else exitBarNum[1];

However, it's still not counting the exits if there's an exit and entry on the SAME bar, such as stop and reverse type of strategies. Further tinkering around the entry and exit variables (such as deleting the "!hasEntry" condition) did not result in any more improvement.
 
M

Max911

New member
I'm able to make the script recognize more exits by changing this line:
Code:
def exitBarNum = if !hasEntry and !IsNaN(exitBarsTemp[1]) then bn else exitBarNum[1];

To:
Code:
def exitBarNum = if !hasEntry and !IsNaN(exitBarsTemp) then bn else exitBarNum[1];
Great! That works! Thank you!
 
M

Max911

New member
hi, i am getting error, please can you share the new script with the changes? thank you very much
Just one change only:
Old Code: def exitBarNum = if !hasEntry and !IsNaN(exitBarsTemp[1]) then bn else exitBarNum[1];
To New Code: def exitBarNum = if !hasEntry and !IsNaN(exitBarsTemp) then bn else exitBarNum[1];
 
F

FOTM_8888

Member
VIP
Just one change only:
Old Code: def exitBarNum = if !hasEntry and !IsNaN(exitBarsTemp[1]) then bn else exitBarNum[1];
To New Code: def exitBarNum = if !hasEntry and !IsNaN(exitBarsTemp) then bn else exitBarNum[1];
got it! thank u!
 
StockT8er

StockT8er

Member
VIP
@eddielee394 I have an indicator/strategy that needs something to decrease repainting, I think portions of this could resolve the issue.
 
Last edited by a moderator:
L

Learnbot

Member
VIP
Hello guys/gals,

I have a question, below is my simple strategy that I am trying to back test, I don’t have any question regarding the “BUY” portion of this script however, I do have some questions regarding “EXIT” part.

So what I wanted to do is have either or option, meaning if my stop loss is triggered then exit else exit based on MACD avg crossing above value line. Any ideas as to how I am able to do that?

The added image is how I was thinking about putting my stop loss, basically, stop out when price crosses below the low of the candle prior to the “entry” candle.



Code:
def BUY = MACD()."Value" is less than or equal to MACD()."ZeroLine" and MACD()."Value" crosses above MACD()."Avg";

def exit = MACD()."Avg" crosses above MACD()."Value";

input tradesize = 10;

input percent= 1;

addOrder(OrderType.BUY_TO_OPEN, buy, close[0], tradeSize, Color.CYAN, Color.CYAN);

addOrder(OrderType.SELL_TO_CLOSE, exit, close [1], tradeSize, Color.MAGENTA, Color.MAGENTA);

Thank you for your time, and help as always
 
BenTen

BenTen

Administrative
Staff
VIP
@Learnbot You would need to create another sell condition for your script as a stoploss. This way, the strategy will trigger a sell if the stoploss condition appears before the original MACD sell condition. I hope that helps.
 
L

Learnbot

Member
VIP
@Learnbot You would need to create another sell condition for your script as a stoploss. This way, the strategy will trigger a sell if the stoploss condition appears before the original MACD sell condition. I hope that helps.
Thanks ben for the input, i was thinking along the same line as you suggested however, I got stuck because I am not able to figure out how to script so stop loss would be low of the bar prior to the entry signal.
 
Shooters_Gotta_Shoot

Shooters_Gotta_Shoot

New member
Hey guys,

I wondering if someone has a simple script plug-in that will calculate the win rate, profit/loss, largest winners, largest losers, etc. of a strategy. The FloatingPL indicator is good for how much money you could potentially make, but just isn't enough info for me. Just looking for some extra statistics to further confirm or tweak my strategy. Appreciate any help!
 

Similar threads

Top