Questions from Beginners MQL5 MT5 MetaTrader 5 - page 485

 
-Aleks-:

Please help me to understand!

I need to find the maximum drawdown on each (let it be) day (writing to the file happens once a day) - MT4 terminal.

The max drawdown is the distance on the chart from the peak to the current drawdown of funds and the drawdown of funds is the current loss.

I wrote such code

   if(Analiz_Prosadki==true)
     {
      if(ContolSavaTXT==1)
        {
         ProfitNew=0;
         ProfitMin=0;
         ContolSavaTXT=0;
        }

      if(ContolSavaTXT==0)
        {
         ProfitNew=AccountInfoDouble(ACCOUNT_PROFIT);
         BalansNew==AccountInfoDouble(ACCOUNT_BALANCE);  //Текузее значение баланса
         if (BalansNew>BalansMax) BalansMax=BalansNew;
         if (ProfitNew<ProfitMin && BalansNew>=BalansMax) ProfitMin=ProfitNew;
         if (ProfitNew<ProfitMin && BalansNew<BalansMax) ProfitMin=ProfitNew-(BalansMax-BalansNew);
        }

      ContolSavaTXT=Printer.Write((string)TimeCurrent(),ProfitMin);   // Пишем информацию в файл - функция возвращает 1
     }

But it is correct at some charts and abnormal at others.

Apparently I've made a mistake in the code or logic, but I can't figure out which one.

As far as I know, we should count from the Equity peak to the Equity pit, not from the balance, which makes drawdowns look so much better!

And please use the SRC button, not some other button, so that your code is not repeated in the reply, cluttering up the site!

 
Boris:

As far as I know, you have to count from the Equity peak to the Equity pit, not from the balance, which really softens the drawdown picture!

And please use the SRC button, not some other button, so that your code is not repeated in the reply, cluttering up the site!

Changed balance to funds - still doesn't add up with tester - value in tester is greater.ACCOUNT_PROFIT is actually a delta between balance and funds, maybe that's the point...On the other hand, the task is to find out how much money the EA needs to work, and it makes no sense to take into account the maximum amount of funds, it seems to me...

I don't know what SRC is and that's why I don't use it.

 
-Aleks-:

ACCOUNT_PROFIT is actually a delta between balance and funds, maybe that's it... On the other hand, the task is to find out how much money is needed for the EA to work, and it makes no sense to take into account the maximum amount of funds, it seems to me...

I don't know what SRC is and that's why I don't use it.

That button to the left of the video camera (see above) is for code insertion!

About Equity! If you replaced balance with Equity, you have to excludeACCOUNT_PROFIT, because Equity = balance + profit. However, in the terminal we always see Equity changing and the tester reports fix Equity and drawdowns at positions closing, therefore interim drawdowns are not fixed by the tester. As a result the market situation is more attractive in the tester than in reality. Recently I've been printing in the tester, in demo and real all information I need for each action of an EA, opening, modification, closing, etc., in order to avoid bullshit, which I can neither tolerate from myself or from others! But you probably already noticed that. ;)

 

Good evening, could you tell me, please, about CTrailingFixedPips class from the standard library, what is the meaning of "trailing" TakeProfit level? It will never be reached, will it? Either I don't understand something or the code or the meaning.

From the documentation:

"If the price indentation is higher than the Stop Loss level, a new Stop Loss price of the position is suggested.If the Stop Loss modification condition is met and the Take Profit level is not zero, it is suggested to set a new Take Profit price for the position. "

<Expert\Trailing\CTrailingFixedPips.mqh>

//+------------------------------------------------------------------+
//| Checking trailing stop and/or profit for long position.          |
//+------------------------------------------------------------------+
bool CTrailingFixedPips::CheckTrailingStopLong(CPositionInfo *position,double &sl,double &tp)
  {
...
...   
   delta=m_stop_level*m_adjusted_point;
   if(price-base>delta)
     {
      sl=price-delta;
      if(m_profit_level!=0) tp=price+m_profit_level*m_adjusted_point;
     }
...
  }
 
Hexen:

Good evening, could you tell me, please, about CTrailingFixedPips class from the standard library, what is the meaning of "trailing" TakeProfit level? It will never be reached, will it? Either I don't understand something or the code or the meaning.

From the documentation:

"If the price indentation is higher than the Stop Loss level, a new Stop Loss price of the position is suggested.If the Stop Lossmodification condition is met and the Take Profit level is not zero,it is suggested to set a new Take Profit price for the position. "

<Expert\Trailing\CTrailingFixedPips.mqh>

This is most likely a misprint. It should be Stop Loss.
 
Hexen:

Good evening, could you tell me, please, about CTrailingFixedPips class from the standard library, what is the meaning of "trailing" TakeProfit level? It will never be reached, will it? Either I don't understand something or the code or the meaning.

From the documentation:

"If the price indentation is higher than the Stop Loss level, a new Stop Loss price of the position is suggested.If the Stop Loss modification condition is met and the Take Profit level is not zero, it is suggested to set a new Take Profit price for the position. "

<Expert\Trailing\CTrailingFixedPips.mqh>

Judging by the above code, TP moves synchronously with SL. Screw such a trailing stop)
 
Boris:

This button to the left of the video camera (see above) is for inserting code!

There's a "Code" style for code - it's selected from the menu - that's what I use.

Boris:

About Equity! If you replace balance with Equity, you must excludeACCOUNT_PROFIT, because Equity = balance + profit. However, in the terminal we always see Equity changing and the tester reports fix Equity and drawdowns at positions close, therefore interim drawdowns are not fixed by the tester. As a result the market situation is more attractive in the tester than in reality. Recently I've been printing in the tester, in demo and real all information I need for each action of an EA, opening, modification, closing, etc., in order to avoid bullshit, which I can neither tolerate from myself or from others! But you probably already noticed that. ;)

If drawdown fixing in the tester for the report was only at position closing, there would be no drawdown when using one order in the market, which is not the case ;)

ACCOUNT_PROFIT = Funds-balance, so I see no reason not to use this indicator in calculations...

 
-Aleks-:

Please help me to understand!

I need to find the maximum drawdown on each (let it be) day (writing to the file happens once a day) - MT4 terminal.

The max drawdown is the distance on the chart from the peak to the current drawdown of funds and the drawdown of funds is the current loss.

I wrote the following code

   if(Analiz_Prosadki==true)
     {
      if(ContolSavaTXT==1)
        {
         ProfitNew=0;
         ProfitMin=0;
         ContolSavaTXT=0;
        }

      if(ContolSavaTXT==0)
        {
         ProfitNew=AccountInfoDouble(ACCOUNT_PROFIT);
         BalansNew== AccountInfoDouble(ACCOUNT_BALANCE);  //Текузее значение баланса
         if (BalansNew>BalansMax) BalansMax=BalansNew;
         if (ProfitNew<ProfitMin && BalansNew>=BalansMax) ProfitMin=ProfitNew;
         if (ProfitNew<ProfitMin && BalansNew<BalansMax) ProfitMin=ProfitNew-(BalansMax-BalansNew);
        }

      ContolSavaTXT=Printer.Write((string)TimeCurrent(),ProfitMin);   // Пишем информацию в файл - функция возвращает 1
     }

But it shows correctly in some charts and not in others. At the same time, the charts are visually the same.

I must have made a mistake in the code or logic, but I can't figure out which one.

I found a mistake in the code - an extra equal sign should be

 BalansNew=AccountInfoDouble(ACCOUNT_BALANCE); 

However, even that did not bring me much closer to solving the cause of the difference between the potic calculation and the result in the tester.

I checked Boris' theory with equity calculations and the result was negative as well.

   if(Analiz_Prosadki==true)

     {

      if(ContolSavaTXT==1)

        {

         ProfitNew=0;

         ContolSavaTXT=0;

        }


      if(ContolSavaTXT==0)

        {

         BalansNew=AccountInfoDouble(ACCOUNT_EQUITY);  //Текущее значение баланса

         if (BalansNew>BalansMax) BalansMax=BalansNew;

         if (BalansNew<BalansMax) ProfitNew=BalansNew-BalansMax;

         if (ProfitNew<ProfitMin) ProfitMin=ProfitNew;

        }


      ContolSavaTXT=Printer.Write((string)TimeCurrent(),ProfitNew);   // Пишем информацию в файл - функция возвращает 1

     }

By the way, it is not clear how equity and balance are out of sync on the chart in the tester when only one lot is traded.

 
-Aleks-:

I found a mistake in the code - an extra equal sign should be so

 BalansNew=AccountInfoDouble(ACCOUNT_BALANCE); 

However, even this did not bring me closer to solving the reason of the difference between the calculated equities and the result in the tester.

I checked Boris' theory with equity calculations and the result was negative as well.

   if(Analiz_Prosadki==true)

     {

      if(ContolSavaTXT==1)

        {

         ProfitNew=0;

         ContolSavaTXT=0;

        }


      if(ContolSavaTXT==0)

        {

         BalansNew=AccountInfoDouble(ACCOUNT_EQUITY);  //Текущее значение баланса

         if (BalansNew>BalansMax) BalansMax=BalansNew;

         if (BalansNew<BalansMax) ProfitNew=BalansNew-BalansMax;

         if (ProfitNew<ProfitMin) ProfitMin=ProfitNew;

        }


      ContolSavaTXT=Printer.Write((string)TimeCurrent(),ProfitNew);   // Пишем информацию в файл - функция возвращает 1

     }

By the way, it is not clear how equity and balance are de-synchronized in the tester on the chart if only one lot is traded.

To understand the "how the equity and balance are out of sync", we need to understand what the balance is and what the equity is.

Balance - the amount of equity in the account.

Equity - the current, variable amount of money on the account.

The same function is present in your tester, it fixes the drawdown, but not in the way you want it to.

To sum up, in order to calculate the maximal drawdown, you need to declare static or global variable for storing the value, and then rewrite this variable, just like in your code.

This is what you have written now...

if (BalansNew>BalansMax) BalansMax=BalansNew; // Если новое значение эквити больше зафиксированного в прошлый раз - перезапишем значение переменной

// но это не просадка, это максимальная прибыль

if (BalansNew<BalansMax) ProfitNew=BalansNew-BalansMax;  // А здесь BalansNew уже равно BalansMax и эта строка не выполняется никогда...

You have to make two variables max and min and write values in them.

And here is a thought out loud: You'd better write in the file not for the day, but when closing the order and rewrite the variables. At that moment equity is equal to balance and we should zeroise the variable where max and min are written.

 
Alexey Viktorov:

To understand "how equity and balance are out of sync" you need to understand what a balance is and what equity is.

Balance is the fixed amount of funds in the account.

Equity - the current, variable amount of money on the account.

The same function is present in the tester that you are writing now and it fixes the drawdown, but not in the way you want it to.

To sum up, in order to calculate the maximal drawdown, you need to declare static or global variable for storing the value, and then rewrite this variable, just like in your code.

That's what you have written now...

if (BalansNew>BalansMax) BalansMax=BalansNew; // Если новое значение эквити больше зафиксированного в прошлый раз - перезапишем значение переменной

// но это не просадка, это максимальная прибыль

if (BalansNew<BalansMax) ProfitNew=BalansNew-BalansMax;  // А здесь BalansNew уже равно BalansMax и эта строка не выполняется никогда...

You have to make two variables max and min and write values in them.

And here is a thought out loud: You'd better write in the file not for the day, but when closing the order and rewrite the variables. At that moment equity is equal to balance and we should zeroise the variable where max and min are written.

Thank you for responding to the request for help!

The global variable is relevant to real market operations - I need test information - that's why I didn't bother with it.

What is equity and balance - of course I know, but I failed to calculate the drawdown. My code samples show that I tried to take both balance and funds as a maximum and balance and funds as a minimum.

Why do you think that the inequalityif (BalansNew<BalansMax) ProfitNew=BalansNew-BalansMax; is never satisfied? It is only not fulfilled on the bar when the new balance maximum is reached (or equity - still not true), but at that point I fix the profit drawdownProfitMin=ProfitNew.

Recording the file for the day is more relevant as the maximum drawdown is usually not reached at the moment of closing an order and the goal is to calculate the average amount of money necessary for the work of the Expert Advisor.

Reason: