Position Sizing question

I would like to backtest a strategy on a portfolio level with a watchlist of stocks. How exactly can I specify the position sizing to make sure at any bar, the entire available fund is utilized and position for all signals are taken equally with cash available?

for example,
if I have starting capital 100,000, and for simplicity lets say price of all stocks do not change and are at 10.
at bar 0, I have four signals, buy Stock1, buy Stocks2, Short stock3, short stock4
i.e at this point, I would allocate 25,000 to each of the above four positions, (qty 2500 each)

at bar 1, I have four signals, sell stock1, cover stock3, buy stock5, buy stock6
at this point, I would buy stock5 and stock6 with the proceeds from the exit of stock1 and stock3
i.e assuming no change in price of stocks, sell and cover 250 qty of stock1 and stock3 , and using the cash, buy equal amount of stock5 and stock6 ( in this case assuming no change of price from bar 0 to bar 1, that would mean 25000 worth of stock5 and stock6 each)

Positionsize here should be a function of number of entries and exits at each bar and available cash.

Is this possible without CBT?

Assuming this needs CBT, would the below work?

SetCustomBacktestProc("");

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

    for (i = 0; i < BarCount; i++)	//  Loop through all bars
    {
        numberOfEntrySignals = 0;
        cashFromExit = 0;
        for (sig = bo.GetFirstSignal( i ); sig; sig = bo.GetNextSignal( i ) )
        {
            if( sig.IsEntry()  )
            {	
                numberOfEntrySignals = numberOfEntrySignals + 1;
            }
            if( sig.IsExit() )
            {	

                bo.ExitTrade(i,sig.Symbol, sig.Price); // assuming this will update cash variable to be used in below loop
            }
                        
        }	
        
        for (sig = bo.GetFirstSignal( i ); sig; sig = bo.GetNextSignal( i ) )
        {
            if( sig.IsEntry() )
            {	
                sig.PosSize = bo.cash/(numberOfEntrysignals); 
            } 
        }	
        
        bo.ProcessTradeSignals( i );	//  Process trades at bar (always required)
    }	  
      
    bo.PostProcess();	//  Do post-processing (always required)
}

Thanks

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