Download MetaTrader 5

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

To add comments, please log in or register
Mimi
507
Mimi  

Hi, I made an Ea, but when I do back testing its creating many same pending orders on each tick. How to stop that. I just need one order at a time, i don't need its copy.

if I run this EA on hourly chart with "open price only" then it doesn't create copy.

Plus my ea has option for reversal trade if buy limit fails then it opens sell trade. Please also let me know that i set up it properly or not. Any help will be appreciated. 

Thank you for your time. 

// Main function//
if(condition)
   {
     if( condition )
      {
      if ( BuyTicket == 0)
      BuyCall(S0);
      }    
     if( condition)
      {
      if ( BuyTicket == 0)
      BuyCall(S1);
      }  

   etc etc... // many conditions
}

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

   // reverse trade//
     if( Bid < BuyStopLoss && SellTicket == 0 )
     {  
        SellStopLoss = Bid + (StopLoss * CalcPoint1);
        SellTakeProfit = Bid - (TakeProfit * CalcPoint1);
        SellTicket = OrderSend(Symbol(),OP_SELL,LotSize,Bid,UseSlippage,SellStopLoss,SellTakeProfit,"Sell Order",MagicNumber,0,Red);
        BuyTicket = 0;
     }    
   }
      
return(BuyTicket);
}
whroeder1
15910
whroeder1  
cashcube: How to stop that.
How do you think? You check for an already exiting one before opening a new one.
Mimi
507
Mimi  
WHRoeder:
cashcube: How to stop that.
How do you think? You check for an already exiting one before opening a new one.

Then what to do? Remove the buyticket == 0 & sellticket == 0 code?
whroeder1
15910
whroeder1  
EA's must be coded to recover. If the power fails, OS crashes, terminal or chart is accidentally closed, on the next tick, any static/global ticket variables will have been lost. You will have an open order but don't know it, so the EA will never try to close it, trail SL, etc. How are you going to recover? Use a OrderSelect loop to recover, or persistent storage (GV/file) of ticket numbers required.
Mimi
507
Mimi  
WHRoeder:
EA's must be coded to recover. If the power fails, OS crashes, terminal or chart is accidentally closed, on the next tick, any static/global ticket variables will have been lost. You will have an open order but don't know it, so the EA will never try to close it, trail SL, etc. How are you going to recover? Use a OrderSelect loop to recover, or persistent storage (GV/file) of ticket numbers required.

Hi, I added it change the code in following way.

//---------------------- Buy/Sell function (limit orders)
int BuyCall( double BC)
{  
  if(!OrderSelect(BuyTicket, SELECT_BY_TICKET)==true)
    {      
  if ( BuyTicket == 0)
  {
   BuyStopLoss = BC - (StopLoss * CalcPoint1);
   BuyTakeProfit = BC + (TakeProfit *  CalcPoint1);
   BuyTicket = OrderSend(Symbol(),OP_BUYLIMIT,LotSize,BC,UseSlippage,BuyStopLoss,BuyTakeProfit,"Buy limit Order",MagicNumber,expiration,Green);
   SellTicket = 0;
   // counter trade//
     if( Bid < BuyStopLoss && SellTicket == 0 )
     {  
        if(!OrderSelect(SellTicket, SELECT_BY_TICKET) == true)
        {
        SellStopLoss = Bid + (StopLoss * CalcPoint1);
        SellTakeProfit = Bid - (TakeProfit * CalcPoint1);
        SellTicket = OrderSend(Symbol(),OP_SELL,LotSize,Bid,UseSlippage,SellStopLoss,SellTakeProfit,"Sell Order",MagicNumber,0,Red);
        BuyTicket = 0;
        }
     }    
  }
 }

It still taking same orders multiple times with each tick.

Plus if I remove the "!" sign, then its not taking any trades. 

Last do i have to remove the buyticket & sellticket checking from my main function? 

Thank you 

whroeder1
15910
whroeder1  
if(!OrderSelect(BuyTicket, SELECT_BY_TICKET)==true)
  1. Don't paste code
    Play video
    Please edit your post.
    For large amounts of code, attach it.

  2. You would never write if( (2+2 == 4) == true) would you? if(2+2 == 4) is sufficient. So Don't write if(bool == true), just use if(bool) or if(!bool). Code becomes self documenting when you use meaningful variable names, like bool isLongEnabled. Long_Entry sounds like a trigger price or a ticket number and "if long entry" is an incomplete sentence.
  3. Where do you check if BuyTicket has closed? Selecting by ticket will always be true (given a valid ticket number,) the order could be any of pending, open, closed, or deleted.
  4. You completely ignored my suggestion of an OrderSelect loop.
Mimi
507
Mimi  

Hello WHReder thank you for your reply.

Yes I added those on SRC code. But it didn't showed when I edited.

I removed == true terms as you suggested. 

Please don't get me wrong, i have a weak point understanding this orderselect loop. I didn't ignored. I tried adding it, its too complex though, running it shows  16 errors & 2 warnings.  In that code what is MN.Count?

// extern int     Magic.Number.Base          = ...
// int      magic.number.max;                   // Export to OpenOrder/MySelect
// string   market.pair;                        // Export to OpenOrder/MySelect
// int init(){
//    market.pair       = Symbol();
//    magic.number.max  = Magic.Number.Base + MN.COUNT - 1;

 

About checking buy ticket closed. I didn't checked it because, i checked if current price is below the buystoploss then buy trade got closed. so opened sell order

 Bid < BuyStopLoss 

Regards

Mimi
507
Mimi  

I arrange the code some how like this...But showing 2 error now.."Myselect" & "MyOrdersTotal" can be decleared only in global scope.

int BuyCall( double BC)
{  
//-----------code
bool MySelect(int iWhat, int eSelect, int ePool=MODE_TRADES){
   if(!OrderSelect(iWhat, eSelect, ePool) )  return (false);
   int      mn = OrderMagicNumber();
   if(mn < MNB1              )  return (false);
   if(mn > MNB2               )  return (false);
   if(OrderSymbol()      != Pair   )  return (false);
   if(ePool != MODE_HISTORY               )  return (true);
   return(OrderType() <= OP_SELL); 
                                   
                                   
}
int MyOrdersTotal(int op=-1, int ePool=MODE_TRADES){   #define OP_ALL -1
   if(ePool == MODE_TRADES)            iPos = OrdersTotal()        - 1;
   else                                iPos = OrdersHistoryTotal() - 1;
   for(nOrders=0; iPos >= 0; iPos--) if(
      MySelect(iPos, SELECT_BY_POS, ePool)) if(
      op == OP_ALL || op == OrderType()
   )  nOrders++;
   return(nOrders);
}

  if(!OrderSelect(BuyTicket, SELECT_BY_TICKET))
    {      
  if ( BuyTicket == 0)
  {
   BuyStopLoss = BC - (StopLoss * CalcPoint1);
   BuyTakeProfit = BC + (TakeProfit *  CalcPoint1);
   BuyTicket = OrderSend(Symbol(),OP_BUYLIMIT,LotSize,BC,UseSlippage,BuyStopLoss,BuyTakeProfit,"Buy limit Order",MagicNumber,expiration,Green);
   SellTicket = 0;
   // counter trade//
     if( Bid < BuyStopLoss && SellTicket == 0 )
     {  
        if(!OrderSelect(SellTicket, SELECT_BY_TICKET))
        {
        SellStopLoss = Bid + (StopLoss * CalcPoint1);
        SellTakeProfit = Bid - (TakeProfit * CalcPoint1);
        SellTicket = OrderSend(Symbol(),OP_SELL,LotSize,Bid,UseSlippage,SellStopLoss,SellTakeProfit,"Sell Order",MagicNumber,0,Red);
        BuyTicket = 0;
        }
     }    
  }
 }
      
return(BuyTicket);
}
Keith Watford
Moderator
10652
Keith Watford  
You cannot declare a function inside another function
Mimi
507
Mimi  
GumRai:
You cannot declare a function inside another function

Then how can I do this. I mean I have many conditions to check individually to open a buy/sell order.

That's why I made a buycall/sellcall function to make the code easier. 

Its just EA is taking so many same trades. Don't know how to stop that.

Keith Watford
Moderator
10652
Keith Watford  
GumRai:
You cannot declare a function inside another function

cashcube:

Then how can I do this. I mean I have many conditions to check individually to open a buy/sell order.

That's why I made a buycall/sellcall function to make the code easier. 

Its just EA is taking so many same trades. Don't know how to stop that.

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. 

To add comments, please log in or register