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

 
yaaarik777:
I'm just learning, I'm trying to use the guide, but I realise I'm getting some nonsense, I'd like to see an example of how to do it.
Read this article, it will be usefulhttps://www.mql5.com/ru/articles/1399
События в МetaТrader 4
События в МetaТrader 4
  • 2006.05.29
  • Andrey Khatimlianskii
  • www.mql5.com
Статья посвящена программному отслеживанию событий в терминале МetaТrader 4, таких как открытие, закрытие и модификация ордеров, и рассчитана на пользователя, обладающего базовыми навыками работы с терминалом и программирования на MQL 4.
 
yaaarik777:

Good day everyone! Can you please advise how to set the deletion of an order in an EA?

The situation is as follows:

We place 2pending orders in different directions, as soon as one of them triggers, the other is removed and is no longer exhibited.

I would be very grateful for any help.

Thank you.

Something like this

   for(int pos=OrdersTotal()-1,ticket=0,trade=0;pos!=-1;pos--)
     {
      if(OrderSelect(pos,SELECT_BY_POS,MODE_TRADES) && OrderType()>1)//Если найденный ордер отложенный
        {
         ticket=OrderTicket();//Запомним его тикет
        }
      else//Если ордер не отложенный
        {
         trade=1;//Запомним,что есть открытая позиция
        }
      if(ticket!=0 && trade==1)//И если известен отложенный и открыта позиция
        {
         int OD=OrderDelete(ticket);//Удаляем отложенный
        }
     }
 
Nikolay Gaylis:
"Let's keep looking" needs to be removed!
You can edit your own posts on the forum. Everything you've written in separate posts above can be fit into one.
 
Vitalie Postolache:
You can edit your own posts on the forum. Everything you wrote in separate posts above can be fit into one.
Thank you!
 

Hi! Introduced a takeprofit trawl using the following algorithm:

if(total>0)
{for (int i=OrdersTotal()-1; i>=0; i--)
if(OrderSelect(i,SELECT_BY_POS)==true)
{
if(OrderMagicNumber()==magic && OrderSymbol()==asset& OrderType()==OP_BUY)
{
RefreshRates();
if(TimeCurrent()>OrderOpenTime() && TimeCurrent()<=(OrderOpenTime()+3600))
{
TAKEPROFIT CONDITION;
ORDER MODIFICATION TO A NEW TAKEPROFIT;
}
}
}
}

But the time limit doesn't work for some reason and it happens that if a position is not closed in the current timeframe, it also trawls in the next timeframe if the conditions are met.

What's wrong with my last if()?

 
владимир:

Hello! I have introduced a takeprofit trawl using the following algorithm:

if(total>0)
{for (int i=OrdersTotal()-1; i>=0; i--)
if(OrderSelect(i,SELECT_BY_POS)==true)
{
if(OrderMagicNumber()==magic && OrderSymbol()==asset && OrderType()==OP_BUY)
{
RefreshRates();
if(TimeCurrent()>OrderOpenTime() && TimeCurrent()<=(OrderOpenTime()+3600))
{
TRAILING TP CONDITION;
{ MODIFICATION OF THE ORDER TO THE NEW TAKEPROFIT;
}
}
}
}

But for some reason the time limit does not work, and it happens that if the position is not closed in the current timeframe, it trails in the next one too, if the conditions are met.

What is wrong with my last if()?

Well, I don't know, I have not seen anything wrong there yet. I have removed the redundant check on the number of orders (it is already present in the loop conditions) and added a check for both types of positions (Buy and Sell)

//+------------------------------------------------------------------+
for(int i=OrdersTotal()-1; i>=0; i--) {
   if(OrderSelect(i,SELECT_BY_POS)) {
      if(OrderType()>OP_SELL) continue;
      if(OrderMagicNumber()==magic && OrderSymbol()==asset) {
         if(TimeCurrent()>OrderOpenTime() && TimeCurrent()<=OrderOpenTime()+PeriodSeconds(PERIOD_H1)) {
            // УСЛОВИЕ ТРАЛА ТЭЙКПРОФИТА;
            // МОДИФИКАЦИЯ ОРДЕРА НА НОВЫЙ ТЭЙКПРОФИТ;
            }
         }
      }
   }
//+------------------------------------------------------------------+

Where // the trawl and modification conditions, insert Print() with the values of the time variables and see what the log says.

 
Artyom Trishkin:

I don't know, I don't see what's wrong with it yet with a quick glance. I removed unnecessary check on number of orders (it is already there in cycle conditions) and added check on both types of positions (Buy and Sell)

//+------------------------------------------------------------------+
for(int i=OrdersTotal()-1; i>=0; i--) {
   if(OrderSelect(i,SELECT_BY_POS)) {
      if(OrderType()>OP_SELL) continue;
      if(OrderMagicNumber()==magic && OrderSymbol()==asset) {
         if(TimeCurrent()>OrderOpenTime() && TimeCurrent()<=OrderOpenTime()+PeriodSeconds(PERIOD_H1)) {
            // УСЛОВИЕ ТРАЛА ТЭЙКПРОФИТА;
            // МОДИФИКАЦИЯ ОРДЕРА НА НОВЫЙ ТЭЙКПРОФИТ;
            }
         }
      }
   }
//+------------------------------------------------------------------+

Where // trawl and modification conditions, insert Print() with values of time variables and see what the log says.

I have different conditions for Sell, so I have only shown BUY.

" with time variables" - do you mean TimeCurrent()? Sorry if this surprises you - I am a zero-minus in programming.

And I do not understand this -OrderType()>OP_SELL. Why >? OrderType OP_SELL has value = 1 andOP_BUY = 0. Therefore it must be <= ? Thank you.

 
владимир:

I have different trawl conditions for SELL, so I only showed BAI.

" with time variable values" - do you mean TimeCurrent()? Sorry if this surprises you - I'm a zero-minus in programming.

And I do not understand this -OrderType()>OP_SELL. Why >? OrderType OP_SELL has value = 1 andOP_BUY = 0. Therefore it must be <= ? Thank you.

If the type is greater than 1, then go to the next iteration of the loop.
 

Greetings. Could you please tell me why in the case of the following entry

if (x!=maxpr1){x=maxpr1; OrderDelete(ticketD);}

if (z!=minpr1){z=minpr1; OrderDelete(ticketUP);}

The system constantly generates errors

: invalid ticket for OrderDelete function

: OrderDelete error 4051

According to the reference book it is

Invalid value of the function parameter

Although the parameters seem to be the same.

Although if you remove the condition and leave only

OrderDelete(ticketD);

OrderDelete(ticketUP);

it deletes successfully (but I need it by condition). What am I doing wrong?

 
spoiltboy:

Greetings. Could you please tell me why in the case of the following entry

if (x!=maxpr1){x=maxpr1; OrderDelete(ticketD);}

if (z!=minpr1){z=minpr1; OrderDelete(ticketUP);}

The system constantly generates errors

: invalid ticket for OrderDelete function

: OrderDelete error 4051

According to the reference book it is

Invalid value of the function parameter

Although the parameters seem to be the same.

Although if you remove the condition and leave only

OrderDelete(ticketD);

OrderDelete(ticketUP);

it deletes successfully (but I need it by condition). What am I doing wrong?

The error is somewhere above the code, in the order selection area.
Reason: