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

 
GumRai:

First of all don't try to declare a function inside another function.

I am unable to work out what you are trying to do by looking at the code that you have posted. So I can't help you. 

 

Thank you GumRai for your prompt reply.  My program has many conditions like following. I checked upto P12. Its like a pivot. If conditions satisfies, then it opens limit trades on those levels P1, P2,....as its number is 12. I didn't want to write up buy/sell code on every instance. That's why made separate buycall/sellcall functions. Below my old code for sell.

     if( P1 == 1 || P1 ==5 || P1 ==7)
      {
      if ( BuyTicket == 0)
      BuyCall(S0);
      }    
     if( P2 == 1 || P2 ==5 || P2 ==7)
      {
      if ( BuyTicket == 0)
      BuyCall(S1);
      }  
         .......................many more
 
int SellCall(double SC)
{

if(!OrderSelect(SellTicket, SELECT_BY_TICKET))
  {    
  if( SellTicket == 0)
   {
   SellStopLoss = SC + (StopLoss * CalcPoint1);
   SellTakeProfit = SC - (TakeProfit * CalcPoint1);
   SellTicket = OrderSend(Symbol(),OP_SELLLIMIT,LotSize,SC,UseSlippage,SellStopLoss,SellTakeProfit,"Sell Limit Order",MagicNumber,expiration,Red);
   BuyTicket = 0; 
   // counter tradee //
   if ( Ask > SellStopLoss && BuyTicket == 0)
   {   if(!OrderSelect(BuyTicket, SELECT_BY_TICKET))
       {
       BuyStopLoss = Ask - (StopLoss * CalcPoint1);
       BuyTakeProfit = Ask + (TakeProfit *  CalcPoint1);
       BuyTicket = OrderSend(Symbol(),OP_BUY,LotSize,Ask,UseSlippage,BuyStopLoss,BuyTakeProfit,"Buy Order",MagicNumber,0,Green);
       SellTicket = 0;
       }
   }
  } 
 }
   return(SellTicket);
}
 

This is the example. Same trade placing on each tick. I need to place this on each hour. As its an hourly pivot.

 
int SellCall(double SC)
{

if(!OrderSelect(SellTicket, SELECT_BY_TICKET))
  {    
  if( SellTicket == 0)
   {
   SellStopLoss = SC + (StopLoss * CalcPoint1);
   SellTakeProfit = SC - (TakeProfit * CalcPoint1);
   SellTicket = OrderSend(Symbol(),OP_SELLLIMIT,LotSize,SC,UseSlippage,SellStopLoss,SellTakeProfit,"Sell Limit Order",MagicNumber,expiration,Red);
   BuyTicket = 0; 
   // counter tradee //
   if ( Ask > SellStopLoss && BuyTicket == 0)
   {   if(!OrderSelect(BuyTicket, SELECT_BY_TICKET))
       {
       BuyStopLoss = Ask - (StopLoss * CalcPoint1);
       BuyTakeProfit = Ask + (TakeProfit *  CalcPoint1);
       BuyTicket = OrderSend(Symbol(),OP_BUY,LotSize,Ask,UseSlippage,BuyStopLoss,BuyTakeProfit,"Buy Order",MagicNumber,0,Green);
       SellTicket = 0;
       }
   }
  } 
 }
   return(SellTicket);
}

 When you open a sell limit, you set BuyTicket to 0.

Next you check if BuyTicket==0, of course it does, you've just set it.

Then you set SellTicket to 0.

Next tick, Sellticket==0 so you open a sell limit and set BuyTicket to 0 again.

and so on and so on............ 

 
GumRai:

 When you open a sell limit, you set BuyTicket to 0.

Next you check if BuyTicket==0, of course it does, you've just set it.

Then you set SellTicket to 0.

Next tick, Sellticket==0 so you open a sell limit and set BuyTicket to 0 again.

and so on and so on............ 

 

Then should I remove that two lines you highlighted. I did that. Now showing just one hour result for 2 months data back testing. 

Another thing I notice, in my sellcall, I have a reverse trade on buy. If sell trade get stopped out then buy trade will open. Now for sellcall function it returns(Selltickets) so is it ok?

Thank you 

 

int SellCall(double SC)
{

if(!OrderSelect(SellTicket, SELECT_BY_TICKET))
  {    
   if( SellTicket == 0)
   {
   SellStopLoss = SC + (StopLoss * CalcPoint1);
   SellTakeProfit = SC - (TakeProfit * CalcPoint1);
   SellTicket = OrderSend(Symbol(),OP_SELLLIMIT,LotSize,SC,UseSlippage,SellStopLoss,SellTakeProfit,"Sell Limit Order",MagicNumber,0,Red);
   
   // reverse tradee //
   if ( Ask > SellStopLoss && BuyTicket == 0)
   {   if(!OrderSelect(BuyTicket, SELECT_BY_TICKET))
       {
       BuyStopLoss = Ask - (StopLoss * CalcPoint1);
       BuyTakeProfit = Ask + (TakeProfit *  CalcPoint1);
       BuyTicket = OrderSend(Symbol(),OP_BUY,LotSize,Ask,UseSlippage,BuyStopLoss,BuyTakeProfit,"Buy Order",MagicNumber,0,Green);
       
       }
   }
 } 
 }
   return(SellTicket);

 I understand now in this code, first it checks sellticket == 0 yes obvious, then placed sell order, next buyticket == 0 yes, next places buy order...but when return...again check sellticket == 0 , no its not now...so that's why Ea stopped after taking one set. But how to remove this problem. In my EA inactive pending orders get deleted on each 59 minutes from start hour. I set it in that way.

 

Now I changed the code in following way, it reading for all support & resistance.

I have maximum 6 support & 6 resistance for an hour. Among those 6 meet the conditions, then it opens limits orders on that 6. If 1 meet then opens limits orders for that one. This is the algo.

So I set the code in following way for function call.

int BuyCall( double BC)
{  
      
if ( BuyTicket >= 0 && BuyTicket <= 5)
  {
   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)
{
  
 if( SellTicket >= 0 && SellTicket <= 5)
   {
   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);
}

 Now it runs for one hour & checks for 6 levels for each buy/sell. But runs for only a hour. What to do here to run it perfectly for full data set. 

 
if ( BuyTicket >= 0 && BuyTicket <= 5)

This will only work in the strategy tester and will do nothing more once the first 6 orders are opened.

You cannot use a ticket number for a test in this way 

 
GumRai:

This will only work in the strategy tester and will do nothing more once the first 6 orders are opened.

You cannot use a ticket number for a test in this way 

Thank you for your reply. 

Then what should I do. If I remove the BuyTicket =>0 or any BuyTicket conditions. Then EA is taking many same trades with every tick.....Giving me ordersend error of 148.

 
cashcube: Then what should I do.
Previously answered.
 
cashcube:

Thank you for your reply. 

Then what should I do. If I remove the BuyTicket =>0 or any BuyTicket conditions. Then EA is taking many same trades with every tick.....Giving me ordersend error of 148.

 

 

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. 

Reason: