Algorithm/Timeframes problem

athan
249

Hi

 i ve got the following algorithm.  what i m trying to do is to check 1M TF from a higher TF. 

 while (i>=0)  // i is the standard IndicatorCounted()
    {
     log ("while i is :"+i);    
        
        for (j=0;j<Period();j++)
        {
            candles_look=i*Period()+j ;         
             dt=iTime(Symbol(),PERIOD_M1,candles_look);
        log("Checking candle: "+TimeDay(dt)+"/"+TimeMonth(dt)+"/"+TimeYear(dt)+" "+TimeHour(dt)+":"+TimeMinute(dt)+" i is:"+i+" i*Period()+j:"+(i*Period()+j) ); //Write data on a file
         }  // for j
           i--;
      }// while i    


But when i m running the above its a total mess the way it brings the data!  This is what i get on the log output when i switch on the 5M TF:

14:47:58 - - - while i is :49

14:47:58 - - - Checking candle: 30/1/2013 12:47 i is:49 i*Period()+j:245

14:47:58 - - - Checking candle: 30/1/2013 12:46 i is:49 i*Period()+j:246

14:47:58 - - - Checking candle: 30/1/2013 12:45 i is:49 i*Period()+j:247

14:47:58 - - - Checking candle: 30/1/2013 12:44 i is:49 i*Period()+j:248

14:47:58 - - - Checking candle: 30/1/2013 12:43 i is:49 i*Period()+j:249

14:47:58 - - - while i is :48

14:47:58 - - - Checking candle: 30/1/2013 12:47 i is:48 i*Period()+j:240

14:47:58 - - - Checking candle: 30/1/2013 12:46 i is:48 i*Period()+j:241

14:47:58 - - - Checking candle: 30/1/2013 12:45 i is:48 i*Period()+j:242

14:47:58 - - - Checking candle: 30/1/2013 12:44 i is:48 i*Period()+j:243

14:47:58 - - - Checking candle: 30/1/2013 12:43 i is:48 i*Period()+j:244

14:47:58 - - - while i is :47

14:47:58 - - - Checking candle: 30/1/2013 12:47 i is:47 i*Period()+j:235

14:47:58 - - - Checking candle: 30/1/2013 12:46 i is:47 i*Period()+j:236

14:47:58 - - - Checking candle: 30/1/2013 12:45 i is:47 i*Period()+j:237

14:47:58 - - - Checking candle: 30/1/2013 12:44 i is:47 i*Period()+j:238

14:47:58 - - - Checking candle: 30/1/2013 12:43 i is:47 i*Period()+j:239

 etc etc. More or less same stuff in higher TFs as well.

 Any ideas? I know there is something wrong with the i*Period()+j but i m not sure how to solve this.

Thanks 

Simon Gniadkowski
17411
athanfx:

Hi

 i ve got the following algorithm.  what i m trying to do is to check 1M TF from a higher TF. 

You haven't exactly said what you are trying to achieve . . .   check M1 TF in what way ?

Perhaps you need . . .

candles_look = (i-1) * Period() + j; 

 ?

William Roeder
25844
William Roeder  
while (i>=0){ // i is the standard IndicatorCounted()
   int      iM1Beg = iBarShift(NULL, PERIOD_M1, Time[i]);
   if (i = 0) int iM1End = 0;
   else           iM1End = iBarShift(NULL, PERIOD_M1, Time[i-1])+1;
   for (j=iM1End;j>=iM1Beg;j++){
      dt=iTime(NULL, PERIOD_M1, j);
      :
athan
249
athan  

ok here is my code again

  int history=50, //how many bars to look back
  i, j,  candles, counted_bars, candles_look;
   
   int btbars=backtest_bars;   
   
    counted_bars=IndicatorCounted(); // amount of bars that haven't changed since the last start() call.
   
   if (candles<0) return (0);   
         candles=Bars-counted_bars-1;                     // Index of the first uncounted
 
     if (candles>history-1)                             // If too many bars ..
        candles=history-1;                             // ..calculate specified amount
     
        i=candles;   

    while (i>=0)   
    {
     
      log ("while i is :"+i);    
        
        // ---- calculate RM -----------------------
        for (j=0;j<Period();j++)
        {
               candles_look=i*Period()+j ;         
             dt=iTime(Symbol(),PERIOD_M1,candles_look);

                log("Checking candle: "+TimeDay(dt)+"/"+TimeMonth(dt)+"/"+TimeYear(dt)+" "+TimeHour(dt)+":"+TimeMinute(dt)+" i is:"+i+" i*Period()+j:"+(i*Period()+j) );
                  
         }  // for j
           i--;
      }// while i    

  return(0);
}

 what i m just trying to achieve is:

- no matter on what timeframe i am

- get every bar of the timeframe i am

-break it into 1M bars

- make some calculations

I use iTime to print all the bars that i bring back from the  timeframe i m looking at.

Now the main problem is that  instead of bringing back all bars in a series,  bars are suffled somehow!

 I m trying to find out how to solve this.

athan
249
athan  
WHRoeder:


Thanks for your reply.

The result of what i m getting is not what i m willing. The problem still occurs.

What i need to get is from a higher TF like H1, to proccess all 1M candles in a series.

I use j to check how many candles to go through on a lower TF (M1) for that Higher TF bar and then i get each 1M bar and display the date+time of the 1M bar.

But when i move on to the next higher TF bar (while loop, i --) then instead of displaying the following 1M bar occurance it displays a totally wrong one.

Any help? 

Simon Gniadkowski
17411
athanfx:


Any help?  

Try this . . .

  int history=50, //how many bars to look back
  i, j,  candles, counted_bars, candles_look;
   
   int btbars=backtest_bars;   
   
    counted_bars=IndicatorCounted(); // amount of bars that haven't changed since the last start() call.
   
   if (candles<0) return (0);   
         candles=Bars-counted_bars-1;                     // Index of the first uncounted
 
     if (candles>history-1)                             // If too many bars ..
        candles=history-1;                             // ..calculate specified amount
     
        i=candles;
   
    i *= Period();
    while (i>=0)   
    {
    log ("while i is :"+i);    
        
    dt=iTime(Symbol(), PERIOD_M1, i);

    log("Checking candle: "+TimeDay(dt)+"/"+TimeMonth(dt)+"/"+TimeYear(dt)+" "+TimeHour(dt)+":"+TimeMinute(dt)+" i is: "+i );
              
    i--;
    }// while i    

  return(0);
}
James Hodges
4532
James Hodges  

Ok I assume you know that if you are on H1 timeframe and you want to do some calculations on the M1 data that you can do that by simply putting that in the Call to your indicator. such as with a moving average you can say you want the 21 moving average of the M1 chart even though you are on a H1 chart.. iMA(NULL,1,21,0,MODE_SMA,PRICE_CLOSE,0);or for a M5 chart iMA(NULL,5,21,0,MODE_SMA,PRICE_CLOSE,0)

The numbers you received in your post are what you are asking for.. are starting with 50-1 or 49... and you are multiplying it by the timeframe which is 5 so you get 245. You have told it in your for loop to do this the number of times of the timeframe you are on and to keep incrementing the answer by 1 so it does it 5 times since you are on the 5 timeframe.. 49X5= 245,246,247,248,249, After it does this 5 times then it decrementts the 49 down to a 48 and mutliplies it by the timeframe of 5 and you get 240,241,242,243,244.... and then 47 times 5 235,236,237,238,239. I'm not sure exactly what you are trying to do here but I hope this will explain to you why you are getting the numbers written into the log that you are getting... I see no where in here that you ask for the open,high,low or close of these M1 candles so I'm not sure what data you are trying to parse into M1 candles.. but all that data can be called from the M1 data that is already in MT4 as soon as you open it. for instance if you want the open of a Minute bar 12 minutes ago you could use iOpen(NULL,1,12) you can also use iHigh, iLow, and iClose.

double iOpen( string symbol, int timeframe, int shift)
 

 Hope this helps... Jimdandy.

athan
249
athan  
RaptorUK:

Try this . . .


thanks. i finally found out how to do it. 

it was quite tricky but it was possible to done in 4 different ways. 

William Roeder
25844
William Roeder  

athanfx: What i need to get is from a higher TF like H1, to proccess all 1M candles in a series.

for (j=iM1End;j>=iM1Beg;j++){
      dt=iTime(NULL, PERIOD_M1, j); // All M1 candles inclusive the H1[i] bar.
Exactly what I posted. You can NOT use i*period() etc, because of possible missing bars.
athan
249
athan  
WHRoeder:
Exactly what I posted. You can NOT use i*period() etc, because of possible missing bars.


Why not? actually what i ve used is a  the following code. Now my main problem is how to check if the M1 bar is between the i bar (higher TF i m looking on the screen) limits. eg. if i m looking at M30 TF and 1M bar i m checking is 18:12 then i need ot make a check that this 1M candle is between 18:00-18:29. I tried quite a few ways but i think i need to change the whole loop algorithm and use just one while loop.

Any ideas?

      i=0;
        
    while (i<=candles)  
    {
        for (j=Period()-1;j>=0;j--) 
        {
            int tempTime=TimeMinute(iTime(Symbol(),PERIOD_M1,0));
            int shift=MathMod(tempTime,Period())+1;   
            

            int ipj=  i*Period()+j+shift;  

            dt=iTime(Symbol(),PERIOD_M1,ipj); 

William Roeder
25844
William Roeder  
  1. athanfx: Why not? 
    What part of "because of possible missing bars." is not clear. If you have 25 M1 bars covering one M30 bar, then M1(i+30) means you are looking at a M1 bar beyond the M30 bar.
  2. athanfx: Now my main problem is how to check if the M1 bar is between the i bar
    In the code I posted
    for (j=iM1End;j>=iM1Beg;j++){
    ALL OF THE BARS (iM1Beg to iM1End inclusive) were inside the higher TF bar.
Asked and Answered.