coding problem. - page 2

 
Ais:
Any MQL4 program correctly recognises any available bar and much more
Changes are essence of market

Hi Ais,


Good day,


Thanks again.


O.k, then why my EA is not able to place orders each time the condition occur???


Do you have an explanation why is that happening??


Please let me know.


Best wishes,

 

Good day

Sooner or later any program will work

I will help to solve the code

Best regards

 

Insert alerts in the code

 
There is a lot of mystical in this world
 
Ais:

Insert alerts in the code

Hi Ais,


Good day,


why do I have to insert alerts in the code??


Do you mean that alerts would help me out finding where the errors are??


I notice that the EA is skipping some bars and I still don't know why.


O.k, I may post the EA next time if I didn't reach a solution so it becomes clear.


Best wishes,

 
Alerts will help to find everything
 
Delete alerts after final system adjustment
Insert again in case of questions arised
 
Ais:
Delete alerts after final system adjustment
Insert again in case of questions arised

Hi,


You confused me now after this many posts.


I guess I can make it short for myself and everyone else.


Here is the code as shown:

#property link      ""
..............   
.......................
// Closing of Pending Orders      
void PendOrdDel()
{ ........}
//-------------------
void CloseOrdbyTime()
{..............}       
//------------------  
void TrailStops()
{ ............. } 
//-----------------
void SellOrdOpen()
{		  
        double HigH=iHigh(Symbol(), PERIOD_D1,1);   
        double LoW=iLow(Symbol(), PERIOD_D1,1);
		  
		  double SellPrice=LoW;
		  if (InitialStop) SellStop=HigH+spread;
        Profit=0;
	   
		  ticket = OrderSend(Symbol(),OP_SELLSTOP,Lotsi,NormalizeDouble(SellPrice,digit), Slippage,
		                     NormalizeDouble(SellStop,digit),Profit,"SELL",Magic,0,Red);
    
        OrderOpenDay  = DayOfWeek();   
        
        SellInTrade=false;            
            
            if(ticket<0)
            {
            Print("OrderSend failed with error #",GetLastError());
            return(0);
            }
}

void BuyOrdOpen()
{		  
        double LoW=iLow(Symbol(), PERIOD_D1,1);   
        double HigH=iHigh(Symbol(), PERIOD_D1,1);
        
		  double BuyPrice =HigH+spread;
		  if (InitialStop) BuyStop =LoW;
        Profit=0;
		 
		  ticket = OrderSend(Symbol(),OP_BUYSTOP ,Lotsi,NormalizeDouble(BuyPrice ,digit), Slippage,
		           NormalizeDouble(BuyStop ,digit),Profit,"BUY",Magic,0,Blue);
                
        
        OrderOpenDay  = DayOfWeek();
        BuyInTrade=false;            
            
            if(ticket<0)
            {
            Print("OrderSend failed with error #",GetLastError());
            return(0);
            }
} 
     

// ---- Scan Trades
int ScanTrades()
{   
   int total = OrdersTotal();
   int numords = 0;
      
   for(cnt=0; cnt<total; cnt++) 
   {        
   OrderSelect(cnt, SELECT_BY_POS);            
   if(OrderSymbol() == Symbol() && OrderType()>=OP_BUY && OrderMagicNumber() == Magic) 
   numords++;
   }
   return(numords);
}

datetime OrderOpenDate()
{
   int total = OrdersTotal();
   datetime date;
   for(cnt=0; cnt<total; cnt++) 
   {        
   OrderSelect(cnt, SELECT_BY_POS);            
   if(OrderSymbol() == Symbol() && OrderType()>=OP_BUY && OrderMagicNumber() == Magic) 
   date = StrToTime(TimeToStr(OrderOpenTime(),TIME_DATE));
   }
   return(date);
}  	                    

//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
{
   if(Bars < 1) {Print("Not enough bars for this strategy"); return(0);}
  
   if ( Trace ) SetTrace();
  
   string   TimeTrade = "00:00";
   StartTime  = StrToTime(TimeTrade) + TimeZone*3600;
  
 //  if(CurTime() >= StartTime && CurTime() <= StartTime+60)
 //  {
      if ( OrderOpenDate() < StrToTime(TimeToStr( StartTime,TIME_DATE))) 
      { PendOrdDel(); if( TradePeriod > 0 )CloseOrdbyTime(); }
      
      if(ScanTrades()<1)
      {
      if (TrailingStop) InitialStop=true; 
   
      ArrayCopyRates(rates_h1, Symbol(), PERIOD_H1);
      open = rates_h1[0][1];
      high=0; low=10000000;
      for (i=24;i>=1;i--)
      {
      high = MathMax( high, rates_h1[i][3]);
      low  = MathMin( low , rates_h1[i][2]);      
      }   
      Print(" high=",high,"low=",low);  
      range =(high-low); 
      
      smin = Bid - StopPercent*range/100.0;
      smax = Ask + StopPercent*range/100.0;
      
      // all days of week except Sunday
   if (TimeDayOfWeek(TimeCurrent())!=0)
   if (High[0]<High[1] && Low[0]>Low[1])
   {
      BuyOrdOpen();
      SellOrdOpen();
   } 

      }
 //  }
   if (TrailingStop) TrailStops(); 
 return(0);
}
 

I'm trying to get bar[1] and bar[2] every time they change since my EA can't recognize them. I believe you know that bar[0] is going to be bar[1] if new bar[new] show up, and thus bar[1] is going to be bar[2] and so on.

Yes High[1] becomes High[2] and High[0] becomes High[1].

start() {
   	static datetime Time.newBar;	bool newBar = ( Time.newBar < Time[0]);
	if (newBar) {					Time.newBar = Time[0];
               //...
 
WHRoeder:

Yes High[1] becomes High[2] and High[0] becomes High[1].

Hi WHRoeder,


Good day,


Thanks a lot for helping me out.


O.k, what I understand is that I need to identify the new bar based on Time[0]. In this case my code would be as the following:


static datetime Time.newBar;

bool newBar = ( Time.newBar < Time[0]);

if (newBar) { Time.newBar = Time[0];

if (High[0]<High[1] && Low[0]>Low[1])

 {
      BuyOrdOpen();
      SellOrdOpen();
   } 

}


is that what it supposed to be??? I'm not pro coder yet my friend :). I'm however trying my best to be pro coder so that's why I'm trying to learn new bar coding first since it is important.


Please let me know if this is the right way, and I will try it now anyways and see how things can be done.


I appreciate it.


Best wishes,

Reason: