Walk Foward using my metric as Optimization target

During Walk Foward using my metric as Optimization target , whenever there is no trade in OOS, Analysys display the following error:
walk

Is there a way to initialize MyMetric even if there is no trade ?

1 Like

@Tomasz is it possible to initialize my metric even when there is no trade so Walk foward can finish normally ?

@awilson Can you clarify the problem? What happens if you do something like this?

bo = GetBacktesterObject();
bo.Backtest();
st = bo.GetPerformanceStats(0); // get stats for all trades
allQty = st.GetValue("AllQty"); // number of trades
if (allQty < 1)
    myMetric = 0;
else
    myMetric = CalcMyMetric();
bo.AddCustomMetric("MyMetric", myMetric);

Hi Steve, thanks for answering.

I tried that but the error persists.
The problem only happens when the optimization set choosen during phase "IS" does not generate any trade in OOS phase. If I use pre defined metrics the error does not show up and OOS line appears empty and the walk foward proceeds, but if I use mymetric the error shows up and walk foward test is interrupted.

OF COURSE the following code is only to force the error

Using database Data which comes with Amibroker.
With AAPL set as current symbol
I run a walk foward which results in error after some steps

SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) Vol " +WriteVal( V, 1.0 ) +" {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 )) ));
Plot( C, "Close", ParamColor("Color", colorDefault ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() ); 

dw = DayOfWeek();
optdw = Optimize("DayOfWeek", 1, 1, 4, 1);
Buy = dw == optdw;
Sell = Ref(Buy , -1);
Short = Cover = False;


SetCustomBacktestProc(""); 
if( Status("action") == actionPortfolio ) 
{ 
    bo = GetBacktesterObject(); 
	bo.Backtest();
	
	st = bo.GetPerformanceStats(0); // get stats for all trades
	allQty = st.GetValue("AllQty"); // number of trades
	if (allQty < 1)
		myMetric = 0;
	else
		myMetric = st.GetValue("NetProfit"); ;
	bo.AddCustomMetric("MyMetric", myMetric);
}

walk2

walk3

The project if someone wants to help find a solution. Thanks!
WalFwd.apx (8.3 KB)

Anyone could reproduce the problem ?

1 Like

@Tomasz is there a workaround or not for this error ?

Generate at least one trade.

To do that I would have to look into the future, during "IS" phase. Thanks anyway for answering

Follow up to others that might face the same problem.
The error occurs for days that symbol DOES NOT HAVE ANY tick.
If you use your own metric, you need to generate trade even for days that there is no tick (holidays), and for that you have to invent data.

You can simply enable "Pad and align to reference symbol".

Dear @Tomasz I already did, but during some holidays even the reference symbol do not have any tick. But it is a workaround, I can create a Symbol and invent data for it for all year. Thanks for the suggestion.

Final update! I finally got it to work.

  1. Create a symbol having tick for ALL days, including Saturday and Sunday
  2. In Analysis use pad and align to that symbol
  3. Disable Weekend filtering at Intraday settings of database settings

The above solve the problem with walk foward, but do not do what I wanted.
I had to create a new symbol, without "day holes", by making it's date to be sequential
Aux2 of text file retains the real date for foward reference.

// Do an explore set periodicity to database interval, Range 1 recent bar
SetBarsRequired(sbrAll);

newSymbol = "SEQDATE";
fh = fopen( "C:\\" + newSymbol + ".txt", "w" );

if( fh )
{
    fputs( "Symbol,Date,Time,Open,High,Low,Close,Volume,Aux1,Aux2,OpenInt\n", fh );

    dt = Datetime();
    dn = DateNum();
    tn = TimeNum();
    DayChg = Day() != Ref(Day(), 1);
    
    ndt = dt[0];
	nday = dn[0];

    for( i = 0; i < BarCount - 1; i++ )
    {
		c1 = C[i];
		h1 = H[i];
		l1 = L[i];
		o1 = O[i];
		v1 = V[i];
		A1 = Aux1[i];
		//Aux2 of text file will contain the real date
		Line = newSymbol +
			   StrFormat( ",%06.0f,%06.0f,%g,%g,%g,%g,%g,%g,%g,%g\n",
						  nday % 1000000,
						  tn[ i ],
						  O1, H1, L1, C1, V1,
						  A1, dn[ i ] % 1000000, OpenInt[ i ] );
		fputs( Line, fh );
		
		if(DayChg[i]) {
			ndt = DateTimeAdd( ndt, 1, inDaily ); 
			nday = DateTimeConvert( 0, ndt );//returns DateNum 
		}
    }
    
	fclose( fh );
}
Buy = Sell = 0; // for scan
Filter = Status( "lastbarinrange" );
AddTextColumn( "Export done", "Status" );