Set a maximum position size using SetPositionSize( PctSize, spsPercentOfEquity )

When I use SetPositionSize( PctSize, spsPercentOfEquity ) as my equity gets larger my position sizes get very large. Is there any way to still use spsPercentOfEquity but limit maximum value of any trade to say $30,000.

for example possize = Min(30000,SetPositionSize( 5, spsPercentOfEquity ));

I have had a look in the forum but cannot find anything. If wrong could someone please point out the location. Thanks Phill

To mix percent of equity with some position value threshold one may use CBT (custom backtester).
Similar thread exists.

So,

procedure CBT_MaxPositionValue( bo, sig, pcnt, max_posvalue, trace ) {
	/// CBT solution to 
	/// @link https://tinyurl.com/vk9truc
	/// by fxshrat@gmail.com
	local eq, pv, size;
	//
	eq = bo.Equity;
	pv = eq * pcnt / 100;	
	size = Min(pv, max_posvalue);
	//
	if ( sig.IsEntry() && trace )
		_TRACEF("equity: %g, pv: %g, pos. size: %g", eq, pv, size);
	return size;
}

SetOption( "InitialEquity", 100000);

// n-percent of equity but max 30000 position value
pcnt = 15;// percent of equity
max_posvalue = 30000;// position value threshold

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

SetCustomBacktestProc( "" );
if ( Status( "action" ) == actionPortfolio ) {
    bo = GetBacktesterObject();
    bo.PreProcess();

    for ( i = 0; i < BarCount; i++ ) {        
        // iterating through all trade signals and adjust pos size
        for ( sig = bo.GetFirstSignal( i ); sig; sig = bo.GetNextSignal( i ) ) {   
            sig.PosSize = CBT_MaxPositionValue( bo, sig, pcnt, max_posvalue, true );
        }
        bo.ProcessTradeSignals( i );
    }    
    bo.PostProcess();
}

10

7 Likes