Volume-check before opening a trade

 

Hello,

Today I was very surprised that my EA opened a new trade after I closed the order. This did happen because the open and the close were in the same candle, which caused a positive signal of the function "IsNewCandle()".

I think the best way to fix this problem is to add a volume-check in the IsNewCandle() function. 

I've add a line to make sure volume isn't higher than 0. Is 0 a good amount to use, or do I have to use a higher number?

Thanks in advance,

XtractAlpha 

 

bool IsNewCandle()
{
if iVolume(NULL,240,0)<0
{
   static int BarsOnChart=0;
        if (Bars == BarsOnChart)
        return (false);
        BarsOnChart = Bars;
        return(true);
}  
}
 

I test for a new candle this way:

datetime ThisBar;
OnTick(){
   ...
   bool isNewCandle = false;
   if (ThisBar != Time[0] ) {
      ThisBar = Time[0];
      isNewCandle = true;
   }
   ...
}

or variations of this

 

Thanks for your comment!

I would like to stick to the volume instead the time. 

 

Will this piece of code work? 

I highly doubt it because volume has to be below 1, which is just a millisecond..  

What do you programmers think is the best value for x in the following line :

Thanks in advance. 

if(Volume[0]>X) return(false);
bool IsNewCandle()
{
   if(Volume[0]>1) return(false);
   	{
   	static int BarsOnChart=0;
   	if (Bars == BarsOnChart)
   	return (false);
  	BarsOnChart = Bars;
  	return(true);
   	}
}  
 

Best to stick to Time[0] when checking for a new bar. Volume has proven unreliable.

It's been covered before if you search back.

 
xtractalpha: I think the best way to fix this problem is to add a volume-check in the IsNewCandle() function. 

I've add a line to make sure volume isn't higher than 0. Is 0 a good amount to use, or do I have to use a higher number?

if iVolume(NULL,240,0)<0
  1. Volume will never be zero. No tick, no new bar.
  2. Volume will never be negative.
  3. If you miss the first tick of a bar, your test fails. Never use volume.
  4. If you reach maximum bars on chart... Never use bars.
  5. Always use time.
    OnTick(){
       static datetime Time0=0; bool isNewCandle = Time0 != Time[0]; Time0 != Time[0];
       ...
    See also New candle - MQL4 forum
  6. I always like making things functions, except for new bar. If you call gooly's function a second time per tick it fails.
 

Thanks everyone,

I really appreciate your help. I did lose today many percents of my account on USDCHF in just 2 hours time, because my EA did open many orders on the first bear candle.. http://prntscr.com/5swqnx

 I have learned my lesson... 

 

Anyways...  with the help of you people I've build this, thanks.  I think this will do the job correctly. 

 

Thanks in advance,

Thierry 

int start()
{
  {
   static datetime timeCur; datetime timePre = timeCur; timeCur=Time[0];
   bool isNewBar = timeCur != timePre;
   if(isNewBar)
  
     { 
      if(IsNewCandle())CheckForMaTrade();
     }
    // every tick
   }



}

 

bool IsNewCandle()
{
   {
   static int BarsOnChart=0;
        if (Bars == BarsOnChart)
        return (false);
        BarsOnChart = Bars;
        return(true);
        }
}