Questions from Beginners MQL5 MT5 MetaTrader 5 - page 890

 

There is an Expert Advisor that allows you to determine when a pending order has triggered

https://www.mql5.com/ru/code/17610

I use it to get a ticket for a SELL_STOP order

I have changed the code to

//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   if(bln_find_order) // true -> you should look for a order
     {
      static long counter=0;
      Print("Attempt number ",counter);
      ResetLastError();
      if(HistoryOrderSelect(ul_find_order))
        {
         long type_order=HistoryOrderGetInteger(ul_find_order,ORDER_TYPE);
         if(type_order==ORDER_TYPE_SELL_STOP)
           {
            Print("The pending order ",ul_find_order," is found! Type of order is ",
                  EnumToString((ENUM_ORDER_TYPE)HistoryOrderGetInteger(ul_find_order,ORDER_TYPE)));
            bln_find_order=false;         // true -> you should look for a order
            counter=0;
            return;
           }
         else
           {
            Print("The order ",ul_find_order," is not pending");
            bln_find_order=false;         // true -> you should look for a order
            return;
           }
        }
      else
        {
         Print("Order ",ul_find_order," is not find, error#",GetLastError());
        }
      counter++;
     }
  }

Everything is fine until a certain moment. Until another order type appears in the history.

For example, SELL_LIMIT.

EXAMPLE

As soon as SELL_LIMIT appears in the history, I cannot get a ticket of the last SELL_STOP.

The Expert Advisor throws an error.

The order 9 is not pending

Although there is SELL_STOP in the history

And why do I have to do with 9th if I need the 7th ticket?

It feels like the Expert Advisor searches for the needed order not through all of the history, but only for the last one
TypePendingOrderTriggered
TypePendingOrderTriggered
  • votes: 21
  • 2017.02.14
  • Vladimir Karputov
  • www.mql5.com
()   {    (bln_find_order)      {        counter=;       (,counter);       ();         {          (type_order== || type_order== ||             type_order== ||type_order==)            {             (,ul_find_order,,                   (()(ul_find_order,)));             bln_find_order=;                     counter=;             ;            }...
 

Hello. I get error ifStopLoss_=0

failed modify #2 sell 3.00 Si-9.18 sl: 0, tp: 62749 -> sl: 0, tp: 62774 [Invalid stops]


 if(m_position.PositionType()==POSITION_TYPE_SELL)
                 {
                  if(m_symbol.NormalizePrice(m_position.PriceOpen()-ExtTakeProfit)!=m_position.TakeProfit())
                    {
                     if(StopLoss_<=0)
                       {
                        m_trade.PositionModify(m_position.Ticket(),
                                               m_position.StopLoss(),
                                               m_symbol.NormalizePrice(m_position.PriceOpen()-ExtTakeProfit)
                                               );
                        Sleep(200);
                       }

                     if(StopLoss_>0)
                       {
                        m_trade.PositionModify(m_position.Ticket(),
                                               m_symbol.NormalizePrice(m_position.PriceOpen()+StopLoss_*m_adjusted_point),
                                               m_symbol.NormalizePrice(m_position.PriceOpen()-ExtTakeProfit)
                                               );
                        Sleep(200);
                       }
                    }
                 }
ifi put zero instead ofm_position.StopLoss(), same error. What's wrong?
 
Sile Si:

Hello. I get an error ifStopLoss_=0

failed modify #2 sell 3.00 Si-9.18 sl: 0, tp: 62749 -> sl: 0, tp: 62774 [Invalid stops]

   if(m_position.PositionType()==POSITION_TYPE_SELL)
     {
      if(m_symbol.NormalizePrice(m_position.PriceOpen()-ExtTakeProfit)!=m_position.TakeProfit())
        {
         if(StopLoss_<=0)
           {
            m_trade.PositionModify(m_position.Ticket(),
                                   m_position.StopLoss(),
                                   m_symbol.NormalizePrice(m_position.PriceOpen()-ExtTakeProfit)
                                   );
            Sleep(200);
           }

         if(StopLoss_>0)
           {
            m_trade.PositionModify(m_position.Ticket(),
                                   m_symbol.NormalizePrice(m_position.PriceOpen()+StopLoss_*m_adjusted_point),
                                   m_symbol.NormalizePrice(m_position.PriceOpen()-ExtTakeProfit)
                                   );
            Sleep(200);
           }
        }
     }

ifI put zero instead ofm_position.StopLoss(), same error. What is it?

Incorrect logic in writing the program.

When StopLoss_=0, the upper condition triggers - an attempt to modify TakeProfit. The error is that you subtract from the OPEN PRICE - in this case TakeProfit level may be higher than the CURRENT PRICE and this is an error.

In your case, subtract from the RIGHT PRICE (m_position.PriceCurrent).

 
Vladimir Karputov:

A sell position was opened and selllimit was activated, I need to move the tp relative to the new price of the position, not relative to the current price.

What kind of check should I do to avoid trying to modify to an invalid price?

 
Sile Si:

A sell position was opened and selllimit was activated, I need to move the tp relative to the new position price, not the current price.

What kind of check should I do to not try to modify to an invalid price?

Check:

if(m_position.PriceOpen()-ExtTakeProfit<m_position.PriceCurrent()
   {
    m_trade.PositionModify(m_position.Ticket(),
                           m_position.StopLoss(),
                           m_symbol.NormalizePrice(m_position.PriceOpen()-ExtTakeProfit)
                           );
...
 
Vladimir Karputov:

Check:

Thank you, but the error remains.

aqa

Could be unacceptably close to the current price. How can I check this?

 
Sile Si:

Thank you, but the error remains.

Could be unacceptably close to the current price. How do you check this?

I don't have access to an exchange, so either do it yourself or work on forex instruments. Sorry.


Added: introduce another check - for a Sell position the new TakeProfit (calculated value) must not be HIGHER or EQUAL to the current TakeProfit of the position.

 
Vladimir Karputov:

I don't have access to an exchange, so either do it yourself or work on forex instruments. Sorry.


Added: introduce another check - for a Sell position the new TakeProfit (calculated value) must not be HIGHER or EQUAL to the current TakeProfit of the position.

Probably the opening price (POSITON_PRICE_OPEN) ?

 
Sergey Savinkin:

Probably the opening price (POSITON_PRICE_OPEN) ?

This option was above. See the posts above. You answered in the middle of the conversation.

 
Vladimir Karputov:...enter another check - for a Sell position the new TakeProfit (calculated value) must not be HIGH or EQUAL to the current TakeProfit of the position.

Does not fit, modify by ATR What other checks can be done for this error? All possible. In mt4 there isSTOPLEVEL, but here what?

Reason: