Questions from Beginners MQL5 MT5 MetaTrader 5 - page 672

 
fxsaber:
Thank you!!!
 

I am writing an EA which works on a martingale basis.

How can I get it to close all the orders it has opened when the maximum permissible loss in pips on one currency pair is reached?

 
RichLux:

I am writing an EA which works on a martingale basis.

How can I get it to close all the orders it has opened when the maximum permissible loss in pips on one currency pair is reached?


double GetProfitinCurrency(string order_symbol="",int order_type=-1,int order_magic=-1)
  {
   double profit=0;
   int    i,k=OrdersTotal();

   if(order_symbol=="0") order_symbol=Symbol();
   for(i=0; i<k; i++)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
        {
         if((OrderSymbol()==order_symbol || order_symbol=="") && (order_type<0 || OrderType()==order_type))
           {
            if(OrderType()==OP_BUY || OrderType()==OP_SELL)
              {
               if(order_magic<0 || OrderMagicNumber()==order_magic)
                 {
                  profit+=OrderProfit()+OrderCommission()+OrderSwap();
                 }
              }
           }
        }
     }
   return(profit);
  }


the function returns profits in open positions, can be filtered by symbol, type, magik

Then you close all positions

void CPD(string order_symbol="",int order_type=-1,int order_magic=-1)
  {
   int i,k=OrdersTotal();

   if(order_symbol=="0") order_symbol=Symbol();
   for(i=k-1; i>=0; i--)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
        {
         if((OrderSymbol()==order_symbol || order_symbol=="") && (order_type<0 || OrderType()==order_type))
           {
            if(OrderType()==OP_BUY || OrderType()==OP_SELL)
              {
               if(order_magic<0 || OrderMagicNumber()==order_magic) OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),1,CLR_GREEN);
              }
           }
        }
     }
  }
 
Vladislav Andruschenko:

...

function returns profits in october poses, can be filtered by symbol, type, magik

Then close all positions

Thanks, but I am interested in closing on loss. How can this be implemented?
 

I mean, I gave you the above,

So profit is a generic term.

-20 is -20

if(GetProfitinCurrency(Symbol(),-1,-1)<-20)CPD(Symbol(),-1,-1)

 
Vladislav Andruschenko:

double GetProfitinCurrency(string order_symbol="",int order_type=-1,int order_magic=-1)
  {
   double profit=0;
   int    i,k=OrdersTotal();

   if(order_symbol=="0") order_symbol=Symbol();
   for(i=0; i<k; i++)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
        {
         if((OrderSymbol()==order_symbol || order_symbol=="") && (order_type<0 || OrderType()==order_type))
           {
            if(OrderType()==OP_BUY || OrderType()==OP_SELL)
              {
               if(order_magic<0 || OrderMagicNumber()==order_magic)
                 {
                  profit+=OrderProfit()+OrderCommission()+OrderSwap();
                 }
              }
           }
        }
     }
   return(profit);
  }

Thanks now I understand.

What is still not clear in the code is:(order_type<0||OrderType()==order_type) THIS can be read as "and order to sell or buy"?

 
RichLux:

yesorder_type= OP_BUY OP_SELL -1

 

The compiler also complains aboutClosePosBySelect.

Does this function need to be written separately?

 
Comments not relevant to this topic have been moved to "Putting the current time on the graph".
 

Vladislav Andruschenko

RichLux:

The compiler also complains aboutClosePosBySelect.

Does this function need to be written separately?

void CPD(string order_symbol="",int order_type=-1,int order_magic=-1)
  {
   int i,k=OrdersTotal();

   if(order_symbol=="0") order_symbol=Symbol();
   for(i=k-1; i>=0; i--)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
        {
         if((OrderSymbol()==order_symbol || order_symbol=="") && (order_type<0 || OrderType()==order_type))
           {
            if(OrderType()==OP_BUY || OrderType()==OP_SELL)
              {
               if(order_magic<0 || OrderMagicNumber()==order_magic) OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),1,CLR_GREEN);
              }
           }
        }
     }
  }
Reason: