How to add a condition to High Low EA??

 

Hi guys,


Good day,


I'm an intermediate programmer, and I want to know how to add a condition to my EA for buying and selling.


what I mean is that I want the EA only to place positions if the conditions are met.


The condition is: High[2]<High[1] && Low[2]<Low[1]


I'm using ArrayCopyRates to get rates from the chart.


if (High[i+1]<High[i+2] && Low[i+1]>Low[i+2])
     {
  if(TimeDay(Time[i]) != TimeDay(Time[i+1]))

   {
      ArrayCopyRates(rates_h1, Symbol(), PERIOD_D1);
      open = rates_D1[0][1];
      high=0; low=10000000;
   
   OpenOrders();
}
}

I'm not yet sure whether this code is totally right, but the rest of the EA when testing works fine.


The problem now is that the EA still open positions normally at each bar, and this means that the condition is not valid or working let us say.


Please help me out with your suggestions.


Best wishes.

 

Hi again.


Come on guys :) I'm sure that there are a lot of experts here in this great forum.


I just want to know how to add this condition and where in the code.


And are there different ways how to do that??


Best of luck.

 
scarface wrote >>

Hi again.

Come on guys :) I'm sure that there are a lot of experts here in this great forum.

I just want to know how to add this condition and where in the code.

And are there different ways how to do that??

Best of luck.

You have not shown in the code what i is. it is assumed that this set somewhere else on your code? From the code it apprears that you are trying to identify an inside bar.

You should also know that "if(TimeDay(Time[i]) != TimeDay(Time[i+1]))" is always true.

You should try to use a variable here.

whocares

 
whocares:

You have not shown in the code what i is. it is assumed that this set somewhere else on your code? From the code it apprears that you are trying to identify an inside bar.

You should also know that "if(TimeDay(Time[i]) != TimeDay(Time[i+1]))" is always true.

You should try to use a variable here.

whocares

Hi whocares,


Good day,


Thanks a lot for your valuable information. This is the only way I learn by asking about everything.


O.k, now let's take an example here.


#property copyright "Copyright © 2006, Forex-TSD.com "
#property link      "https://www.forex-tsd.com/"
..............   
.......................
// Closing of Pending Orders      
void PendOrdDel()
{ ........}
//-------------------
void CloseOrdbyTime()
{..............}       
//------------------  
void TrailStops()
{ ............. } 
//-----------------
void SellOrdOpen()
{		     

		  double SellPrice=open - range*SellPercent/100.0 - spread;
		  
		  if (InitialStop) SellStop=SellPrice + StopPercent*range/100.0; else SellStop=0;
        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 BuyPrice =open + range*BuyPercent/100.0  + spread;
		  if (InitialStop) BuyStop = BuyPrice - StopPercent*range/100.0; else BuyStop=0;
        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;
      
      if ( Monday    ) if(DayOfWeek()==1){BuyOrdOpen(); SellOrdOpen();}
      if ( Tuesday   ) if(DayOfWeek()==2){BuyOrdOpen(); SellOrdOpen();}
      if ( Wednesday ) if(DayOfWeek()==3){BuyOrdOpen(); SellOrdOpen();} 
      if ( Thursday  ) if(DayOfWeek()==4){BuyOrdOpen(); SellOrdOpen();} 
      if ( Friday    ) if(DayOfWeek()==5){BuyOrdOpen(); SellOrdOpen();}
      }
   }
   if (TrailingStop) TrailStops(); 
 return(0);
}

This code is for High Low Daily Breakout. The problem is that if we want to add a condition say:

would this example below work in this case?? and if not, how would it work then??

if(High[1]<High[2] && Low[1]>Low[2]
{      }
 

Hi again,


I'm still wondering how would conditional order would take place.


I have many examples, but at least if some pro coder or programmer could explain a little how to do that.


How about the equation itself, is it correct ???

if(High[1]<High[2] && Low[1]>Low[2]

//or:

int i=0

if(High[i+1]<High[i+2] && Low[i+1]>Low[i+2]


do I have to identify a loop so the next candle would be recognized??

Please let me know as soon as possible.

Best wishes and Thanks all for this great forum "I have learned a lot from MQL4 forum"

 
int start() {
    static datetime Time.newBar; bool newBar =(Time[0] > Time.newBar);
    if (newBar) {                               Time.newBar = Time[0];
        //...
 

HI WHRoeder,


Thanks for your post.


Is that code for new bar function?? well, I see it as new bar function.


However, I'm still wondering how to set up this condition:


if(High[1]<High[2] && Low[1]>Low[2]

//or:

int i=0

if(High[i+1]<High[i+2] && Low[i+1]>Low[i+2]

would it possible someone link this H L code to my EA code above in the 3rd post, please??

If also possible to explain why ti has to be this place, not that one.

Best wishes,
 

Hi guys,


Good day,


I'm wondering where did the professional coders go???


No one wants to share opinion or what......

 
The clue is in the name. The "professional" programmers have gone back to our professions after the holidays. Could I just point out that this is not a place where you can arrive and "expect" or "demand" that others repair your code. It is on the other hand, somewhere you can expect to be helped to learn MQL programming, if you wish to learn how to repair your own code. What aspect are you currently having trouble with? CB
 
cloudbreaker:
The clue is in the name. The "professional" programmers have gone back to our professions after the holidays. Could I just point out that this is not a place where you can arrive and "expect" or "demand" that others repair your code. It is on the other hand, somewhere you can expect to be helped to learn MQL programming, if you wish to learn how to repair your own code. What aspect are you currently having trouble with? CB

Hi CB,


Good day,


Finally, some pro code showed up. And thanks by the way for your comments :).


well, I'm not expecting anyone to fix my code or anything like that. All I want is to ask how and where to add such a conditional code to my example EA above:


if(High[1]<High[2] && Low[1]>Low[2]


Also, I want to know whether this condition is written correctly or not. And do I have to use a code like this one below or not since bars are going to change "new bar will be created":


if(High[i+1]<High[i+2] && Low[i+1]>Low[i+2]


I have another question: what is the difference between using iHigh as a function or using High[] ...


I hope no one takes it the wrong way. I have been here for a while before studying mql4, but later started creating EA and learning as well much more so now I can ask question about things I don't know.


sorry for being a silent member for a while.


Please let me know if possible.


Thanks again CB for your post. It is good to see you around in the forum.


Best wishes,

 
scarface wrote >>

Hi CB,

Good day,

Finally, some pro code showed up. And thanks by the way for your comments :).

well, I'm not expecting anyone to fix my code or anything like that. All I want is to ask how and where to add such a conditional code to my example EA above:

Also, I want to know whether this condition is written correctly or not. And do I have to use a code like this one below or not since bars are going to change "new bar will be created":

I have another question: what is the difference between using iHigh as a function or using High[] ...

I hope no one takes it the wrong way. I have been here for a while before studying mql4, but later started creating EA and learning as well much more so now I can ask question about things I don't know.

sorry for being a silent member for a while.

Please let me know if possible.

Thanks again CB for your post. It is good to see you around in the forum.

Best wishes,

High[] take the value from the current chart (Timeframe/Symbol) https://docs.mql4.com/predefined/variables/High

iHigh() you can choose the timeframe, symbol... https://docs.mql4.com/series/iHigh

Reason: