Help for only once ordersend per bar! - page 2

 
onewithzachy:

seungbaek, if possible could you attach the whole EA, so we can figure it out an not just Danjp (Danjp probably maybe not here). kinda difficult to scroll up and down just to figure out the whole EA.

And can you elaborate/explain more on "Exit order", I'm not so sure what you want from that.

I'm not sure I can help you either. LOL.


Hi onewithzachy,

Thanks for your help and the below EA has been tested, and the result showed all of "Buy" order were executed correctly & properly without any problem on audusd 5min chart.

However the result also showed any single one of "Exit" order has NEVER been executed, i didn't have any close of the position by exit order from the test result.

Unfortunately I do not know what was wrong on exit order.

The exit order is very simple. If MA7 cross up MA11 close the short position, if MA7 cross down MA11 close the long position.

Could you plesae help me?

extern double Lots = 0.1;
extern int  MaxLot = 10;
extern int NextTrade = 12; //<---- number of bars to wait
extern int dif = 500;

void init()
   {return;}
int deinit()
   {return(0);}
int start()
{
   CheckBuy();
   return(0);
}

void CheckBuy()
{
  int  ticket; 
  
   double Ma7Current    = iMA(NULL,0,7,0,MODE_SMA,PRICE_CLOSE,0);   //<--- wild guess for testing.
   double Ma7Previous   = iMA(NULL,0,7,0,MODE_SMA,PRICE_CLOSE,1);   //<--- wild guess for testing.
   double Ma7Previous2  = iMA(NULL,0,7,0,MODE_SMA,PRICE_CLOSE,2);   //<--- wild guess for testing.

   double Ma11Current   = iMA(NULL,0,11,0,MODE_SMA,PRICE_CLOSE,0);  //<--- wild guess for testing.
   double Ma11Previous  = iMA(NULL,0,11,0,MODE_SMA,PRICE_CLOSE,1);  //<--- wild guess for testing.
   double Ma11Previous2 = iMA(NULL,0,11,0,MODE_SMA,PRICE_CLOSE,2);  //<--- wild guess for testing.

  
    if(NewBar())                                                                //<---- new bar 
        if(!MaxOpenOrders())                                                    //<---- maxlot ok 
            if(!(AccountFreeMargin()<(2000*Lots)))                              //<---- margin ok        
                if((Ma7Current>Ma11Current) &&  (Ma7Previous>Ma11Previous) &&   //<---- trade setup ok
                   (Ma7Previous2<Ma11Previous2) && (Ma7Current>Ma7Previous))            
                    if((OrdersTotal()<1) && OkToTradeBar())                     //<---- first order ok to trade
                    {
                        ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,30,0,0,"#1",16384,0,Green);  //<---- execute trade
                        if(ticket>0)
                            if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) 
                                Print("BUY order opened : ",OrderOpenPrice());                 //<---- order ok
                         else
                             Print("Error opening BUY order : ",GetLastError());               //<---- order failed
                     }         
                      else if((OkToTradeBar()) && (Bid<OrderOpenPrice()-dif*Point))            //<---- open new orders after n bars have passed 
                      {                                                                        //<---- and the price is LESS THEN the prev order
                          ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,30,0,0,"#2",16384,0,Green); 
                          if(ticket > 0)                                  
                              if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) 
                                  Print("BUY order opened : ",OrderOpenPrice());               //<---- order ok
                          else                   
                              Print("Error opening BUY order : ",GetLastError());              //<---- order failed    
                    }                     
                    return(0);                                                    //<--- n bars have not passes yet
                return(0);                                                        //<----setup rules not met        
            
    for(cnt=0;cnt<total;cnt++)                                                     // start exit entry (want to put this exit entry) 
     {
      OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
      if(OrderType()<=OP_SELL &&                                                 // check for opened position 
         OrderSymbol()==Symbol())                                                // check for symbol
        {
         if(OrderType()==OP_BUY)                                                 // long position is opened
           {
             if((Ma7Current<Ma11Current) &&  (Ma7Previous<Ma11Previous) &&   
                   (Ma7Previous2>Ma11Previous2) && (Ma7Current<Ma7Previous))  
                {
                 OrderClose(OrderTicket(),OrderLots(),Bid,30,Violet);           // close long position
                 return(0);                                                    
                }
           }
         else                                                                   // go to short position
           {
             if((Ma7Current>Ma11Current) &&  (Ma7Previous>Ma11Previous) &&   
                   (Ma7Previous2<Ma11Previous2) && (Ma7Current>Ma7Previous))  
              {
               OrderClose(OrderTicket(),OrderLots(),Ask,30,Violet);             // close short position
               return(0);                                                       
              }
           }
        }
     }                                                                          // end exit entry

            Print("We have no money. Free Margin = ", AccountFreeMargin());       //<--- no margin left
            return(0);       
        Print("No More Orders Allowed, Maximum Lots = ",MaxLot);                  //<--- max lots opened
        return(0);  
    return(0);                                                                    //<---- not a newbar
}                                                                                 //<--- end function   
         
bool NewBar()
{
        static datetime OldTime = 0;

        if(OldTime < Time[0])
        {
                OldTime = Time[0];
                return(true);
        }
        else
        {
                return(false);
        }
}


bool OkToTradeBar(bool trade=True)
{
        static bool FirstTimeIn = true;
        static datetime NextTradeTime = 0;      
        
        if(FirstTimeIn)
        {
                NextTradeTime = Time[0]; //<--- first time in return true and set NextTimeTrade to True.
                FirstTimeIn = false;
                return(true);
        }       
   if(trade)
           if(Time[0]>=NextTradeTime) //<--- check to see if current time[0] is greater then our last trade time.
         return(true);
      else
         return(false);
   else
      {
        NextTradeTime = Time[0]+ 60*NextTrade*Period(); //<--- (extern NextTrade) = n bars to skip before more trade attemps
         return(false);                                 //<---- set new trade time foward after trade is made without errors should be put after ticket check
      }
   return(false); //<---- should never get here
}
             
bool MaxOpenOrders()                      
{
        if(OrdersTotal() >= MaxLot)                 //<------- Max open orders for ALL outstanding trades on this account (extern MaxLots)
                return(true);                            //<------- Hopefully prevent some type of catastrophe
        return(false);
}
 
seungbaek:


Hi onewithzachy,

Thanks for your help and the below EA has been tested, and the result showed all of "Buy" order were executed correctly & properly without any problem on audusd 5min chart.

However the result also showed any single one of "Exit" order has NEVER been executed, i didn't have any close of the position by exit order from the test result.

Unfortunately I do not know what was wrong on exit order.

The exit order is very simple. If MA7 cross up MA11 close the short position, if MA7 cross down MA11 close the long position.

Could you plesae help me?

Hi seungbaek,

Actually I'm a little busy right now and and didn't look the whole things so my corrections here probably need more corrections. I think you copy paste the code without knowing how this work, coz several problem I could find is that you are using a lot of return (0);.

#include <stdlib.mqh>
extern double Lots = 0.1;
extern int    MaxLot = 10;
extern int    NextTrade=12; //<---- number of bars to wait
extern int    dif=500;

//+------------------------------------------------------------------+
void init()
  {return(0);}
//+------------------------------------------------------------------+  
int deinit()
  {return(0);}
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int start()
  {
   CheckBuy();
   return(0);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void CheckBuy()
  {
   int  ticket;

   double Ma7Current    = iMA(NULL,0,7,0,MODE_SMA,PRICE_CLOSE,0);   //<--- wild guess for testing.
   double Ma7Previous   = iMA(NULL,0,7,0,MODE_SMA,PRICE_CLOSE,1);   //<--- wild guess for testing.
   double Ma7Previous2  = iMA(NULL,0,7,0,MODE_SMA,PRICE_CLOSE,2);   //<--- wild guess for testing.

   double Ma11Current   = iMA(NULL,0,11,0,MODE_SMA,PRICE_CLOSE,0);  //<--- wild guess for testing.
   double Ma11Previous  = iMA(NULL,0,11,0,MODE_SMA,PRICE_CLOSE,1);  //<--- wild guess for testing.
   double Ma11Previous2 = iMA(NULL,0,11,0,MODE_SMA,PRICE_CLOSE,2);  //<--- wild guess for testing.

   if(NewBar())                                                            //<---- new bar 
      if(!MaxOpenOrders())                                                 //<---- maxlot ok 
         if(!(AccountFreeMargin()<(2000*Lots)))                            //<---- margin ok        
            if((Ma7Current>Ma11Current) &&  (Ma7Previous>Ma11Previous) &&  //<---- trade setup ok
               (Ma7Previous2<Ma11Previous2) && (Ma7Current>Ma7Previous))
               if((OrdersTotal()<1) && OkToTradeBar()) //<---- first order ok to trade
                 {
                  ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,30,0,0,"#1",16384,0,Green);  //<---- execute trade
                  if(ticket>0)
                     if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
                        Print("BUY order opened : ",OrderOpenPrice());                    //<---- order ok
                  else
                     Print("Error opening BUY order : ",GetLastError());                  //<---- order failed
                 }
   else 
   if((OkToTradeBar()) && (Bid<OrderOpenPrice()-dif*Point))                   //<---- open new orders after n bars have passed 
     {                                                                        //<---- and the price is LESS THEN the prev order
      ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,30,0,0,"#2",16384,0,Green);
      if(ticket>0)
         if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
            Print("BUY order opened : ",OrderOpenPrice());               //<---- order ok
      else
         Print("Error opening BUY order : ",GetLastError());             //<---- order failed    
     }
   //return(0);   // these two return ....                               //<--- n bars have not passes yet
   //return(0);   // ... prevent the whole EA from closing position      //<----setup rules not met        
   
   int cnt, total;                  // ==>> declare cnt and total
   total = OrdersTotal();           // ==>> assign value to total
   
   for(cnt=0;cnt<total;cnt++) // start exit entry (want to put this exit entry) 
     {
      if (!OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES)) continue;   // if false continue
      if(OrderType()<=OP_SELL &&                                   // check for opened position 
         OrderSymbol()==Symbol())                                  // check for symbol
        {
         if(OrderType()==OP_BUY)                                   // long position is opened
           {
            if((Ma7Current<Ma11Current) && (Ma7Previous<Ma11Previous) && 
               (Ma7Previous2>Ma11Previous2) && (Ma7Current<Ma7Previous))
              {
               if(!OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),30,Violet))     // close long position
                 {
                 Print ("Failed to close sell ",OrderTicket()," with error ",ErrorDescription(GetLastError()));
                 }        
               return(0);                                                                 
              }
           }
         else                                                                              // go to short position
           {
            if((Ma7Current>Ma11Current) && (Ma7Previous>Ma11Previous) && 
               (Ma7Previous2<Ma11Previous2) && (Ma7Current>Ma7Previous))
              {
               if(!OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),30,Violet))      // close short position
                 {
                 Print ("Failed to close buy ",OrderTicket()," with error ",ErrorDescription(GetLastError()));
                 }                     
               return(0);
              }
           }
        }
     }                                                                  // end exit entry

   Print("We have no money. Free Margin = ",AccountFreeMargin());       //<--- no margin left
   //return(0);
   Print("No More Orders Allowed, Maximum Lots = ",MaxLot);              //<--- max lots opened
   //return(0);
   return(0);                                                            //<---- not a newbar
  }                                                                      //<--- end function   
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
bool NewBar()
  {
   static datetime OldTime=0;

   if(OldTime<Time[0])
     {
      OldTime=Time[0];
      return(true);
     }
   else
     {
      return(false);
     }
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
bool OkToTradeBar(bool trade=True)
  {
   static bool FirstTimeIn=true;
   static datetime NextTradeTime=0;

   if(FirstTimeIn)
     {
      NextTradeTime=Time[0];     //<--- first time in return true and set NextTimeTrade to True.
      FirstTimeIn=false;
      return(true);
     }
   if(trade)
      if(Time[0]>=NextTradeTime) //<--- check to see if current time[0] is greater then our last trade time.
         return(true);
   else
      return(false);
   else
     {
      NextTradeTime = Time[0]+ 60*NextTrade*Period(); //<--- (extern NextTrade) = n bars to skip before more trade attemps
      return(false);                                  //<---- set new trade time foward after trade is made without errors should be put after ticket check
     }
   return(false); //<---- should never get here
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
bool MaxOpenOrders()
  {
   if(OrdersTotal()>=MaxLot) //<------- Max open orders for ALL outstanding trades on this account (extern MaxLots)
      return(true);          //<------- Hopefully prevent some type of catastrophe
   return(false);
  }
//+------------------------------------------------------------------+
 
onewithzachy:

Hi seungbaek,

Actually I'm a little busy right now and and didn't look the whole things so my corrections here probably need more corrections. I think you copy paste the code without knowing how this work, coz several problem I could find is that you are using a lot of return (0);.


Hi onewithzachy,

Thanks for your reply!!! I will fix the above following your instructions exactly. Then I will test again.

Really appreciated your working...