Guest User

NMS

a guest
Apr 7th, 2012
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.00 KB | None | 0 0
  1. #property indicator_separate_window
  2. #property indicator_buffers 2
  3. #property indicator_color1 Magenta
  4. #property indicator_color2 Silver
  5. #property indicator_level1 0
  6. #property indicator_levelcolor Gray
  7. #property indicator_levelstyle 2
  8.  
  9. extern int     NMS_period  = 40;
  10. extern bool    Show_SD     = true;
  11. extern int     SD_len      = 30;
  12. extern double  SD_up       = 2;
  13. extern double  SD_dn       = 2;
  14.  
  15. double NMS[];
  16. double SD[];
  17.  
  18. int init()
  19. {
  20.   string nmsname = "NMS(" + NMS_period + ")";
  21.   string sdname = "NMS_SD(" + SD_len + ")";
  22.   IndicatorShortName(nmsname);
  23.   IndicatorBuffers(2);
  24.   SetIndexBuffer(0, NMS);
  25.   SetIndexLabel(0, nmsname);
  26.   SetIndexBuffer(1, SD);
  27.   SetIndexLabel(1, sdname);
  28.   return(0);
  29. }
  30.  
  31. int start()
  32. {
  33.   int limit, i, ii, counted_bars = IndicatorCounted();
  34.  
  35.   if(Bars <= NMS_period) return(0);
  36.   if(counted_bars < 0) counted_bars = 0;
  37.   if(counted_bars > NMS_period) limit = Bars - counted_bars;
  38.   else                          limit = Bars - NMS_period - 1;
  39.  
  40.   for(i = limit; i >= 0; i--)
  41.     {
  42.     double nms2 = 0;
  43.     for(ii = 1; ii <= NMS_period; ii++)
  44.       {
  45.       double oLRSlope = 0, SumXY = 0, SumY = 0, SumX, SumXSqr, Divisor;
  46.       if(ii > 1)
  47.         {
  48.         SumX = ii * (ii - 1) * 0.5;
  49.         SumXSqr = ii * (ii - 1) * (2 * ii - 1) * 1/6;
  50.         Divisor = (SumX * SumX) - ii * SumXSqr;
  51.         for(int iii = 0; iii < ii; iii++)
  52.           {
  53.           SumXY += iii * MathLog(Close[i+iii]);
  54.           SumY += MathLog(Close[i+iii]);
  55.           }
  56.         oLRSlope = (ii * SumXY - SumX * SumY) / Divisor;
  57.         }
  58.       nms2 += oLRSlope * MathSqrt(ii);
  59.       }
  60.     NMS[i] = nms2 * 100;
  61.     }
  62.  
  63.   if(Show_SD)
  64.     for(i = limit; i >= 0; i--)
  65.       if(i < Bars - NMS_period - 1 - SD_len)
  66.         {
  67.         if(NMS[i] == 0)     SD[i] = 0;
  68.         else if(NMS[i] > 0) SD[i] = iBandsOnArray(NMS, 0, SD_len, SD_up, 0, MODE_UPPER, i);
  69.         else if(NMS[i] < 0) SD[i] = iBandsOnArray(NMS, 0, SD_len, SD_dn, 0, MODE_LOWER, i);
  70.         }
  71.  
  72.   return(0);
  73. }
Advertisement
Add Comment
Please, Sign In to add comment