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

 
WinProject:

Please tell me how to delete a graphical object. There are vertical lines whose names are constructed from variable values.

string name=="test1"; string Vertline="line_"+name; ObjectCreate(0,VertLine,OBJ_VLINE,0,time,cena); How can I delete only vertical lines with the name test1?


You may find this function useful:

Deletes all objects of the specified type by the name prefix in a subwindow.

intObjectsDeleteAll(
longchart_id,// chart ID
const stringprefix,// object name prefix
intsub_window=EMPTY,// index of the window
intobject_type=EMPTY//type of object to be deleted
);

 
Vladislav Andruschenko:


you may find this function useful:

Deletes all objects of the specified type by the name prefix in the chart subwindow.

intObjectsDeleteAll(
longchart_id,//chart identifier
const stringprefix,// prefixobject name
intsub_window=EMPTY,// window index
intobject_type=EMPTY// object type to remove
);


Thanks, as far as I understand this function understands the prefix as the first substring in the object name, but how to delete by the second substring in the object name? Is there any way to use StringSubstr and StringFind to the object name for this purpose?

 
WinProject:

Thanks, as far as I understand, by prefix this function understands the first substring in the object name, but how to delete by the second substring in the object name? Is there any way to use StringSubstr and StringFind to the object name for this purpose?


wait,

You have specified the code:

string Vertline="линия_"+name; 


Where you have the prefix.

That's what everyone does.

That's why I suggested you the function to delete objects by prefix.


"линия_"

If you need to delete a line by suffix, you canuse StringFind to search for all objects

for example:

void ObjectsDelete()
  {
string NameEA="суффикс";
   for(int i=ObjectsTotal();i>=0;i--)
     {
      if(StringFind(ObjectName(i),NameEA)!=-1)ObjectDelete(ObjectName(i));
     }
  }
 

Thank you Vladislav, everything works, with such help from forum members, MQL is much more interesting. Interesting site you have, thank you!

 

Hello. Please help me to find out why it stops trawling after positions appear.

The point is this: open two positions 1, 2. at one price the first is closed by trawl, the second trawl is activated on the condition that at the opening price of the position, the position is one.

Everything works fine, position 1 is closed, position 2 is trawled, but when two more positions 3 and 4 open at differing prices, the trawl does not move on to position 2.

Why does it stop trawling?

void Tral_SL_one(int _TrailingStop=0,int _TrailingStep=0)
  {
   if(_TrailingStop<=0)
      return;
   for(int i=0; i<OrdersTotal(); i++)
     {
      if(!(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)))
         continue;
      if(OrderSymbol()!=Symbol())
         continue;
      if(OrderMagicNumber()!=_MagicNumber)
         continue;
      if(OrderType()==OP_BUY)
        {
         if(NumPosByPrice(Symbol(),OP_BUY,OrderOpenPrice(),_MagicNumber)==1)
           {
            if(NormalizeDouble(Bid-OrderOpenPrice(),Digits)>NormalizeDouble(_TrailingStop *Point,Digits))
              {
               if(NormalizeDouble(OrderStopLoss(),Digits)<NormalizeDouble(Bid -(_TrailingStop+_TrailingStep-1)*Point,Digits) || OrderStopLoss()==0)
                 {
                  if(OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(Bid-_TrailingStop*Point,Digits),OrderTakeProfit(),OrderExpiration()));
                   
                 }
              }
           }
        }

      if(OrderType()==OP_SELL)
        {

         if(NumPosByPrice(Symbol(),OP_SELL,OrderOpenPrice(),_MagicNumber)==1)
           {
            if(NormalizeDouble(OrderOpenPrice()-Ask,Digits)>NormalizeDouble(_TrailingStop *Point,Digits))
              {
               if(NormalizeDouble(OrderStopLoss(),Digits)>NormalizeDouble(Ask+(_TrailingStop+_TrailingStep-1)*Point,Digits) || OrderStopLoss()==0)
                 {
                  if(OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(Ask+_TrailingStop*Point,Digits),OrderTakeProfit(),OrderExpiration()));
                    
                 }
              }
           }
        }
     }
  }
//===
int NumPosByPrice(string sy="",int op=-1,double pp=0,int mn=-1) 
  {
   double px,py;
   int    d,i,k=OrdersTotal(),num=0;

   if(sy=="0") sy=Symbol();
   for(i=0; i<k; i++) 
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) 
        {
         if((OrderSymbol()==sy || sy=="") && (op<0 || OrderType()==op)) 
           {
            if(OrderType()==OP_BUY || OrderType()==OP_SELL) 
              {
               if(mn<0 || OrderMagicNumber()==mn) 
                 {
                  d=MarketInfo(OrderSymbol(),MODE_DIGITS);
                  px=NormalizeDouble(pp, d);
                  py=NormalizeDouble(OrderOpenPrice(), d);
                  if(pp<=0 || px==py) num++;
                 }
              }
           }
        }
     }
   return(num);
  }
 
mila.com:

Hello. Please help me to find out why it stops trawling after positions appear.

The point is this: open two positions 1, 2. at one price the first is closed by trawl, the second trawl is activated on the condition that at the opening price of the position, the position is one.

Everything works fine, position 1 is closed, position 2 is trawled, but when two more positions 3 and 4 open at differing prices, the trawl does not move on to position 2.

Why does it stop trawling?


I would only point out a gross error.

   for(int i=0; i<OrdersTotal(); i++)
     {
      if(!(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)))
         continue;
      if(OrderSymbol()!=Symbol())
         continue;
      if(OrderMagicNumber()!=_MagicNumber)
         continue;
      if(OrderType()==OP_BUY)
        {
         if(NumPosByPrice(Symbol(),OP_BUY,OrderOpenPrice(),_MagicNumber)==1)


You go through the orders and then run the function counting the number of positions, thereby selecting another position in theNumPosByPrice function

so the further construction

 if(NormalizeDouble(Bid-OrderOpenPrice(),Digits)>NormalizeDouble(_TrailingStop *Point,Digits))


will not work correctly.


there are many ways to solve this.

For example:

void Tral_SL_one(int _TrailingStop=0,int _TrailingStep=0)
  {
   if(_TrailingStop<=0)
      return;
   for(int i=0; i<OrdersTotal(); i++)
     {
      if(!(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)))
         continue;
      if(OrderSymbol()!=Symbol())
         continue;
      if(OrderMagicNumber()!=_MagicNumber)
         continue;
      if(OrderType()==OP_BUY)
        {
         if(NumPosByPrice(Symbol(),OP_BUY,OrderOpenPrice(),_MagicNumber)==1)
           {
OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
            if(NormalizeDouble(Bid-OrderOpenPrice(),Digits)>NormalizeDouble(_TrailingStop *Point,Digits))


Although this is fundamentally wrong, it will do for a quick understanding.

after:


if(NumPosByPrice(Symbol(),OP_BUY,OrderOpenPrice(),_MagicNumber)==1)

add :

OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
 
Vladislav Andruschenko:


I'll just point out the gross error.

There are many ways to solve this.

Thank you, I see, trawl)

 

Can you please tell me how to reopen the alerts window in mt4? I used to have a script that created a fake signal and opened the alerts window, now after reinstalling the system I can't find it on the internet. Maybe someone has one?

 
amenrazp:

Can you please tell me how to reopen the alerts window in mt4? I used to have a script that created a fake signal and opened the alerts window, now after reinstalling the system I can't find it on the internet. Maybe someone has one?

Alert(""); such a script?
 
Vladislav Andruschenko:
Alert(""); such a script?

Looks like it. Do you have a way of attaching it here?