As an Amazon Associate I earn from qualifying purchases.
Post Views: 17,180
4 Min Read
In the last post, we looked at how you can dynamically load strategies into your trader. Today, I will show you how to dynamically calculate the lot size your algorithmic trading bot. The position size of a trade will depend on a percentage of risk, you are willing to take on your account balance. E.g. if your risk percentage is 1% and you have a $5000 account then the maximum amount of money you can lose in one trade is $50.
Contents hide
1Calculate the value of one pip
2Dynamically calculate the lot size for your algorithmic trading bot
3Adding new properties to the strategy json file
4Testing the code
Calculate the value of one pip
To start, you will need to install a new library called forex-python
. You can install this using the command below or by going here. Forex Python is a Free Foreign exchange rates and currency conversion which we will be using to calculate the value of a pip.
pip install forex-python
After this has been installed, go to your constants.py
file and import this new library as follows:
import talib as tafrom forex_python.converter import CurrencyRates
Next, let’s add a method to our constants.py
file to calculate the pip value. We need to calculate the size of one pip for a given instrument. You can call this new method get_pip_value
and it should take arguments of symbol
and account_currency
:
def get_pip_value(symbol, account_currency):
Now we need to split up our symbol into 2 currencies. I.e if we passed “EURUSD” as a symbol then we need to split it up as “EUR” and “USD”:
def get_pip_value(symbol, account_currency):symbol_1 = symbol[0:3]symbol_2 = symbol[3:6]
Finally, using the CurrencyRates module lets calculate the value of one pip and convert it to our local currency. More information on how the convert
method works can be found here:
def get_pip_value(symbol, account_currency):symbol_1 = symbol[0:3]symbol_2 = symbol[3:6]c = CurrencyRates()return c.convert(symbol_2, account_currency, c.convert(symbol_1, symbol_2, 1))
Dynamically calculate the lot size for your algorithmic trading bot
Let’s have a look at your trader.py
file again. You will be adding a new method called calc_position_size
which takes the following arguments: symbol, strategy
.
def calc_position_size(symbol, strategy):
To calculate a percentage of your balance you are willing to risk you need to actually know the current balance of your account. Retrieve this by using the acount_info
method from the MT5 API. More information on this can be found here. Call this method and assign it to a variable named account
. Also, add a print statement to say that you are calculating the position size for a given symbol.
def calc_position_size(symbol, strategy): print("Calculating position size for: ", symbol) account = mt5.account_info()
With this done, create a new variable named balance
and get the balance from account
:
balance = float(account.balance)
Now, we need to calculate the pip value of the symbol. This is done by calling the method created earlier in constants.py
. Remember, you will need to pass in the symbol and account currency for this method. Assign this to a new variable called pip_value
:
pip_value = constants.getPipValue(symbol, strategy['account_currency'])
Don’t worry about referencing account_currency
in your strategy. You will be adding this entry to your strategy json file later. You will now need to calculate the lot size based of your account balance. To calculate the lot size use the following equation:
Lot size = (balance * risk) / pip value * stop loss
Your python code should look like the following:
lot_size = (float(balance) * (float(strategy["risk"])/100)) / (pip_value * strategy["stopLoss"])
Finally, round the value to 2 decimal places (as MT5 only accepts lot sizes rounded to 2 decimal places) and return:
lot_size = round(lot_size, 2) return lot_size
Let’s go back to the check_trades
methods created earlier. In that method, find where you are opening a position (open_position
…) and above this method call add the following line:
lot_size = calc_position_size(pair, strategy)
In your open_position
call, replace the current lot size with the one from the variable created above:
open_position(pair, "BUY", lot_size, float (strategy['takeProfit']), float(strategy['stopLoss']))
Adding new properties to the strategy json file
You will remember that we references 2 new properties from the strategy in this post called account_currency
and risk
. These properties do not exist in our strategy yet. Let’s fix that!
Go to your strategy.json
file and add the account_currency
property to the top of the file. In my case, I will add USD as my account currency:
{ "account_currency" : "USD", "strategy_name": "myStrategy",
After strategy_name
define your risk as a percentage value. E.g. 2 = 2%:
{ "account_currency" : "USD","strategy_name": "myStrategy","pairs": ["EURUSD","USDCAD","GBPUSD" ], "risk" : 2,
Testing the code
Now let’s test our code. For this test I will simply run the trader and wait for a trade to open. The lot size should be dynamically calculated based on my account balance and risk.
C:\Users\conor\Documents\blog files>python trader.py strategyTrading bot started with strategy: strategyConnected: Connecting to MT5 ClientRunning trader at 2021-01-29 08:30:00.939338Connected: Connecting to MT5 ClientRunning trader at 2021-01-29 08:45:00.242999Connected: Connecting to MT5 ClientRunning trader at 2021-01-29 09:00:00.561294Connected: Connecting to MT5 ClientRunning trader at 2021-01-29 09:15:00.412300Connected: Connecting to MT5 ClientRunning trader at 2021-01-29 09:30:00.343254Connected: Connecting to MT5 ClientRunning trader at 2021-01-29 09:45:00.835452Connected: Connecting to MT5 ClientCalculating position size for: EURUSDOrder successfully placed!
As you can see above, the trader ran and opened a position on EURUSD with a calculated lot size of 5.53.
If you are interested in learning more about algo trading and trading systems, I highly recommend reading this book. I have taken some of my own trading ideas and strategies from this book. It also provided me a great insight into effective back testing. Check it out here.
That’s all for now! Check back on Monday to see how you can send trading alerts to your phone via slack! As always, if you have any questions or comments please feel free to post them below. Additionally, if you run into any issues please let me know.