enum or switch? - switching orders for closing? - page 2

 

Well you have to "tag" the order upon its creation somehow, in a unique way. Later on you retrieve the tag. I do not think that there is a simpler way.

Also I think that you have not explained your problem well: how do you create orders - manually? Is there always to be four orders? And maximally four orders? And so on..

 
Sorry, yeah there are always 4 and a maximum of 4. They're placed via pending order.
 

Yeah that is of course a problem because you cannot tag manually created orders with magic numbers (however comment is possible - you can use the comment as a tag).

Maybe try to distinguish the orders by their time of creation (not sure if this is possible). 

 

Forgive me for doubting what you're saying, but I have a hunch that there is a way. What I do doubt is that we've found a limitation of MT4... When you do a for loop by position, it'll index 0 through to nth number....

Each open position has a fixed take profit target that is different. 4th has the further Reward ratio and so-forth...

Is there a way I could do it using the TP? 

 

I am not quite sure what you are looking for - may be this helps?

To close an order you have to know the ticket number!

You either have saved it:  ticket = OrderSend(..) or you have to get it by OrderSelect() and e.g. OrderMagicNumber()..

 
DomGilberto:

Forgive me for doubting what you're saying, but I have a hunch that there is a way. What I do doubt is that we've found a limitation of MT4... When you do a for loop by position, it'll index 0 through to nth number....

Each open position has a fixed take profit target that is different. 4th has the further Reward ratio and so-forth...

Is there a way I could do it using the TP? 

Sure. Anything that can identify an order will do the trick.

 
I know the ticket number as I saved it, but that switch malarkey doesn't work... It appears that I have the TickerNumber but it doesnt actually store it. It changes irrespective of what "SelectOrder" says...

...

   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 );
                  }
            }

...
So for example, lets say I select (extern) "Order3" ... Well, this is supposed to be BuyTicketOrder3, or ticket number #3 (for the first ever 4 trades placed). Now, this whole thing above, closes the trade where it should and the right trade. Then the next 4 trades open from another setup... no more closing. I printed it out and OrderSelect endlessly changes through the OrderTickets...

Just looking for some guidance as to gaurantee that what I select "Order3" is going to correspond with the right ticket...?
 
DomGilberto:
Just looking for some guidance as to gaurantee that what I select "Order3" is going to correspond with the right ticket...?

Not sure but ticket numbers might be unique for the run, while position numbers are not:

for(int b=OrdersTotal()-1; b>=0; b--)
     {
     if( OrderSelect(b, SELECT_BY_TICKET)==True )
 

Your code (and or the program-logic) is wrong :

SelectOrder == OrderTicket() // this only accidentally true for the first 4 orders in the tester!
...
//this is completely wrong!!
OrderClose( SelectOrder, OrderLots(), Bid, 5, CLR_NONE );

OrderTicket is an Integer: -2 147 483 648 .. is 2 147 483 647!!

SelectOrder an enum between 0..4

Where have you saved the ticket-number of OrderSend()?

The ticket is a 'reference' of you broker! I think you should use the Magic number (this is 'your reference') for what I think you want to achieve.

 
Thanks for you reply. Ordersend I've stored globally under the variable "BuyTicketOrder1" and so forth?

Ahhhhh I seeeeee! Well, can you see what I was trying to do though?

I was under the impression that SelectOrder would "store" the corresponding variable "BuyTicketOrderx" with "Orderx" but it just holds 0 through 4.

I am wanting a way that when I select externally the order ticket number, in that code above, it will use that ticket number within the function and close just that ticket number? Hope that makes sense?

MagicNumber would work but it's a ball-ache for now change a lot of my code when I am convinced I can do it this way^?
Reason: