How can my EA enters one order per bar and it get signal when the current bar close?

 

How can my EA enters one order per bar and it get signal when the current bar close? 

Please help! 

 
int start()
{
static datetime currTime;if(currTime==Time[0])return;currTime=Time[0];
 //Get Signal
 //Trade

}
 
heelflip43:

 


Thank. But can you show me what's wrong with my code?

it's warn " OrderModify error 4108" .

int start()
{
   int i ,times;
   double DIPlus1;
   double DIPlus0;
 
   double DIMinus1;
   double DIMinus0;
   
   double Main1;
   double Main0;
   
   double MACD_main;
   double MACD_signal;
   double Rsi;
   
   double err = 25;
   double x = 70;
   double xx = 30;
   
   double sl = 300;
   double tp = 600;
   
   double  Stoc_main0, Stoc_signal0, Stoc_main1, Stoc_signal1, Stoc_main2;

      
   int MagicNumber1=1234,MagicNumber2=4321;
   int   TrailingStop_Sell=150;
   int   TrailingStop_Buy=150;
//----

DIPlus1=iADX(NULL,0,14,PRICE_CLOSE,MODE_PLUSDI,1);
DIPlus0=iADX(NULL,0,14,PRICE_CLOSE,MODE_PLUSDI,0);
 
DIMinus1=iADX(NULL,0,14,PRICE_CLOSE,MODE_MINUSDI,1);
DIMinus0=iADX(NULL,0,14,PRICE_CLOSE,MODE_MINUSDI,0);

Main1=iADX(NULL,0,14,PRICE_CLOSE,MODE_MAIN,1);
Main0=iADX(NULL,0,14,PRICE_CLOSE,MODE_MAIN,0);

//Rsi = iRSI(NULL,0,14,PRICE_CLOSE,0);

Stoc_main0 = iStochastic(NULL,0,14,3,3,MODE_SMA,0,MODE_MAIN,0);
Stoc_signal0 = iStochastic(NULL,0,14,3,3,MODE_SMA,0,MODE_SIGNAL,0);
Stoc_main1 = iStochastic(NULL,0,14,3,3,MODE_SMA,0,MODE_MAIN,1);
Stoc_main2 = iStochastic(NULL,0,14,3,3,MODE_SMA,0,MODE_MAIN,2);
Stoc_signal1 = iStochastic(NULL,0,14,3,3,MODE_SMA,0,MODE_SIGNAL,1);

//----
{
static datetime currTime;if(currTime==Time[0])return;currTime=Time[0];
if(Minute() % 15 ==0 && Seconds() ==0)
{     
    if( DIPlus0 > DIMinus0 && DIPlus1 < DIMinus1)
                  {
                   if( (Stoc_main0 - Stoc_signal0) > 0 && Stoc_main0 <80  
                   || (Stoc_main0 - Stoc_signal0) > 0 && Stoc_main1 <80
                    || (Stoc_main0 - Stoc_signal0) > 0 && Stoc_main2 <80)
                   {
                     if(!OrderSend(Symbol(),OP_BUY,0.03,Ask,5,Ask-sl*Point,Ask+tp*Point,"",MagicNumber1,0,Blue))
                        Print (" Error: ", GetLastError());
                  }
                    }     
    if(  DIPlus0 < DIMinus0 && DIPlus1 > DIMinus1 ) 
                   {
                   if((Stoc_main0 - Stoc_signal0) < 0 && Stoc_main0 > 20  
                   || (Stoc_main0 - Stoc_signal0) < 0 && Stoc_main1 > 20
                   || (Stoc_main0 - Stoc_signal0) < 0 && Stoc_main2 > 20)
                   {
                     if(!OrderSend(Symbol(),OP_SELL,0.03,Bid,5,Bid+sl*Point,Bid-tp*Point,"",MagicNumber2,0,Red))
                         Print (" Error: ", GetLastError());  
                   }
                   }
   
}
     
  //---
int PositionIndex;    
 
int TotalNumberOfOrders;  

TotalNumberOfOrders = OrdersTotal();   

for(PositionIndex = TotalNumberOfOrders - 1; PositionIndex >= 0 ; PositionIndex --)  
  {
   if( ! OrderSelect(PositionIndex, SELECT_BY_POS, MODE_TRADES) ) continue;   
   
    Print("Stoc_main0: ", DoubleToStr(Stoc_main0, Digits), " Stoc_main1: ", DoubleToStr(Stoc_main1, Digits), " Main0: ",DoubleToStr(Main0, Digits), " Stoc_signal0: ",DoubleToStr(Stoc_signal0, Digits), " Stoc_signal1: ",DoubleToStr(Stoc_signal1, Digits) );
  

       {
         if( OrderMagicNumber() == MagicNumber2      
            && OrderSymbol() == Symbol()          
            && ( OrderType() == OP_BUY           
            ||   OrderType() == OP_SELL ) )   
           {if(  DIPlus0 > DIMinus0
              ||Stoc_main0 < 20 )
               {if ( ! OrderClose( OrderTicket(), OrderLots(), OrderClosePrice(), 3 ) )
                   Print("Order Close failed, order number: ", OrderTicket(), " Error: ", GetLastError() ); 
               }
            } 
         if( OrderMagicNumber() == MagicNumber1      
            && OrderSymbol() == Symbol()          
            && ( OrderType() == OP_BUY           
            ||   OrderType() == OP_SELL ) )   
          { if(  DIPlus0 < DIMinus0
             || Stoc_main0 > 80)
               {  if ( ! OrderClose( OrderTicket(), OrderLots(), OrderClosePrice(), 3 ) )               
                  Print("Order Close failed, order number: ", OrderTicket(), " Error: ", GetLastError() ); 
               }
          } 
        }          
    
}


   
   
       
    if(OrderType()==OP_BUY)
         {
           
           if(TrailingStop_Buy>0)  
                              
               if(Bid-OrderOpenPrice()>Point*TrailingStop_Buy) 
                 
                   if(OrderStopLoss()<Bid-Point*TrailingStop_Buy)
                     
                       OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*TrailingStop_Buy,OrderTakeProfit(),0,Green);
          
                       return(0);
                     
           }      
             
         
   if(OrderType()==OP_SELL)
         {
           if(TrailingStop_Sell>0)  
                             
               if((OrderOpenPrice()-Ask)>(Point*TrailingStop_Sell))  
                 
                   if((OrderStopLoss()>(Ask+Point*TrailingStop_Sell)) || (OrderStopLoss()==0))
                     
                       OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*TrailingStop_Sell,OrderTakeProfit(),0,Red);
                       
                       return(0);
                       
           }          
                 
            
       }
  
 return(0);
 }
 
toi10005doi:


Thank. But can you show me what's wrong with my code?

it's warn " OrderModify error 4108" .

I don' read the whole code of yours, I only read some of them. 

Add this on top of your code

#include <stdlib.mqh>

and for every error checking write something like this

Print ("Error ",ErrorDescription(GetLastError()));

That way you can read on expert tab, that the error 4108 is invalid ticket.

BTW, the return type of OrderSend is integer (of Ticket Number), and not a bool, so you can not use ! in front of OrderSend(). The correct one should be

ticket = OrderSend(.....);
if (ticket < 0)
   {
   Print ("Order Send error ",ErrorDescription(GetLastError()));
   return (0);
   }
 
phi.nuts:

I don' read the whole code of yours, I only read some of them. 

Add this on top of your code

and for every error checking write something like this

That way you can read on expert tab, that the error 4108 is invalid ticket.

BTW, the return type of OrderSend is integer (of Ticket Number), and not a bool, so you can not use ! in front of OrderSend(). The correct one should be



Thank you!

Can you help more?

I have " unmatched data error (high value 1.60977 at 2012.10.29 00:30 is not reached from the least timeframe, high price 1.60926 mismatches)" errors.

please help me fix it! 

 
toi10005doi:


Thank you!

Can you help more?

I have " unmatched data error (high value 1.60977 at 2012.10.29 00:30 is not reached from the least timeframe, high price 1.60926 mismatches)" errors.

please help me fix it! 

 

Try this first https://www.mql4.com/search#!keyword=unmatched%20data%20error&module=mql4_module_forum
 
phi.nuts:
Try this first https://www.mql4.com/search#!keyword=unmatched%20data%20error&module=mql4_module_forum

Thank!

i have a code with "2012.11.10 01:23:48 2012.10.30 13:45  IMF GBPUSD,M15: OrderModify error 4108" and "2012.11.10 01:23:01 2012.10.08 16:19  IMF GBPUSD,M15: OrderModify error 4051"

please help me!


int i;    
 
int Total;  

Total = OrdersTotal();   

for(i = Total - 1; i >= 0 ; i --)  
  {
   if( ! OrderSelect(i, SELECT_BY_POS, MODE_TRADES) ) continue;   
   
Print("DIPlus1: ", DoubleToStr(DIPlus1, Digits), " DIMinus1: ", DoubleToStr(DIMinus1, Digits), " DIPlus2: ",DoubleToStr(DIPlus2, Digits), " DIMinus2: ",DoubleToStr(DIMinus2, Digits) );
  

       {
         if( OrderMagicNumber() == MagicNumber2      
            && OrderSymbol() == Symbol()          
            && ( OrderType() == OP_BUY           
            ||   OrderType() == OP_SELL ) )   
           {if(  DIPlus1 > DIMinus1 && DIPlus2 < DIMinus2)
               {if ( ! OrderClose( OrderTicket(), OrderLots(), OrderClosePrice(), 3 ) )
                     Print ("Error ",ErrorDescription(GetLastError())) ; 
               }
            } 
         if( OrderMagicNumber() == MagicNumber1      
            && OrderSymbol() == Symbol()          
            && ( OrderType() == OP_BUY           
            ||   OrderType() == OP_SELL ) )   
          { if(  DIPlus1 < DIMinus1 && DIPlus2 > DIMinus2)
               {  if ( ! OrderClose( OrderTicket(), OrderLots(), OrderClosePrice(), 3 ) )               
                    Print ("Error ",ErrorDescription(GetLastError())); 
               }
          } 
        }          
    
}


   
   
       
    if(OrderType()==OP_BUY)
         {
           
           if(TrailingStop_Buy>0)  
                              
               if(Bid-OrderOpenPrice()>Point*TrailingStop_Buy) 
                 
                   if(OrderStopLoss()<Bid-Point*TrailingStop_Buy)
                     
                       OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*TrailingStop_Buy,OrderTakeProfit(),0,Green);
          
                       return(0);
                     
           }      
             
         
   if(OrderType()==OP_SELL)
         {
           if(TrailingStop_Sell>0)  
                             
               if(OrderOpenPrice()-Ask > Point*TrailingStop_Sell)  
                 
                   if(OrderStopLoss()>Ask+Point*TrailingStop_Sell) 
                     
                       OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*TrailingStop_Sell,OrderTakeProfit(),0,Red);
                       
                       return(0);
                       
           }          
                 
            
       
   return(0);
  }
//+------------------------------------------------------------------+
 

You already get an error message on OrderModify, so why don't you print ErrorDescription(GetLastError()) to OrderModify() as well ?,

and comb your codes for better reading, You will spot your error if you write the code neatly.

Here, I com it for you, but please look for the err your self (hits : find matching curly brackets, "{" or "}" )

in  t i;    
int Total;  
Total = OrdersTotal();   

for(i = Total - 1; i >= 0 ; i --)  
  {
  if( ! OrderSelect(i, SELECT_BY_POS, MODE_TRADES) ) continue;   
  
  Print("DIPlus1: ", DoubleToStr(DIPlus1, Digits), " DIMinus1: ", DoubleToStr(DIMinus1, Digits), " DIPlus2: ",DoubleToStr(DIPlus2, Digits), " DIMinus2:
  ",DoubleToStr(DIMinus2, Digits) );
  
     {
     if( OrderMagicNumber() == MagicNumber2      
         && OrderSymbol()   == Symbol()          
         && ( OrderType()   == OP_BUY  ||  OrderType() == OP_SELL ) 
        )   
           {
            if(  DIPlus1 > DIMinus1 && DIPlus2 < DIMinus2)
               {
                if ( ! OrderClose( OrderTicket(), OrderLots(), OrderClosePrice(), 3 ) ) 
                Print ("Error ",ErrorDescription(GetLastError())) ; 
               }
            } 

         if( OrderMagicNumber() == MagicNumber1      
             && OrderSymbol()   == Symbol()          
             && ( OrderType() == OP_BUY ||   OrderType() == OP_SELL ) )   
             { 
              if(  DIPlus1 < DIMinus1 && DIPlus2 > DIMinus2)
                  {  
                   if ( ! OrderClose( OrderTicket(), OrderLots(), OrderClosePrice(), 3 ) )               
                   Print ("Error ",ErrorDescription(GetLastError())); 
                   }
              } 
        }          
   }
       
    if(OrderType()==OP_BUY)
       {
        if(TrailingStop_Buy>0)  
           if(Bid-OrderOpenPrice()>Point*TrailingStop_Buy) 
               if(OrderStopLoss()<Bid-Point*TrailingStop_Buy)
                  OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*TrailingStop_Buy,OrderTakeProfit(),0,Green);
        return(0);
        }      
             
         
     if(OrderType()==OP_SELL)
         {
          if(TrailingStop_Sell>0)  
             if(OrderOpenPrice()-Ask > Point*TrailingStop_Sell)  
                 if(OrderStopLoss()>Ask+Point*TrailingStop_Sell) 
                     OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*TrailingStop_Sell,OrderTakeProfit(),0,Red);
           return(0);
           }          

   return(0);
  }
//+------------------------------------------------------------------+
Reason: