how to obtain current Equity array?

#1
Dear Traderji and Amibroker friends,

for my TS I need to know Equity array step by step during backtest.

From Amibroker usersguide:

'if you call Foreign("~~~Equity", "C" ) from within the formula that is currently being backtested, you will receive previous backtest equity, not current one.
To access current equity, you need to use custom backtester interface. It has
"Equity" property in the backtester object that holds current account equity. If
you need equity as an array there are two choices, either collect values this
way:


SetOption("UseCustomBacktestProc", True );
if( Status("action") == actionPortfolio )
{
bo = GetBacktesterObject();
bo.PreProcess(); // Initialize backtester
PortEquity = Null; // will keep portfolio equity values
for(bar=0; bar < BarCount; bar++)
{
bo.ProcessTradeSignals( bar );
// store current equity value into array element
PortEquity[ i ] = bo.Equity;
}
bo.PostProcess(); // Finalize backtester


But it doesnt work: during backtest 'if function' is not read cause
Status("action") == actionPortfolio never comes true.
so I changed actionPortfolio with actionBacktest
Status("action") == actionBacktest
but I have this error: 'COM/object handle is null'

is there a way to obtain the equity array step by step while backtest is running?

thanks

cippo
 
#2
This is the reason for which I need the current Equity: I want to realize something like a martingale: ScaleIn when prize decrease, ScaleOut when prize increase.
I use cash array to follow the variation of prize, but cash and equity array are updated only to the end of backtest. So when I use cash array during backtest it has the value of the previous backtest and doesn't work properly. To solve the problem I need to update several times the cash array, running several times the backtest. Each time the previous cash array is update of 1 trade. When all the trades are updated I eventually can check the results of backtest.
Now, you can understand that all this is very tedious and, besides, it doesn't allow to use portfolio backtest.
So I ask you, kindly, to help me with this TS:

SetTradeDelays (1,1,1,1);
PositionSize = 1500;
PyramidThreshold = 10;

Equityy = Foreign("~~~EQUITY", "C" );
cash = Foreign("~~~EQUITY", "L");
Inv=Equityy-cash;

Buy= BarIndex()>200;

Sell=C< Ref(LLV(C,200),-1);

Buy=ExRem (Buy, Sell);
Sell=ExRem(Sell, buy);

InTrade = Ref(Flip(Buy, Sell),-1);

BarBuy= Ref(abs(cash-Ref(cash,-1)), -1) > 1000;

PreviousBuyPrice=ValueWhen(BarBuy, Ref(O,-2),1);

ThresholdBuy = PreviousBuyPrice*(1-PyramidThreshold/100);
ThresholdSell = PreviousBuyPrice*(1+PyramidThreshold/100);

DoScaleIn = InTrade AND Cross(ThresholdBuy,C);
DoScaleOut = InTrade AND Cross(C, ThresholdSell);

Buy = Buy + DoScaleIn *sigScaleIn + DoScaleOut *sigScaleout;
 
Last edited:

trash

Well-Known Member
#5
But it doesnt work: during backtest 'if function' is not read cause
Status("action") == actionPortfolio never comes true.
so I changed actionPortfolio with actionBacktest
Status("action") == actionBacktest
but I have this error: 'COM/object handle is null'
Your claim is just nonsense.
Of course it works.

If you would actually use _trace or _tracef then you would see that it does run the if statement indeed!!

So stop telling false claims resulting from zero knowledge on how things work.

Code:
SetOption( "UseCustomBacktestProc", True );
if( Status( "action" ) == actionPortfolio ) {
    [b]_TRACEF( "status portfolio BT: %g", Status( "action" ) );[/b]
    
    bo = GetBacktesterObject();
    bo.PreProcess(); // Initialize backtester
    PortEquity = Null; // will keep portfolio equity values

    for( bar = 0; bar < BarCount; bar++ ) {
        bo.ProcessTradeSignals( bar );
	// store current equity value into array element
        PortEquity[ bar ] = bo.Equity;        
    }

    bo.PostProcess(); // Finalize backtester    
  
}

// some generic dummy system
period = 20; // number of averaging periods 
m = MA( Close, period ); // simple moving average
Buy = Cross( Close, m ); // buy when close crosses ABOVE moving average
Sell = Cross( m, Close ); // sell when closes crosses BELOW moving average
Short = Cover = 0;
_Trace output:








Status("action") == actionBacktest
but I have this error: 'COM/object handle is null'
Because you have to run portfolio backtest for CBT code, so you have to use actionPortfolio!!


As for accessing cash you have to use AB's CBT interface!

Code:
objcash = bo.cash;
https://www.amibroker.com/guide/a_custombacktest.html
 
Last edited: