Help needed with position size based on current equity

I am doing an equity portfolio backtest on 20 symbols but I would like to size positions based on unadjusted close because in the AFL function below adjusted is what is used when data are adjusted:

SetPositionSize( 100/20, spsPercentOfEquity);

With Norgate data we can access the original close value as follows:

oc = NorgateOriginalCloseTimeSeries();

So I want to use something like:

SetPositionSize( Port_current_Equity/(20*oc), spsShares );

What I don't know is how to access current portfolio equity value. I have read knowledge base and I understand I cannot use Equity() because that's old backtester equity for one symbol and I cannot also also use ~~~equity as foreign symbol because that is populated after the backtest finishes.

So help I need is how to access current equity during a backtest. Thank you.

Like this?

SetOption("PriceBoundChecking", False);

oc = NorgateOriginalCloseTimeSeries();
Close = oc;

BuyPrice = Close;
SellPrice = Close;

SetTradeDelays(0,0,0,0);
SetPositionSize( 100/20, spsPercentOfEquity);

RestorePriceArrays();// restore original Close field

m = MA( Close, 20 );
Buy = Cross( Close, m ); 
Sell = Cross( m, Close );
Short = Cover = 0;


Thanks for taking the time to reply. I tried your code but this is what it happens:

If I include

BuyPrice = Close;
SellPrice = Close;

before restoring price arrays, the equity becomes erratic due to stock splits. I don't want to trade actual price, I only want to size based on actual price.

On the other hand, if those two lines are not added before restoring arrays nothing changes.

So I thought of this trick without changing arrays.

SetPositionSize( (100*c)/(20*oc), spsPercentOfEquity)

What do you think? It makes a difference in backtest and there are no jumps in equity but since I don't know AFL internals and I'm a rusty programmer I'm not sure it does the intended job.

You didn't explicitaly say so.

Either that or via mid level CBT if you want to do anything in addition.

SetCustomBacktestProc( "", true);
if ( Status( "action" ) == actionPortfolio ) {
    bo = GetBacktesterObject(); // Get backtester object
    bo.PreProcess(); // Do pre-processing 

    for ( i = 0; i < BarCount; i++ ) {
        eq = bo.Equity; 

        for ( sig = bo.GetFirstSignal( i ); sig; sig = bo.GetNextSignal( i ) )  {
            if ( sig.IsEntry() ) {
                oc = StaticVarGet( "OC_" + sig.Symbol );
                sig.PosSize = -2000 - 0.05*eq / oc[i];// shares 
            }
        }
        bo.ProcessTradeSignals( i ); // Process trades at bar 
    }
    bo.PostProcess(); // Do post-processing
}

oc = NorgateOriginalCloseTimeSeries();
StaticVarSet( "OC_" + Name(), oc );

SetTradeDelays(0,0,0,0);

BuyPrice = Close;
SellPrice = Close;

m = MA( Close, 20 );
Buy = Cross( Close, m ); 
Sell = Cross( m, Close );
Short = Cover = 0;
2 Likes

Thanks for the help.

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