- Searching for a missing order!
- MQL4 Learning
- Please help me add Stop Loss & Take Profit function to this EA.
Can anyone help me with a loop to select orders by magic number? I am looking to replace orders once they are filled and closed. Would be just as easy to call by orderopen price? Any help would be greatly appreciated! Dan
Hi Daniel,
Here's some code I use to count opened (active) orders that were placed by my EA:
int ActiveOrders = 0; // Count Orders placed by this EA
if(OrdersTotal() > 0)
{
for( int cnt = 0 ; cnt < OrdersTotal() ; cnt++ )
{
OrderSelect( cnt, SELECT_BY_POS, MODE_TRADES );
if( OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber )
{
ActiveOrders++;
}
}
}
The ActiveOrders variable has the number of orders that this EA has opened when the loop finishes. It checks both MagicNumber and Currency pair (just in case you forgot to change the MagicNumber in another window). The magicNumber variable is typically an external (extern) variable in your code.
- Tovan
Hi Daniel,
Here's some code I use to count opened (active) orders that were placed by my EA:
int ActiveOrders = 0; // Count Orders placed by this EA
if(OrdersTotal() > 0)
{
for( int cnt = 0 ; cnt < OrdersTotal() ; cnt++ )
{
OrderSelect( cnt, SELECT_BY_POS, MODE_TRADES );
if( OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber )
{
ActiveOrders++;
}
}
}
The ActiveOrders variable has the number of orders that this EA has opened when the loop finishes. It checks both MagicNumber and Currency pair (just in case you forgot to change the MagicNumber in another window). The magicNumber variable is typically an external (extern) variable in your code.
- Tovan
In your piece above, the part after the "if" statement containing ActiveOrders++.......is that where you would put what you want it to do? I was trying to use this simply instead of looping(probably where I am going wrong).....
if((OrderSelect(5,SELECT_BY_POS)==false) && UB > 0)
OrderSend(C1,OP_BUYLIMIT,lot,B4,3,value4,B5,"Hedge"+C1,MagicNumber5,0,Green);
UB is a calculated value from a custom indicator. I was hoping to have it look and see if there is an order with MagicNumber5, if not, then place another trade at that level. It is not working? One issue with this is that if price is too low or high, it may call a buylimit when a correct order would be a buystop since current price is below that value I am trying to buy at....any help???
In your piece above, the part after the "if" statement containing ActiveOrders++.......is that where you would put what you want it to do? I was trying to use this simply instead of looping(probably where I am going wrong).....
if((OrderSelect(5,SELECT_BY_POS)==false) && UB > 0)
OrderSend(C1,OP_BUYLIMIT,lot,B4,3,value4,B5,"Hedge"+C1,MagicNumber5,0,Green);
UB is a calculated value from a custom indicator. I was hoping to have it look and see if there is an order with MagicNumber5, if not, then place another trade at that level. It is not working? One issue with this is that if price is too low or high, it may call a buylimit when a correct order would be a buystop since current price is below that value I am trying to buy at....any help???
Yes. The code you have there, if I'm not mistaken, would be looking for the fifth order placed, not an order with MagicNumber = 5.
- Tovan
Yes. The code you have there, if I'm not mistaken, would be looking for the fifth order placed, not an order with MagicNumber = 5.
- Tovan
Tovan,
thanks for the insight...I am trying to deal with what I call a double negative. If I select orders and identify an order with the magicnumber...cool. I am looking to find the situation when the order does NOT exist. Any clue on how I could write that by either using magicnumber or the open price (which my EA calculates and assigns a deignation such as "PB"? AGain, I appreciate the help! Dan
Tovan,
thanks for the insight...I am trying to deal with what I call a double negative. If I select orders and identify an order with the magicnumber...cool. I am looking to find the situation when the order does NOT exist. Any clue on how I could write that by either using magicnumber or the open price (which my EA calculates and assigns a deignation such as "PB"? AGain, I appreciate the help! Dan
Dan,
You might try assigning unique MagicNumbers to orders (with enough numbers that they won't get reused until the order must have been closed). Then you can look for orders with a particular MagicNumber. You can also use the Comment field when you place orders to do something similair, but I prefer working with the MagicNumber field because int types or easier to compare in code than strings.
- Tovan
Tovan,
thanks for the insight...I am trying to deal with what I call a double negative. If I select orders and identify an order with the magicnumber...cool. I am looking to find the situation when the order does NOT exist. Any clue on how I could write that by either using magicnumber or the open price (which my EA calculates and assigns a deignation such as "PB"? AGain, I appreciate the help! Dan
Same way he wrote that loop. If at the end of the loop check "activeOrders." If it's still zero, you don't have any open orders with your magic number.
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use