Discussion of article "Functions for Money Management in an Expert Advisor"

 

New article Functions for Money Management in an Expert Advisor is published:

The development of trading strategies primarily focuses on searching for patterns for entering and exiting the market, as well as maintaining positions. If we are able to formalize some patterns into rules for automated trading, then the trader faces the question of calculating the volume of positions, the size of the margins, as well as maintaining a safe level of mortgage funds for assuring open positions in an automated mode. In this article we will use the MQL5 language to construct simple examples of conducting these calculations.

Author: MetaQuotes

 

Oh, here is the MOST delicious in the "Reading Room" appeared, let's read what and how ...

Especially from such an author. :)

 

I would also like to see the swap calculation function. There are 5 kinds of swaps, and each has its own nuances.

Sometimes you don't get to the right place by the scientific method.

I found three variants by selection, but there is still SYMBOL_SWAP_MODE_BY_INTEREST. I can't check it because I can't find a broker with this calculation method.

Please - if there is a possibility to post in CodeBase the function of swap calculation for the specified currency and lot - we will be grateful.

 
sergeev:

I would also like to see the swap calculation function. There are 5 kinds of swaps, and each has its own nuances.

Sometimes you don't get to the right place by the scientific method.

I found three variants by selection, but there is still SYMBOL_SWAP_MODE_BY_INTEREST. I can't check it because I can't find a broker with this calculation method.

Please - if there is a possibility to post in CodeBase the function of swap calculation for the specified currency and lot - we will be grateful.

+1. Or better yet, implement all this calculation in the standard library, and we'll see how and what to do correctly...
 
I think it says in the documentation. and in mcl4 it was the same only the formulas were changed.
 
Если даже торговля на данном счете разрешена, то это еще не означает, что эксперт имеет право торговать. Чтобы проверить, разрешено ли торговать эксперту, пишем:

What are these cases, trading is allowed, but the Expert Advisor is not allowed to trade?

Does it mean the broker's prohibition or terminal settings?

By analogy with 4 : - " ...if the expert is allowed to trade and the flow for trade operations is free..."

i.e. IsTradeAllowed = IsExpertEnabled + IsTradeContextBusy ??? ?

IsTradeAllowed - Документация на MQL4
  • docs.mql4.com
IsTradeAllowed - Документация на MQL4
 

Interesting and necessary article! I tried to check the formulas in this article and this is what I got. Let's take EURGBP as an example. The account currency is in dollars. The nearest currency pair is EURUSD, i.e. mode=true. So far so good? We use the formulas from the article to determine the required margin:

   if(direction==POSITION_TYPE_BUY)
     {
      //--- reverse quote
      if(mode)
        {
         //--- count at the purchase price for the reverse quote
         calc_price=tick.ask;
         answer=lot*lot_size*calc_price;
        }
...
if(direction==POSITION_TYPE_SELL)
     {
      //--- reverse quote
      if(mode)
        {
         //--- count by selling price for the reverse quote
         calc_price=tick.bid;
         answer=lot*lot_size*calc_price;
        }

That is, when buying, according to the article, the required margin should be:

BUY: margin = lot*lot_size*tick_ask/leverage

and when selling:

SELL: margin = lot*lot_size*tick_bid/leverage

We code these formulas in a simple EA and try to sell and buy EURUSD with low funds in advance so that the metatrader will tell us what margin is needed. At the same time we output our calculated margin. We get these messages from the tester:

2010.07.06 16:34:01 Core 1 no enough money [instant sell 0.10 EURGBP at 0.82227 sl: 0.83296 tp: 0.79796]
2010.07.06 16:34:01 Core 1 PrevBalance: 10.00, PrevEquity 10.00, PrevMargin: 0.00, NewMargin: 122.91, NewFreeMargin: -112.91 - margin calculated by the tester
2010.07.06 16:34:01 Core 1 Error: not enough money. Free margin = 10, required margin = 122.899 - margin calculated by us.


2010.07.06 16:34:01 Core 1 no enough money [instant buy 0.10 EURGBP at 0.81247 sl: 0.80191 tp: 0.82988]
2010.07.06 16:34:01 Core 1 PrevBalance: 10.00, PrevEquity 10.00, PrevMargin: 0.00, NewMargin: 122.72, NewFreeMargin: -112.72 - margin calculated by the tester
2010.07.06 16:34:01 Core 1 Error: not enough money. Free margin = 10, required margin = 122.737 - margin calculated by us.

There's a difference. Now we change the formulae for calculating the required margin to

BUY: margin = lot*lot_size*tick_bid/leverage

SELL: margin = lot*lot_size*tick_ask/leverage

which contradicts the article. We get such messages from the tester:

2010.07.06 16:39:49 Core 1 no enough money [instant sell 0.10 EURGBP at 0.82227 sl: 0.83296 tp: 0.79796]
2010.07.06 16:39:49 Core 1 PrevBalance: 10.00, PrevEquity 10.00, PrevMargin: 0.00, NewMargin: 122.91, NewFreeMargin: -112.91 - margin calculated by the tester
2010.07.06 16:39:49 Core 1 Error: not enough money. Free margin = 10, required margin = 122.911 - margin calculated by us

2010.07.06 16:39:49 Core 1 no enough money [instant buy 0.10 EURGBP at 0.81247 sl: 0.80191 tp: 0.82988]
2010.07.06 16:39:49 Core 1 PrevBalance: 10.00, PrevEquity 10.00, PrevMargin: 0.00, NewMargin: 122.72, NewFreeMargin: -112.72 - margin calculated by the tester
2010.07.06 16:39:49 Core 1 Error: not enough money. Free margin = 10, required margin = 122.722 - margin calculated by us
.

Here the margin calculated by the Expert Advisor using the "wrong" method is exactly the same as the margin calculated by the tester. It turns out that either in the tester or in the article bid and ask are rearranged when calculating crosses like EURGBP. Who is right: the article or the tester?

 

The article is good and useful for me.

Both for understanding and time saving. Thank you

I just don't understand what we get with this function.

If the amount of collateral required, shouldn't the contract value be divided by the leverage?

Документация по MQL5: Стандартные константы, перечисления и структуры / Состояние окружения / Информация об инструменте
Документация по MQL5: Стандартные константы, перечисления и структуры / Состояние окружения / Информация об инструменте
  • www.mql5.com
Стандартные константы, перечисления и структуры / Состояние окружения / Информация об инструменте - Документация по MQL5
 
SHOOTER777:

The article is good and useful for me.

Both for understanding and time saving. Thank you

I just don't understand what we get with this function.

If the size of the required collateral, then shouldn't the contract value be divided by the leverage?

I didn't get it right away. It took me an hour to find out why the result was 100 times different from the actual one.
 
SHOOTER777:

If the amount of margin required, shouldn't the contract value be divided by the leverage?

The contract value is not the margin amount. The code then divides the value by the leverage to get the margin amount.
 
on the EURUSD pair on your demo... with available funds of 10 000 I can't open with lot 10... why? why? and how to calculate the maximum possible lot based on the available funds. thank you.