Ea is creating multiple same pending/limit orders [Help pls] - page 3

 
GumRai:

We don't know what you are trying to do

If you only want one open trade at a time, check that there are no open orders before sending a new one.

If you only want one trade per bar, only test once per bar

If you want a combination of conditions, test the combination. 

I explain here:

I have maximum 6 support & 6 resistance for each hour. It can be 2 support & 2 resistance or anything but maximum 6 support & 6 resistance for each hours.

I want to open limit order those S/R levels on start of the each hour. 

I created one buy function & one sell function...so that I dont need to write down the same buy/sell code for total 12 S/R levels. 

Now. when I used this following code:

int SellCall(double SC)
{
   SellStopLoss = SC + (StopLoss * CalcPoint1);
   SellTakeProfit = SC - (TakeProfit * CalcPoint1);
   SellTicket = OrderSend(Symbol(),OP_SELLLIMIT,LotSize,SC,UseSlippage,SellStopLoss,SellTakeProfit,"Sell Limit Order",MagicNumber,TimeCurrent()+3540,Red);

   return(0);
}

 It creates multiple same orders:

 I used WHRoeder code too. In a seperate function. As i can't declare function within a function. 

 

Your problem is not the function that you have posted, your problem is in the code that calls the function.

Obviously there is no check to see if the orders have already been placed

 
GumRai:

Your problem is not the function that you have posted, your problem is in the code that calls the function.

Obviously there is no check to see if the orders have already been placed

code for calling function (snapshot)

 if(FS == 1 || FS == 5 || FS == 7)
   {
     if( P1 == 1 || P1 ==5 || P1 ==7)
      {
      
      BuyCall(S0);
      }    
     if( P2 == 1 || P2 ==5 || P2 ==7)
      {
      
      BuyCall(S1);
      }  
     if( P3 == 1 || P3 ==5 || P3 ==7)
      {
  
      BuyCall(S2);
      }  
     if( P4 == 1 || P4 ==5 || P4 ==7)
      {
      
      BuyCall(S3);
      }      
          
     if( P5 == 1 || P5 ==5 || P5 ==7)
      {
     
      BuyCall(S4);
      }      
     if( P6 == 1 || P6 ==5 || P6 ==7)
      {
     
      BuyCall(S5);
      }  
     if( P7 == 1 || P7 ==5 || P7 ==7)
      {
      
      SellCall(R0);
      } 
     
      if( P8 == 1 || P8==5 || P8 ==7)
      {
    
      SellCall(R1);
      }  
      
     if( P9 == 1 || P9==5 || P9 ==7)
      {
      
      SellCall(R2);
      }          
          
    if( P10 == 1 || P10==5 || P10 ==7)
      {
     
      SellCall(R3);
      }   
    if( P11 == 1 || P11==5 || P11 ==7)
      {
     
      SellCall(R4);
      }                       
    if( P12 == 1 || P12==5 || P12 ==7)
      {
       
      SellCall(R5);
      }            
          
    }
   

 How to check that orders have been already placed or not? With buyticket > 0 conditions?

 
cashcube:

code for calling function (snapshot)

 How to check that orders have been already placed or not? With buyticket > 0 conditions?

It is difficult to give advice as I don't know what you are doing at the end of the hour with un-triggered orders and how you are managing triggered orders.

You could create a globally declared array (or 2) and store the ticket numbers for the open orders. Before opening a new order, check the array element that corresponds to the level for a value >0.

Of course, you will also need to check the ticket numbers and if you delete un-triggered orders, set the array element to 0. You may also need to check if the order has closed and depending on your logic, re-set it to 0 

 
GumRai:

It is difficult to give advice as I don't know what you are doing at the end of the hour with un-triggered orders and how you are managing triggered orders.

You could create a globally declared array (or 2) and store the ticket numbers for the open orders. Before opening a new order, check the array element that corresponds to the level for a value >0.

Of course, you will also need to check the ticket numbers and if you delete un-triggered orders, set the array element to 0. You may also need to check if the order has closed and depending on your logic, re-set it to 0 

As I set expiry levels of Un-triggered orders, it get expired at 0:59 or 59th minute. For Triggered order SL & TP are set.

Previously I designed Ea which takes one either buy or sell order in each hour.. no problem code was simple. But here it seems very difficult.

Ok, i will try to code it as you said. If problem arise I will post here. Plus it will be good if you can share any simple code example for counting ticket number.

Thank you for your suggestion.

 

I solved my problem temporarily with this following code & running it on Hourly chart. Simple  

 //--- go trading only for first tiks of new bar
   if(Volume[0]>1) return(0);
 
Bars is unreliable (a refresh/reconnect can change number of bars on chart) volume is unreliable (miss ticks) Always use time. New candle - MQL4 forum
 
WHRoeder:
Bars is unreliable (a refresh/reconnect can change number of bars on chart) volume is unreliable (miss ticks) Always use time. New candle - MQL4 forum

Thank you for the code. I added it on Tick function. But now my EA is not taking any trades. As I use buycall/sell call function for placing pending orders. I couldn't add this function inside the function.

//---------------------- Buy/Sell function (limit orders)
int BuyCall( double BC)
{     
              BuyStopLoss = BC - (StopLoss * CalcPoint1);
              BuyTakeProfit = BC + (TakeProfit *  CalcPoint1);
              BuyTicket = OrderSend(Symbol(),OP_BUYLIMIT,LotSize,BC,UseSlippage,BuyStopLoss,BuyTakeProfit,"Buy limit Order",MagicNumber,TimeCurrent()+3540,Green);
         
return(0);
}


int SellCall(double SC)
{ 
   SellStopLoss = SC + (StopLoss * CalcPoint1);
   SellTakeProfit = SC - (TakeProfit * CalcPoint1);
   SellTicket = OrderSend(Symbol(),OP_SELLLIMIT,LotSize,SC,UseSlippage,SellStopLoss,SellTakeProfit,"Sell Limit Order",MagicNumber,TimeCurrent()+3540,Red);

   return(0);
}
       
//+------------------------------------------------------------------+
//| OnTick function                                                  |
//+------------------------------------------------------------------+
void OnTick(){
   static datetime timeCur; datetime timePre = timeCur; timeCur=Time[0];
   bool isNewBar = timeCur != timePre;
   if(isNewBar){ 
     return; // Once per bar
   }
   return; // every tick
}  

 Any Idea?

 

Plus the reverse trade on stopped orders is also not working.

//-------------Reverse trade for buy
     for(xx =OrdersHistoryTotal()-1;xx >=0;xx --)
     {
         if(OrderSelect(xx, SELECT_BY_POS,MODE_HISTORY))
         {
         if(OrderSymbol()==Symbol() && OrderMagicNumber()== MagicNumber)
           {
          //for buy order reverse
          if(OrderType()==OP_BUY && OrderProfit()<0) // if last buy closed with loss
          { 
            //--- go trading only for first tiks of new bar
            if(Volume[0]>1) return(0);
            SellStopLoss = Bid + (StopLoss * CalcPoint1);
            SellTakeProfit = Bid - (TakeProfit * CalcPoint1);
            SellTicket = OrderSend(Symbol(),OP_SELL,LotSize,Bid,UseSlippage,SellStopLoss,SellTakeProfit,"Sell Reverse Order",MagicNumber,0,Red);
          }
         break; 
          }
          }
     } 
 

Any highlight on the reverse code? why its not working?

Thank you.