Not getting variable with staticvarget


I'm currently trying out CBT to adjust my position sizing based on the closing price the day before my entryprice. The position size is based on the following formula: minimum of (quantity=equity*ps(%)/stop andquantity = equity * 10%/ close (prev day). For accessing the stop values, I'm using staticvar. However I'm not retrieving the corresponding values. Can someone see my mistake here? Thank you in advance.

StaticVarSet("stopx",  stopxb );

if (Status("action") == actionPortfolio) 
    bo = GetBacktesterObject();
    symstop = StaticVarGet("stopx");
    for (i = 0; i < BarCount; i++)	//  Loop through all bars
        for (sig = bo.GetFirstSignal( i ); sig; sig = bo.GetNextSignal( i ) )
			if (sig.IsEntry())
				_TRACEF(NumToStr(possize)+ "-"+NumToStr(symstop[i])+" "+NumToStr(bo.Equity));
				if(qnt*Close[i]>bo.Equity*maxcapitalalloc/100) qnt=bo.Equity*(maxcapitalalloc/100)/Close[i];
				if(qnt*Close[i]>bo.Cash) sig.IsEntry()==False;
				else sig.PosSize=-2000-qnt;
        bo.ProcessTradeSignals( i );       

To be more specific: Tracef shows that symstop[i] is always 0, so not retrieving the array.

Look at response in your previous thread

Your code is basically all incorrect.

Incorrect. E.g. it makes zero sense to put true/false check there in such way.

Using Close[i] .. being incorrect. Since it is signal loop it would rather be sig.Price.

And other things...

Take a look at CBT manual

1 Like

Your code must create a different Static var for each Name() in the backtest.

// outside of the CBT 
StaticVarSet("stopx_" + Name(),  stopxb );

/// in the CBT phase
   // retrieve it INSIDE the loops - based on the symbol processed - as needed like:
   symStop = StaticVarGet( "stopx_" + sig.Symbol );

   // or when processing trades
   symStop = StaticVarGet( "stopx_" + trade.Symbol );

This KB article may help too.


It seems I don't understand the CBT yet. I will read it further and try some different things. Thank you for pointing me in the right direction.

If you aren't ready for the complexity of the CBT, you can probably achieve your position sizing goals without a CBT. Look for forum posts related to risk-based position sizing.

1 Like

That did the trick, many thx!

@RobinG, in addition to the "must-read" reference linked above, you can find further examples of using CBT in the User KB (scroll down to the center of the page, to the section titled "AmiBroker Custom Backtester Interface").

In any case, when you post some code, you should try to post the ENTIRE code (ideally, that will execute without errors), so other users may test it and reproduce any issue without any guessing. I kindly suggest you to review the thread How to ask a good question

Mama mia!
FYI, this has been posted in post #3 already with link to KB article on how to pass from 1st phase to 2nd phase. See your own previous thread!
Do you remember?
If not then... Hint -> Open your eyes a little bit more and read post #3 a 2nd time around (slowly, which includes reading your previous threads more carefully).

And that you have to use sig object instead of trade object should be obvious (except for case you need repeating "coding nanny" to lay out every step letter by letter. But then you should stay away from CBT code even more so).
Besides nothing did trick yet because entire CBT code is incorrect.

"As aside" ... there are KB articles about position size based on risk without CBT aleady. No forum posts required.

1 Like

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