Ordersend related problem of unwanted duplicate order? - page 2

 
// External Double variable
double EquityPerLot=2000;    //How much equity do I want per 0.1 Lots



// Call function in EA
MyNumOfLots=NumOfLots(EquityPerLot);      // Calculates No. of lots based on Equity


//----------------------------------------------- MY FUNCTION # 7 ------------------------------------------------------------
//------------------------------------ Calculates No. of lots based on Equity ------------------------------------------------
double NumOfLots(double Equity)
   {//
    double Real=(AccountEquity( )/Equity);  //
    double Round=MathRound(Real);   // rounds off answer
    double iNumOfLot=(Round*0.1);   // multilplies answer by 0.1 lots
    return(iNumOfLot);
   }//

Hi V,

Above should be clearer now,

I specify $2,000 per 0.1 lot with an external double variable...

Regards, M

 
Error:


return(1);   // Return(1) will make program retry sending the order it was trying


My instinct is telling me the problem is with the error block function or specifically what you do with the output... I think to confirm, try commenting out the function call and just put an alert with an error code and see if that solves the problem... when you return 1 what happens next? the comments suggest you go on to re send the order somewhere. I would also make sure to call get last error as a variable directly after the order send just to make sure we are picking up the correct order... I admit I am perplexed though as if the oder is succesful it should skip the else part... maybe try this and see how it works for you...

MyNumOfLots=NumOfLots(EquityPerLot);      // Calculates No. of lots based on Equity
   ticketlong=OrderSend(Symbol(),OP_BUY,MyNumOfLots,Ask,slippage*Point,0,0,"My buy order",magic,0,Green); // Open order...
   err=GetLastError();
   if (err!=0)
      {
      Error_Long=ErrorBlock(err);
      }
   else
      {
      Alert(Symbol(),": LONG opened:",Ask,", Ticket:",ticketlong);
      }
   
 
Viffer:


My instinct is telling me the problem is with the error block function or specifically what you do with the output... I think to confirm, try commenting out the function call and just put an alert with an error code and see if that solves the problem... when you return 1 what happens next? the comments suggest you go on to re send the order somewhere. I would also make sure to call get last error as a variable directly after the order send just to make sure we are picking up the correct order... I admit I am perplexed though as if the oder is succesful it should skip the else part... maybe try this and see how it works for you...


Hi V,

Thanks once again for your time and input,

The code below is what I have at the moment and I ran it overnight and for the day and it seems to be working now, I havent had any "alien" orders since my adjustements.... holding thumbs

You'll notice that my main change was the longcount & ticketlong condition at the start of the loop.

I have noted your comments on using the if function on the error loop, as well as your earlier comments on the sleep function after ordersend not being necessary, I'll save a revision and try those suggestions.

Regards, Mike

//--------------------------------- Function: Count Orders opened by this EA --------------------------------------------
   longcount=0;
   shortcount=0;
   for(int i=OrdersTotal()-1; i>=0; i--)                                                
      {//
       if (OrderSelect(i,SELECT_BY_POS)==true)                           
         {//                  
                                               
          type=OrderType();    // Short = 1, Long = 0, etc... https://docs.mql4.com/constants/trading
          if (OrderSymbol()!=Symbol()||type>1 || OrderMagicNumber()!=magic) // filters out pending orders and orders from other EA's
          // if Symbol of selected order != Symbol of chart OR there are pending orders OR magic numbers dont match
            {//
             continue;
            }//                        
         if (type==0)
            {//
             longcount++;
            }//
         if (type==1)
            {//
             shortcount++;
            }// 
         }// 
      }// 
//<><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><> 
     
    MovingAve=iMA(NULL,0,PeriodMA,0,0,0,0);   //Calculate the MA Value...
    
//<><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><>    
//------------------------------------------------- LONG LOOP ---------------------------------------------------
    if(longcount==0&&ticketlong==-1)      // If I'm not already long...
      {//
       if (Bid>MovingAve||Error_Long==1)     // If Price is above Moving average & I'm not already long...
                                                          // OR I have a LONG position i am battling to open  
         {//
          CandleHighE=Fractal_High(NumofBarsE,Shift1);     // What is the Candle Fractal High value for entry...          
          if (Bid>CandleHighE||Error_Long==1)  // Price has broken through Candle Fractal High...
                                            // OR I have a LONG position i am battling to open 
            {//
             MyNumOfLots=NumOfLots(EquityPerLot);      // Calculates No. of lots based on Equity
             ticketlong=OrderSend(Symbol(),OP_BUY,MyNumOfLots,Ask,slippage*Point,0,0,"My buy order",magic,0,Green); // Open order...
             err=GetLastError();
             Sleep(sleeptime);  // 
             if(ticketlong>-1)   // If order was opened...         
               {//
                if(OrderSelect(ticketlong,SELECT_BY_TICKET,MODE_TRADES)) 
                Alert(Symbol(),": LONG opened:",OrderOpenPrice(),", Ticket:",ticketlong);
                longclosed=false;   // Zero variables...
                Error_Long=0;       // Zero variables...        
               }// 
             else    // Order didnt open... why? & try fix...
             Error_Long=ErrorBlock(err);
            }//
         }// 
      }//   
Reason: