Many of the trading models and market studies on this website used a fixed share or fixed contract position size. This means the same number of shares or contracts is traded for each position. For the futures market this often means trading one contract. This is done simply to exclude the effects of dynamic positioning and reinvesting profits so we can focus on the performance of the strategy rules.

Position sizing is an integral part of both system trading and discretionary trading alike. It answers the question, how many shares or contracts should I buy? More specifically, how many shares or contracts should I buy that will not overly put my trading account at risk? Too often this question is answered by nothing more than an educated guess or out of ignorance. In this article I’m going to demonstrate how to create a percent risk-based position sizing algorithm which also reinvests profits. The code created in this article will be written in EasyLanguage and will be freely available at the bottom of this article so you can add it to your strategies.

This is a very simple and well known concept to implement. This method is highly dependent upon three factors:

- Your account size
- Your hard stop placement
- The amount of risk per trade

Your account size is easy enough to determine however, an important consideration in all trading is your stop placement. For this position sizing model to work you’ll need to know what your stop value is and how much you’re going to risk per trade. What does that mean?

If you’ve been in the trading world for any amount of time now you’re likely to run across the notion that you should never risk more than a small percentage of your trading account. The idea is, if you risk only a small portion of your trading account on each trade you can easily survive a string of losing trades.

The most common number you hear in regards to risk is 2% of your account. What does that mean? Let’s say you have a $10,000 trading account and are trading E-mini futures. Well, if your goal is to risk no more than 2% of your trading account that means you’ll not risk more than $200. This was computed by taking 2% of your trading account ($10,000 x .02 ).

This simple calculation determines your maximum stop loss. As such, this also determines other factors including, if a trade is too risky to take and if your trading account is just too small to trade effectively. For example, if your particular setup places a hard stop value at a recent support level and in doing so you risk $350, you should skip the trade. Why? It’s outside of your risk tolerance. If another trading method demands a $500 stop for all trades and you only wish to risk 2% of your account, you can easily come to the conclusion that your trading account ($10,000) is under capitalized for this particular trade strategy and the best choice is not to trade it.

Of course risking 2% of your trading account is just a guideline but, you’ll find that many traders state they keep their risk per trade small. I’m guessing many are well under 5% and others will be closer to 1%.

So, back to our percent risk position sizing which can be summed up in this formula:

Units To Buy = Account Size * Percent Risk / (Buy Price – Stop Price )

Let’s make some assumptions here and state we have a $50,000 trading account and wish to risk no more than 2% of our account for each trade. Thus, our formula becomes:

Units To Buy = $1,000 / (Buy Price – Stop Price )

On our $50,000 trading account we determined we can risk $1,000 on each trade. This dollar amount is then divided by the dollar amount “at risk” per share. How is this determined? With our stop value. Let’s say for example that we wish to buy shares in the SPY which is trading at $200 per share. We buy at this level and place our stop according to our trading model. Let’s say this results at placing a stop at $190 per share. This means we are risking $10 per share (entry price minus our stop loss).

Now we can easily determine the number of shares to buy without violating our 2% risk:

Units To Buy = $1,000 / (Buy Price – Stop Price )

Units To Buy = $1,000 / $10

Units To Buy = 100

It’s that simple. So now we know how many shares to buy and maintain our risk tolerance of risking no more than 2% of our trading account. But how do we do this using EasyLanguage?

First we’ll need to determine what our trading account size is. This is simply taking our net profit from all our closed trades and adding it to our starting equity. Below is the line of code which will do this. A new variable, **vTotalEquity**, will hold the sum of our starting equity (**startEquity**) and then add our net profit from all closed trades (**NetProfit**).

`vTotalEquity = startEquity + NetProfit;`

Next, let’s determine the amount of dollars we’re going to place at risk. A new variable, **vDollarsToRisk, **is created. Notice we are using **vTotalEquity** computed above and multiplying it by our percent risk (**iRiskPercent**).

`vDollarsToRisk = iRiskPercent * vTotalEquity;`

Finally, we can compute the number of units to buy by dividing our **vDollarsToRisk, **our stop value (iStop$). The number of units to buy will be stored in the variable, **vCon**. Notice in the code below, we also only use the integer portion of this computation by enclosing the code within the **Intportion** function. We can’t buy a fraction of a share so, we must always deal with integers when determining how many units to buy.

`VCon = Intportion( vDollarsToRisk / iStop$ );`

I like to build this type of functionality into a “function” which allows me to easily reuse this code in any number of strategies. Below is an example showing the code required to compute the number of units to buy.

Inputs:

iRiskPercent( NumericSimple ),

iStop$( NumericSimple ),

istartEquity( Numericsimple );

Variables:

retVal(false),

vCon(0),

vTotalEquity(0),

vDollarsToRisk(0);

`vTotalEquity = istartEquity + NetProfit;`

vDollarsToRisk = iRiskPercent * vTotalEquity;

VCon = Intportion( vDollarsToRisk / iStop$ );

**Sanity Check**

At this point we have our function determining the number of units to buy but we can improve our function by adding a few checks to the code. We can make our position sizing function a bit smarter!

Let’s make our function check that we don’t attempt to buy more shares than we can afford to purchase. With a very small stop loss value it becomes possible we may attempt to buy more shares than we can afford! First we’ll need a new input to hold the price at which we wish to buy.

`iBuyPrice( NumericSimple );`

Below is the code to limit the number of shares we can buy based upon our account size.

If ( VCon * iBuyPrice ) > vTotalEquity then VCon = Intportion( vTotalEquity / iBuyPrice);

What the above code does is limit the number of shares to buy to your total account equity if the originally calculated number of shares to buy would require you to spend more than your total equity (**vTotalEquity**).

**Range Checking**

Maybe we also wish to ensure the number of units to purchase is within a range. For example, maybe we never wish to hold more than 10 oil contracts based upon our trading plan. Thus, we want a way to limit the number of contracts to buy even if we have more than enough dollars in our futures account.

Furthermore, let’s also say we desire to always trade a minimum of 1 contract even when our position sizing method says we should be trading zero contracts. In other words, we wish to always take a trade when our system issues a signal.

How do we do that? Simple! We introduce two inputs to our function which contains our minimum and maximum number units we wish to buy.

`iMaxUnits( NumericSimple );`

`iMinUnuts( Numericsimple );`

Now it’s just a matter of performing the following check to ensure our units are within our defined range.

If ( VCon > iMaxUnits ) then VCon = iMaxUnits;

If ( VCon < iMinUnuts ) then VCon = iMinUnuts;

Our function is complete. We can now simply call this function within our strategy and it will determine the number of units based on a percent risk position sizing algorithm. The code will also perform the following additional checks:

- We don’t buy more units that we can afford
- Optionally limit the number of units within a range

In the following example we are having our new function compute the number of shares to buy based upon a 2% risk of a $20,000 starting equity:

SharesToBuy = CE_Position_Size_Percent_Risk( .02, 350, Close, 20000, 10, 1 );

The inputs are:

- The percentage of your trading account to risk on a given trade (2%)
- Our stop loss in dollars per unit ($350 per share or contract)
- Price to buy (current close)
- The starting equity in our trading account ($20,000)
- Maximum units to buy (10)
- Minimum units to buy (1)

We now have a function which can be used in your strategies to perform percent risk based position sizing. There is room to improve this function and that would make a great future article. Until then there are two links below that explain position sizing and below those links is the source code for the function.

For more information on this topic see:

Percent Risk Function in EasyLanguage ( TradeStation ELD, text file)

Jeff is the founder of System Trader Success - a website and mission to empowering the retail trader with the proper knowledge and tools to become a profitable trader the world of quantitative/automated trading.