Custom Indicator only showing last calculated value

 
//---- the indicator will be plotted in the main window
#property indicator_separate_window
//---- three buffers will be used for the calculations and plot of the indicator
#property indicator_buffers 4 
//---- only one graphic plot is used 
#property indicator_plots   1
//---- the indicator should be plotted as a line
#property indicator_type1   DRAW_LINE
//---- the color of the indicator's line is red 
#property indicator_color1  Red 

// PMARP Long 

input group "PMARP"
input int     pmarpLookback = 350; // Lookback
input int     pmarpLength   = 50;  // Length 

//---- the declaration of the dynamic array
//that will be used further as an indicator's buffer
double pmarpArr[]; 
double pmarArr[];
double VWMAArr[];
double pmarpSumArr[];
double VWMAHandle;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+  
void OnInit()
  {
//----+
//---- assign the dynamic array pmarpArr with 0th indicator's buffer and other calculations with others
   SetIndexBuffer(0,pmarpArr,INDICATOR_DATA);
   SetIndexBuffer(1,pmarpSumArr,INDICATOR_CALCULATIONS);
   SetIndexBuffer(2,pmarArr,INDICATOR_CALCULATIONS);
   SetIndexBuffer(3,VWMAArr,INDICATOR_CALCULATIONS);
   ArraySetAsSeries(VWMAArr, true);
   ArraySetAsSeries(pmarArr, true);
   ArraySetAsSeries(pmarpSumArr,true);

//---- set plot begin from the bar with number pmarpLookback
   PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,pmarpLookback);  

//---- declare handle for custom VWMA indicator 
   VWMAHandle = iCustom(NULL,0,"Market/VWMA Volume Weighted Moving Average",pmarpLength,VOLUME_TICK,PRICE_CLOSE);

//----+
  }
//+------------------------------------------------------------------+
//| 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[]){
  
 
//----+ 
      //---- check for the presence of bars, sufficient for the calculation
   if (rates_total < pmarpLookback ) return(0);
   
   if(IsStopped()) return 0;
   
   //---- declaration of local variables 
   int s=0;
   int pmarpSum;
   //Reverse Index close price 
   ArraySetAsSeries(close, true);
   
   //---- CopyBuffer function to pull VWMA values and place in custom array
   CopyBuffer(VWMAHandle,0,0,pmarpLookback,VWMAArr);



   pmarArr[0] = 0.0;
   pmarpSumArr[0] = 0.0;

   // Calculate all PMAR Values
   for(int i=1;i<rates_total;i++)
     {
      pmarArr[i]=MathAbs(close[i]/VWMAArr[i]);

      pmarpSum = 0;
      // cycle through previous lookback bars and assign 1 or 0 to sum if pmar value <||> current value
      for (s = 0;s <pmarpLookback ;s++){
          pmarpSum += ( pmarArr[s] > pmarArr[1] ? 0 : 1 );
          
      }  
      pmarpSumArr[i] = pmarpSum;
      pmarpArr[i] = NormalizeDouble((pmarpSumArr[i] / pmarpLookback) * 100,2);
             
     }
//----+     
   return(rates_total);
  }
                
//+------------------------------------------------------------------+

I want to create a version of an indicator in TradingView in MQL5. I have tried my best to code it in MQL5 but all I get in the indicator window is a horizontal  line that displays the last bars value for that indicator (which ive verified is correct) and it updates with each tick. Ive attached both my code and the code for the indicator in tradingview as well as what my indicator window shows. Any help would be much appreciated as I'm relatively new to MQL5. (NB: I'm only using the VWMA option for the moving average, hence the code)

 
maxcalcroft96:
pmarpSum += ( pmarArr[s] > pmarArr[1] ? 0 : 1 );
pmarpSum += ( pmarArr[s] > pmarArr[1] ? 0 : 1 );

replace with 

pmarArr[i]
 

You copy values of VWMA as far as pmarLookback.

   CopyBuffer(VWMAHandle,0,0,pmarpLookback,VWMAArr);

But you try reading values even further in the main loop:

   for(int i=1;i<rates_total;i++)
     {
      pmarArr[i]=MathAbs(close[i]/VWMAArr[i]);
...

There are more to be discussed regarding your selective usage of ArraySetAsSeries. All in all there are many logical errors.

 

Man thank you so much! that solved it, I can make a start from here, now its actually showing what I want more or less. Yes there are a lot of errors, I need to learn more and optimise the code better. Thank you for the guidance, much appreciated :)


Yashar Seyyedin #:

You copy values of VWMA as far as pmarLookback.

But you try reading values even further in the main loop:

There are more to be discussed regarding your selective usage of ArraySetAsSeries. All in all there are many logical errors.