Download MetaTrader 5
To add comments, please log in or register
Have you published a program in the Market? Place it in your blog as well - the widget will help!
James Beach
22
James Beach 2014.02.21 18:42 

Hi there,

I have written some code which basically loops through open orders and then filters them by their magic number. Something along the lines of

extern int BuyMagicNumber = xxxxx;

for(int PositionIndex = OrdersTotal() - 1; PositionIndex >= 0; PositionIndex --)    
  {
    if(!OrderSelect(PositionIndex,SELECT_BY_POS,MODE_TRADES))
      {
        Print("OrderSelect Failed:", GetLastError());
        continue;
      }  

    if(OrderMagicNumber() == BuyMagicNumber)
      {
         OrderPrint();

        // my code

      }
   }

            

however when I print the details of the order, its magic number is not BuyMagicNumber!!!! How is this possible?? Is it to do with the new MT4 build?

Thanks in advance,

James.

Simon Gniadkowski
Moderator
17963
Simon Gniadkowski 2014.02.21 22:00  
beachboy44:

Hi there,

I have written some code which basically loops through open orders and then filters them by their magic number. Something along the lines of

however when I print the details of the order, its magic number is not BuyMagicNumber!!!! How is this possible?? Is it to do with the new MT4 build?

Thanks in advance,

James.

Before the OrderPrint() add this . . .

   Print("Ticket # ", OrderTicket(), " OrderMN ", OrderMagicNumber(), " BuyMagicNumber ", BuyMagicNumber );

   OrderPrint();

. . . rerun your code then copy and paste here this bit of modified code and the output it produces for these lines of code . . . please.

James Beach
22
James Beach 2014.02.21 23:48  
RaptorUK:

Before the OrderPrint() add this . . .

. . . rerun your code then copy and paste here this bit of modified code and the output it produces for these lines of code . . . please.


RaptorUK
:

Before the OrderPrint() add this . . .

. . . rerun your code then copy and paste here this bit of modified code and the output it produces for these lines of code . . . please.

Just to help a little - the EA opens two orders at the same time; one has a fixed TP and the other has a trailing stop once that TP level is reached. (Hence the "SplitMagicNumbers")...
if(CurrentTimeStampClose != Time[0])
  {                        
    for(int PositionIndex = OrdersTotal() - 1; PositionIndex >= 0; PositionIndex --)    
      {
        if(!OrderSelect(PositionIndex,SELECT_BY_POS,MODE_TRADES))
          {
            Print("OrderSelect Failed:", GetLastError());
            continue;
          }  
            
        RefreshRates();
            
        if(OrderSymbol() == Symbol() && (OrderMagicNumber() == BuySplitMagicNumber || OrderMagicNumber() == SellSplitMagicNumber) && OrderOpenTime() < Time[0])
          {
            if(OrderType() == OP_BUY || OrderType() == OP_SELL)
              { 
                if((OrderMagicNumber() == BuySplitMagicNumber && !IsSLTrailing(0)) || (OrderMagicNumber() == SellSplitMagicNumber && !IsSLTrailing(1)))
                  {
                    Print("Ticket # ", OrderTicket(), " OrderMN ", OrderMagicNumber(), " SellSplitMagicNumber ", SellSplitMagicNumber );
                    OrderPrint();
                    if(EODTradeClose_Mode == 0 && OrderCloseTime() == 0 )
                      {
                        if(!OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),MaxSlippage))  
                          {
                            Print("Order Close failed, Order Number:", OrderTicket(), OrderComment(), "Error:", GetLastError()); 
                          }
                      }
                    
                    else if(EODTradeClose_Mode == 1 && OrderProfit() > 0 && OrderCloseTime() == 0)
                      {   
                        if(!OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),MaxSlippage))  
                          {
                            Print("Order Close failed, Order Number:", OrderTicket(), OrderComment(), "Error:", GetLastError()); 
                          }
                      }  
                  }
              }

            else if(OrderType() == OP_BUYLIMIT || OrderType() == OP_SELLLIMIT)
              {
                if(!OrderDelete(OrderTicket(),Red))  
                  {
                    Print("Order Delete failed, Order Number:", OrderTicket(), OrderComment(), "Error:", GetLastError()); 
                  }
              }
          }
      }
    CurrentTimeStampClose = Time[0];
  } 

and the output was...

2014.02.21 23:36:02.193 2003.06.11 00:00 LTBasis_v5 forum EURUSD,Daily: #1 2003.05.30 08:21:01 buy 0.49 EURUSD 1.18498 1.17498 1.19098 2003.05.30 15:40:04 1.17498 -3.43 0.00 -294.92 LazyTrader 3EMA Buy Limit O[sl] 5299289

2014.02.21 23:36:02.193 2003.06.11 00:00 LTBasis_v5 forum EURUSD,Daily: Ticket # 1 OrderMN 5299289 SellSplitMagicNumber 529973557

Basically orders 7 and 8 were both opened okay and order 7 closed as it should, but this order 1 seems to get 'stuck' when trying to close order 8. And I also can't understand how it shows that the currently selected order has hit its SL even though I am using MODE_TRADES and OrderCloseTime() == 0?

James Hodges
2802
James Hodges 2014.02.21 23:50  
You might try it this way... 
  
extern int BuyMagicNumber = xxxxx;

for(int PositionIndex = OrdersTotal() - 1; PositionIndex >= 0; PositionIndex --)    
  {
    if(OrderSelect(PositionIndex,SELECT_BY_POS,MODE_TRADES))//if it is able to select an order and OrderSelect returns true.
     {
       if(OrderMagicNumber() == BuyMagicNumber)//then if the numbers match
        {
          OrderPrint();//print the order
          // my code
        }
     }
    else
    {  //otherwise if it is not able to select and order and OrderSelect returns false...
       Print("OrderSelect Failed:", GetLastError());//tell us why
    }
  } 
Or maybe combine the if's with an && to see if that works. 	
extern int BuyMagicNumber = xxxxx;

for(int PositionIndex = OrdersTotal() - 1; PositionIndex >= 0; PositionIndex --)    
  {
    if(OrderSelect(PositionIndex,SELECT_BY_POS,MODE_TRADES) && OrderMagicNumber() == BuyMagicNumber)
     {
       OrderPrint();
       // my code
     }
    else
     {
       Print("OrderSelect Failed:",GetLastError());//tell us why
     }
  } 
However by combining both conditions into one if statement this would make it say it failed just because the numbers did not match.. :-{ 
James Hodges
2802
James Hodges 2014.02.22 00:05  
Ok you were adding a post while I was adding mine...LOL 
you may want to add some parentheses in this line like you did the others around each half..
if(OrderSymbol() == Symbol() && (OrderMagicNumber() == BuySplitMagicNumber || OrderMagicNumber() == SellSplitMagicNumber) && OrderOpenTime() < Time[0])
also I do this sometimes..
            //instead of
            if(OrderType() == OP_BUY || OrderType() == OP_SELL)
            //I sometimes say
            if(OrderType()<2)
            // since buy is 0 and sell is 1
Of course either way works fine.. just thought I'd share that.. PipPIp...Jimdandy         
James Beach
22
James Beach 2014.02.22 00:17  
Jimdandy:


Haha sorry about that :)

I've added some parentheses but it hasn't done the trick unfortunately. I can't see why it's selecting an order which has closed, despite the OrderCloseTime() == 0 condition.

Thanks for that little tip though, much more elegant :)

Simon Gniadkowski
Moderator
17963
Simon Gniadkowski 2014.02.22 11:45  
beachboy44:
Just to help a little - the EA opens two orders at the same time; one has a fixed TP and the other has a trailing stop once that TP level is reached. (Hence the "SplitMagicNumbers")...

and the output was...

2014.02.21 23:36:02.193 2003.06.11 00:00 LTBasis_v5 forum EURUSD,Daily: #1 2003.05.30 08:21:01 buy 0.49 EURUSD 1.18498 1.17498 1.19098 2003.05.30 15:40:04 1.17498 -3.43 0.00 -294.92 LazyTrader 3EMA Buy Limit O[sl] 5299289

2014.02.21 23:36:02.193 2003.06.11 00:00 LTBasis_v5 forum EURUSD,Daily: Ticket # 1 OrderMN 5299289 SellSplitMagicNumber 529973557

Basically orders 7 and 8 were both opened okay and order 7 closed as it should, but this order 1 seems to get 'stuck' when trying to close order 8. And I also can't understand how it shows that the currently selected order has hit its SL even though I am using MODE_TRADES and OrderCloseTime() == 0?

It woud have helped if you had show the real code to begin with . . . now please show the code for IsSLTrailing() or at least confirm that the function contains an OrderSelect() call ? and then read this post: https://www.mql5.com/en/forum/149588
James Beach
22
James Beach 2014.02.22 13:36  
RaptorUK:
It woud have helped if you had show the real code to begin with . . . now please show the code for IsSLTrailing() or at least confirm that the function contains an OrderSelect() call ? and then read this post: https://www.mql5.com/en/forum/149588


Yeah there was an OrderSelect() in IsSLTrailing()... I'm an idiot!!! Storing the parameters of the first OrderSelect() and then passing those to the OrderClose() does the trick. Thanks for your help, much appreciated.
Simon Gniadkowski
Moderator
17963
Simon Gniadkowski 2014.02.22 18:08  
beachboy44:

Yeah there was an OrderSelect() in IsSLTrailing()... I'm an idiot!!! Storing the parameters of the first OrderSelect() and then passing those to the OrderClose() does the trick. Thanks for your help, much appreciated.
No, you aren't an idiot, you made a mistake, the only people who don't make mistakes are those that don't do anything. Glad I could help
whroeder1
13656
whroeder1 2014.02.22 23:11  
Yep, you're an idiot. To err is human, to really foul things up requires a computer. You did both. Welcome to the club. :) I've been coding for 41 (sic) years - I don't make misteaks, much.
/
To add comments, please log in or register