Help needed for adding positions during retracement

 

Hello

  //+------------------------------------------------------------------+
//| OPEN INITIAL POSITIONS                                                                 |
//+------------------------------------------------------------------+
   if(InitOrder==buy)
     {
      if(!TotalOpenBuyOrders() && !TotalOpenSellOrders())                  //
         if(If_Closed_Continue)                                            //
            EnterTrade(OP_BUY);                                            //
     }                                                                     //   THIS WORKS OK FOR OPENING INITIAL POSITION WHEN STARTING EA..
   else if(!TotalOpenBuyOrders() && !TotalOpenSellOrders())                //
   if(If_Closed_Continue)                                                  //
      EnterTrade(OP_SELL);                                                 //
  }
//+------------------------------------------------------------------+
//|  ADD POSITIONS ON RETRACEMENT                                                                |
//+------------------------------------------------------------------+
void CheckForSignal()

  {
   double retracement=Ask+risk*pips;
   Comment("retracement is ",(string)retracement+"\nTotal Orders....: ",(string)OrdersTotal()+"\nOrderType is ",(string)OrderType()+"\nprice ",OrderOpenPrice());

   if(OrdersTotal()==1)
     {
      if(OrderType()==OP_BUY)                      //
         if(OrderOpenPrice()>retracement)          //  THIS SEEMS TO WORK OK FOR FIRST RETRACEMENT POSITION
            EnterTrade(OP_BUY);                    //
     }

   else if(OrdersTotal()==2)                       //
     {                                             //
      if(OrderType()==OP_BUY)                      // THIS DOESNT WANT TO ADD ADDITIONAL POSITION .. :(
         if(OrderOpenPrice()>retracement*2)        // (I WANT CODE TO BE ABLE TO KEEP ADDING AS PRICE RETRACES FURTHER BY MULTIPLES OF 'R')
            EnterTrade(OP_BUY);                    // Thought I would be able to copy and paste similar code with retracement*3 etc to add positions

     }
   if(OrderSelect(0,SELECT_BY_POS)==true)                            //
      Print("open price for the order 1 ",OrderOpenPrice());         //      THIS IS MY CHECK , NOT NECESSARY FOR ABOVE
   else                                                              //
      Print("OrderSelect returned the error of ",GetLastError());    //

 
  }

Please can someone help me with my code..

I am very new as you will notice with my question so please go easy on me...

I am slowly writing an EA to open a market order when i place the EA on the chart (have managed this :)) then as price retraces 'R' pips the EA will add another position (have managed this..:)) but my problem is my code does not add any more positions, only initial and second position. 

I have attached the part of the code in question ..

Hope some can steer me in right direction,

 

Cheers

Craig 

 
craigt: my problem is my code does not add any more positions, only initial and second position.
  1. else if(OrdersTotal()==2)
    That's what you wrote.
  2. if(OrderSelect(0,SELECT_BY_POS)==true
    Using OrdersTotal directly and/or a specific position index, means your EA in incompatible with every other, including itself on other charts and manual trading. No filtering on your OrderSelect loop means your code is incompatible with every EA (including itself on other charts and manual trading.) Symbol Doesn't equal Ordersymbol when another currency is added to another seperate chart . - MQL4 forum
  3. You call OrderType but haven't selected a order.
 

Hi 

I understand that using OrdersTotal directly could cause problems, I will try and keep the format correct.

I have managed to get the EA working placing initial market order and subsequent retracement orders using OrdersTotal as shown (which I believe could cause problems), how would you suggest using OrdersTotal in this situation?

Cheers

     
    
      if(OrdersTotal()==1){
         if(OrderOpenPrice() > buyRetracement) 
      EnterTrade(OP_BUY);  
      }      
      else if(OrdersTotal()==2){    
              if(OrderOpenPrice() > buyRetracement2 ) 
      EnterTrade(OP_BUY);  
      } 
      else if(OrdersTotal()==3){    
              if(OrderOpenPrice() > buyRetracement3 ) 
      EnterTrade(OP_BUY);  
      }
      else if(OrdersTotal()==4){    
              if(OrderOpenPrice() > buyRetracement4 ) 
      EnterTrade(OP_BUY);  
      }   
 

Please can someone tell me how I could put this above into a loop so i dont use (ordersTotal()== , as i believe it is incorrect, bad coding. i have tried but failed as it keeps adding multiple trades on first new order, my attempt shown below, all im trying to do is open additional BUY orders when the price moves "retracementRisk" away from the OrderOpenPrice of each new order..

hope someone can assist

cheers Craig

double retracementRisk = risk*pips;
 int total=OrdersTotal();
 for(int i = total-1; i >= 0; i--)
 {
   Comment("retracementtimes risk is: ",(string)retracementRisk+"\ntotal orders: ",(string)OrdersTotal()+"\nPips: ",(string)pips+"\nopen price : ",(string)OrderOpenPrice()+"\nAsk :" ,Ask);

   if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
   {
      if(OrderMagicNumber()==magic)
        if(OrderSymbol() != Symbol())continue;
          if(OrderType()==OP_BUY)
            if(OrderOpenPrice() -Ask > retracementRisk)
            {
               if(OrderOpenPrice()>Ask)
                  EnterTrade(OP_BUY);
            }
               
 
         //else if(OrderType()==OP_SELL)
         //{
         //   if(Bid-OrderOpenPrice() > retracementRisk*pips)
         //      if(OrderOpenPrice()<Bid)
         //         EnterTrade(OP_SELL);
         //}
   }
 
   double retracementRisk=risk*pips;
   int total=OrdersTotal();
   double lowest_buy_entry=0;
   double highest_sell_entry=0;
   
   for(int i=total-1; i>=0; i--)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
        {
         if(OrderMagicNumber()==magic)
            if(OrderSymbol()!=Symbol())continue;
         if(OrderType()==OP_BUY)
           {
            if(lowest_buy_entry==0 || OrderOpenPrice()<lowest_buy_entry)
               lowest_buy_entry=OrderOpenPrice();
           }
         else if(OrderType()==OP_SELL)
           {
            if(OrderOpenPrice()>highest_sell_entry)
               highest_sell_entry=OrderOpenPrice();
           }
        }
     }
   if(lowest_buy_entry -Ask>=retracementRisk)
     {
      EnterTrade(OP_BUY);
     }

   if(highest_sell_entry!=0 && Bid-highest_sell_entry>=retracementRisk)
     {
      EnterTrade(OP_SELL);
     }

Not compiled or tested

 

Thank you GumRai

Ill study what you have written, try and understand step by step.

Appreciate your time to help

Cheers Craig

Reason: