Number of contracts based position size modification in CBT

Hello,
I read in the documentation that to get (and modify) position size on the CBT signal object, one should use PosSize.

float PosSize
requested position size (positive numbers mean dollar value, negative values mean percent of portfolio equity)

Also on this page:
http://www.amibroker.org/userkb/category/amibroker-features/custom-backtester/

It shows exemple on how to intercept entry signal and modify position size, based on dollars.

Instead of specifying position size by dollar value or percent value, can it be specified by number of contract?

Example code from above link:

SetCustomBacktestProc("");
if (Status("action") == actionPortfolio)
{
    bo = GetBacktesterObject();    //  Get backtester object
    bo.PreProcess();    //  Do pre-processing
    for (i = 0; i < BarCount; i++)    //  Loop through all bars
    {
        for (sig = bo.GetFirstSignal(i); sig; sig = bo.GetNextSignal(i))
        {    //  Loop through all signals at this bar
            if (sig.IsEntry() && sig.IsLong())    //  If this signal is a long entry (ie. buy)
            {
                evol = Foreign("~evol_"+sig.Symbol, "V");   //  Get stock's composite volume array
                psize = sig.PosSize;    //  Get position size specified in AFL code
                if (psize < 0)    //  If it's negative (a percentage of equity)
                    psize = (-psize/100) * bo.Equity;  //  Convert to dollar value using current equity
                scnt = psize / sig.Price;    //  Calculate number of shares for position size
                if (scnt > evol[i] / 10)    //  If number of shares is > 10% of volume EMA
                {
                    scnt = evol[i] / 10;    //  Limit number of shares to 10% of EMA value
                    psize = scnt * sig.Price;    //  Calculate new position size
                }
                if (psize < 5000)    //  If position size is less than $5,000
                    psize = 0;    //  Set to zero so buy signal will be ignored
                else
               {
                    if (psize > 50000)    //  If position size is greater than $50,000
                        psize = 50000;    //  Limit to $50,000
                }
                sig.PosSize = psize;    //  Set modified position size back into object
            }
        }    //  End of for loop over signals at this bar
        bo.ProcessTradeSignals(i);    //  Process trades at this bar
    }    //  End of for loop over bars
    bo.PostProcess();    //  Do post-processing
}

Would there be anyway to modify:

    if (psize > 50000)    //  If position size is greater than $50,000
                        psize = 50000;    //  Limit to $50,000

to specify a number of contract instead of 50000?

Can

SetPositionSize( 1, spsShares ); 

in regular AFL code have any influence?

If size value is smaller than -2000 then it encodes to shares/contracts e.g.

shares = 1; // shares/contracts
sig.PosSize = -2000 - shares;

See manual:

AFL Function Reference - SETPOSITIONSIZE

New SetPositionSize function automatically encodes new methods of expressing position size into old "positionsize" variable as follows:

  • values below -2000 encode share count,
  • values between -2000 and -1000 encode % of current position
  • values between -1000 and 0 encode % of portfolio equity
  • values above 0 encode dollar value
2 Likes