not sure how to reference previous buffer value

 

Hello Forum

Wondering if someone might explain to me, or point me to the right part of the documentation ( I am not sure what topic to look under) how I go about achieving the following.

I have attached a simple indicator that draws up arrows when certain conditions are satisfied and down arrows when other conditions are satisfied.

The conditions themselves aren't important here and the attached example has grossly simplified conditions just to aid clarity

Anyway I would like to add consideration of divergence to my indicator, but I am not sure how to reference the previous occurrence that my buffer conditions are satisfied.

In this example when the ExtMapBuffer[i] conditions are satisfied and an up pointing arrow is drawn.

But if I want to compare the MACD values for this candle to the MACD value that occurred the last time an arrow was drawn, I need to be able to define last successful buffer.  

And I am not sure how to do this? I assume each arrow drawn by a buffer becomes a unique object, but how are they referenced?

Any help would be appreciated, thanks in advance.

   //+-----------------------------------------------------------------------------------------------------------------------------+
//|                                                 system.mq4                                                             |
//|                                                                                                                             |
//+-----------------------------------------------------------------------------------------------------------------------------+
#property copyright "Copyright © 2006, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net/"
//------------------------------------------------------------------------------------------------------------------------------|
#property indicator_chart_window
#property indicator_buffers                           2
#property indicator_color1                            LimeGreen      //-- Long  Trade Arrows      
#property indicator_color2                            OrangeRed      //-- Short Trade Arrows

#property indicator_width1                            1
#property indicator_width2                            1

//------------------------------------------------------------------------------------------------------------------------------|
//---- input parameters
//
   extern string       Gap_CandleToEnergyArrow_Help   ="GapBetweenEnergyArrow&Candle";
   extern double       EnergyArrowGap                 =1;
      
//------------------------------------------------------------------------------------------------------------------------------|
//---- system variables
   double              c0;  
   double              c1;
   double              arrow,arrowUp,arrowDown;
   
//------------------------------------------------------------------------------------------------------------------------------|
//---- buffers
   double              ExtMapBuffer1[];                              //-- Long Trade Energy Arrows
   double              ExtMapBuffer2[];                              //-- Short Trade Entry Arrows
   
//------------------------------------------------------------------------------------------------------------------------------|  
//| Custom indicator initialization function                                                                                    |
//+-----------------------------------------------------------------------------------------------------------------------------+
int init()
  {
//---- indicator buffers mapping  
   SetIndexBuffer      (0,ExtMapBuffer1);
   SetIndexBuffer      (1,ExtMapBuffer2); 
   
//------------------------------------------------------------------------------------------------------------------------------|
//---- drawing settings
   SetIndexStyle       (0,DRAW_ARROW);
   SetIndexArrow       (0,233);
   SetIndexStyle       (1,DRAW_ARROW);
   SetIndexArrow       (1,234);
   
//------------------------------------------------------------------------------------------------------------------------------|
//----
   SetIndexEmptyValue  (0,0.0);
   SetIndexEmptyValue  (1,0.0);
   
//------------------------------------------------------------------------------------------------------------------------------|
//---- name for DataWindow
   SetIndexLabel       (0,"TrEnergyLong");
   SetIndexLabel       (1,"TrEnergyShort");
   
//------------------------------------------------------------------------------------------------------------------------------|
//------------------------------------------------------------------------------------------------------------------------------
//---- initialization done   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
   {
   int counted_bars=IndicatorCounted();
   //---- the last calculated bar will be recalculated   
   if (counted_bars > 0) 
       counted_bars--;
   int limit = Bars - counted_bars;
   int Sit; 
//---- the main loop       
   for(int i = limit; i>0; i--)
   {

//------------------------------------------------------------------------------------------------------------------------------|
//--   Variable Calulations                                                                           |
//------------------------------------------------------------------------------------------------------------------------------|

   c0=iMA(NULL,0,10,0,MODE_EMA,PRICE_CLOSE,i);

   c1=(iMA(NULL,0,12,0,MODE_EMA,PRICE_CLOSE,i)-iMA(NULL,0,26,0,MODE_EMA,PRICE_CLOSE,i))-
           (iMA(NULL,0,12,0,MODE_EMA,PRICE_CLOSE,i+1)- iMA(NULL,0,26,0,MODE_EMA,PRICE_CLOSE,i+1));

   if  ((Close[i]>c0) && c1>0 && (Close[i]>High[i+1]))
   
        {                                                            //**********************************************************|
        ExtMapBuffer1[i]=(High[i]+(EnergyArrowGap)*Point);           //-- Draw Long Trade Arrows                                 |
        arrow = arrowUp;
        }                                                            //**********************************************************|
   
   else if ((Close[i]<c0) && c1<0 && (Close[i]<Low[i+1]) )
        
        {                                                           //**********************************************************|
        ExtMapBuffer2[i]=(Low[i]-(EnergyArrowGap)*Point);          //-- Draw Short Trade Arrows                                |
        arrow= arrowDown;
        }                                                            //**********************************************************|
    
   

            }                                                         
//----
   return(0);
  }
 
pullend:

Hello Forum

Wondering if someone might explain to me, or point me to the right part of the documentation ( I am not sure what topic to look under) how I go about achieving the following.

I have attached a simple indicator that draws up arrows when certain conditions are satisfied and down arrows when other conditions are satisfied.

The conditions themselves aren't important here and the attached example has grossly simplified conditions just to aid clarity

Anyway I would like to add consideration of divergence to my indicator, but I am not sure how to reference the previous occurrence that my buffer conditions are satisfied.

In this example when the ExtMapBuffer[i] conditions are satisfied and an up pointing arrow is drawn.

But if I want to compare the MACD values for this candle to the MACD value that occurred the last time an arrow was drawn, I need to be able to define last successful buffer.  

And I am not sure how to do this? I assume each arrow drawn by a buffer becomes a unique object, but how are they referenced?

Indicator arrows are not Objects,  don't even use the word object loosely to describe them or you risk confusing yourself and others.   Indicator buffers are,  by default,  filled with a value of EMPTY_VALUE,  if you don't change the value of the buffer index locations they will all contain EMPTY_VALUE.  So you can tell when a location has a value in it as it will no longer be EMPTY_VALUE.  To find the previous occurrence look through the previous values in the buffer until you find the next one that is not EMPTY_VALUE.
 
RaptorUK:
Indicator arrows are not Objects,  don't even use the word object loosely to describe them or you risk confusing yourself and others.   Indicator buffers are,  by default,  filled with a value of EMPTY_VALUE,  if you don't change the value of the buffer index locations they will all contain EMPTY_VALUE.  So you can tell when a location has a value in it as it will no longer be EMPTY_VALUE.  To find the previous occurrence look through the previous values in the buffer until you find the next one that is not EMPTY_VALUE.


Thanks Raptor, I'll take care with my use of the word Objects.

I understand your advice that the arrow buffer will be either EMPTY_VALUE or not, depending on the conditions and I have used statements like" if (Buffer[i] != 0 )... previously in my code, but I am still not sure how to go about finding (using code) the previous non EMPTY_VALUE so that I can compare it with the current bar non EMPTY_VALUE

In words what I am wanting to do is. if the current buffer value is not empty, find the next occurrence of the  buffer value[i] where (i>=1) such that buffer i does not equal 0

Extending on this it would seem logical to define variables (current_value and previous_value) such that:

if (Buffer[0] != 0), Buffer[0]=current_value

if (Buffer[i] != 0, where i>0) Buffer[i] = previous_value

But how would I write this such that I only got the last occasion of previous value

I suppose how do I stop the loop at that point so that I can compare current_value to previous_value

This is where my knowledge of how to code this falls short of what I can deduce logically

A little help would be greatly appreciated.

Thanks in advance

 
pullend:

Thanks Raptor, I'll take care with my use of the word Objects.

I understand your advice that the arrow buffer will be either EMPTY_VALUE or not, depending on the conditions and I have used statements like" if (Buffer[i] != 0 )... previously in my code, but I am still not sure how to go about finding (using code) the previous non EMPTY_VALUE so that I can compare it with the current bar non EMPTY_VALUE

In words what I am wanting to do is. if the current buffer value is not empty, find the next occurrence of the  buffer value[i] where (i>=1) such that buffer i does not equal 0

Extending on this it would seem logical to define variables (current_value and previous_value) such that:

if (Buffer[0] != 0), Buffer[0]=current_value

if (Buffer[i] != 0, where i>0) Buffer[i] = previous_value

But how would I write this such that I only got the last occasion of previous value

A Buffer is an Array but a little bit different, but it is indexed the same way as the bars are,  0 is the buffer value for the current bar,  1 is for the next bar to the left, etc.  If Buffer[0] is you current arrow then you most likely have a re-painting indicator,  that aside,  what you want now is to find the previous arrow,  so create a loop,  for or while,  they are interchangeable,  that starts at 0,  the last arrow position and loops through checking if Buffer[i] is != EMPTY_VALUE,  when you find the next one that isn't EMPTY_VALUE it is your next arrow.
 
I think 
pullend:


if (Buffer[0] != 0), Buffer[0]=current_value

if (Buffer[i] != 0, where i>0) Buffer[i] = previous_value


It seems to me you think EMPTY_VALUE  is same as 0

Don't make that mistake .....  It isn't   change ' != 0'  for  ' != EMPTY_VALUE'

 
deVries:
I think 

It seems to me you think EMPTY_VALUE  is same as 0

Don't make that mistake .....  It isn't   change ' != 0'  for  ' != EMPTY_VALUE'

Indeed . . .

EMPTY_VALUE0x7FFFFFFFDefault custom indicator empty value.

 

EMPTY_VALUE 

 
deVries:
I think 

It seems to me you think EMPTY_VALUE  is same as 0

Don't make that mistake .....  It isn't   change ' != 0'  for  ' != EMPTY_VALUE'


Thank you I had definitely confused EMPTY_VALUE and 0, but can obviously see the distinction now you point it out


Very much appreciate the continuing help you guys give us newbies.

 
RaptorUK:
A Buffer is an Array but a little bit different, but it is indexed the same way as the bars are,  0 is the buffer value for the current bar,  1 is for the next bar to the left, etc.  If Buffer[0] is you current arrow then you most likely have a re-painting indicator,  that aside,  what you want now is to find the previous arrow,  so create a loop,  for or while,  they are interchangeable,  that starts at 0,  the last arrow position and loops through checking if Buffer[i] is != EMPTY_VALUE,  when you find the next one that isn't EMPTY_VALUE it is your next arrow.


Thanks Raptor, suspect this is going to take me a while to learn, but I will get there and post results, or my best attempt at your suggested way forward.

 
Did you ever figure this one out? I am looking to an answer for this as well :) you gave me a starting point!
 
int iLast = iNotEmpty(Buffer),
    iPrev = iNotEmpty(Buffer, iLast+1);
//////////////////////////////////////////////////////////////////////////////////
int iNotEmpty(double ar[], int iBeg=0, int iLimit=WHOLE_ARRAY, int w=EMPTY_VALUE){
   if(iLimit == WHOLE_ARRAY)    iLimit = ArraySize(ar);
   for(; iBeg < iLimit; iBeg++) if(ar[iBeg] != w) return(iBeg);
   return(EMPTY);                                                             }
How hard was that?
Reason: