-nan(ind) is selected over other values as walk forward target


Periodicity - 1 minutes

Walk forward setting
a. Easy mode (intraday)
b. Start/End same day e.g. 8/3/2020 - 8/3/2020
c. Step 1 day
b. Optimize target - Avg Profit/Loss


Output from optimize shows Avg Profit/Loss has range of values (one day optimize 2020/8/3)

When walk forward testing is doing one day optimization, if the target has "-nan(ind)" , it is selected over other values (blue row, 2020/8/3)

I have tried using CBT to add a custom metric which is simply Nz of "AllAvgProfitLoss" and assign it as optimize target "EPY". (code at the end). It does circumvent -nan(ind) issue, but fails when walk forward tester evaluates market close days (e.g. Sat) saying target "EPY" is unavailable.


Questions / possible solutions:

  1. Is there a way to skip closed market days during walk forward test?
  2. Instead of setting custom metric, can I manually set performance stat? If so, I can just reset "AllAvgProfitLoss" with Nz and problem solved.

CBT code

// from https://www.amibroker.com/guide/a_custommetrics.html
/* First we need to enable custom backtest procedure and
** tell AmiBroker to use current formula


/* Now custom-backtest procedure follows */

if( Status("action") == actionPortfolio )
    bo = GetBacktesterObject();

    bo.Backtest(); // run default backtest procedure

    st = bo.GetPerformanceStats(0); // get stats for all trades

    // Here we add custom metric to backtest report
    bo.AddCustomMetric( "EPY", Nz(st.GetValue("AllAvgProfitLoss")) );

Nan is NOT-A-NUMBER. It means that somewhere in your formula and/or calculations you have Indeterminate math expression such as:

  1. Zero divided by zero
  2. Various expressions (division, subtraction, multiplication) with Infinity as argument

Or some math expressions that do not have real result (they have complex result)

  1. square root from negative number
  2. logarithm from negative number
  3. tangent from 90 degrees

and similar

If for example you had only winning trades (profit>0), then average LOSS will be zero and you will get division by zero.

To prevent testing on Sundays, don't include Sundays. Use View->Filtering->Filter Weekends

Thank you @Tomasz for explanation on Nan. I will watch for that in my code.

At the moment, nan is not produced by my code, rather, it shows up as "AllAvgProfitLoss" metric. I believe it's caused by exactly what you described - having only winning trades and no losing trade.
Because there's no losing trade (losing trade = 0), division by zero occurs.

My code was an attempt to workaround such situation by applying Nz over it and use it as custom metric/target. Is it possible for walk forward test to pick a numerical result as target, instead of nan when it shows up as part of the results?

Happy New Year and thanks for the great software!

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.