Multiple Pairs messing up my for loop again! - page 3

 
But my EA will only ever produce one order per market at any one time. It will never have two orders open at any one time on any one market...

I appreciate that you're being really patient with me, so I want to say thanks in before I piss you off :P

I've stripped this down for simplicity... Assuming what I've told you with regards to the fact that I only have one order open at any one time per market; where I've put the breaks, would this work in accordance with keeping the loops working on the correct order... or am I just not getting it...

//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+

int start()
 {

for(PositionIndex = TotalNumberOfOrders - 1; PositionIndex >= 0 ; PositionIndex --) 
  {  
   if( ! OrderSelect(PositionIndex, SELECT_BY_POS, MODE_TRADES) ) continue;
    if( OrderMagicNumber() == MagicNumber  
      && OrderSymbol() == Symbol() )                 
         {
            if( OrderType()==OP_SELL )
               {
               MA_Trail(); // < -- Once this is complete, the break will return it to here with the same order selected from the above loop?
               CloseHalfOrder1(); // < -- Once this is complete, the break will return it to here with the same order selected from the above loop?
               
               double Points_To_Deduct = (( OrderOpenPrice()-OrderTakeProfit()) / RewardRatio ); // < -- This will then complete? And so forth...
               FirstTarget_Sell = OrderOpenPrice()-Points_To_Deduct;
               double Points_To_Deduct2 = (( OrderOpenPrice()-OrderTakeProfit()) / ( RewardRatio / 2 ));
               TwoRatio_Sell_Target = OrderOpenPrice()-Points_To_Deduct2;

...


  
void MA_Trail()

{

   for(int b=OrdersTotal()-1; b>=0; b--)
     {
      if(OrderSelect(b,SELECT_BY_POS,MODE_TRADES))
         if(OrderMagicNumber()==MagicNumber)
            if(OrderSymbol() == Symbol())
               {

               if(OrderType()==OP_BUY)
                  {
                     bool BuyModify = OrderModify(OrderTicket(),OrderOpenPrice(),BuyStopPrice,OrderTakeProfit(),0,CLR_NONE);
                     
                     break; // < -- The loop will terminate and return to the first loop it was called from above!
                   }

...


void CloseHalfOrder()
{   
   
   int PositionIndex;    
   int TotalNumberOfOrders;   
   TotalNumberOfOrders = OrdersTotal();   
   
for(PositionIndex = TotalNumberOfOrders - 1; PositionIndex >= 0 ; PositionIndex --) 
    {
     bool CurrentOrder = OrderSelect(PositionIndex,SELECT_BY_POS,MODE_TRADES);
      if( !CurrentOrder ) continue;  
       if(OrderMagicNumber()==MagicNumber)
        if(OrderSymbol() == Symbol())
        { 
         if( CurrentOrder == True)  
            {
               double half_1st =MathFloor(OrderLots()/First_Target/lotStep)*lotStep;
               double half_2nd =MathFloor(OrderLots()/EMA_Target/lotStep)*lotStep;
               double Target_2 =MathFloor(OrderLots()/Second_Target/lotStep)*lotStep;
               FirstTarget_Buy =OrderOpenPrice()+(( OrderTakeProfit()-OrderOpenPrice()) / RewardRatio );
               TwoRatio_Buy = OrderOpenPrice()+(( OrderTakeProfit()-OrderOpenPrice()) / ( RewardRatio / 2 )); 
            }
            
          //<<-- First Target Order Close function -->>\\  

          if( FetchDateOfFirstClose != OrderOpenTime() && OrderType()==OP_BUY && Bid - FirstTarget_Buy > - Point ) 
              
              {
               if( half_1st - minLot > - Point )
                  {   
                  
                  bool Close_Half_Order_Buy=OrderClose(OrderTicket(),half_1st,Bid,5,Blue);

                     break; // < -- The loop will terminate and return to the first loop it was called from above!
                  } 

...
 
DomGilberto:
But my EA will only ever produce one order per market at any one time. It will never have two orders open at any one time on any one market...

I appreciate that you're being really patient with me, so I want to say thanks in before I piss you off :P

I've stripped this down for simplicity... Assuming what I've told you with regards to the fact that I only have one order open at any one time per market; where I've put the breaks, would this work in accordance with keeping the loops working on the correct order... or am I just not getting it...

Personally I would just reword your 2 functions so that you pass them the ticket and they just operate on that one Order, it will also make them a little faster . . . alternatively you could just add a 2nd OrderSelect() . . .

double MinLots = MarketInfo( Symbol(), MODE_MINLOT );

for(PositionIndex = TotalNumberOfOrders - 1; PositionIndex >= 0 ; PositionIndex --) 
  {  
   if( ! OrderSelect(PositionIndex, SELECT_BY_POS, MODE_TRADES) ) continue;
    if( OrderMagicNumber() == MagicNumber  
      && OrderSymbol() == Symbol() )                
         {
            if(OrderType()==OP_SELL )
               {
               MA_Trail();
               CloseHalfOrder1(); 

               if( ! OrderSelect(PositionIndex, SELECT_BY_POS, MODE_TRADES) ) continue;   //  <---- added here . . .

               double Points_To_Deduct = (( OrderOpenPrice()-OrderTakeProfit()) / RewardRatio);
               FirstTarget_Sell = OrderOpenPrice()-Points_To_Deduct;
               double Points_To_Deduct2 = (( OrderOpenPrice()-OrderTakeProfit()) / ( RewardRatio / 2 ));
               TwoRatio_Sell_Target = OrderOpenPrice()-Points_To_Deduct2;


Currently you only have one order open for each symbol/magic number, by what about tomorrow, next week, next month, next year ? if you make your functions flexible and work for most foreseeable eventualities you won't be modifying them so often when your strategy changes.

 

Ah I see - thank you for helping me :)

I understand a little better, and you're definitely right with regards to being more prudent in the way I write my code in terms of evolution...

Where you have put that OrderSelect (second one), would I not also need to put:

if( OrderMagicNumber() == MagicNumber  
      && OrderSymbol() == Symbol() )

So that it knows which order to work on then? Or does it already know that, it's just reselecting it?

 
DomGilberto:


Where you have put that OrderSelect (second one), would I not also need to put:

So that it knows which order to work on then? Or does it already know that, it's just reselecting it?

No, you already selected it and checked that it matched your Symbol() and Magic Number, you don't need to do the check again . . . just the Order selection
 
            if(OrderType()==OP_SELL )
               {
               MA_Trail();        // Has its own OrderSelect loop
               CloseHalfOrder1(); // Has its own OrderSelect loop

               if( ! OrderSelect(PositionIndex, SELECT_BY_POS, MODE_TRADES) ) continue;   //  <---- added here . . .

This will not work. If CloseHalfOrder1() closes an order with a earlier position than PositionIndex, PositionIndex will now select a different order.

You could, save the ticket number, call the two functions and then reselect by ticket, not by position. But you still have to check if the ticket is now closed.

The real problem is MA_Trail and CloseHalfOrder1 doing an select loop. Why is it trailing all orders multiple times (you're already in a loop). MA_Trail should do the selected order only.

 
WHRoeder:

This will not work. If CloseHalfOrder1() closes an order with a earlier position than PositionIndex, PositionIndex will now select a different order.

Good point . . thats what I get for suggesting a hack !
 

Ah I see what you're saying WHRoeder - Do I even need a for loop within CloseHalfOrder1()?

What would be the easiest solution for the time being? I didnt think that putting the new OrderSelect in (shown in that piece of code above) wouldn't make sense, because if I've called the CloseHalfOrder1() it will have closed half of that order with OrderClose(), and presumably move position when it comes back round and does this new OrderSelect ?

This is becoming a little frustrating... I'm just looking for a quick easy fix for the time being, and then i'll develop it into something a little more efficient...

 

Btw, the bit after MA_Trail and CloseHalfOrder1() - just simply puts a line on the chart for me... so in terms of getting the wrong "line" or price, well, I'm not as stressed with this part I guess... I guess what is important, is when it actually goes to close the CloseHalfOrder1() void...

I guess in terms of what I was seeing in the Print's was a mix up of what the CloseHalfOrder1() will ACTUALLY be doing...

Afterall, MA_Trail() is called, then CloseHalfOrder1() - (which has it's own loop, finds the correct order, checks the exit price again, completes it and then returns) .... I'm sorry I did not make that clear at the beginning...

So with all that said it should look like this:

1st) As soon as SELL_STOP has triggered into a OP_SELL it will check MA_Trail()

2nd) It will then find it either needs to move stop or stay the same - it then comes back.

3rd) It then goes to CloseHalfOrder1() but finds out that price is no where near any of the CloseOrder targets set within that void - it then comes back.

4th) it then OrderSelects the correct order, Prints out the first and second targets, also wacks a line on the chart and that's it - they will never be called again.

Once CloseHalfOrder1() has been called to close the position, it will not do anything BENEATH that new OrderSelect line that RaptorUK has put in, as it has already completed it...

            if(OrderType()==OP_SELL )
               {
               MA_Trail();
               CloseHalfOrder1(); 
               
               if( ! OrderSelect(PositionIndex, SELECT_BY_POS, MODE_TRADES) ) continue;
               
               double Points_To_Deduct = (( OrderOpenPrice()-OrderTakeProfit()) / RewardRatio);
               FirstTarget_Sell = OrderOpenPrice()-Points_To_Deduct;
               double Points_To_Deduct2 = (( OrderOpenPrice()-OrderTakeProfit()) / ( RewardRatio / 2 ));
               TwoRatio_Sell_Target = OrderOpenPrice()-Points_To_Deduct2;
                         
               
               if(  OrderOpenTime() != Sell_Targets_Confirmed )
                  {    
                   Print(" First Target: ",DoubleToStr(FirstTarget_Sell,Digits), " On: ", Symbol());
                   Print(" Second Target: ",DoubleToStr(TwoRatio_Sell_Target,Digits), " On: ", Symbol());
                   Sell_Targets_Confirmed = OrderOpenTime();
                  }
               
               bool Short_Line = ObjectCreate("First Short Target", OBJ_HLINE, 0, 0, FirstTarget_Sell);
                  if( Short_Line == True)
                     {
                      bool Short_Line_Edit = ObjectSet("First Short Target", OBJPROP_STYLE, STYLE_DASHDOT);
                     }  

               bool Short_Two = ObjectCreate("Second Short Target", OBJ_HLINE, 0, 0, TwoRatio_Sell_Target);
                  if( Short_Two == True)
                     {
                      bool Short_Two_Edit = ObjectSet("Second Short Target", OBJPROP_STYLE, STYLE_DASHDOT);
                     }      
               }
 
Just want to say thank you for your kind contribution too! I would never be able to do any of this without the help of the members on this site!

Thanks :D
Reason: