Help needed : working on 3 different Times Frames : values change according to TimeFrame used on MT4

 

Hello,

My EA use date from 3 different Time Frames (I have incorporated an indicator code).

It gets correct values concerning the Time Frame I am viewing on my MT4 but wrong results for the others I am not seeing.

If I change and look at an other Time Frame, then the values are ok for this one and get wrong for the previous one:

if I am on the M1 : value are correct for M1 but not for M5, M15 related values.

if I change the Time Frame to M5, the values are correct for M5 now, but become wrong for M1 and M15.

Does anyone has an idea on what's happenning and how to react ?

exemple of an instruction :

Low(NULL, PERIOD_M5, MODE_LOW, 8, 1)

Thanks for your answer.

 

Show your code

 
phy wrote >>

Show your code

Here it is

// -------------------------------------------------
// H1StochSignal()  
// -------------------------------------------------
int H1StochSignal()
  {
///////////////////////////////////////////
// Perform Stochastic value for H1
///////////////////////////////////////////
//---- input parameters
int qPeriod = 8;
int rPeriod = 3;
int EMAfast = 3;
int CountBars = 3;
//---- buffers
double TDS[3];
double TDS_EMA[3];
//----
double krPeriod, kEMAfast;
double mtm, mtmInt, mtmIntPrev, mtm1, mtmInt1, mtmIntPrev1, DS_EMA;
double DS_EMAtopPrev, DS_EMAtop, DS_EMAbottomPrev, DS_EMAbottom, DS;
//---- 
   krPeriod = 2 / (rPeriod + 1.0);
   kEMAfast = 2 / (EMAfast + 1.0);
//----
int shift;
   mtmIntPrev = 0.0; 
   mtmIntPrev1 = 0.0; 
   DS_EMAtopPrev = 0.0; 
   DS_EMAbottomPrev = 0.0;
   if (Bars < CountBars + qPeriod)
      {if (DebugMode) {Print ("Not enough Bars for calculation, Bars = ", Bars);}
       return(0);
      }
   for(shift = CountBars; shift >= 0; shift--)
     {
       Print("==> M1 bars : ", iBars(NULL,PERIOD_M1));
       Print("==> M5 bars : ", iBars(NULL,PERIOD_M5));
       Print("==> H1 bars : ", iBars(NULL,PERIOD_H1));                               (Bars numbers ok in all cases)
       mtm = Close[shift] - Low[Lowest(NULL, PERIOD_M1, MODE_LOW, qPeriod, shift)];
       Print("==> M1 : ", mtm);                                                        <---
       mtm = Close[shift] - Low[Lowest(NULL, PERIOD_M5, MODE_LOW, qPeriod, shift)];
       Print("==> M5 : ", mtm);                                                        <---  Those values differs
       mtm = Close[shift] - Low[Lowest(NULL, PERIOD_H1, MODE_LOW, qPeriod, shift)];
       Print("==> H1 : ", mtm);                                                        <---
       if(mtm == 0) 
           mtm = Point;
       mtm1 = High[iHighest(NULL, PERIOD_H1, MODE_HIGH, qPeriod, shift)] - 
              Low[iLowest(NULL, PERIOD_H1, MODE_LOW, qPeriod, shift)];
       if(mtm1 == 0) 
           mtm1 = Point;
       mtmInt = mtmIntPrev + krPeriod*(mtm - mtmIntPrev); 
       mtmInt1 = mtmIntPrev1 + krPeriod*(mtm1 - mtmIntPrev1); 
       DS_EMAtop = DS_EMAtopPrev + kEMAfast*(mtmInt - DS_EMAtopPrev); 
       DS_EMAbottom = DS_EMAbottomPrev + kEMAfast*(mtmInt1 - DS_EMAbottomPrev); 
       if(!CompareDouble(mtmInt1, 0.0))
           DS = 100*mtmInt / mtmInt1;
       if(!CompareDouble(DS_EMAbottom, 0.0))       
           DS_EMA = 100*DS_EMAtop / DS_EMAbottom;
       TDS[shift] = DS;
       TDS_EMA[shift] = DS_EMA;
       mtmIntPrev = mtmInt; 
       mtmIntPrev1 = mtmInt1; 
       DS_EMAtopPrev = DS_EMAtop; 
       DS_EMAbottomPrev = DS_EMAbottom;
     }
///////////////////////////////////////////////
// Performing Stochastic values for H1 - END -
/////////////////////////////////////////////// 


      //
      if (DebugMode) {Print("DS = ", TDS[1],"DS_EMA = ", TDS_EMA[1]);}
      if (DebugMode) {Print("StochGap = ", TDS[1] - TDS_EMA[1]);}
      if (TDS[1] - TDS_EMA[1] > 0)
         {if (DebugMode) {Print("H1StochSignal : BUY");}
          return(DIRECTION_BUY);
         }
      else   
         {if (DebugMode) {Print("H1StochSignal : SELL");}
          return(DIRECTION_SELL);
         }
   } 
   
//+------------------------------------------------------------------+
//| CompareDouble                      |
//+------------------------------------------------------------------+
bool CompareDouble (double Number1, double Number2)
  {
    bool Compare = NormalizeDouble(Number1 - Number2, 8) == 0;
    return(Compare);
  }
//+------------------------------------------------------------------+

 

Those values differs

Well...

assume shift == 3

mtm = Close[3] - Low[Lowest(NULL, PERIOD_M1, MODE_LOW, 8, 3)]; // returns Chart Close of 3 periods ago minus lowest low of 8 1 minute periods 3 minutes ago
mtm = Close[3] - Low[Lowest(NULL, PERIOD_M5, MODE_LOW, 8, 3)]; // returns Chart Close of 3 periods ago minus lowest low of 8 5 minute periods 15 minutes ago
mtm = Close[3] - Low[Lowest(NULL, PERIOD_H1, MODE_LOW, 8, 3)]; // returns Chart Close of 3 periods ago minus lowest low of 8 60 minute periods 3 hours ago

... which is probably not what you were looking for

So, rethink the logic.

 
phy wrote >>

Well...

assume shift == 3

mtm = Close[3] - Low[Lowest(NULL, PERIOD_M1, MODE_LOW, 8, 3)]; // returns Chart Close of 3 periods ago minus lowest low of 8 1 minute periods 3 minutes ago
mtm = Close[3] - Low[Lowest(NULL, PERIOD_M5, MODE_LOW, 8, 3)]; // returns Chart Close of 3 periods ago minus lowest low of 8 5 minute periods 15 minutes ago
mtm = Close[3] - Low[Lowest(NULL, PERIOD_H1, MODE_LOW, 8, 3)]; // returns Chart Close of 3 periods ago minus lowest low of 8 60 minute periods 3 hours ago

... which is probably not what you were looking for

So, rethink the logic.

Here I corrected all instructions wich fetch values from other time frame (exple: iClose replace Close, iLow, Low, etc.)
That should respect the logic of the indicator from wich I have this code.
The problem is now : the iClose instructions return always the same value : the last bar close value from M1 (time frame where I put the EA on) .
I dont understand why. I was expecting that, for exemple, iClose(NULL,PERIOD_M5,1) would have return the Close value of the last candle of the M5 timeframe, and so what so ever the time frame where I run the EA.
Do you have an explaination ? Do I misunderstand some thing? I have been thru codes of EA using different timeframe on the website and I haven't seen something special... (thank you so much for your attention)
for(shift = CountBars; shift >= 0; shift--)
     {
       Print("==> M1 bars : ", iBars(NULL,PERIOD_M1));
       Print("==> M5 bars : ", iBars(NULL,PERIOD_M5));
       Print("==> H1 bars : ", iBars(NULL,PERIOD_H1));

       Print(" H1  CLOSE: ",iClose(NULL,PERIOD_H1,shift));
       Print(" M5  CLOSE: ",iClose(NULL,PERIOD_M5,shift));
       Print(" M1  CLOSE: ",iClose(NULL,PERIOD_M1,shift));
       Print(" OLD CLOSE: ",Close[1]);

       mtm = iClose(NULL,PERIOD_M1,shift) - Low[iLowest(NULL, PERIOD_M1, MODE_LOW, qPeriod, shift)];
       Print("==> M1 : ", mtm);
       mtm = iClose(NULL,PERIOD_M5,shift) - Low[iLowest(NULL, PERIOD_M5, MODE_LOW, qPeriod, shift)];
       Print("==> M5 : ", mtm);   
       mtm = iClose(NULL,PERIOD_H1,shift) - Low[iLowest(NULL, PERIOD_H1, MODE_LOW, qPeriod, shift)];
       Print("==> H1 : ", mtm);
       if(mtm == 0) 
           mtm = Point;
       mtm1 = High[iHighest(NULL, PERIOD_H1, MODE_HIGH, qPeriod, shift)] - 
              Low[iLowest(NULL, PERIOD_H1, MODE_LOW, qPeriod, shift)];
       if(mtm1 == 0) 
           mtm1 = Point;
       mtmInt = mtmIntPrev + krPeriod*(mtm - mtmIntPrev); 
       mtmInt1 = mtmIntPrev1 + krPeriod*(mtm1 - mtmIntPrev1); 
       DS_EMAtop = DS_EMAtopPrev + kEMAfast*(mtmInt - DS_EMAtopPrev); 
       DS_EMAbottom = DS_EMAbottomPrev + kEMAfast*(mtmInt1 - DS_EMAbottomPrev); 
       if(!CompareDouble(mtmInt1, 0.0))
           DS = 100*mtmInt / mtmInt1;
       if(!CompareDouble(DS_EMAbottom, 0.0))       
           DS_EMA = 100*DS_EMAtop / DS_EMAbottom;
       TDS[shift] = DS;
       TDS_EMA[shift] = DS_EMA;
       mtmIntPrev = mtmInt; 
       mtmIntPrev1 = mtmInt1; 
       DS_EMAtopPrev = DS_EMAtop; 
       DS_EMAbottomPrev = DS_EMAbottom;
     }
 

iClose() with shift of 0 returns the same values on all timeframes. It is the current price.

iClose() on different timeframes with shift > 0 (historical bars) will show different values

I dont understand why. I was expecting that, for exemple, iClose(NULL,PERIOD_M5,1) would have return the Close value of the last candle of the M5 timeframe, and so what so ever the time frame where I run the EA.
That will return the value of the last completed 5m bar, yes.
 
phy wrote >>

iClose() with shift of 0 returns the same values on all timeframes. It is the current price.

iClose() on different timeframes with shift > 0 (historical bars) will show different values

That will return the value of the last completed 5m bar, yes.

I haven't found the solution but I resolved my problem by leaving the indicator outside of my EA. Thanks for all, I really appreciated.

Reason: