Run code on stop order activation

 

Hello again mql5 community!

Im back with another question im hoping someone has the answer for.

I am trying to make a "scanForOrderModify" function that will add SL and TP when a BUYSTOP or SELLSTOP order is activated. This being my first time using pending orders instead of just market orders.... im finding it a little difficult.

If I run what I have as "OnTick" to ensure I don't miss any activated trades it works great but I run into serious memory/cpu use issues.

a basic example of what im going for:


if(OrderType()==(OP_BUY||OP_SELL) && OrderTakeProfit()==0 && OrderStopLoss==0) //Catch any that haven't been modified, while not changing any that have already been modified.

{
   ScanForModify();
}

However, it seems to increase terminal load immensely.

 I have searched through the documentation, however I cant seem to find something like "OnActivated" or "If(Activated()==1)".

Does anyone know of a solution or something I can read that may lead me down the right path?

 
Stephen Davies:

Hello again mql5 community!

Im back with another question im hoping someone has the answer for.

I am trying to make a "scanForOrderModify" function that will add SL and TP when a BUYSTOP or SELLSTOP order is activated. This being my first time using pending orders instead of just market orders.... im finding it a little difficult.

If I run what I have as "OnTick" to ensure I don't miss any activated trades it works great but I run into serious memory/cpu use issues.

a basic example of what im going for:


However, it seems to increase terminal load immensely.

 I have searched through the documentation, however I cant seem to find something like "OnActivated" or "If(Activated()==1)".

Does anyone know of a solution or something I can read that may lead me down the right path?

Nobody can help you unless you post your code.


Also this is an invalid expression. 

if(OrderType()==(OP_BUY||OP_SELL)

which is the same as 

if(OrderType() == true)

it should be 

if(OrderType() < 2)
 

Also this is an invalid expression. 

if(OrderType()==(OP_BUY||OP_SELL)

which is the same as 

if(OrderType() == true)


I mean no offence, but im not sure that's quite true... although im rather new to mql4 isn't there also the option of OP_BUYLIMIT, OP_BUYSTOP, etc, etc..... or do they not return an int value?

From documentation:

OP_BUYLIMIT2Buy limit pending order
OP_SELLLIMIT3Sell limit pending order

My plan with that statement was to only include active orders and not pending. However if that's not the correct way to do it then im always open to suggestions :).especially since that way eats up so many resources..... And I don't have a working current code for what im trying to do sadly, or anything closer then what I posted.

Am I to understand there is no built in way to do "OnActivated"m or to check for pending order being activated? Would that be a mql5 feature?

 
Stephen Davies:

Also this is an invalid expression. 

which is the same as 


I mean no offence, but im not sure that's quite true... although im rather new to mql4 isn't there also the option of OP_BUYLIMIT, OP_BUYSTOP, etc, etc..... or do they not return an int value?

From documentation:

OP_BUYLIMIT2Buy limit pending order
OP_SELLLIMIT3Sell limit pending order

My plan with that statement was to only include active orders and not pending. However if that's not the correct way to do it then im always open to suggestions :). And I don't have a working current code for what im trying to do, am I to understand there is no built in way to do "OnActivated"m or to check for pending order being activated?

As nicholishen said (OrderType()<2), or as below as I don't like to use hardcoded value :

if(OrderType()==OP_BUY || OrderType()==OP_SELL ...)
 
then I apologize for my ignorance :) thank you both for your help, I will give that a try and see if it helps with the resource draw. Is there really no OnEvent::Activated or OnActivated built in variable? that's surprising to me for such a versatile language.
 
Stephen Davies:

Also this is an invalid expression. 

which is the same as 


im not sure that's quite true... although im rather new to mql4 isn't there also the option of OP_BUYLIMIT, OP_BUYSTOP, etc, etc..... or do they not return an int value? My plan with that statement was to only include active orders and not pending. However if that's not correct then im open to suggestions. And I don't have a working current code for what im trying to do, am I to understand there is no built in way to do "OnActivated"m or to check for pending order change?

It's 100% not valid. It's not valid because the expression order of operations is wrong. 

(OP_BUY || OP_SELL) == true; //true

You cannot check membership in an expression that way, ever. This is the correct way to do it.  

if(OrderType() == OP_BUY || OrderType() == OP_SELL)

Since OP_BUY = 0 and OP_SELL = 1 you can avoid calling the ordertype function twice...

bool is_live_trade(int order_type)
{
   return order_type < 2; //less than two is a live trade.
}
 
Stephen Davies:
then I apologize for my ignorance :) thank you both for your help, I will give that a try and see if it helps with the resource draw. Is there really no OnEvent::Activated or OnActivated built in variable? that's surprising to me for such a versatile language.

You need to use MT5 for those type of features. 

 
Stephen Davies:
then I apologize for my ignorance :) thank you both for your help, I will give that a try and see if it helps with the resource draw. Is there really no OnEvent::Activated or OnActivated built in variable? that's surprising to me for such a versatile language.

No. You need to monitor your orders.

If you know the ticket of your pending, you can just check the type without having to loop all orders.

 
nicholi shen:

It's 100% not valid. It's not valid because the expression order of operations is wrong. 

You cannot check membership in an expression that way, ever. This is the correct way to do it.  

Since OP_BUY = 0 and OP_SELL = 1 you can avoid calling the ordertype function twice...

You are trying to win microseconds now ? ;-)
 
Alain Verleyen:
You are trying to win microseconds now ? ;-)

I normally (almost always) would agree with you about hard-coded values, however, OrderType()<2 is the most commonly used pattern for live trade testing in all of MQL4. Those order constants will never change so I lazily type it like a lot of other devs. Technically speaking yours is better though. I also use this pattern on occasion... 

enum XORDER_TYPES
{
   BUY          = 1<<0,
   SELL         = 1<<1,
   BUY_LIMIT    = 1<<2,
   SELL_LIMIT   = 1<<3,
   BUY_STOP     = 1<<4,
   SELL_STOP    = 1<<5
};

bool is_type(int order_type, int type_flags)
{
   return type_flags & (1<<order_type);
}
void OnStart()
{
   ...  
      Print(is_type(OrderType(), BUY|BUY_LIMIT|BUY_STOP));

}


 

 
nicholi shen:

I normally (almost always) would agree with you about hard-coded values, however, OrderType()<2 is the most commonly used pattern for live trade testing in all of MQL4. Those order constants will never change so I lazily type it like a lot of other devs. Technically speaking yours is better though. I also use this pattern on occasion... 

Sure there is a lot of ways. There is little chance "2" will change but still I don't like it because is not clear what that means...well of course we know it...anyway you see what I mean. Matter of taste.

For sure on my "real" code I don't use 2 times a function in a row...but on a forum it's good enough ;-)

Reason: