How to programmatically get the value of an indicator which is applied to the value of another indicator - page 2

 
GumRai:

I don't see why you would need to change the series on a buffer.

Buffers are automatically re-sized to the number of bars and when a new bar is formed there is a new position [0]. The old pos[0] is shifted to pos[1] etc.

By using  ArraySetAsSeries(BufferRSI, false), on the next new bar tick, the new position will be added at the other end, at pos[Bars-1] and the loop will be overwriting the same positions every time ( [limit] to [0] ) because you reset the buffer to TimeSeries

Because iRSIOnArray (& iMAOnArray) calculates from left to right.

 From the docs on them:

Note

Unlike iRSI(...), the iRSIOnArray() does not take data by symbol name, timeframe, the applied price. The price data must be previously prepared. The indicator is calculated from left to right. To access to the array elements as to a series array (i.e., from right to left), one has to use the ArraySetAsSeries() function. 

 
DerkWehler:

Because iRSIOnArray (& iMAOnArray) calculates from left to right.

 From the docs on them:

Note

Unlike iRSI(...), the iRSIOnArray() does not take data by symbolname, timeframe, the applied price. The price data must be previously prepared.The indicator is calculated from left to right. To access to the array elementsas to a series array (i.e., from right to left), one has to use the ArraySetAsSeries()function. 

I believe this documentation to be misleading and confusing

Put this code into an indicator

#property copyright "GumRai"
#property link      "none"
#property version   "1.00"
#property strict
#property indicator_separate_window
#property indicator_minimum 0
#property indicator_maximum 100
#property indicator_buffers 3
#property indicator_plots   3
//--- plot RealRSI
#property indicator_label1  "RealRSI"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrRed
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1
//--- plot ClosePrice
#property indicator_label2  "ClosePrice"
#property indicator_type2   DRAW_LINE
#property indicator_style2  STYLE_SOLID
#property indicator_width2  1
//--- plot RSIOnArray
#property indicator_label3  "RSIOnArray"
#property indicator_type3   DRAW_LINE
#property indicator_color3  clrDodgerBlue
#property indicator_style3  STYLE_SOLID
#property indicator_width3  1
//--- input parameters
input int      RSIPeriod=14;
//--- indicator buffers
double         RealRSIBuffer[];
double         ClosePriceBuffer[];
double         RSIOnArrayBuffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,RealRSIBuffer);
   SetIndexBuffer(1,ClosePriceBuffer);
   SetIndexBuffer(2,RSIOnArrayBuffer);
   
     
   IndicatorSetInteger(INDICATOR_LEVELS,3);
   IndicatorSetDouble(INDICATOR_LEVELVALUE,0,30);
   IndicatorSetDouble(INDICATOR_LEVELVALUE,1,50);
   IndicatorSetDouble(INDICATOR_LEVELVALUE,2,70);

   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
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[])
  {
  int limit;
  if(prev_calculated==0)
    limit=rates_total-RSIPeriod-1;
  else
    limit=1;
  for (int x=limit;x>=0;x--)
     {
     RealRSIBuffer[x]=iRSI(Symbol(),0,RSIPeriod,PRICE_CLOSE,x);
     ClosePriceBuffer[x]=Close[x];
     }
  for (int x=limit;x>=0;x--)
     {
     RSIOnArrayBuffer[x]=iRSIOnArray(ClosePriceBuffer,0,RSIPeriod,x);
     }  
  
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+

 Attach it to an M1 chart and check the data window, you will see that the iRSIOnArray exactly follows the real RSI

Then change

  for (int x=limit;x>=0;x--)
     {
     RSIOnArrayBuffer[x]=iRSIOnArray(ClosePriceBuffer,0,RSIPeriod,x);
     } 

 to

  for (int x=limit;x>=0;x--)
     {
     ArraySetAsSeries(ClosePriceBuffer, true);
     RSIOnArrayBuffer[x]=iRSIOnArray(ClosePriceBuffer,0,RSIPeriod,x);
     ArraySetAsSeries(ClosePriceBuffer, false);
     } 

 Then see what happens when a new bar opens

There is no need to mess about with SetAsSeries 

 
GumRai:

I believe this documentation to be misleading and confusing

Put this code into an indicator

 Attach it to an M1 chart and check the data window, you will see that the iRSIOnArray exactly follows the real RSI

Then change

 to

 Then see what happens when a new bar opens

There is no need to mess about with SetAsSeries 

 

That looks like some MQ5 stuff I saw, but since they seem compatible... great!  Thank you very much, I will try to play with this first.  Much appreciated.
 
DerkWehler:
That looks like some MQ5 stuff I saw, but since they seem compatible... great!  Thank you very much, I will try to play with this first.  Much appreciated.

GumRai:

I tried the code below. It worked rather well, except for a couple things...

EDIT: Actually, looking a little further into it, I found they were all my bugs essentially.  Works great now, so thank you for the MQ5-like code, and especially for clearing up my long-misunderstood array reversal issues.

Reason: