Risk-Based Lot Size and Account Currency Conversion - MetaTrader 4 Script - MT4/MT5 Resources
I know you won't read everything, so I'll start by saying this: This is a script! So I read the script folder
That’s it, the code below is based on the article below https://www.fxtrademaker.com/fx_calculation.htm . I've had a lot of jobs where clients asked me to add risk-based lot sizes (so that when you lose a trade on a stop loss, you only lose a specific percentage of your account, rather than being more based on a specific lot size), but I never knew the right formula. Then I found out that you have 3 cases. (The US dollar will become the main currency used)
//direct exchange rate if (symbol_currency_right==acc_currency){ // So in our example when the correct currency is the same as your account currency (XXXUSD) lot size = money_risk/(dix*sl); //Then divide the amount of money you want to risk by the number of pips times the lot size (standard, micro) times the stop loss in pips }
- The second case is " indirect rate ", which has a type $XXX. Just check the article to know how it is constructed, or analyze the code:
Else if (acc_currency==symbol_currency_left){ //GBPXXX lot_size=money_risk*Ask/(dix*sl); }
- The last and most complicated case is when the main currency (USD) is not in the symbol at all, and you have to convert it correctly via an intermediate currency, so that the type is XXXXXX . The code is a bit complex, one mistake at the beginning is that when you try to get the current price, you can't find the middle symbol in market watch, so first you have to add it
Else if (acc_currency!=symbol_currency_left&&acc_currency!=symbol_currency_right){ ///XXXXXX string symbol_2= string connection (symbol_currency_left,acc_currency); symbolSelect (symbol_2, true ); double r__2 = symbol_info double (symbol_2, SYMBOL_ASK ); if (r__2== 0 ){ Symbol_2= String concatenation (acc_currency,symbol_currency_left); symbol_select (symbol_2, true ); r__2 = symbol_info double (symbol_2, SYMBOL_ASK ); } lot_size=ask*money_risk/(dix*sl*r__2); }
This code will not work in the strategy tester unless you add the fixed quotes of the intermediate symbol as an entry.
Attachment download
📎 lot_sizing.mq4 (4.06 KB)
Source: MQL5 #28324
💡 Featured Recommendations
✍️ Latest by the author
- •
- •
- •
- •
- •
- •
📌 Popular topics
- •
- •
- •
- •
- •
- •
- •
- •
🔗 You May Be Interested In
- •
- •
- •
- •
- •
- •