pine-scriptpine-script-v5mql5

pine script - reffering to previous values of local variable


i have this function but i can't understand _f[1],_f[2],... values . _f is local variable of function so my question is are _f[1] reffer to value of _f in previous run of function? or something else.

f_filt9x (_a, _s, _i) => 
    int _m2 = 0, int _m3 = 0, int _m4 = 0, int _m5 = 0, int _m6 = 0, 
    int _m7 = 0, int _m8 = 0, int _m9 = 0, float _f = .0, _x = (1 - _a)
  
    // Weights. 
    // Initial weight _m1 is a pole number and equal to _i
    _m2 := _i == 9 ? 36  : _i == 8 ? 28 : _i == 7 ? 21 : _i == 6 ? 15 : _i == 5 ? 10 : _i == 4 ? 6 : _i == 3 ? 3 : _i == 2 ? 1 : 0
    _m3 := _i == 9 ? 84  : _i == 8 ? 56 : _i == 7 ? 35 : _i == 6 ? 20 : _i == 5 ? 10 : _i == 4 ? 4 : _i == 3 ? 1 : 0
    _m4 := _i == 9 ? 126 : _i == 8 ? 70 : _i == 7 ? 35 : _i == 6 ? 15 : _i == 5 ? 5  : _i == 4 ? 1 : 0
    _m5 := _i == 9 ? 126 : _i == 8 ? 56 : _i == 7 ? 21 : _i == 6 ? 6  : _i == 5 ? 1  : 0 
    _m6 := _i == 9 ? 84  : _i == 8 ? 28 : _i == 7 ? 7  : _i == 6 ? 1  : 0 
    _m7 := _i == 9 ? 36  : _i == 8 ? 8  : _i == 7 ? 1  : 0 
    _m8 := _i == 9 ? 9   : _i == 8 ? 1  : 0 
    _m9 := _i == 9 ? 1   : 0
    // filter
    _f :=   pow(_a, _i) * nz(_s) + 
      _i  *     _x      * nz(_f[1])      - (_i >= 2 ? 
      _m2 * pow(_x, 2)  * nz(_f[2]) : 0) + (_i >= 3 ? 
      _m3 * pow(_x, 3)  * nz(_f[3]) : 0) - (_i >= 4 ? 
      _m4 * pow(_x, 4)  * nz(_f[4]) : 0) + (_i >= 5 ? 
      _m5 * pow(_x, 5)  * nz(_f[5]) : 0) - (_i >= 6 ? 
      _m6 * pow(_x, 6)  * nz(_f[6]) : 0) + (_i >= 7 ? 
      _m7 * pow(_x, 7)  * nz(_f[7]) : 0) - (_i >= 8 ? 
      _m8 * pow(_x, 8)  * nz(_f[8]) : 0) + (_i == 9 ? 
      _m9 * pow(_x, 9)  * nz(_f[9]) : 0)

i write this function for mql5 and instead of using _f as local variable i use it as a global buffer but results aren't same. this is the mql5 function i wrote:

//f_filt9x
double f_filt9x(double _a, double _s, int _i, int i)
{
   int _m2, _m3, _m4, _m5, _m6, _m7, _m8, _m9;
   _m2 = 0;
   _m3 = 0;
   _m4 = 0;
   _m5 = 0;
   _m6 = 0;
   _m7 = 0;
   _m8 = 0;
   _m9 = 0;
   
   double _x = (1.0 - _a);
   //Weights
   // Initial weight _m1 is a pole number and equal to _i
   _m2 = (_i==9) ? 36   : (_i==8) ? 28 : (_i==7) ? 21 : (_i==6) ? 15 : (_i==5) ? 10 : (_i==4) ? 6  :  (_i==3) ? 3 : (_i==2) ? 1  : 0;
   _m3 = (_i==9) ? 84   : (_i==8) ? 56 : (_i==7) ? 35 : (_i==6) ? 20 : (_i==5) ? 10 : (_i==4) ? 4  :  (_i==3) ? 1 : 0;
   _m4 = (_i==9) ? 126  : (_i==8) ? 70 : (_i==7) ? 35 : (_i==6) ? 15 : (_i==5) ? 5  : (_i==4) ? 1  : 0;
   _m5 = (_i==9) ? 126  : (_i==8) ? 56 : (_i==7) ? 21 : (_i==6) ? 6  : (_i==5) ? 1  : 0;
   _m6 = (_i==9) ? 84   : (_i==8) ? 28 : (_i==7) ? 7  : (_i==6) ? 1  : 0;
   _m7 = (_i==9) ? 36   : (_i==8) ? 8  : (_i==7) ? 1  : 0; 
   _m8 = (_i==9) ? 9    : (_i==8) ? 1  :  0;
   _m9 = (_i==9) ? 1    : 0;
   //filter
   _f[i] = (MathPow(_a,_i) * _s)  + _i  *  _x  * _f[i-1]    
                                   -( (_i>=2) ?_m2   * MathPow(_x,2)*  _f[i-2] : 0)
                                   +( (_i>=3) ?_m3   * MathPow(_x,3)*  _f[i-3] : 0)
                                   -( (_i>=4) ?_m4   * MathPow(_x,4)*  _f[i-4] : 0)  
                                   +( (_i>=5) ?_m5   * MathPow(_x,5)*  _f[i-5] : 0)  
                                   -( (_i>=6) ?_m6   * MathPow(_x,6)*  _f[i-6] : 0)  
                                   +( (_i>=7) ?_m7   * MathPow(_x,7)*  _f[i-7] : 0)  
                                   -( (_i>=8) ?_m8   * MathPow(_x,8)*  _f[i-8] : 0)  
                                   +( (_i>=9) ?_m9   * MathPow(_x,9)*  _f[i-9] : 0) ;

  return _f[i]; 

}

Solution

  • as Gu5tavo71 commented, _f[1] reffer to value of _f in previous run of the function. and equivalent function in mql5 is correct but i find out the reason of different return values of functions was that throughout the main mql5 code, function called several times with different _s values so the previous values of _f get messed up. I solve this problem with duplicating function and use different _f arrays for each _s. somehow this not happen in pine script because of Ability to reffer previous values of local variables.