Position Sizing when using an ATR Trailing stop

I use a 2.5 ATR trailing stop in one of my systems and I would like to know what the correct way is to calculate the position size so that it is always 1% of my total equity. I would also like no more than 10% of my equity in any one position.

I have seen two different versions and I’m not sure if either one is what I’m after. Could someone please tell me if either one is correct

The first version is

_SECTION_BEGIN("Position Sizing");	


Stop=2.5*ATR(10);

StaticVarSet ( Name() + "stop", stop );

RiskPct=Param("Position Sizing Risk Percent",1,0.5,2,0.1);
risk = RiskPct / 100;

MaxPosSize= Param("Maxiumum % of Equity - One Position",10,1,20,1)/100;

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

    for ( bar = 0; bar < BarCount; bar++ ) //  Loop through all bars
    {

        eq = bo.equity;
        portfolioAllocation = eq * MaxPosSize; // Max 10% of equity will be used for all the signals
                
        for ( sig = bo.GetFirstSignal( bar ); sig; sig = bo.GetNextSignal( bar ) )
        {
            stop = StaticVarGet ( sig.symbol + "stop" ); // get the stop value in points
            percentageStop = stop [bar] / sig.price; // convert to percentage
            risklimit = eq * risk / percentagestop; // calculate allocation for max loss of risk value

            sig.posSize = Min ( portfolioAllocation, risklimit );
            
        }

        bo.ProcessTradeSignals( bar ); // Process trades at bar (always required)
    }

    // iterate through closed trades and add some info
    for ( trade = bo.GetFirstTrade(); trade; trade = bo.GetNextTrade() )
    {
        trade.AddCustomMetric( "Risk", Lookup( StaticVarGet( trade.Symbol + "stop" ), trade.EntryDateTime ), 4 );

    }

    bo.PostProcess(); // Do post-processing (always required)

}

_SECTION_END();

The second version is this.

RiskPerShare =  2.5 * ATR( 10 );
//ApplyStop( stopTypeLoss, stopModePoint, RiskPerShare, True );
//
// risk 1% of entire equity on single trade
PositionRisk = 1;
//
// position size calculation
PctSize =  PositionRisk * BuyPrice / RiskPerShare;
SetPositionSize( PctSize, spsPercentOfEquity );

You’ll find examples in the knowledge base: http://www.amibroker.com/kb/2014/10/12/position-sizing-based-on-risk/

2 Likes

Thanks Helixtrader. That might have been where I saw the second version. I have altered that so that the maximum position size is 10% of equity

_SECTION_BEGIN("Position Sizing ");

RiskPerShare =  2.5 * ATR( 10 );
RiskPct=Param("Risk %",1,0.5,5,0.1);
MaxPosSize= Param("Max % of Equity - One Position",10,1,20,1);

// risk % of entire equity on single trade
PositionRisk = RiskPct;
//
// position size calculation
PctSize =  PositionRisk * BuyPrice / RiskPerShare;
SetPositionSize( Min(PctSize,MaxPosSize), spsPercentOfEquity );

_SECTION_END();

Assuming that I have that correct, how would I enter a minimum position size of 3% of my equity?

MinPosSize = 3;
SetPositionSize( Min(Max(PctSize, MinPosSize),MaxPosSize), spsPercentOfEquity );

In the Max function, if PctSize is less than MinPosSize it will use MinPosSize.

2 Likes

I still seem to be getting some very small positions every now and then. I want the minimum position size to be 3% of equity and the maximum size to 10% of equity. What could be causing this?

Capture

I'm using the following position sizing.

_SECTION_BEGIN("Exploration ");

Filter = Buy;

AddTextColumn(FullName(), "Security Name", 1.0, colorDefault, colorDefault, 350);
AddColumn(Close, "Close",1.3,colorDefault,colorDefault,95);

SetTradeDelays(0,0,0,0); 
SetOption("priceboundchecking",True);
SetOption("InitialEquity",70000);
SetOption("MaxOpenPositions", 15);
SetOption("UsePrevBarEquityForPosSizing",True);
SetOption("AllowSameBarExit",False);

PositionScore = Random();							

RiskPerShare =  2.5 * ATR( 10 );
RiskPct=Param("Risk %",1,0.5,5,0.1);
MaxPosSize= Param("Max % of Equity - One Position",10,1,20,1);
MinPosSize= Param("Min % of Equity - One Position",3,1,20,1);

// risk % of entire equity on single trade
PositionRisk = RiskPct;
//
// position size calculation
PctSize =  PositionRisk * BuyPrice / RiskPerShare;
SetPositionSize( Min(Max(PctSize, MinPosSize),MaxPosSize), spsPercentOfEquity );

_SECTION_END();