how to get the previous data of indicator

 

In Mql4, there is a shift in many indicators, that I can get the value in bar number 0, in bar number 1 etc

but not in Mql5...

How can I get these values in Mql5?

 
BrunoPio:

In Mql4, there is a shift in many indicators, that I can get the value in bar number 0, in bar number 1 etc

but not in Mql5...

How can I get these values in Mql5? 

Hi BrunoPio,

Please read MQL5 doc Timeseries and Indicators Access, especially CopyBuffer() function - click those.

:D  

 
onewithzachy:

Hi BrunoPio,

Please read MQL5 doc Timeseries and Indicators Access, especially CopyBuffer() function - click those.

:D  

Ok...

But its is a kind of dificult to me...

What is the problem with this code to get the bar number 1 in iTEMA?


//+------------------------------------------------------------------+
//|                                                       TEMA-1.mq5 |
//|                                              http://www.mql5.com |
//+------------------------------------------------------------------+
#property link      "http://www.mql5.com"
#property version   "1.00"
 
#property indicator_separate_window
#property indicator_buffers 2
#property indicator_plots   1
//---- plot MA
#property indicator_label1  "TEMA-1"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrRed
#property indicator_style1  STYLE_SOLID
#property indicator_width1  2
//--- input parameters
input int                  ma_period=14;              // period of averaging
input int                  ma_shift=0;                // shift
input ENUM_APPLIED_PRICE   applied_price=PRICE_CLOSE; // type of price
input string               symbol=" ";                // symbol 
input ENUM_TIMEFRAMES      period=PERIOD_CURRENT;     // timeframe
//--- indicator buffers
double                   TEMABuffer[];
int                      tema_handle;
double                   TEMA1[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,TEMABuffer,INDICATOR_DATA);
   SetIndexBuffer(1,TEMA1,INDICATOR_CALCULATIONS);
//---
   tema_handle=iTEMA(symbol,period,ma_period,ma_shift,applied_price);
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const int begin,
                const double &price[])
  {
   CopyBuffer(tema_handle,0,0,rates_total,TEMA1);
   for(int i=0;i<rates_total;i++)
   {
      TEMABuffer[i] = TEMA1[i-1];
     
   }
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+


 
BrunoPio:

Ok...

But its is a kind of dificult to me...

What is the problem with this code to get the bar number 1 in iTEMA?

Hi BrunoPio,

1. Don't forget about that input symbol otherwise you get err 4302 (Symbol is not selected in MarketWatch). Better yet make it like this if you want.

tema_handle = iTEMA(Symbol(),period,ma_period,ma_shift,applied_price);

2. Let's display your CI first, this is how to display your CI. Always use if statement when using any Copy...() function, and return zero if it fails.

    ResetLastError();
   if (CopyBuffer(tema_handle,0,0,rates_total,TEMABuffer) <= 0) 
     {
     Print (GetLastError());  
     return(0);
     }
  /* for(int i=0;i<rates_total;i++)
   {
      TEMABuffer[i] = TEMA1[i-1];
     
   }*/

3. There are 3 ways to get value of bar 1,

1. use input ma_shift = 1 and it will be displayed shifted to the right.

2. use TEMABuffer [rates_total - 2] and input ma_shift = 0 but it won't displayed shifted to the right. This is because rates_total - 1 is bar 0, and so rates_total - 2 is bar 1. In MQL5 to get data of the most current bar (or bar 0 or the very right bar) we use rates_total - 1, while to get data of the oldest bar (or the very left bar) we use zero.

3. I haven't figure out the third way.

:D

 
onewithzachy:

Hi BrunoPio,

1. Don't forget about that input symbol otherwise you get err 4302 (Symbol is not selected in MarketWatch). Better yet make it like this if you want.

2. Let's display your CI first, this is how to display your CI. Always use if statement when using any Copy...() function, and return zero if it fails.

3. There are 3 ways to get value of bar 1,

1. use input ma_shift = 1 and it will be displayed shifted to the right.

2. use TEMABuffer [rates_total - 2] and input ma_shift = 0 but it won't displayed shifted to the right. This is because rates_total - 1 is bar 0, and so rates_total - 2 is bar 1. In MQL5 to get data of the most current bar (or bar 0 or the very right bar) we use rates_total - 1, while to get data of the oldest bar (or the very left bar) we use zero.

3. I haven't figure out the third way.

:D

 


It works!

=)

I tried to make a subtraction of bar 0 and bar 1 of TEMA to see a clear result, but the update is always 0...

=(


//+------------------------------------------------------------------+
//|                                                         TEMA.mq5 |
//|                                              http://www.mql5.com |
//+------------------------------------------------------------------+
#property link      "http://www.mql5.com"
#property version   "1.00"
 
#property indicator_separate_window
#property indicator_buffers 1
#property indicator_plots   1
//---- plot MA
#property indicator_label1  "TEMA"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrRed
#property indicator_style1  STYLE_SOLID
#property indicator_width1  2
//--- input parameters
input int                  ma_period=14;              // period of averaging
input int                  ma_shift=0;                // shift
input ENUM_APPLIED_PRICE   applied_price=PRICE_CLOSE; // type of price
input ENUM_TIMEFRAMES      period=PERIOD_CURRENT;     // timeframe
//--- indicator buffers
double                   TEMABuffer[];
double                   TEMABuffer1[];
double                   TEMABuffer2[];
int                      tema_handle1;
int                      tema_handle2;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,TEMABuffer,INDICATOR_DATA);
   SetIndexBuffer(1,TEMABuffer1,INDICATOR_CALCULATIONS);
   SetIndexBuffer(2,TEMABuffer2,INDICATOR_CALCULATIONS);

//---
   tema_handle1=iTEMA(Symbol(),period,ma_period,ma_shift,applied_price);
   tema_handle2=iTEMA(Symbol(),period,ma_period,ma_shift,applied_price);
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const int begin,
                const double &price[])
  {
   ResetLastError();
   if (CopyBuffer(tema_handle1,0,0,rates_total-1,TEMABuffer1) <= 0) 
     {
     Print (GetLastError());  
     return(0);
     }
   ResetLastError();
   if (CopyBuffer(tema_handle2,0,0,rates_total-2,TEMABuffer2) <= 0) 
     {
     Print (GetLastError());  
     return(0);
     }
   
   for(int i=0;i<rates_total && !IsStopped();i++)
   TEMABuffer[i] = TEMABuffer1[i]-TEMABuffer2[i];
   
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
 
BrunoPio:


It works!

=)

I tried to make a subtraction of bar 0 and bar 1 of TEMA to see a clear result, but the update is always 0...

=(


Hi BrunoPio, 

I don't understand why you want to subtract bar 0 with bar 1, but lets try this.

:D

//+------------------------------------------------------------------+
//|                                                         TEMA.mq5 |
//|                                              http://www.mql5.com |
//+------------------------------------------------------------------+
#property link      "http://www.mql5.com"
#property version   "1.00"
 
#property indicator_separate_window
#property indicator_buffers 1
#property indicator_plots   1
//---- plot MA
#property indicator_label1  "TEMA"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrRed
#property indicator_style1  STYLE_SOLID
#property indicator_width1  2
//--- input parameters
input int                  ma_period=14;              // period of averaging
input int                  ma_shift=0;                // shift
input ENUM_APPLIED_PRICE   applied_price=PRICE_CLOSE; // type of price
input ENUM_TIMEFRAMES      period=PERIOD_CURRENT;     // timeframe
//--- indicator buffers
double                   TEMABuffer[];
double                   TEMABuffer1[];
double                   TEMABuffer2[];
int                      tema_handle1;
int                      tema_handle2;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,TEMABuffer,INDICATOR_DATA);
   SetIndexBuffer(1,TEMABuffer1,INDICATOR_CALCULATIONS);
   //SetIndexBuffer(2,TEMABuffer2,INDICATOR_CALCULATIONS);

//---
   tema_handle1=iTEMA(Symbol(),period,ma_period,ma_shift,applied_price);
   //tema_handle2=iTEMA(Symbol(),period,ma_period,ma_shift,applied_price);
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const int begin,
                const double &price[])
  {
   //static int Tick;
   //Tick ++;
   //Print("1 Tick ",IntegerToString(Tick)," prev_calculated ",prev_calculated," rates_total ",rates_total);
  
   ResetLastError();
   if (CopyBuffer(tema_handle1,0,0,rates_total,TEMABuffer1) <= 0) 
     {
     Print (GetLastError());  
     return(0);
     }
     
   /*
   ResetLastError();
   if (CopyBuffer(tema_handle2,0,0,rates_total,TEMABuffer2) <= 0) 
     {
     Print (GetLastError());  
     return(0);
     }
   */
   for(int i = prev_calculated - 1;i < rates_total && !IsStopped(); i ++)
      {
      if (i - 1 < 0)continue;
      TEMABuffer[i] = (TEMABuffer1[i] - TEMABuffer1[i - 1]);//*MathPow(10, _Digits);
      }
   
   //Print("Tema ", DoubleToString (TEMABuffer [rates_total - 1], 20));
   
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+

 

 
onewithzachy:

Hi BrunoPio, 

I don't understand why you want to subtract bar 0 with bar 1, but lets try this.

:D

Hi!

It does not update too...

I close, compile, close again and again and again... (MT and MetaEditor)

But I do not know why the last value is always 0 and do not update...

=(


EDIT: try to see it in M1.

 
BrunoPio:

Hi!

It does not update too...

I close, compile, close again and again and again... (MT and MetaEditor)

But I do not know why the last value is always 0 and do not update...

=(


EDIT: try to see it in M1.

I can see...

TEMABuffer1 return 0...

It is why TEMABuffer return 0...

=(

 

Hi BrunoPio,

Sorry, I just edit the code,  would please take a look at it.

Thank you.

PS : Your buffer return value is very small like 0.00000012345, so it's look like the return is zero, while its not. So try to print its value with 10 or 20 digits after decimal point.

:D 

 
onewithzachy:

Hi BrunoPio,

Sorry, I just edit the code,  would please take a look at it.

Thank you.

PS : Your buffer return value is very small like 0.00000012345, so it's look like the return is zero, while its not. So try to print its value with 10 or 20 digits after decimal point.

:D 

Hi!

I was thinking about it...

In tick, the value will be very small...

You are so fast and nice!

Thank you!

I uploaded 2 indicators because my happiness in learning Mql5 and for people like you (and everyone!)!

:D

 
BrunoPio:

Hi!

I was thinking about it...

In tick, the value will be very small...

You are so fast and nice!

Thank you!

I uploaded 2 indicators because my happiness in learning Mql5 and for people like you (and everyone!)!

:D

Hi BrunoPio,

Great !, Hopes that's what you looking for. My apology for the previous error in code. I was still working on MT4 and still logged on MQL5 so I read your reply, and decided to take a look again at that codes. Hopes, there's no error now,I tested it, but its Tokyo hours now and everything move a little slow on EURUSD, I know its 30 pips moves but still ... :)

:D 

Reason: