How to return the previous bar value of a custom function or variables?

 

Hello all

I used to write in pine script. But I wanna translate the code below into MetaQuote.


In Pine Script, I can use var1[1] to retrieve the var1 value of the previous bar.

The code below works in pine script.

But it seems [ ] in MetaQuote can only be used as array. 

I tried using iCustom() but it only works with a complete custom indi. 


I have kept searching everywhere about this question tried keywords like  "shift value" "bars back" "previous bar" for days but still got no clue about it.

So could anyone help?


Many thanks

double rngfilt(double x, double r)

   {
      double rngfilt = x;

      if (x > rngfilt[1])
      {
         if (x - r < rngfilt[1])
         {   
            rngfilt = rngfilt[1];
         }
         else
         {   
            rngfilt = x-r;
         }
      }
      else 
      {
         if (x + r > rngfilt[1])
         {   
            rngfilt = rngfilt[1];
         }   
         else
         {   
            rngfilt = x + r;
         }
      }
   
   return rngfilt;
   
   }