EA not updating as expected

 

Hi everyone. I've been trying a different strategy recently, however been having issues with the EA not updating as I thought it would. I'm running the update part of the EA on a timer like so:

void OnTimer()

{

double bbMid = iBands(NULL,0,bbPeriod,0,0,PRICE_CLOSE,0,0);


if(OrderSelect(openOrderID,SELECT_BY_TICKET)==true)

{

int orderType = OrderType();// Short = 1, Long = 0

if(orderType == 0 && bbMid>ORDER_PRICE_OPEN)//long position

{

bool Ans = OrderModify(openOrderID,OrderOpenPrice(),bbMid,OrderTakeProfit(),0);

}

else

if(orderType == 1 && bbMid<ORDER_PRICE_OPEN)

{

bool Ans = OrderModify(openOrderID,OrderOpenPrice(),bbMid,OrderTakeProfit(),0);

}

}

}


I've tried multiple variations of the above but can't get it to work. If anyone has any idea why that would be great!

Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Order Properties
Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Order Properties
  • www.mql5.com
Order Properties - Trade Constants - Constants, Enumerations and Structures - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
if(OrderSelect(openOrderID,SELECT_BY_TICKET)==true)
  1. You select by ticket. Verify the ticket is still open.

  2. You should be able to read your code out loud and have it make sense. You would never write if( (2+2 == 4) == true) would you? if(2+2 == 4) is sufficient. So don't write if(bool == true), just use if(bool) or if(!bool). Code becomes self documenting when you use meaningful variable names, like bool isLongEnabled where as Long_Entry sounds like a trigger price or a ticket number and “if long entry” is an incomplete sentence.

 

Thank you for the reply.

Does this look better?

void OnTimer()

{

double newSL = iBands(NULL,0,bbPeriod,updatedSL,0,PRICE_CLOSE,0,0);

if(OrderSelect(openOrderID,SELECT_BY_TICKET))

{

if(OrderType() == OP_BUY && newSL >= ORDER_PRICE_OPEN)

   {
   bool Ans = OrderModify(openOrderID,OrderOpenPrice(),newSL,OrderTakeProfit(),0);
   }
  else
  {
       if(OrderType() == OP_SELL && newSL <= ORDER_PRICE_OPEN)//long position
   
     {
     bool Ans = OrderModify(openOrderID,OrderOpenPrice(),newSL,OrderTakeProfit(),0);
     } 


}

}


}

I think i've done as you said and also changed the new SL so I can set the level as an input.

 
Matthew ford #: Does this look better?

What part of "verify the ticket is still open" was unclear?

 

Hello guys can you please assist me.


I want to install a trading Robot into my MT4-MQL4-Experts folder but after downloding the Robot it does not want to drag and drop into my MQL4-Experts folder, pleasen advise.

IMG_20211216_142744.jpg 

IMG_20211216_142759.jpg


Pumi

 
William Roeder #:

What part of "verify the ticket is still open" was unclear?

I clearly haven't understood what you meant. Is what I coded not checking if the ticket is still open, and then running the rest of what I wrote based on that?


 
Matthew ford # Is what I coded not checking if the ticket is still open,
Exactly.