Data Handling In Indicators

 
Hi,
How can I determine the index value of the most recently closed bar. 

For instance, this indicator calculates data on the index that is currently being formed. The result is the display of upward and downward arrows if both conditions are met during the formation. Ideally, only one arrow should be displayed per bar.

#property indicator_chart_window
#property indicator_buffers 2
#property indicator_plots   2

double BullishBuffer[], BearishBuffer[];

int OnInit()
  {
   SetIndexBuffer(0, BullishBuffer, INDICATOR_DATA);
   PlotIndexSetInteger(0, PLOT_DRAW_TYPE, DRAW_ARROW);
   PlotIndexSetInteger(0,PLOT_ARROW,233);
   PlotIndexSetInteger(0, PLOT_LINE_COLOR, clrGreen);

   SetIndexBuffer(1, BearishBuffer, INDICATOR_DATA);
   PlotIndexSetInteger(1, PLOT_DRAW_TYPE, DRAW_ARROW);
   PlotIndexSetInteger(1,PLOT_ARROW,234);
   PlotIndexSetInteger(1, PLOT_LINE_COLOR, clrRed);

   return INIT_SUCCEEDED;
  }

int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {

   for(int i = 0; i < rates_total; i++)
     {
      bool BarIsBullish = close[i] > open[i];
      bool BarIsBearish = open[i] > close[i];

      if(BarIsBullish)
        {
         BullishBuffer[i] = low[i];
        }

      else if(BarIsBearish)
           {
            BearishBuffer[i] = high[i];
           }
     }

   return rates_total;
  }
 
codemq: How can I determine the index value of the most recently closed bar. For instance, this indicator calculates data on the index that is currently being formed. The result is the display of upward and downward arrows if both conditions are met during the formation. Ideally, only one arrow should be displayed per bar.
This indicator has a bad for() loop.

The last closed candle is always rates_total - 2.

Please check and understand this post in it's entirety: https://www.mql5.com/en/forum/376322#comment_24273483
Indicators: Need help understanding lookback set-up
Indicators: Need help understanding lookback set-up
  • 2021.08.26
  • www.mql5.com
General: Indicators: Need help understanding lookback set-up
 

Thank you, after reading the post, I've arrived here, when the loop is written this way:

for (i = prev_calculated, i = rates_total, i++)

and these variables are printed:

Print(i, rates_total, prev_calculated);

this is the result:

i: 100853, rates_total: 100860, prev_calculated: 0

i: 100854, rates_total: 100860, prev_calculated: 0

i: 100855, rates_total: 100860, prev_calculated: 0

In this result, "i" has values while prev_calculated is zero, even after they have been assigned the same value.


When the loop is written this way:

for (i = 0, i = rates_total, i++)

and the same variables are printed:

this is the result:

i: 100849, rates_total: 100853, prev_calculated: 100853

i: 100850, rates_total: 100853, prev_calculated: 100853

i: 100851, rates_total: 100853, prev_calculated: 100853

In this result prev_calculated and rates_total have the same value
 

Try the following ...

// Simplified logic (uncompiled, untested, simply typed out) 
for( int i = prev_calculated < 1 ? 0 : prev_calculated - 1; !_StopFlag && i < rates_total; i++ ) {
   BullishBuffer[i] = close[i] > open[ i] ? low[ i] : EMPTY_VALUE; 
   BearishBuffer[i] = close[i] < open[ i] ? high[i] : EMPTY_VALUE;
};
 
codemq: How can I determine the index value of the most recently closed bar. For instance, this indicator calculates data on the index that is currently being formed. The result is the display of upward and downward arrows if both conditions are met during the formation. Ideally, only one arrow should be displayed per bar.

Also, I suggest you use the "#property" definitions to set the colour and other display properties instead of hard coding them during the OnInit().

That way they can be change by the user in the Indicator's properties panel.

 

Thanks for your response. The calculation is currently ongoing on the developing index.

The attached images demonstrate the up arrow initially, followed by the appearance of the down arrow shortly afterwards.

How can I ascertain the index value of a closed bar, such as the bullish one depicted in the reference image, for the code to look like this:

if(BarIsBullish)
{
  BullishBuffer[i] = low[index of bar after closing];
}

else if(BarIsBearish)
{
  BearishBuffer[i] = high[index of bar after closing];
}

These have been modified according to your suggestions.

#property indicator_color1  InputColor1
#property indicator_color2  InputColor2
input color InputColor1 = clrGreen;
input color InputColor2 = clrRed;
Files:
 
codemq #:

Thanks for your response. The calculation is currently ongoing on the developing index.

The attached images demonstrate the up arrow initially, followed by the appearance of the down arrow shortly afterwards.

How can I ascertain the index value of a closed bar, such as the bullish one depicted in the reference image, for the code to look like this:

These have been modified according to your suggestions.

to work with closed bar you need reference the bar [i-1]

if(BarIsBullish)
{
  BullishBuffer[i-1] = low[i-1];
}

else if(BarIsBearish)
{
  BearishBuffer[i-1] = high[i-1];
}


And for this need at least 2 bars, so

for( int i = prev_calculated < 2 ? 1 : prev_calculated - 1; !_StopFlag && i < rates_total; i++ )
 
codemq #: These have been modified according to your suggestions.

No, you are not properly applying the suggested correction. Here is the full example, compiled and tested ...

#property indicator_chart_window
#property indicator_buffers 2
#property indicator_plots   2

#property indicator_label1    "Bullish"
#property indicator_color1    clrGreen
#property indicator_type1     DRAW_ARROW

#property indicator_label2    "Bearish"
#property indicator_color2    clrRed
#property indicator_type2     DRAW_ARROW

double BullishBuffer[], BearishBuffer[];

int OnInit() {
   SetIndexBuffer( 0, BullishBuffer, INDICATOR_DATA );
   SetIndexBuffer( 1, BearishBuffer, INDICATOR_DATA );
   PlotIndexSetInteger( 0, PLOT_ARROW, 233 );
   PlotIndexSetInteger( 1, PLOT_ARROW, 234 );
   return INIT_SUCCEEDED;
};

int OnCalculate( const int rates_total, const int prev_calculated, const datetime &time[],
                 const double &open[], const double &high[], const double &low[], const double &close[],
                 const long &tick_volume[], const long &volume[], const int &spread[] ) {
   for( int i = prev_calculated < 1 ? 0 : prev_calculated - 1; !_StopFlag && i < rates_total; i++ ) {
      BullishBuffer[i] = close[i] > open[i] ? low[ i] : EMPTY_VALUE; 
      BearishBuffer[i] = close[i] < open[i] ? high[i] : EMPTY_VALUE;
   };
   return rates_total;
};

 
codemq #:
#property indicator_color1  InputColor1 #property indicator_color2  InputColor2

actually it should be like this

#property indicator_color1  clrGreen
#property indicator_color2  clrRed

// don't need additional inputs
 

And the update for your most recent request ...

   for( int i = prev_calculated <= 1 ? 1 : prev_calculated - 1; !_StopFlag && i < rates_total; i++ ) {
      BullishBuffer[i] = close[i] > open[i] ? low[ i - 1] : EMPTY_VALUE; 
      BearishBuffer[i] = close[i] < open[i] ? high[i - 1] : EMPTY_VALUE;
   };
 
Thank you, everyone, for providing all of the answers. I appreciate your input, as it has provided all the solutions needed.
Reason: