How many lots do you need to enter if you only wanted to lose a fixed amount?

There are several numbers you wanted to calculate a dynamic lot amount for each trade, these are:

- Entry Price
- Stop Loss Price
- Amount to Risk
- Value per pip (or point, tick) in account currency

## Profit/Loss Formula

To calculate how much profit (or loss) we have made in our trade we would apply the following formula:

```
Profit/Loss (if Long) = (ExitPrice - EntryPrice) * Lots * PipValue * 100,000
Profit/Loss (if Short) = (EntryPrice - ExitPrice) * Lots * PipValue * 100,000
```

Therefore, if we assume our account is in `AUD`

and we have the following numbers, we can calculate our profit or loss from the trade:

- Currency =
`EURCAD`

- Direction =
`Long`

- TickValue = 1.11288
- Entry Price = 1.44935
- Exit Price = 1.44750
- Lots = 0.45

Placing these figures into our calculator we arrive at the following **loss** on this trade:

```
Profit/Loss (Long) = (ExitPrice - EntryPrice) * Lots * PipValue * 100,000
Profit/Loss (Long) = (1.44750 - 1.44935) * 0.45 * 1.11288 * 100000
Profit/Loss (Long) = (-0.00185) * 50,079.60
Profit/Loss (Long) = -92.64726
```

So in my account I would have taken a **loss** on this trade of `92.65`

(rounded to two decimal places).

Let’s now work backwards and try to calculate the number of lots:

- Currency =
`GBPCHF`

- Direction =
`Long`

- TickValue = 1.50286
- EntryPrice = 1.25895
- Stop Loss Price (or Exit Price) = 1.25610
- Risk (or Loss) =
`-100.00`

```
Profit/Loss (Long) = (ExitPrice - EntryPrice) * Lots * PipValue * 100,000
-100.00 = (1.25610 - 1.25895) * Lots * 1.50286 * 100,000
-100.00 = (-0.00285) * Lots * 150,286
-100.00 / (-0.00285 * 150,286) = Lots
0.233472974 = Lots
```

So in our example above if we have our stop loss price, and a fixed dollar amount we are prepared to lose for this trade then the quantity of lots we can afford is **0.23** (my broker allows for mini lots).

We can therefore rearrange our formula to calculate the number of lots per trade by reconfiguring like so:

```
Lots = abs(EntryPrice - StopLossPrice) / (TickValue * 100,000)
```

Where `abs`

in the above formula is the **absolute value** (always positive) for the subtraction of the entry price, and the stop loss price.

## MetaTrader Formula

Now we have the concept, and the formula which underpins our position sizing calculation we can *translate* this into MetaStock code.

However, there are a few more things to consider, before launching directly into the code from our formula. Ponder these thoughts:

- Does my broker impose a minimum or maximum distance between my Entry Price and my Stop Loss Price for each trade?
- Does my broker impose a minimum lot size quantity?
- Does my broker impose a maximum lot size quantity?
- Do I want to impose limits on the amount of margin that is consumed in each trade?

When we consider these additional thoughts in our coding we are now modifying our resulting output to suit these restrictions and limitations.

With our above function we are now able to calculate the amount of lots per trade provided we enter the following parameters:

`entryPrice`

- enter your entry price`stopLossPrice`

- enter where your stop is going (whether or not you manually insert this into the`OrderSend`

method or not)`riskAmount`

- the amount you are willing to risk/lose per trade.`maxMarginAmount`

- the amount of margin you’re willing to use per trade.

To reference this function in our other code we would use it something like this:

```
double entryPrice = ...
double stopLossPrice = ...
double riskPerTrade = AccountEquity() * 0.02; // 2% per trade risked
double maxMarginAmt = AccountEquity() * 0.10; // 10% per trade used as margin
double lots = getLotSize(entryPrice, stopLossPrice, riskPerTrade, maxMarginAmt);
if (lots > 0) {
// continue with placing trade
}
```

## Conclusion

Hopefully with this article you’ve been better able to understand how to calculate lot size and how to implement this within your MetaTrader code. **Be mindful to test your code thoroughly before using this code on any live production accounts.**