How to limit maximum number of trade shares?

Hello,

how can I limit a maximum number of shares which should be open in backtest?
As this option is missing in SetOption configuration I assume it must be done within custom backtest (CBT).

Can you please help mi with this piece of code?

When running a backtest with position sizing I want to have ability to limit maximum number of shares for ticker and limit it to this number.

Thanks,
Tomas

See Set Position Size.

Set Position Size with option spsShares will set number of each trade to fixed number. But this is not what I asked for.

I would like to know, how in backtest when a position sizing is used and equity is growing to limit maximal number of buy/sell shares to avoid buying 100.000 of shares, etc...

See How to ask a good question.

Use spsValue or spsPercentOfEquity to limit the number of shares bought.

spsValue (=1) - dollar value of size (as in previous versions)

spsPercentOfEquity (=2) - size expressed as percent of portfolio-level equity (size must be from ..100 (for regular accounts) or .1000 for margin accounts)

Please tell me how with above setting I can limit maximum number of shares to buy?

OK, a simple example.
Ticker ABC = price 100 USD per 1 share
Initial equity 1000 USD

Let' say initial number of shares to buy is set to 50% of total equity.
So with first trade, we buy 5 shares of ABC ticker => total value 500 = 50% of current equity.

If your equity during backtest grows, let's say it will be 100.000, then we would buy 500 shares of ABC ticker (assuming price at that point will be the same). But I would like to add a restriction to code, to limit max number of shares to let's say 250 shares!
It's a trivial question, however I am not able to find a solution to it.

Can you please help with this?

Forum search.

So modifying this one to

function CBT_fxLimitPosSize( bo_equity, sig, pcnt, min_shares, trace ) {
	/// CBT code to 
	/// @link https://forum.amibroker.com/t/max-position-size/13010/2
	/// @link https://forum.amibroker.com/t/how-to-limit-maximum-number-of-trade-shares/22053/5
	/// by fxshrat@gmail.com
	local pv, shares, shares1, size;	
	pv = bo_equity*pcnt / 100;
	shares1 = pv / sig.Price;
	shares = int(Min(shares1, min_shares));
	size = -2000 - shares;	
	if ( sig.IsEntry() && trace ) {
		_TRACEF( "shares1: %g, min_shares: %g, shares: %g, size: %g", 
				shares1, min_shares, shares, size);	
	}
	return size;
}

// 2% of equity or shares limit
pcnt = 2;// percent of equity size
min_shares = 200;// your shares limit

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

SetCustomBacktestProc( "" );
if ( Status( "action" ) == actionPortfolio ) {
    // retrieving portfolio backtester interface
    bo = GetBacktesterObject();
    bo.PreProcess();

    for ( i = 0; i < BarCount; i++ ) {        
        // iterating through all trade signals and adjust pos size
        eq = bo.Equity;
        for ( sig = bo.GetFirstSignal( i ); sig; sig = bo.GetNextSignal( i ) ) {   
            if ( sig.IsEntry() )  {
				sig.PosSize = CBT_fxLimitPosSize( eq, sig, pcnt, min_shares, false);
            }
        }
        bo.ProcessTradeSignals( i );
    }    
    bo.PostProcess();
}

// 2% of equity or shares limit
pcnt = 2;// percent of equity size
min_shares = 200;// your shares limit

14

5 Likes

Thank you for your effort and solution. I will play with it.

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