Why my code with StaticVars does not work?

Hi - I'm am experimenting with sending arrays and values to a Custom Backtester.

Why - with the below code - does sending "CAR_NP" as a staticvar to the CBT give zero - when it should be a closing array value?

Sending a fixed constant - as in HighestEQMDD - works OK.

Thank you

CAR_NP = C;
HighestEQMDD = 10;
		
StaticVarSet("DCACARNP", CAR_NP);
StaticVarSet("DCAEQMDD", HighestEQMDD);

// ================================== 
// CUSTOM BACKTESTER STUFF :)
// ====================================

SetCustomBacktestProc("");
Buy=Sell=Short=Cover=0;
Buy[0]=1;
SetBacktestMode( backtestRegular);
SetOption("FuturesMode" ,False);
SetOption("MinShares",1);
SetOption( "MarginRequirement", 100);
SetOption("AllowSameBarExit", True);		
SetOption("PriceBoundChecking", True);
SetOption("UsePrevBarEquityForPosSizing", True);		
SetOption("CommissionMode", 2);
SetOption("CommissionAmount", 0);


if( Status("action") == actionPortfolio )
{
    bo = GetBacktesterObject();
    bo.Backtest();
	
	cbteq = StaticVarGet("DCACARNP");
	cbteqmdd = StaticVarGet("DCAEQMDD");
	    
	bo.AddCustomMetric( "FINAL CAR_NP",  cbteq);			// ---- shows as null!!!
	bo.AddCustomMetric( "FINAL CAR_NP",  LastValue(cbteq));			// ---- shows as zero!!!!
	bo.AddCustomMetric( "EQ_MDD",   cbteqmdd);  			// ---- correctly shown as 10
}

Read here


Also doing this

bo.AddCustomMetric( "FINAL CAR_NP",  cbteq);			// ---- shows as null!!!

is incorrect. cbteq is array.
Please read here


SetBacktestMode( backtestRegular);
SetOption("FuturesMode" ,False);
SetOption("MinShares",1);
SetOption( "MarginRequirement", 100);
SetOption("AllowSameBarExit", True);		
SetOption("PriceBoundChecking", True);
SetOption("UsePrevBarEquityForPosSizing", True);		
SetOption("CommissionMode", 2);
SetOption("CommissionAmount", 0);

CAR_NP = C;
HighestEQMDD = 10;	
	
if (Status( "action" ) == actionBacktest) {	
	StaticVarSetText("CBT_Symbol", Name());
	StaticVarSet("DCACARNP"+Name(), CAR_NP);
	StaticVarSet("DCAEQMDD", HighestEQMDD);
}

Buy=Sell=Short=Cover=0;
Buy=1;

// ================================== 
// CUSTOM BACKTESTER STUFF :)
// ====================================
SetCustomBacktestProc("");
if( Status("action") == actionPortfolio )
{
    bo = GetBacktesterObject();
    bo.Backtest();
	
	nm = StaticVarGetText("CBT_Symbol");
	cbteq = StaticVarGet("DCACARNP"+nm);
	cbteqmdd = StaticVarGet("DCAEQMDD");

	bo.AddCustomMetric( "FINAL CAR_NP",  SelectedValue(cbteq));	// get last value of range via selectedvalue
	bo.AddCustomMetric( "EQ_MDD",   cbteqmdd);  			// ---- correctly shown as 10
}

22

1 Like

Thanks fxshrat :smiley:

Instead of making assumptions and invalid statements, use A QUESTION form in the post subject.

So instead of:
"StaticVars don't work"

use
"Why my code with StaticVars does not work?"

Using "StaticVars don't work" you are making invalid and insulting claim.
AmiBroker is a perfectionist piece of software. It just works.

If you use "Why my code with StaticVar does not work?" you are not insulting anyone.

And YOUR code works, precisely the way you wrote it. You are instructing AmiBroker to overwrite over and over again the same static variable each time it runs with current content of "C" array.

CAR_NP = C; // OVERWRITE ALL THE TIME THE SAME THING
StaticVarSet("DCACARNP", CAR_NP); // OVERWRITE

Your code is run:

  1. for EVERY symbol under test AND
  2. for ~~~EQUITY symbol in portfolio backtest phase. There is no "Close" price in ~~~EQUITY symbol BEFORE portfolio backtest has finished. So you got zero.

The Knowledge Base contains article that shows how to use static variables to pass data to custom backtest phase:

http://www.amibroker.com/kb/2014/11/20/how-to-show-indicator-values-in-backtest-trade-list/

5 Likes

Gosh, sorry I hadnt seen this reply before. Thank you Tomasz. Good point about the wording of the title - I shall make a note for future. It is easy to jump to the wrong conclusion and I can be quick to make a wrong judgement!

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