Questions from Beginners MQL5 MT5 MetaTrader 5 - page 518

 
Sergey Gritsay:

Here is another option using the class

Test result

Thank you very much, Sergei ! If I understand correctly:
In the first case, an array of static variables is created, each for its own symbol with a fixed timeframe, i.e. no other timeframe can be used for that symbol;
In the second case, a class for each combination of symbol + timeframe.
 
MikeZv:
Thank you very much, Sergei ! If I understand correctly:
In the first case, an array of static variables is created, each for its own symbol with a fixed timeframe, i.e. no other timeframe can be used for that symbol;
In the second case, a class for each combination of a symbol+timeframe.
Yes, right, but if you think a little, you can replace the one-dimensional array with a two-dimensional one in the array variant.
 
Such a question. Has anyone anywhere seen a template code for opening, checking and closing orders, without an opening and closing condition? I will try to write a condition myself, but error upon error when I start writing a close order... Especially, I am bad at finding and checking for orders to close.
 
Vitaliy Medvedev:
Here is a question. Has anyone anywhere else seen the template code for opening, checking and closing of orders without any conditions for opening and closing? I tried to write a condition myself, but when I start to write a close order error on error... Especially, I am bad at finding and checking orders to close them.

It is not surprising that it does not work out well. After all, if there are no closing conditions, then finding and checking such orders should be in the spirit of: find this, I don't know what, and close it.

You should first define the conditions for choosing the order to be closed, and then it will be much easier to analyze it. After all, if there's some misunderstanding, we will just have to check for the compliance with the known conditions.

 
Yury Reshetov:

It is not surprising that it does not work out well. If we do not have closing conditions, then the search and check of such orders should be in the following spirit: find something and close it.

First, decide on the conditions of order selection for closing, and then it will be easier to sort it out. If there is some misunderstanding, we will just have to check whether the conditions we know for sure are correct.

This is all clear, I have defined the conditions, but the opening of orders according to the conditions (opening is still a rubbish, I can hardly manage to open them ...),

And the more so, correctclosing of orders ... What a mess!

 
Vitaliy Medvedev:

It's all clear, I've worked out the conditions, but the opening of orders according to the conditions, (opening is still a nonsense, opening, with difficulty...),

And the more so, correctclosing of orders ... What a mess!

If you know which order you have chosen to close, then close it by its ticket number.
 
Vitaliy Medvedev:

It's all clear, I've worked out the conditions, but the opening of orders according to the conditions, (opening is still a nonsense, opening, with a bit of a struggle...),

And the more so, correctclosing of orders ... What a mess!

Here's how it's torn from my heart...

int Total_orders(int type)
  {
   int n=0;
   int total=OrdersTotal();

   for(int i=0; i<total; i++)
     {
      if(!OrderSelect(i,SELECT_BY_POS))continue;
      if(OrderType()!=type)continue;
      if(OrderMagicNumber()!=Magic)continue;
      if(OrderSymbol()!=_Symbol)continue;
      n++;
     }
   return(n);
  }
//+------------------------------------------------------------------+
void OrderOpen(int type,double sl)
  {
   double price=0.0;
   double lot;

   if(error>1)return;
   if(type==WRONG_VALUE) return;

   if(Risk!=0.0)lot=LotNormalize(LotVolume(Risk,sl));
   else lot=LotNormalize(Lot);

   do
     {
      if(type==OP_BUY)
        {
         RefreshRates();
         price=NormalizeDouble(Ask,_Digits);
         if(AccountFreeMarginCheck(_Symbol,type,lot)<=0)return;
        }
      if(type==OP_SELL)
        {
         RefreshRates();
         price=NormalizeDouble(Bid,_Digits);
         if(AccountFreeMarginCheck(_Symbol,type,lot)<=0)return;
        }

      int Ticket=OrderSend(_Symbol,type,lot,price,10000,0,0,CommentOrder,Magic);
      if(Ticket>0)
        {
         error=Fun_Error(_Symbol,GetLastError());
         Alert(__FUNCTION__,": open order ",StrToType(type)," ",_Symbol," :",Ticket);
        }
      else error=Fun_Error(_Symbol,GetLastError());
     }
   while(error==1 && !IsStopped());

  }
//+------------------------------------------------------------------+
void Order_Close(int type)
  {
   double price=0.0;
   int total=OrdersTotal();
   if(error>1)return;

   for(int i=total-1; i>=0; i--)
     {
      if(!OrderSelect(i,SELECT_BY_POS))continue;
      if(OrderType()!=type)continue;
      if(OrderMagicNumber()!=Magic)continue;
      if(OrderSymbol()!=_Symbol)continue;
      do
        {
         if(OrderType()==OP_BUY)
           {
            RefreshRates();
            price=NormalizeDouble(Bid,_Digits);
           }
         if(OrderType()==OP_SELL)
           {
            RefreshRates();
            price=NormalizeDouble(Ask,_Digits);
           }
         int res=OrderClose(OrderTicket(),OrderLots(),price,10000);
         if(res)
           {
            Alert(__FUNCTION__,": close order ",StrToType(OrderType())," ",OrderSymbol()," :",OrderTicket());
            error=Fun_Error(_Symbol,GetLastError());
           }
         else error=Fun_Error(_Symbol,GetLastError());
        }
      while(error==1 && !IsStopped());
     }
  }
//+------------------------------------------------------------------+
void ModifySL(int Stop_Loss)
  {
   if(Stop_Loss<=0)return;
   double sl=0.0;
   bool res;

   int total=OrdersTotal();

   for(int i=0; i<total; i++)
     {
      if(!OrderSelect(i,SELECT_BY_POS))continue;
      if(OrderMagicNumber()!=Magic)continue;
      if(OrderSymbol()!=_Symbol)continue;
      if(OrderStopLoss()!=0)continue;
      if(OrderType()==OP_BUY)  sl=NormalizeDouble(OrderOpenPrice()-(New_Stop(Stop_Loss)*_Point),_Digits);
      if(OrderType()==OP_SELL) sl=NormalizeDouble(OrderOpenPrice()+(New_Stop(Stop_Loss)*_Point),_Digits);
      res=OrderModify(OrderTicket(),OrderOpenPrice(),sl,OrderTakeProfit(),0);
      if(!res)Print(__FUNCTION__,": Error modifying StopLoss order ",StrToType(OrderType())," № - ",GetLastError()," ",OrderSymbol()," ",OrderTicket());
     }
  }
//+------------------------------------------------------------------+
void ModifyTP(int Take_Profit)
  {
   if(Take_Profit<=0)return;
   double tp=0.0;
   bool res;

   int total=OrdersTotal();

   for(int i=0; i<total; i++)
     {
      if(!OrderSelect(i,SELECT_BY_POS))continue;
      if(OrderMagicNumber()!=Magic)continue;
      if(OrderSymbol()!=_Symbol)continue;
      if(OrderTakeProfit()!=0)continue;
      if(OrderType()==OP_BUY) tp=NormalizeDouble(OrderOpenPrice()+(New_Stop(Take_Profit)*_Point),_Digits);
      if(OrderType()==OP_SELL) tp=NormalizeDouble(OrderOpenPrice() -(New_Stop(Take_Profit)*_Point),_Digits);
      res=OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),tp,0);
      if(!res)Print(__FUNCTION__,": Error modifying TakeProfit order ",StrToType(OrderType())," № - ",GetLastError()," ",OrderSymbol()," ",OrderTicket());
     }
  }
//+------------------------------------------------------------------+
double LotVolume(double procent,double sl)
  {
   double lot=0.0;
   double tv=MarketInfo(_Symbol,MODE_TICKVALUE);
   double minlot=MarketInfo(_Symbol,MODE_MINLOT);
   double maxlot=MarketInfo(_Symbol,MODE_MAXLOT);

   if(sl!=0 && tv!=0) lot=(AccountFreeMargin()*procent)/(sl*tv*100.0);

   if(lot < minlot)lot = minlot;
   if(lot > maxlot)lot = maxlot;
   return(lot);
  }
//+------------------------------------------------------------------+
double LotNormalize(double lot)
  {
   double minlot=MarketInfo(_Symbol,MODE_MINLOT);
   double maxlot=MarketInfo(_Symbol,MODE_MAXLOT);

   if(lot < minlot)lot = minlot;
   if(lot > maxlot)lot = maxlot;

   if(minlot==0.001) return(NormalizeDouble(lot,3));
   if(minlot==0.01)  return(NormalizeDouble(lot,2));
   if(minlot==0.1)   return(NormalizeDouble(lot,1));

   return(NormalizeDouble(lot,0));
  }
//+------------------------------------------------------------------+
string StrToType(int type)
  {
   if(type==OP_BUY)return("Buy");
   if(type==OP_SELL)return("Sell");
   return(NULL);
  }
//+------------------------------------------------------------------+
int New_Stop(int Parametr)
  {
   int Min_Dist=(int)MarketInfo(_Symbol,MODE_STOPLEVEL);
   if(Parametr<Min_Dist)
     {
      Parametr=Min_Dist*2+(int)MarketInfo(_Symbol,MODE_SPREAD);
      Alert(_Symbol," Увеличина дистанция стоп-приказа.");
     }
   return(Parametr);
  }
//+------------------------------------------------------------------+
int Fun_Error(string symbol,int Error)
  {
   switch(Error)
     {
      case 0:return(0);
      case 1:Alert(symbol," ",Error,":No error returned, but the result is unknown");return(2);
      case 2:Alert(symbol," ",Error,":Common error");return(2);
      case 3:Alert(symbol," ",Error,":Invalid trade parameters");return(2);
      case 4:Alert(symbol," ",Error,":Trade server is busy");Sleep(3000);return(1);
      case 5:Alert(symbol," ",Error,":Old version of the client terminal");return(2);
      case 6:Alert(symbol," ",Error,":No connection with trade server");Sleep(3000);return(1);
      case 7:Alert(symbol," ",Error,":Not enough rights");return(2);
      case 8:Alert(symbol," ",Error,":Too frequent requests");return(2);
      case 9:Alert(symbol," ",Error,":Malfunctional trade operation");return(2);
      case 64:Alert(symbol," ",Error,":Account disabled");return(2);
      case 65:Alert(symbol," ",Error,":Invalid account");return(2);
      case 128:Alert(symbol," ",Error,":Trade timeout");return(1);
      case 129:Alert(symbol," ",Error,":Invalid price");return(2);
      case 130:Alert(symbol," ",Error,":Invalid stops");return(2);
      case 131:Alert(symbol," ",Error,":Invalid trade volume");return(2);
      case 132:Alert(symbol," ",Error,":Market is closed");Sleep(10000);return(1);
      case 133:Alert(symbol," ",Error,":Trade is disabled");return(2);
      case 134:Alert(symbol," ",Error,":Not enough money");return(2);
      case 135:Alert(symbol," ",Error,":Price changed");return(1);
      case 136:Alert(symbol," ",Error,":Off quotes");Sleep(3000);return(1);
      case 137:Alert(symbol," ",Error,":Broker is busy");Sleep(3000);return(1);
      case 138:Alert(symbol," ",Error,":Requote");return(1);
      case 139:Alert(symbol," ",Error,":Order is locked");Sleep(10000);return(1);
      case 140:Alert(symbol," ",Error,":Buy orders only allowed");return(2);
      case 141:Alert(symbol," ",Error,":Too many requests");return(2);
      case 145:Alert(symbol," ",Error,":Modification denied because order is too close to market");Sleep(10000);return(1);
      case 146:Alert(symbol," ",Error,":Trade context is busy");Sleep(3000);return(1);
      case 147:Alert(symbol," ",Error,":Expirations are denied by broker");return(2);
      case 148:Alert(symbol," ",Error,":The amount of open and pending orders has reached the limit set by the broker");return(2);
      case 149:Alert(symbol," ",Error,":An attempt to open an order opposite to the existing one when hedging is disabled");return(2);
      case 150:Alert(symbol," ",Error,":An attempt to close an order contravening the FIFO rule");return(2);
      default:Alert(symbol,":Error - № ",Error);return(2);
     }
   return(0);
  }
 
Sergey Gritsay:

Here you go, like it's from my heart.

Oh... No shit! I haven't done this much...! Yeah, well... Now I know why things don't work like they should.

Thank you very much, humanly.


 
Sergey Gritsay:

Hold it like it's from my heart.

May I ask why you are doing this?

int total=OrdersTotal();

 
Vasyl Nosal:

May I ask why this is done?

int total=OrdersTotal();

Because while the loop is executing, the value ofOrdersTotal(); might change and you might miss an order or the loop body might glitch