Stochastic Crossover is not detected when the crossover occurs exactly at the opening of a new candle

 

I am testing Stochastic crosses and I have noticed that sometimes it fails when the cross occurs exactly when a new candle opens. This happens very often and I think too much precision is lost.

Is this normal or is it a bug in my code?

Is there any way that this does not happen?

Here is my code, and I also attach an image.


class Stochastic : Indicator
{
public:    
   Signal signal0;
   Signal signal1;   
public:
   //+------------------------------------------------------------------+
   void Stochastic
            (
                  string pSymbol                      = NULL,            
                  ENUM_TIMEFRAMES pTimeFrame          = PERIOD_CURRENT,  
                  int pKPeriod                        = 5,               
                  int pDPeriod                        = 3,
                  int pSlowling                       = 3,
                  ENUM_MA_METHOD pMaMethod            = MODE_SMA,
                  ENUM_STO_PRICE pStoPrice            = STO_LOWHIGH                  
            )
   
   {   

      handler = iStochastic(pSymbol, pTimeFrame, pKPeriod, pDPeriod, pSlowling, pMaMethod, pStoPrice);           
      CheckHandler(handler, "Stochastic");
   }
   //+------------------------------------------------------------------+
   ~Stochastic()
   {    
   }
   //+------------------------------------------------------------------+
   bool CanBuy(int index=1)
   {
      return 
      (signal0.array[index] > signal1.array[index]) &&
      (signal0.array[index+1] < signal1.array[index+1]);  
   }
   //+------------------------------------------------------------------+
   bool CanSell(int index=1)
   {
      return 
      (signal0.array[index] < signal1.array[index]) &&
      (signal0.array[index+1] > signal1.array[index+1]);    
   }
};



//+------------------------------------------------------------------+
//********************************************************************
//+------------------------------------------------------------------+


class Signal
{
   bool isArraySet;   
public:     
   double array[];    
public:
   //+------------------------------------------------------------------
   void Signal() 
   {
      isArraySet=false;
      ArraySetAsSeries(array, true);
   }  
   //+------------------------------------------------------------------
   ~Signal() 
   {
      if(isArraySet)ArrayFree(array);      
   }    
   //+------------------------------------------------------------------+
   void Update(int handler, int index=0, int pSize=3)
   {
      if(GetArray(handler, 0, pSize, array, index))      
         isArraySet = true;
   }
   //+------------------------------------------------------------------+   
   bool GetArray(int indicatorHandler, int start, int end,double &dataArray[], int indicatorIndex=0)
   { 
      if(CopyBuffer(indicatorHandler, indicatorIndex, start, end, dataArray)==-1)
      {
         Print(__FUNCTION__, " Error coping buffer!! Code = ", GetLastError() );
         return false;
      }   
      return true;
   }  
};

Thank you so much!!

 
karp wak: I have noticed that sometimes it fails when the cross occurs exactly when a new candle opens. This happens very often and I think too much precision is lost.

Is this normal or is it a bug in my code?

  1. How To Ask Questions The Smart Way. (2004
              Don't rush to claim that you have found a bug.
    Questions Not To Ask
              My program doesn't work. I think system facility X is broken.

    It is almost always your code.

  2. Always post all relevant code. You get the handle in OnInit (or on load). Where do you declare your class objects?
 
William Roeder:
  1. How To Ask Questions The Smart Way. (2004
              Don't rush to claim that you have found a bug.
    Questions Not To Ask
              My program doesn't work. I think system facility X is broken.

    It is almost always your code.

  2. Always post all relevant code. You get the handle in OnInit (or on load). Where do you declare your class objects?

Hi Mr William.
I have selected the part of my code that I think is most relevant. I am using the SOLID principles and I have my code divided into a large number of classes. (too many files to post here).

Yes, I'm calling the indicators functions in the constructors of classes and the classes are instantiated in the OnInit () function.

I was thinking that maybe something is missing in my code for it to work correctly ... but I don't know what it could be ...

This is the only code that I have in the OnInit function. Everything else is instantiated in the class constructors.


//+------------------------------------------------------------------+
EA1 *ea1;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
{   
   ea1 = new EA1();
   return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
   if(ea1.OnDeinit(reason))  
   {  
      delete ea1;
      ExpertRemove();
   }
}


Thank you so much!!

 
William Roeder:
  1. How To Ask Questions The Smart Way. (2004
              Don't rush to claim that you have found a bug.
    Questions Not To Ask
              My program doesn't work. I think system facility X is broken.

    It is almost always your code.

  2. Always post all relevant code. You get the handle in OnInit (or on load). Where do you declare your class objects?

Ok, I'm sorry, maybe this is necessary too.


//+------------------------------------------------------------------+
class Test 
{
   Stochastic *stochastic; 
   MACD *macd;
public:
   //+------------------------------------------------------------------+
   void Test
               (  
                  string pSymbol, 
                  ENUM_TIMEFRAMES pTimeFrame,                  
                  //------------ MACD
                  int pFastEmaPeriod,             
                  int pSlowEmaPeriod,               
                  int pSignalPeriod,                
                  ENUM_APPLIED_PRICE  pAppliedPrice, 
                  //-----------STO
                  int pKPeriod,               
                  int pDPeriod,
                  int pSlowling,
                  ENUM_MA_METHOD pMaMethod,
                  ENUM_STO_PRICE pStoPrice 
                               
               )
   {   
      
 

      
      macd = new MACD
      (
         pSymbol,            
         pTimeFrame,         
         pFastEmaPeriod,      
         pSlowEmaPeriod,     
         pSignalPeriod,       
         pAppliedPrice        
      );
      
      
      stochastic = new Stochastic
      (
         pSymbol,
         pTimeFrame,
         pKPeriod,       
         pDPeriod,
         pSlowling,
         pMaMethod,
         pStoPrice       
      );     
   
 
   
   }
   //+------------------------------------------------------------------+
   ~Test()
   { 
      delete macd;
      delete stochastic;   
   }      
   //+------------------------------------------------------------------+
    void Update()
   {        
      stochastic.Update();  
      macd.Update();     
   }
   //+------------------------------------------------------------------+
   bool CanSell( )             
   {   
      Update();     
      
      return 
      //macd.CanSell(1) &&
      stochastic.CanSell(1); 

   }  
   //+------------------------------------------------------------------+
   bool CanBuy( )
   {       
      Update();    
      
      return 
      //macd.CanBuy(1) &&
      stochastic.CanBuy(1);          
   }
   //+------------------------------------------------------------------+
   bool CloseBuy()
   {
      Update();
     
      return stochastic.CanSell(1);
   }
   //+------------------------------------------------------------------+  
   bool CloseSell()
   {
      Update();
     
      return stochastic.CanBuy(1);         

   }
};
//+------------------------------------------------------------------+
 
William Roeder:
  1. How To Ask Questions The Smart Way. (2004
              Don't rush to claim that you have found a bug.
    Questions Not To Ask
              My program doesn't work. I think system facility X is broken.

    It is almost always your code.

  2. Always post all relevant code. You get the handle in OnInit (or on load). Where do you declare your class objects?

Mr. William

I think this guy have the same issue like me. Maybe is not a coding problem... Maybe the EA needs a special configuration type to works to 100%? Thank you so much!!

https://www.mql5.com/en/forum/375318#comment_23955865

EA trade opening issue (opens on new candle not when signal given)
EA trade opening issue (opens on new candle not when signal given)
  • 2021.08.10
  • www.mql5.com
Hi all, I am having an issue with an EA that I am using and was wondering if anyone could help me please? I am currently using Promax Gold EA V4...
 
karp wak:

Mr. William

I think this guy have the same issue like me. Maybe is not a coding problem... Maybe the EA needs a special configuration type to works to 100%? Thank you so much!!

https://www.mql5.com/en/forum/375318#comment_23955865

Ok, i was investigating and i think the problem is to programing with "close prices"... it is not so precise as "Every tick" and is logic to losse a lot of signal between bar and bar. (It is the difference between obtaining a continuous signal and a discrete one).

The question is ... Is it worth programming the "open prices" and saving time? Or is it better to program every tick?

I have found an interesting article although it does not answer that question


https://www.mql5.com/en/forum/13556

1minute OHLC vs every tick - opposite results
1minute OHLC vs every tick - opposite results
  • 2013.08.21
  • www.mql5.com
I'm getting complete opposite result when testing on every tick or 1min OHLC...
Reason: