## Stop loss using Dollar Risk approach

#### Quantitative Model

Risk management is important and necessary for long terms investment. A good risk management mechanism helps keep your account safe from unexpected events and unlucky times, and makes you distinguish from an amateur trader.

From the article Understand Stop Loss Mechanism, it already discussed:

• basic stop loss
• trailing stop loss

These stop-loss rules simply take into account market price changes. In the following, we will discuss another risk management approach. That is Dollar Risk.

### Understand Dollar Risk

It is to risk on each trade only a small percentage of your entire account. This serves to prevent your account from going straight to zero in case of a streak of losing trades.

It is beyond the scope of this guide to explain what is the optimal percentage of balance to risk per trade, which also significantly depends on your strategy and trading style. Our focus here is to show you how to calculate the position size.

### How to calculate Position Size?

For example, your account balance is US\$10,000. Assume you want to risk only 1% of your balance on each trade. In other word, you want to cap the maximum loss for each individual trade to US\$100.

US\$10,000 * 1% = US\$100

Assuming this trade is EUR/USD, a standard lot has contract size of 100,000 units and therefore every pip has a value of US\$10.

100,000*0.0001 = US\$10

Also suppose you want the stop-loss is 50 pips from the open price. This means that 50 pips are valued US\$500 for a standard lot.

US\$10*50 pips = US\$500

Since you want to only risk \$100, while maintaining your stop-loss 50 pips away, then your position size should be 0.2 standard lot.

\$100 / (\$10*50) = 0.2

To summarize, we need the following to calculate the position size:

• Percentage of risk for a single trade
• Stop loss in pips/points/ticks
• Pip/point/tick value

### Calculate Position Size in ALGOGENE

First of all, we create a simple python function to calculate the position size.

 ```1 2``` ```def CalculateLotSize(bal, risk_pct, sl, contractSize, tickValue): return round(bal*risk_pct/(sl*contractSize*tickValue),2) ```

To further implement on ALGOGENE, the following functions could be useful:

• getAccountBalance() - returns the current account balance
• getContractSpec() - returns the contract size and minimum tick
 ``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27``` ```from AlgoAPI import AlgoAPIUtil, AlgoAPI_Backtest def CalculateLotSize(bal, risk_pct, sl, contractSize, tickValue): return round(bal*risk_pct/(sl*contractSize*tickValue),2) class AlgoEvent: def __init__(self): self.risk_pct = 0.01 self.stoploss = 500 #in no. of min ticks self.init_balance = 0 self.contractSize = 0 self.minTick = 0 def start(self, mEvt): self.evt = AlgoAPI_Backtest.AlgoEvtHandler(self, mEvt) # get initial balance self.init_balance = mEvt['NAV'] # get contract size and min tick instrument = mEvt['subscribeList'][0] res = self.evt.getContractSpec(instrument) self.contractSize = res["contractSize"] self.minTick = res['minTick'] self.evt.start() ```

### Demo

To illustrate the concept for position size with Dollar Risk approach, let's consider a random trading strategy as follows:

• Each trade set risk at 10% of initial account balance (that mean our account will blow up for 10 times of consecutive losses)
• Every hour we generate a random indicator for either buy, sell or do nothing
• If the indicator show a buy/sell signal, we open an order setting stop-loss to 100 tick away with the calculated the position size
 ``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58``` ```from AlgoAPI import AlgoAPIUtil, AlgoAPI_Backtest from datetime import datetime, timedelta import random def CalculateLotSize(bal, risk_pct, sl, contractSize, tickValue): return round(bal*risk_pct/(sl*contractSize*tickValue),2) class AlgoEvent: def __init__(self): self.timer = datetime(1970,1,1) self.risk_pct = 0.1 self.stoploss = 100 #in no. of min ticks self.init_balance = 0 self.contractSize = 0 self.minTick = 0 def start(self, mEvt): self.evt = AlgoAPI_Backtest.AlgoEvtHandler(self, mEvt) # get initial balance self.init_balance = mEvt['NAV'] # get contract size and min tick instrument = mEvt['subscribeList'][0] res = self.evt.getContractSpec(instrument) self.contractSize = res["contractSize"] self.minTick = res['minTick'] self.evt.start() def on_marketdatafeed(self, md, ab): if md.timestamp >= self.timer + timedelta(hours=1): self.timer = md.timestamp # random buysell signal. 1 for buy, -1 for sell, 0 for nothing buysell = random.choice([-1,0,1]) if buysell==0: return # calculate stoploss level if buysell==1: sl = md.bidPrice - self.minTick*self.stoploss else: sl = md.askPrice + self.minTick*self.stoploss # calculate lot size lot = CalculateLotSize(self.init_balance, self.risk_pct, self.stoploss, self.contractSize, self.minTick) # send market order order = AlgoAPIUtil.OrderObject( instrument = md.instrument, volume = lot, openclose = 'open', buysell = buysell, ordertype = 0, stopLossLevel = sl ) self.evt.sendOrder(order) ```

### Conclusion

Dollar risk position sizing is a risk management approach based on capital consideration. The idea is similar to gambling where each turn you try to risk at the same amount.

The implementation is relatively simple yet effective in many trading scenario.