Any questions from newcomers on MQL4 and MQL5, help and discussion on algorithms and codes - page 46

 
sile:

Hello.

I am asking for help.

How do I know that the loss from the beginning of the day, for closed positions was 10%?

This Expert Advisor shows in the upper left corner the percentage of profit or loss for a set period

//+------------------------------------------------------------------+
//|                                                       test02.mq4 |
//|                                                   Sergey Gritsay |
//|                         https://www.mql5.com/ru/users/sergey1294 |
//+------------------------------------------------------------------+
#property copyright "Sergey Gritsay"
#property link      "https://www.mql5.com/ru/users/sergey1294"
#property version   "1.00"
#property strict
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
enum mode_type
  {
   BUY=OP_BUY,
   SELL=OP_SELL,
   ALL = -1
  };
input string Symbols=NULL;
input int Magic=-1;//Identification number
input mode_type ModeType=ALL;
input ENUM_TIMEFRAMES TimeFrame=PERIOD_D1;

double procent=0.0;
double prev_balans=0.0;
double Profit=0.0;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---

//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---

  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
   Profit=LastProfit(iTime(_Symbol,TimeFrame,0),Symbols,Magic,ModeType);
   prev_balans=Balans(0,iTime(_Symbol,TimeFrame,0));
   if(prev_balans!=0.0)procent=Profit/prev_balans*100.0;
   Comment(
           "\nTime = ",iTime(_Symbol,TimeFrame,0),
           "\nProfit = ",DoubleToStr(Profit,2),
           "\nprev_balans = ",DoubleToStr(prev_balans,2),
           "\nprocent = ",DoubleToStr(procent,4)," %"
           );
  }
//+------------------------------------------------------------------+
double LastProfit(
                  datetime time,      // Дата, представленная в виде количества секунд, прошедших после 00:00 1 января 1970 года.
                  string symbol=NULL, // символ, если NULL то любой символ
                  int magic=-1,       // идентификатор, если -1 то любой идентификатор
                  int cmd=-1          // торговая операция, если -1 любая торговая операция
                  )
  {
   double profit=0;
   int total=OrdersHistoryTotal();
   for(int i=total-1;i>=0;i--)
     {
      if(!OrderSelect(i,SELECT_BY_POS,MODE_HISTORY))continue;
      if(OrderSymbol()==symbol || symbol!=NULL)
        {
         if(OrderMagicNumber()==magic || magic==-1)
           {
            if(OrderType()==cmd || cmd==-1)
              {
               if(OrderCloseTime()<time)continue;
               profit+=OrderProfit()+OrderCommission()+OrderSwap();
              }
           }
        }
     }
   return(profit);
  }
//+------------------------------------------------------------------+
double Balans(
              datetime start_time,// C какой даты. Дата, представленная в виде количества секунд, прошедших после 00:00 1 января 1970 года.
              datetime stop_time,// По какую дату. Дата, представленная в виде количества секунд, прошедших после 00:00 1 января 1970 года.
              )
  {
   double profit=0;
   int total=OrdersHistoryTotal();
   for(int i=0; i<total; i++)
     {
      if(!OrderSelect(i,SELECT_BY_POS,MODE_HISTORY))continue;
      if(OrderCloseTime()<start_time || OrderCloseTime()>stop_time)continue;
      profit+=OrderProfit()+OrderCommission()+OrderSwap();
     }
   return(profit);
  }
//+------------------------------------------------------------------+

...

Files:
test02.mq4  8 kb
 
Sergey Gritsay:

This advisor shows in the upper left corner the percentage of profit or loss for a set period


Thank you.

There is a function which calculates the number of losing positions for the whole period.

What can I change to see the number of losing positions closed today in a row?



int GetLastNegativeOrdersCount()
{
  int PosCnt = 0;
  int cnt = HistoryTotal();
  for (int i = cnt-1; i >=0; i--) {
    
    if (!OrderSelect(i, SELECT_BY_POS, MODE_HISTORY)) continue;
    if (OrderSymbol() != Symbol()) continue;
    if (OrderMagicNumber() != Magic) continue;

      
    int type = OrderType();
    if (type != OP_BUY && type != OP_SELL) continue;
      
    if (OrderProfit()+OrderSwap()+OrderCommission() > 0) break;

    PosCnt++;
  }

  return (PosCnt);
}
 
sile:

Thank you.

There is a function which counts the number of losing positions over time.

What should I change to find out the number of losing positions closed today in a row?



int GetLastNegativeOrdersCount()
{
  int PosCnt = 0;
  int cnt = HistoryTotal();
  for (int i = cnt-1; i >=0; i--) {
    
    if (!OrderSelect(i, SELECT_BY_POS, MODE_HISTORY)) continue;
    if (OrderSymbol() != Symbol()) continue;
    if (OrderMagicNumber() != Magic) continue;

      
    int type = OrderType();
    if (type != OP_BUY && type != OP_SELL) continue;
      
    if (OrderProfit()+OrderSwap()+OrderCommission() > 0) break;

    PosCnt++;
  }

  return (PosCnt);
}
Add one more condition: if the order closing time is less than the current D1 candle opening time, then continue;
 
Artyom Trishkin:
Add one more condition: if the closing time of the order is less than the opening time of the current D1 candle, then continue;

Thank you.

if (iTime(Symbol(),1440,0)>OrderCloseTime()) continue;
 
sile:

Thank you.

if (iTime(Symbol(),1440,0)>OrderCloseTime()) continue;


Not quite like this.

Like this - OrderSymbol()

if(iTime(OrderSymbol(),PERIOD_D1,0)>=OrderCloseTime()) continue;

Or like this:

if(OrderCloseTime()<iTime(OrderSymbol(),PERIOD_D1,0)) continue;
 
Artyom Trishkin:

Not quite like this.

Like this - OrderSymbol()


Thank you.

 
Dear. Please advise how to add text to the EA input parameters window.
 
Mikhail Goryunov:
Dear. Please advise how to add text to the EA input parameters window.
input int    MetaQuotes = 30;
input bool   MetaQuotes = true;
input double MetaQuotes = 20.3;
input string MetaQuotes = "Corp";
 
Vitaly Muzichenko:
input int    MetaQuotes = 30;
input bool   MetaQuotes = true;
input double MetaQuotes = 20.3;
input string MetaQuotes = "Corp";

Thanks + in rep. Figured it out, I read the tutorial about externalinput variables, it's not very well described. It's clearer and more obvious from the example.

sinput string MetaQuotes = "бла бла текс";
 
Mikhail Goryunov:

Thanks + in rep. Figured it out, I read the tutorial about externalinput variables, it's not very well described. It's clearer and clearer in the example.

sinput string MetaQuotes = "бла бла текс";

If you add comments to each line, it'll be more interesting:

input    int      MetaQuotes1    =  30;         // Описание входного параметра 1
input    bool     MetaQuotes2    =  true;       // Описание входного параметра 2
input    double   MetaQuotes3    =  20.3;       // Описание входного параметра 3
input    string   MetaQuotes4    =  "Corp";     // Описание входного параметра 4

and if you make an enumeration and use it instead of a bool, it's even more interesting:

enum enumYN
  {
   enYes =  1,    // Да
   enNo  =  0,    // Нет
  };

input    int      MetaQuotes1    =  30;         // Описание входного параметра 1
input    enumYN   MetaQuotes2    =  enYes;      // Описание входного параметра 2
input    double   MetaQuotes3    =  20.3;       // Описание входного параметра 3
input    string   MetaQuotes4    =  "Corp";     // Описание входного параметра 4

and sinput allows you to exclude a variable from the list of variables for optimization. For example, the variable MetaQuotes4 in this context is not necessary for optimization, and it can be excluded:

enum enumYN
  {
   enYes =  1,    // Да
   enNo  =  0,    // Нет
  };

input    int      MetaQuotes1    =  30;         // Описание входного параметра 1
input    enumYN   MetaQuotes2    =  enYes;      // Описание входного параметра 2
input    double   MetaQuotes3    =  20.3;       // Описание входного параметра 3
sinput   string   MetaQuotes4    =  "Corp";     // Описание входного параметра 4
Reason: