Alert in the indicator. How to insert Alert and Arrow into your indicator?
Market MQL5. Problem and errors with installing Expert Advisor and solutions
Description
In this article, I will explain my methods for calculating the lot size based on a percentage (Risk in %) of Free Margin across different currency pairs and various types of deposits. Autolot or Dynamic lot from balance. RiskManager. I will show you my Autolot code.
Let’s begin:
Basic Concepts:
First, let’s highlight a few concepts used in this calculation:
Free Margin ( AccountFreeMargin() ) – funds that are not used as collateral in open positions. Free Margin is calculated using the formula: Free Margin = Current Deposit Balance – Required Margin for Open Positions. This indicator determines the number of transactions that can be opened at the current moment.
Ask is the price at which you buy a certain amount of a financial instrument from the broker. The broker sells it to you at this price. When trading on the Forex market, you will encounter this type of price everywhere: from the trading terminal to stock reports and analytical articles on financial markets.
Bid is the price at which you sell a certain amount of a financial instrument to your broker. The broker buys it from you at this price. When trading on the Forex market, you will encounter this type of price wherever discussions about the international Forex market occur: from the trading terminal and exchange rates on your broker’s website to stock reports and analytical articles on international financial markets.
Leverage is the ratio of the amount of collateral to the amount borrowed. When initially selecting leverage, you choose the leverage size, which indicates the multiplier by which the Client’s deposit will be increased to carry out trading operations on the Forex market. The Client can select leverage from 1:1 to 1:500, meaning the Client can trade with only their own funds or request an increase in their deposit up to 500 times to enter the market with this amount. Leverage, as the ratio of the amount a trader enters the market with to the size of their deposit, plays a crucial role in the trading process because it allows a trader to significantly increase their capital.
A Currency Pair is a designation of two currencies that together form an exchange rate. The currency pair serves as an object for financial transactions. A currency pair is denoted by the successive designations of the two currencies that make up the pair, written either together or separated by a slash in the following form: base currency / quote currency. The base currency is the first currency that is bought and sold, and the quote currency is used to express the price of the base currency. A currency pair is the fundamental concept of the foreign exchange market, present everywhere as soon as it pertains to this market: from market reports to the trading terminal.
Lot Size – the volume of the base currency, commodity, or the number of shares specified in the contract specification. The lot size depends on the amount of currency specified in the contract and is a unit of measure for determining the size of the trading contract. A lot has a specific size, which is used to determine the amount of currency used for financial transactions in the Forex market. To execute a transaction, the Client determines the size of the transaction, expressed in the number of lots. The volume of a trade transaction indicates how much currency will comprise the transaction. A lot is the size of the contract, a fixed amount of currency used in Forex trading.
The exchange rate between two currencies is determined by the interplay of supply and demand at a specific moment in time.
This process is known as currency quoting, and quotes can be either direct or reversed.
A direct quotation reflects the price of a foreign currency in units of the national currency.
Most exchange rates are expressed as direct quotes of the US dollar against the currencies of various countries.
For example, USD/UAH—30.70 means that one US dollar is worth 30.70 Ukrainian hryvnias,
and USD/CHF—1.0850 indicates that one US dollar is equivalent to 1.0850 Swiss francs.
A reverse quotation expresses the value of the national currency in terms of a foreign currency.
Historically, currencies like the British pound, euro, Australian dollar, and New Zealand dollar have been quoted this way.
For instance, EUR/USD—1.3374 signifies that one euro is worth 1.3374 US dollars,
while GBP/USD—1.5970 means one British pound equals 1.5970 US dollars.
There is also the concept of a cross exchange rate, where the US dollar is neither the base nor the quoted currency.
Examples include EUR/JPY, GBP/CHF, and EUR/GBP.
Technical Side
- NormalizeDouble – Rounds a floating-point number to the specified precision.
- MODE_LOTSIZE – Contract size in instrument base currency.
- MODE_LOTSTEP – The step for changing the lot size.
- AccountCurrency – Returns the currency name for the current account.
- MathFloor – The function returns the largest integer less than or equal to x.
Essential: To ensure all calculations are accurate, activate all symbols in the “Market Watch” panel :
My Calculation Formula for Account Currency
My calculation formula looks like this:
USD Deposit
Deposit USD Direct Quote ()
Lot = Free Margin * Risk * Leverage / 100 / Contract Size
Deposit USD Back Quote()
Lot = Free Margin * Risk * Leverage / Ask / 100 / Contract Size
Deposit USD Cross Rate:
Option 1: If there is a current price of the base currency + USD (for example, if the settlement pair is AUDCAD, then the current price of the base currency = AUDUSD)
Lot = Free Margin * Risk * Leverage / (Current Base Currency Price + USD) / 100 / Contract Size
Option 2: If there is no current price of the base currency + USD (for example, CADJPY)
Lot = Free Margin * Risk * Leverage / 100 / Contract Size
EUR Deposit
Deposit EUR Direct Quote
Lot = Free Margin * Risk * Leverage / 100 / Contract Size
Deposit EUR Cross Rate:
Current price = EUR + quote currency (for example, if the settlement pair is AUDCAD, then the current price is quote currency = EURAUD)
Lot = Free Margin * Risk * Leverage / (current price EUR + quote currency) / 100 / Contract Size
UAH Deposit and Other
Deposit UAH Back Quote (for example, USDUAH)
Lot = MathFloor(Free Margin * Risk * Leverage / 100 / Current Price / Contract Size / Lot Step / 20) * Lot Step
Deposit UAH Cross (e.g., EURGBP)
Current price = MarketInfo(StringSubstr(Symbol(), 0, 3) + UAH + StringSubstr(Symbol(), 6), MODE_BID);
if(pr != 0) Lot2 = MathFloor(Free Margin * Risk * Leverage / Current Price / 100 / Contract Size / Lot Step) * Lot Step;
else Lot = MathFloor(Free Margin * Risk * Leverage / 100 / Contract Size / Lot Step) * Lot Step;
The order of these functions was determined by calculating the parameters on different deposits.
If you have suggestions for these calculations, please share your Autolot functions when ordering programming.
Or Freelance: Custom Programming on MetaTrader and MQL! Finish?
External Variables Autolot Calculation Function:
Lots | Fixed lot for opening a position. |
RiskLotType TypeAutolot | Autolot Calculation Type: BALANCE (Risk from Balance) – Autolot is calculated from the current balance. Lot calculation is based on the account balance. If there are open positions on the account, the next lot will be approximately the same because the balance does not decrease from the number of current open positions! FREEMARGIN (Risk from Free Margin) – The standard lot calculation from the current free margin is that the more open positions on the account, the smaller the lot calculated for the next position. This is because when opening a position, the free margin decreases, so each new open position has a smaller lot when calculating the Autolot. Type of Risk Calculation for the RiskLot Option: Available modes: RiskLotType = 0, // Risk from Free Margin RiskLotType = 1, // Risk from Balance |
DynamicLot | A dynamic lot, Autolot, for an open position. You can turn ON dynamic lot calculation as a percentage of free margin and other factors. Calculation of Our Autolot: Formula and code: https://expforex.com/my-autolot-secrets-dynamiclot-autolot/#eb-table-content-4 |
DynamicLotStopLoss | Risk from SL – Calculates the automatic lot, taking into account Stop Loss. The maximum loss as a percentage of the balance when closing a deal at Stop Loss. When calculating the lot, our Expert Advisor considers the stop loss at the moment of opening the deal. If the deal closes at the stop loss, the loss from this deal will equal a percentage of the balance at the time of opening the deal. Important! If your stop loss is changed after the deal is opened, the lot size of the deal cannot be changed. At the closing of the deal, the loss can be more or less. Important! If you use a Trailing Stop, the deal’s Stop Loss changes with each tick, but the lot size cannot be changed. Formula and code: https://expforex.com/my-autolot-secrets-dynamiclot-autolot/#eb-table-content-5 |
LotBalancePcnt RiskLot LotBalancePercent | Percentage for Autolot. The percentage for the dynamic lot (Autolot) function. If RiskLot = 0, then the deal LOT (Volume) will be calculated according to the parameter Lot. If RiskLot is not equal to 0, then a deal at the client terminal will be opened with a lot as a percentage of the client’s free margin (Autolot). |
RiskRate | The rate of your currency against the dollar (or your account currency). By default, RiskRate = 0, which means that the Expert Advisor will try to find the correct rate in the Market Review. For Autolot to work well with all currency pairs, you need to turn ON “Show All Currency Pairs” in the Market Watch. |
MaxLot | The maximum lot that an Expert Advisor can open when calculating an Autolot and Martingale for the first main position. |
Martin | Martingale. The standard multiplication of the lot of the last closed position at a loss. If Martin = 1, then the martingale does not activate (Fixed lots). If Martin = 0, then the Expert Advisor cannot open the next position. If Martin = 2, then the first lot = 0.1, the second lot = 0.2, and so on: 0.4 – 0.8 – 1.6 – 3.2 – 6.4….. If Martin = 0.5, then the first lot = 1, the second lot = 0.5, and so on: 0.25 – 0.125. |
Do you need a code?
Autolot Calculation Code Function:
input double Lots=0.01; //Lots --> Fixed lot of opened positions and orders
input bool DynamicLot=false; //DynamicLot --> The calculation lot from free margin
input bool DynamicLotStopLoss=false; //DynamicLotStopLoss --> The calculation lot from StopLoss and free margin
input double LotBalancePercent=1; //LotBalancePercent --> Percentage for the calculation of the lot
extern double RiskRate=0; //RiskRate --> Symbol rate to deposit currency for lot calculation
input double MaxLot=999; //MaxLot --> Maximum lot when opening positions
double GetSizeLot(double ll=1) // The function returns the lot size
{
string Author = "Author of function template: www.expforex.com";
string lotcalc;
double pr;
string Valdepo = AccountCurrency();
// If MM is enabled, then determine the lot size
double Lot2, MinLots, MaxLots;
int lotdig;
if (MarketInfo(Symbol(), MODE_LOTSTEP) == 0.01) lotdig = 2; else lotdig = 1;
if (Valdepo == "USD")
{
if (StringSubstr(Symbol(), 0, 3) == "USD")
Lot2 = NormalizeDouble(AccountFreeMargin() * LotBalancePcnt * AccountLeverage() / 100 / MarketInfo(Symbol(), MODE_LOTSIZE), lotdig);
else if (StringSubstr(Symbol(), 3, 3) == "USD")
Lot2 = NormalizeDouble(AccountFreeMargin() * LotBalancePcnt * AccountLeverage() / Ask / 100 / MarketInfo(Symbol(), MODE_LOTSIZE), lotdig);
else
{
pr = MarketInfo(StringSubstr(Symbol(), 0, 3) + "USD", MODE_ASK);
if (pr != 0)
Lot2 = NormalizeDouble(AccountFreeMargin() * LotBalancePcnt * AccountLeverage() / pr / 100 / MarketInfo(Symbol(), MODE_LOTSIZE), lotdig);
else
Lot2 = NormalizeDouble(AccountFreeMargin() * LotBalancePcnt * AccountLeverage() / 100 / MarketInfo(Symbol(), MODE_LOTSIZE), lotdig);
}
}
if (Valdepo == "EUR")
{
if (StringSubstr(Symbol(), 0, 3) == "EUR")
Lot2 = NormalizeDouble(AccountFreeMargin() * LotBalancePcnt * AccountLeverage() / 100 / MarketInfo(Symbol(), MODE_LOTSIZE), lotdig);
else
{
pr = MarketInfo("EUR" + StringSubstr(Symbol(), 0, 3), MODE_BID);
if (pr != 0)
Lot2 = NormalizeDouble(AccountFreeMargin() * LotBalancePcnt * AccountLeverage() * pr / 100 / MarketInfo(Symbol(), MODE_LOTSIZE), lotdig);
else
Lot2 = NormalizeDouble(AccountFreeMargin() * LotBalancePcnt * AccountLeverage() / 100 / MarketInfo(Symbol(), MODE_LOTSIZE), lotdig);
}
}
if (Valdepo == "UAH")
{
if (StringSubstr(Symbol(), 0, 3) == "USD")
Lot2 = NormalizeDouble(AccountFreeMargin() / 40 * LotBalancePcnt * AccountLeverage() / 100 / MarketInfo(Symbol(), MODE_LOTSIZE), lotdig);
else if (StringSubstr(Symbol(), 3, 3) == "USD")
Lot2 = NormalizeDouble(AccountFreeMargin() / 40 * LotBalancePcnt * AccountLeverage() / Ask / 100 / MarketInfo(Symbol(), MODE_LOTSIZE), lotdig);
else
{
pr = MarketInfo(StringSubstr(Symbol(), 0, 3) + "USD", MODE_ASK);
if (pr != 0)
Lot2 = NormalizeDouble(AccountFreeMargin() / 40 * LotBalancePcnt * AccountLeverage() / pr / 100 / MarketInfo(Symbol(), MODE_LOTSIZE), lotdig);
else
Lot2 = NormalizeDouble(AccountFreeMargin() / 40 * LotBalancePcnt * AccountLeverage() / 100 / MarketInfo(Symbol(), MODE_LOTSIZE), lotdig);
}
}
MinLots = MarketInfo(Symbol(), MODE_MINLOT);
MaxLots = MaxLot;
lotcalc = "Autolot=" + Lot2;
if (!DynamicLot) Lot2 = Lots;
if (Lot2 < MinLots) Lot2 = MinLots;
if (Lot2 > MaxLots) Lot2 = MaxLots;
lotcalc = lotcalc + " MinLots=" + MinLots + " LOT=" + NormalizeDouble(Lot2, lotdig);
Print(lotcalc);
return (NormalizeDouble(Lot2, lotdig));
}
Stoploss Autolot Calculation Function:
If you want to add stop loss Autolot functionality to your EA, you can download our code:
StopLosssd = Stop Loss in points
External Variables:
Lots = 0.1; // Fixed lot
DynamicLotStopLoss = // true/false
LotBalancePcnt (RiskLot) = 20; // % of free margin
Function:
double GetSizeLotStopLoss(double StopLosssd=1) // The function returns the lot size { string Author = "Author of function template: www.expforex.com"; string Valdepo = AccountCurrency(); // If MM is enabled, then determine the lot size double MinLots; int lotdig; if (MarketInfo(Symbol(), MODE_LOTSTEP) == 0.01) lotdig = 2; else lotdig = 1; double Free = AccountFreeMargin(); double LotVal = MarketInfo(Symbol(), MODE_TICKVALUE); // Cost of 1 pip for 1 lot double Min_Lot = MarketInfo(Symbol(), MODE_MINLOT); double Max_Lot = MarketInfo(Symbol(), MODE_MAXLOT); double Step = MarketInfo(Symbol(), MODE_LOTSTEP); double Lot = MathFloor((Free * LotBalancePcnt / 100) / (StopLosssd * LotVal) / Step) * Step; MinLots = MarketInfo(Symbol(), MODE_MINLOT); if (Lot < MinLots) Lot = MinLots; return (NormalizeDouble(Lot, lotdig)); }
Questions?
If you have any questions, please ask them. We do not sell a pig in a poke.
Each of our products can be tested before purchase.
We appreciate your interest and support for our products and strive to maintain high-quality standards.
Thank you for being with us!
Do you have a question?
9 responses to “ My Autolot Secrets. Dynamic Lot or Autolot.”
-
I want to be able to double whatever lot size that is being copied from the master onto the client.. How do I do that with settings… so no matter what every new position the client with be 2x that.. master opens a trade of .03, then it will be copied to client as .06.
-
To double the lot size being copied from the master to the client account using the settings in the Copylot tool from expforex.com, you can adjust the “ratio of the lot” setting. Here’s how to do it:
Open the Copylot Settings:
Ensure you have the Copylot software installed on both the master and client terminals. Open the settings for the Copylot on the client terminal.
Adjust the Lot Ratio:Look for the setting related to “KoefLot” This setting allows you to multiply the lot size of the trades being copied.
Set this ratio to 2.0 to double the lot size. For example, if the master account opens a trade of 0.03 lots, the client account will open a trade of 0.06 lots.
Save and Apply Settings:Save the settings after adjusting the lot ratio. Ensure that the Copylot tool is actively running and connected between the master and client terminals.
-
-
I want to be able to double whatever lot size that is being copied from the master onto the client.. How do I do that with settings… so no matter what every new position the client with be 2x that.. master opens a trade of .03, then it will be copied to client as .06.
-
I want to be able to double whatever lot size that is being copied from the master onto the client.. How do I do that with settings… so no matter what every new position the client with be 2x that.. master opens a trade of .03, then it will be copied to client as .06.
-
I want to be able to double whatever lot size that is being copied from the master onto the client.. How do I do that with settings… so no matter what every new position the client with be 2x that.. master opens a trade of .03, then it will be copied to client as .06.
-
I want to be able to double whatever lot size that is being copied from the master onto the client.. How do I do that with settings… so no matter what every new position the client with be 2x that.. master opens a trade of .03, then it will be copied to client as .06.
-
I want to be able to double whatever lot size that is being copied from the master onto the client.. How do I do that with settings… so no matter what every new position the client with be 2x that.. master opens a trade of .03, then it will be copied to client as .06.
-
I want to be able to double whatever lot size that is being copied from the master onto the client.. How do I do that with settings… so no matter what every new position the client with be 2x that.. master opens a trade of .03, then it will be copied to client as .06.
-
I want to be able to double whatever lot size that is being copied from the master onto the client.. How do I do that with settings… so no matter what every new position the client with be 2x that.. master opens a trade of .03, then it will be copied to client as .06.
This post is also available in: English Українська Portuguese Español Deutsch Chinese Русский Français Italiano Türkçe 日本語 한국어
Leave a Reply