Max Position Size

How can I regulate / set the trade-size?

I want to take

//minimum of --- 
// 2% of my equity or 5% of average liquidity / turnover like below
avgLiq = 0.05 * EMA(C*V,10);

There is setting to limit trade size in analysis settings.
4

But if you want to do by code one may do via CBT.

function CBT_fxMinPosSize( bo, sig, pcnt1, pcnt2, mkt_vol, trace ) {
	/// CBT code to 
	/// @link https://forum.amibroker.com/t/max-position-size/13010/2
	/// by fxshrat@gmail.com
	local eq, shares, shares1, shares2, size;
	eq = bo.Equity;
	shares1 = eq * pcnt1 / 100 / sig.Price;
	shares2 = pcnt2*mkt_vol / 100;
	shares = int(Min(shares1, shares2));
	size = -2000 - shares;	
	if ( sig.IsEntry() && trace ) {
		_TRACEF( "mkt_vol: %g, shares1: %g, shares2: %g, shares: %g, size: %g", 
				mkt_vol, shares1, shares2, shares, size);	
	}
	return size;
}

// 2% of my equity or 5% of average liquidity / turnover
pcnt1 = 2;
pcnt2 = 5;

avgLiq = EMA(V,10);// avg. share volume
StaticVarSet( "mkt_vol" + Name(), avgLiq);

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
        for ( sig = bo.GetFirstSignal( i ); sig; sig = bo.GetNextSignal( i ) ) {        
            
            mkt_vol = StaticVarGet( "mkt_vol" + sig.Symbol ); 
            sig.PosSize = CBT_fxMinPosSize( bo, sig, pcnt1, pcnt2, mkt_vol[i], False );
        }

        bo.ProcessTradeSignals( i );
    }
    bo.PostProcess();
}

32

5 Likes