Trying to calculate RSI type period on my code

 

Hi 

i ve got the following code that scans the bars on a chart from right to left. What it does is to get the bar from the current TF i m looking at and get down to M1 timeframe and scan the bars there.

Eg if were looking at the H1 timeframe then the loop will get each H1 bar and get down to M1 timeframe and scan this 60 x M1 bars till the H1 bar its over.

   counted_bars = IndicatorCounted();
   
   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
         }
                                                       
    total_1M_Bars=candles*Period(); // maximum M1 bars.  
    i=1; // bar counter for higher timeframe. ignore zero bar. 

   dt1=Time[i]; //search from date
   dt2=Time[i-1]-60; //to date
   
     shift=1+MathMod( TimeMinute(dt), (per) );       
      
    // Scan bars (from right to left) 
    while ( (i<=candles) && (shift < total_1M_Bars ) )
    {
         dt=iTime(Symbol(),PERIOD_M1,  shift  ); //get M1 candle based on shift
         if (dt==0) return;         
         
         if ( dt1 <= dt   &&   dt <= dt2  )  // if candle with time 'dt' is between the desired dt1 and dt2  then...
         {
            //C= iClose(Symbol(),PERIOD_M1,shift);
            //O=iOpen(Symbol(),PERIOD_M1,shift);
            //temp+=C+O; //just some calculations for the example
            
            shift++; // go to the next 1M candle
         }
         else // we are outside the current (higher timeframe) bar. Proceed to next higher TF bar
         {
            i++;
            dt1=Time[i]; //search from date
            dt2=Time[i-1]-60; //to date
            //Buf_0[i]=temp/Period(); //just something to draw for the example
            //temp=0; //just for the example
         }
      }  // while 

Now my problem is that i cant find a way to change my code so to check for a specific period time (eg 14 Period, 21 Period etc).
By period i mean how many bars to calculate back...that extern parameter that many indicator has like MAs, RSI etc. eg 14RSI, 21SMA etc... i do not mean the timeframe period.

 
athanfx:

Hi 

i ve got the following code that scans the bars on a chart from right to left. What it does is to get the bar from the current TF i m looking at and get down to M1 timeframe and scan the bars there.

Eg if were looking at the H1 timeframe then the loop will get each H1 bar and get down to M1 timeframe and scan this 60 x M1 bars till the H1 bar its over.

Now my problem is that i cant find a way to change my code so to check for a specific period time (eg 14 Period, 21 Period etc).
By period i mean how many bars to calculate back...that extern parameter that many indicator has like MAs, RSI etc. eg 14RSI, 21SMA etc... i do not mean the timeframe period.


I'm only half understanding what y are trying to do and what your desired end result is,  your variable names don't help .. .   dt1, dt2, cndles, shift, etc  it will help if you use more descriptive names.

This is a problem . . . .

shift = 1 + MathMod( TimeMinute(dt), (per) );

 I assume shift is an int ?  MathMod() returns a double and takes 2 doubles,  so you give it 2 datetimes and use the result with an int ?  why don't you use  %  ?  in fact why don't you just us iBarShift () ?

 
RaptorUK:

I'm only half understanding what y are trying to do and what your desired end result is,  your variable names don't help .. .   dt1, dt2, cndles, shift, etc  it will help if you use more descriptive names.

This is a problem . . . .

 I assume shift is an int ?  MathMod() returns a double and takes 2 doubles,  so you give it 2 datetimes and use the result with an int ?  why don't you use  %  ?  in fact why don't you just us iBarShift () ?

 


thanks for your answer. I ll repost my code in a more complete form.

int start()
{
   
datetime dt,mdt,dt1,dt2; 
int  i, j, k, candles, counted_bars,per,shift,total_1M_Bars,minBars;

 counted_bars = IndicatorCounted();
         
   
   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
         }
     
                                                       
    total_1M_Bars=candles*Period(); // maximum M1 bars.  Limit for j
    i=1; // bar counter. ignore zero bar. Start from 1   eg. 1..99  // for 100 candles

   dt1=Time[i]; //set search from candle date (higher timeframe candle. eg. for H1 dt1=11:00, dt2=11:59 or for M15 dt1=13:15, dt=13:29  
   dt2=Time[i-1]-60; 
   
      dtTemp=dt2;
      found=-1;
      tries=0;
      
      //************** CALCULATE M1 SHIFT   
      //Calculate shift on M1 timeframe
      while (found==-1 && tries<total_1M_Bars)     //&&tries<maxBars to avoid endless loop
      {
          found=iBarShift( Symbol(), PERIOD_M1  , dtTemp ,  true );
          if (found==-1) dtTemp=dtTemp-60;  //search 1 candle (M1) to the left
          tries++;
      }
          shift=found;
      
    // Scan bars starting from right to left 
    // while i<higher tf candles and 
    while ( (i<=candles) && (shift < total_1M_Bars ) )
    {
         dt=iTime(Symbol(),PERIOD_M1,  shift  ); //get datetime of current shifted M1 candle
                  
         if ( dt1 <= dt   &&   dt <= dt2  )  // if datetime of M1 bar (dt) is between the desired dt1 and dt2  then...
         {  
            C= iClose(Symbol(),PERIOD_M1,shift); //just some calculations
            result=result+calculations(C);//just some calculations
            shift++; // go to the next M1 candle
         }
         else // we are outside the current (higher timeframe) bar. Proceed to next higher TF bar, but keep same j (1M bar)
         {
            Buf_0[i]=result; //just some calculations
            i++; //go to next higher TF candle
            dt1=Time[i]; //search from date
            dt2=Time[i-1]-60; //to date
            result=0; //just some calculations
         }
      }  // while 

  return(0);
}
 
  1.   total_1M_Bars=candles*Period(); // maximum M1 bars.  
    There can be missing bars on the M1 vs higher TF. There is Max Bars on Chart. BOGUS
  2. Don't double post. You were already given the solution for this previously. Algorithm/Timeframes problem - MQL4 forum
 
WHRoeder:
  1. There can be missing bars on the M1 vs higher TF. There is Max Bars on Chart. BOGUS
  2. Don't double post. You were already given the solution for this previously. Algorithm/Timeframes problem - MQL4 forum


Hi and thanks for your help.

this is not a double post since what i m asking its completely different with my previous post.

As i said in my first post what i m trying to do is no matter on which TF i am to find a way to check for a specific period time (eg 14 Period, 21 Period etc). 
By period i mean how many bars to calculate back...that extern parameter that many indicator has like MAs, RSI etc. eg 14RSI, 21SMA etc... i do not mean the timeframe period

Also by the total_1M_Bars=candles*Period();  works fine for me. I do not care if there will be any missing bars cause i check for it later on in the 

 dt=iTime(Symbol(),PERIOD_M1,  shift  ); //get datetime of current shifted M1 candle
                  
         if ( dt1 <= dt   &&   dt <= dt2  )  // if datetime of M1 bar (dt) is between the desired dt1 and dt2  then...

 Although thanks for noticing cause now that i m thinking off maybe this is the reason for some hangs i m still getting through weekends.

Debugging in MT4 reminds me the old days back in 80s and 90s...  

Reason: