Problems with returned function values in differents periods and chart timeframes

 

Hi


I need to do something like this


        if ( iAO( Symbol(),PERIOD_M1, 0) <= iAO( Symbol(), PERIOD_M5, 0) )

        {

        ....

        }



But  iAO( Symbol(), PERIOD_M5, 0)  does not work when  my chart timeframe is M1 (It return 0 always).

Is it possible to obtain the  real returned value of this function in M1?

 
ivanez:

But  iAO( Symbol(), PERIOD_M5, 0)  does not work when  my chart timeframe is M1 (It return 0 always).

Is it possible to obtain the  real returned value of this function in M1?

fyi, it works perfectly on my mt4 :).

 
Seng Joo Thio:

fyi, it works perfectly on my mt4 :).

Thank you for your response.  I found out that the problem is something related with the bars number at the different time frames. something like this: 

int BM1 = iBars(NULL,PERIOD_M1);

where

int BM5 = BM1/5;

or

int BM5 = iBars(NULL,PERIOD_BM5);


So, if i do this it does works if i'm in timeframe M1.


int i, n=0, Counted_bars;   

Counted_bars=IndicatorCounted();  

i=Bars-Counted_bars-1;


   while(i>=0)                     

   { 

        if (  iAO( Symbol(),PERIOD_M1, i) <= iAO( Symbol(), PERIOD_M5, i/5)  )

        {

...

        }

i--;

}


But it only works when i'm programing a indicator. It does not works in a Expert Advisor. ¿Any idea?
 
ivanez:

Thank you for your response.  I found out that the problem is something related with the bars number at the different time frames. something like this: 

int BM1 = iBars(NULL,PERIOD_M1);

where

int BM5 = BM1/5;

or

int BM5 = iBars(NULL,PERIOD_BM5);

You're right. Accessing bar 0 is fine, since it'll still be 0 be it PERIOD_M1 or PERIOD_M5, but for any older bars, you'll need to figure out the right bar number for PERIOD_M5. However, it is not right to simply use i/5, as illustrated below (error highlighted in red):

PERIOD_M1 Bar (i)Time at Bar iPERIOD_M5 Bar (i/5)Time at Bar (i/5)
01002 hrs01000 hrs
11001 hrs01000 hrs
::::
40958 hrs01000 hrs
50957 hrs10955 hrs

So a better way is to check the time of bars, and here's one possible way:

void OnTick()
  {
//---
   static datetime LT = 0;
   
   if (LT==0 || LT<iTime(_Symbol,PERIOD_M1,0))
   {
      int M1Bar = MathMin(20,iBars(_Symbol,PERIOD_M1)-1);
      int M5Bar = iBars(_Symbol,PERIOD_M5)-2;
      
      while(M1Bar>0)
      {
         datetime M1T = iTime(_Symbol,PERIOD_M1,M1Bar);
         
         while (M5Bar>0 && M1T>=iTime(_Symbol,PERIOD_M5,M5Bar-1))
         {
            M5Bar--;
            if (M1T<iTime(_Symbol,PERIOD_M5,M5Bar-1))
               break;
         }
         Print ("TimeCurrent() = ", TimeCurrent(), ", ",
                "iAO M1 = ", DoubleToStr(iAO(Symbol(),PERIOD_M1, M1Bar)), ", ",
                "iAO M5 = ", DoubleToStr(iAO(Symbol(), PERIOD_M5, M5Bar)));
         M1Bar--;
      }
      LT = iTime(_Symbol,PERIOD_M1,0);
   }
  }

The green part is where I check the times and change my bar number for PERIOD_M5, and the yellow part is where I tested your claim that these calls doesn't work in PERIOD_M1 in expert? Well, they work... I even set my chart timeframe to PERIOD_H1.

 

You helped me to solve my problem. You gave me a very elaborate response. Thank you very much for spending your time to help me. Very appreciated.


Although there is one thing that I did not fully understand in the next two lines below (numbers in green).


int M1Bar = MathMin(20,iBars(_Symbol,PERIOD_M1)-1);

int M5Bar = iBars(_Symbol,PERIOD_M5)-2;


I also want to make my own contribution to the resolution of this problem. Because I found an alternative way to do the same thing. (And you gave me the idea and the knowledge, thx u).



The follow  works for me in a indicator program:


int start()                         

{ 

   int i, Counted_bars;    

   Counted_bars=IndicatorCounted();  

   i=Bars-Counted_bars-1; 



   while(i>=0)                     

   { 

     datetime CT = iTime(Symbol(), PERIOD_CURRENT,i);

     int BM1 = iBarShift(Symbol(), PERIOD_M1,  CT);

     int BM5 = iBarShift(Symbol(), PERIOD_M5,  CT);   

      

      if( iAO(Symbol(),PERIOD_M1,BM1) > iAO(Symbol(),PERIOD_M5, BM5)  )      

      {       

                .....    

      }

        i--;

  }



For a Expert Advisor is enough to do this:


int start()
{  

     datetime CT = iTime(Symbol(), PERIOD_CURRENT,0);

     int BM1 = iBarShift(Symbol(), PERIOD_M1,  CT);

     int BM5 = iBarShift(Symbol(), PERIOD_M5,  CT);   

      

      if( iAO(Symbol(),PERIOD_M1,BM1) > iAO(Symbol(),PERIOD_M5, BM5)  )      

      {       

                .....    
      }
 }

      
 
ivanez:

You helped me to solve my problem. You gave me a very elaborate response. Thank you very much for spending your time to help me. Very appreciated.

Although there is one thing that I did not fully understand in the next two lines below (numbers in green).

I also want to make my own contribution to the resolution of this problem. Because I found an alternative way to do the same thing. (And you gave me the idea and the knowledge, thx u).

The follow  works for me in a indicator program:

For a Expert Advisor is enough to do this:

Great!

Ok, regarding the 20, -1 and -2, please remove the MathMin and 20 - was for my own testing (oops). as for -1 and -2, they're included to prevent subsequent codes to access non-existent bars.

As for iBarShift, it is convenient and straightforward, but if you notice that your code takes unusually long to run, consider alternative instead.

 
Seng Joo Thio:

Great!

Ok, regarding the 20, -1 and -2, please remove the MathMin and 20 - was for my own testing (oops). as for -1 and -2, they're included to prevent subsequent codes to access non-existent bars.

As for iBarShift, it is convenient and straightforward, but if you notice that your code takes unusually long to run, consider alternative instead.

Yes. i see the signal is not perfect when i to use iBarShift().  it has not curves. It only has straight lines. Maybe this function is not efficient and caused a signal delay.

 
ivanez:

Yes. i see the signal is not perfect when i to use iBarShift().  it has not curves. It only has straight lines. Maybe this function is not efficient and caused a signal delay.

On the contrary, this function is more efficient than retrieving and comparing datetimes by calling iTime(), when tested on less than 15-20 consecutive calls (although the time saving isn't great, since we're talking about differences in terms of microseconds).

However, once you need to make continuous calls for as many as hundreds/thousands or more bars within one loop, iBarShift() pales in comparison (will chalk up total time differences in the region of milliseconds, depending on pc speed).

Apart from time/performance differences, I doubt iBarShift() will cause the issue that you described above... So there could be a bug in your code...

 
Seng Joo Thio:

On the contrary, this function is more efficient than retrieving and comparing datetimes by calling iTime(), when tested on less than 15-20 consecutive calls (although the time saving isn't great, since we're talking about differences in terms of microseconds).

However, once you need to make continuous calls for as many as hundreds/thousands or more bars within one loop, iBarShift() pales in comparison (will chalk up total time differences in the region of milliseconds, depending on pc speed).

Apart from time/performance differences, I doubt iBarShift() will cause the issue that you described above... So there could be a bug in your code...

OK, I think I already know what is happening. Straight lines come out when the signal is seen in a time frame smaller than the signal occurs.


This is the code:


     datetime CT = iTime(Symbol(),  PERIOD_CURRENT,i);
     int BM1  = iBarShift(Symbol(), PERIOD_M1,  CT);
     int BM5  = iBarShift(Symbol(), PERIOD_M5,  CT);
     int BM15 = iBarShift(Symbol(), PERIOD_M15, CT);   
     
     
     Buf_0[i]=iStochastic(Symbol(),PERIOD_M1,14,3,3,MODE_SMA, STO_LOWHIGH,MODE_MAIN,BM1);   //Pink
     Buf_1[i]=iStochastic(Symbol(),PERIOD_M5,14,3,3,MODE_SMA, STO_LOWHIGH,MODE_MAIN,BM5);   //Yellow    
     Buf_2[i]=iStochastic(Symbol(),PERIOD_M15,14,3,3,MODE_SMA, STO_LOWHIGH,MODE_MAIN,BM15); //Red


I mean, i think that the signal in M1 has more points (ticks) per unit of time than the M5 signal, and the M5 signal has more points than the M15 signal. (M1> M5> M15). So when you see M15 and M5 in M1 you see the signal composed of straight lines.


What do you think about it?

Will it be possible to improve the signal quality in some way?


This is the graphics and signals in M1, M5 and M5 timeframe:

Files:
M1.png  18 kb
M5.png  27 kb
M15.png  28 kb
 
ivanez:

I mean, i think that the signal in M1 has more points (ticks) per unit of time than the M5 signal, and the M5 signal has more points than the M15 signal. (M1> M5> M15). So when you see M15 and M5 in M1 you see the signal composed of straight lines.

What do you think about it?

Will it be possible to improve the signal quality in some way?

You got it!

I think we'll just have to accept that we cannot really plot smaller time frame resolution on larger time frame charts. However, if there's a need to compute the signals in EA, you can always use PERIOD_M1 instead of PERIOD_CURRENT for CT.

 
Seng Joo Thio:

You got it!

I think we'll just have to accept that we cannot really plot smaller time frame resolution on larger time frame charts. However, if there's a need to compute the signals in EA, you can always use PERIOD_M1 instead of PERIOD_CURRENT for CT.

Ok!! I'll keep doing tests.

Thank you very much for your great help.

Reason: