Download MetaTrader 5

enum or switch? - switching orders for closing?

To add comments, please log in or register
Dominic Gilbert
1639
Dominic Gilbert  
//--- global ---

enum SelectTicket   {Order1=0,Order2=1,Order3=2,Order4=3};
extern SelectTicket SelectOrder = Order4;

How do I translate the above to correspond to the right TicketOrder()? I am wanting to be able to change the extern variable to say Order2, and then when a certain function is called.

...

          if( OrderType()==OP_BUY )
          {
           if( SelectOrder == 0){Position_1 = BuyTicketOrder1;}
           if( SelectOrder == 1){Position_2 = BuyTicketOrder2;}
           if( SelectOrder == 2){Position_3 = BuyTicketOrder3;}
           if( SelectOrder == 3){Position_4 = BuyTicketOrder4;}

...

 For example, I select "Order2" which equals "1" (enum) -- Well, above that is BuyTicketOrder2 which in turn becomes Position_2... I need this to translate to a static variable that sites within the OrderClose() function where OrderTicket() is.... That way i can just change the extern and the OrderClose() will know exactly which ticket....

Dominic Gilbert
1639
Dominic Gilbert  
extern int OrderPos = 4;

...

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
   
   switch(OrderPos)// extern variable changes the type of trail below.
     {
      //case 0:break;
      case 1:BuyTicketOrder1=True;break;
      case 2:BuyTicketOrder2=True;break;
      case 3:BuyTicketOrder3=True;break;
      case 4:BuyTicketOrder4=True;break;
      default:BuyTicketOrder2=True;break;
     }

...


//+------------------------------------------------------------------+
//| Close at EMA if first target is hit.                             |
//+------------------------------------------------------------------+
void EMA_Close()//bool Yes1)
{   

   
   static datetime EMA_Exit_Set;
   double          EMA_Exit_Get = GlobalVariableGet(" EMA Exit "); 
     
   
   
   double EMA_MA = iMA(NULL,low,EMA_Exit,0,1,0,1);
   double EMA_Bar = iClose(NULL,low,1);
   
   RefreshRates();
   for(int b=OrdersTotal()-1; b>=0; b--)
     {
     if( !OrderSelect(b, SELECT_BY_POS, MODE_TRADES))continue;
      if( OrderMagicNumber()== MagicNumber1  )
        if(OrderSymbol() == Symbol())
        { 
          
          if( OrderType()==OP_BUY )
          {

            if( OrderTicket() != OrderPos )continue;
 
            if( OrderOpenTime() != EMA_Exit_Get && OrderOpenPrice() > EMA_Bar + Point && 
                EMA_MA > EMA_Bar + Point && OrderTicket() == OrderPos && EMA_Bar_Exit == 1 )
               {
               bool Buy_EMA_Close = OrderClose( OrderPos, OrderLots(), Bid, 5, CLR_NONE );
               if( Buy_EMA_Close != True )Print(" EMA_MinLotClose failed - Last Error: ", GetLastError());
               if( Buy_EMA_Close == True ){
                  Print(" EMA Target Hit ");EMA_Exit_Set = GlobalVariableSet(" EMA Exit ", OrderOpenTime());}
               }      
           }

...
I tried using a switch but I cant get it working either?
Carl Schreiber
7240
Carl Schreiber  

as far as I know you can use the enum-content (not tested - try within a script):

enum SelectTicket   {Order1=0,Order2=1,Order3=2,Order4=3};
extern SelectTicket SelectOrder = Order4;
...
switch (SelectOrder) {
    case Order1: Position_1 = BuyTicketOrder1; break
    ...
}

// or
if ( SelectOrder == Order1 ) { ..}
Dominic Gilbert
1639
Dominic Gilbert  
Thanks Gooly. Although I am wanting to do something like this:

            if( OrderOpenTime() != EMA_Exit_Get && OrderOpenPrice() > EMA_Bar + Point && 
                EMA_MA > EMA_Bar + Point && OrderTicket() == SelectOrder && EMA_Bar_Exit == 1 )
               {
               bool Buy_EMA_Close = OrderClose( SelectOrder, OrderLots(), Bid, 5, CLR_NONE ); // <<< SelectOrder
               if( Buy_EMA_Close != True )Print(" EMA_MinLotClose failed - Last Error: ", GetLastError());
               if( Buy_EMA_Close == True ){
                  Print(" EMA Target Hit ");
                     EMA_Exit_Set = GlobalVariableSet(" EMA Exit ", OrderOpenTime());}
               }   
I am wanting to put SelectOrder in where OrderTicket() is. This way, on the extern I can select which Order will close out here... Using what you have said above compiles, but does not work based upon how I am using "SelectOrder" in the OrderTicket() parameter above?
Carl Schreiber
7240
Carl Schreiber  

you can create a function? (not tested!)

double getTicketNo( SelectTicket tType ) {
   switch (tType) {
      case Order1: return(..);
      ...
   } 
}
...
OrderClose( getTicketNo(SelectOrder), OrderLots(), Bid, 5, CLR_NONE ); 
darksideofthemoon
57
darksideofthemoon  
DomGilberto:
I am wanting to put SelectOrder in where OrderTicket() is. 

OrderClose() only accepts a ticket number, not a position number.

Meaning you must retrieve the right ticket number (for the given position number).

How you would best do that depends on how you would create the orders.

Dominic Gilbert
1639
Dominic Gilbert  
gooly:

you can create a function? (not tested!)

You've lost me entirely there. Issue with parenthesis and logic. 
Dominic Gilbert
1639
Dominic Gilbert  
...

   RefreshRates();
   for(int b=OrdersTotal()-1; b>=0; b--)
     {
     if( OrderSelect(b, SELECT_BY_TICKET)==True )
      if( OrderMagicNumber()== MagicNumber1  )
        if(OrderSymbol() == Symbol())
        { 
          
        if( OrderType()==OP_BUY )
         {
               switch (SelectOrder){ 
                  case Order1: BuyTicketOrder1 = True;break;
                  case Order2: BuyTicketOrder2 = True;break;
                  case Order3: BuyTicketOrder3 = True;break;
                  case Order4: BuyTicketOrder4 = True;break;
                  }
                
            if( SelectOrder == OrderTicket() )Print("SelectOrder == ", OrderTicket());
            
            if( EMA_Bar_Exit_TP1 == 1  )// Exit 1 pos. at close beneath EMA after first TP is hit
            {
               if( Bid - Buy_TakeProfit_1 > Point / 2. && OrderOpenTime() != Target1_StoredBuy_OpenTime )
                  {
                  Buy_Target_1_reached = GlobalVariableSet(" Buy Target 1 hit ", OrderOpenTime());
                  } 
         
               if( OrderOpenTime() == Target1_StoredBuy_OpenTime && OpenOrdersThisPair(Symbol()) == 3 &&
                   EMA_MA > EMA_Bar + Point && OrderTicket() == SelectOrder )
                  {
                  bool Buy_EMA_Close = OrderClose( SelectOrder, OrderLots(), Bid, 5, CLR_NONE );
                  }
            }

...
How do I simply store the "BuyTicketOrderX" that corresponds the to extern Order1, Order 2 etc. within SelectOrder? My logic is to return the ticketnumber under SelectOrder and then use SelectOrder within OrderClose()??!?

UPDATE: Ok, so I got it working, but I've realised that if I select "Order4", well... Order4 is not OrderTicket() # 4 because the first TP was hit meaning there are only 3 orders open and #4 turns into #3... How do I make sure that it is ALWAYS the BuyTicketOrder4 that is closed if there is only 2-3-4 orders open?
Dominic Gilbert
1639
Dominic Gilbert  
Seriously... How on EARTH do I correctly select the specific open order that I want to be OrderClose()?!

Driving me up the wall. I want to select extern "Order1" and so fourth which in turn selects 1 - 4 PRECISELY and closes the corresponding trade... going mad here.
darksideofthemoon
57
darksideofthemoon  

For a one-time shot you can encode the order number in its (or as its) magic number (first order gets magic number 1, second 2, etc).

Then you test the magic number of the selected order against the extern number.

 

 Another poss is to use scripts so that each closes out a particular order position (and these you can bind to keys, i.e. [shift] 1 .. 4).

Dominic Gilbert
1639
Dominic Gilbert  
There HAS to be a simpler way surely!?
123
To add comments, please log in or register