Advertisement
Guest User

Ocn_NMSx

a guest
Apr 8th, 2012
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.54 KB | None | 0 0
  1. #property indicator_separate_window
  2. #property indicator_buffers 6
  3. #property indicator_color1 Magenta
  4. #property indicator_color2 Lime
  5. #property indicator_color3 Silver
  6. #property indicator_color4 Silver
  7. #property indicator_color5 Yellow
  8. #property indicator_color6 Aqua
  9. #property indicator_level1 0
  10. #property indicator_levelcolor Gray
  11. #property indicator_levelstyle 2
  12.  
  13. extern int    NMS_period          = 40;
  14. extern bool   Show_ZH             = true;
  15. extern double ZH_displacement     = 1;
  16. extern int    ZH_symbol           = 115;
  17. extern bool   Show_SD             = true;
  18. extern int    SD_len              = 30;
  19. extern double SD_up               = 2;
  20. extern double SD_dn               = 2;
  21. extern bool   Show_SD_1line_only  = true;
  22. extern bool   Show_SD_2lines_up   = true;
  23. extern bool   Show_SD_2lines_dn   = true;
  24. extern int    NMA_period          = 40;
  25. extern int    NMA_LB_min          = 8;
  26. extern bool   Show_NMA            = true;
  27. extern bool   Show_FastNMA        = true;
  28.  
  29. double NMS[];
  30. double ZH[];
  31. double SD[];
  32. double SD2[];
  33. double NMS_NMA[];
  34. double NMS_FastNMA[];
  35.  
  36. int init()
  37. {
  38.   string nmsname = "NMS(" + NMS_period + ")";
  39.   IndicatorShortName(nmsname);
  40.   IndicatorBuffers(6);
  41.   SetIndexBuffer(0, NMS);
  42.   SetIndexLabel(0, nmsname);
  43.   SetIndexBuffer(1, ZH);
  44.   SetIndexLabel(1, "NMS_ZH");
  45.   SetIndexStyle(1, DRAW_ARROW);
  46.   SetIndexArrow(1, ZH_symbol);
  47.   SetIndexBuffer(2, SD);
  48.   SetIndexBuffer(3, SD2);
  49.   if(Show_SD_1line_only)
  50.     {
  51.     SetIndexLabel(2, "NMS_SD(" + SD_len + ")");
  52.     SetIndexLabel(3, "unused");
  53.     }
  54.   else
  55.     {
  56.     if(Show_SD_2lines_up) SetIndexLabel(2, "NMS_SD_up(" + SD_len + ")");
  57.     else                  SetIndexLabel(2, "unused");
  58.     if(Show_SD_2lines_dn) SetIndexLabel(3, "NMS_SD_dn(" + SD_len + ")");
  59.     else                  SetIndexLabel(3, "unused");
  60.     }
  61.   SetIndexBuffer(4, NMS_NMA);
  62.   SetIndexBuffer(5, NMS_FastNMA);
  63.   if(Show_NMA) SetIndexLabel(4, "NMS_NMA(" + NMA_period + ")");
  64.   else         SetIndexLabel(4, "unused");
  65.   if(Show_FastNMA) SetIndexLabel(5, "NMS_FastNMA(" + NMA_period + ", " + NMA_LB_min + ")");
  66.   else             SetIndexLabel(5, "unused");
  67.   return(0);
  68. }
  69.  
  70. int start()
  71. {
  72.   int limit, i, ii, counted_bars = IndicatorCounted();
  73.   double nms2, sum, abssum, ratio, nmsnum, maxnms;
  74.  
  75.   if(Bars <= NMS_period) return(0);
  76.   if(counted_bars < 0) counted_bars = 0;
  77.   if(counted_bars > NMS_period) limit = Bars - counted_bars;
  78.   else                          limit = Bars - NMS_period - 1;
  79.  
  80.   for(i = limit; i >= 0; i--)
  81.     {
  82.     nms2 = 0;
  83.     for(ii = 1; ii <= NMS_period; ii++)
  84.       {
  85.       double oLRSlope = 0, SumXY = 0, SumY = 0, SumX, SumXSqr, Divisor;
  86.       if(ii > 1)
  87.         {
  88.         SumX = ii * (ii - 1) * 0.5;
  89.         SumXSqr = ii * (ii - 1) * (2 * ii - 1) * 1/6;
  90.         Divisor = (SumX * SumX) - ii * SumXSqr;
  91.         for(int iii = 0; iii < ii; iii++)
  92.           {
  93.           SumXY += iii * MathLog(Close[i+iii]);
  94.           SumY += MathLog(Close[i+iii]);
  95.           }
  96.         oLRSlope = (ii * SumXY - SumX * SumY) / Divisor;
  97.         }
  98.       nms2 += oLRSlope * MathSqrt(ii);
  99.       }
  100.     NMS[i] = nms2 * 100;
  101.     if(Show_ZH) { if(NMS[i] == 0) ZH[i] = ZH_displacement; else ZH[i] = EMPTY_VALUE; }
  102.     }
  103.  
  104.   if(Show_SD)
  105.     for(i = limit; i >= 0; i--)
  106.       if(i < Bars - NMS_period - 1 - SD_len)
  107.         {
  108.         if(Show_SD_1line_only)
  109.           {
  110.           if(NMS[i] == 0)     SD[i] = 0;
  111.           else if(NMS[i] > 0) SD[i] = iBandsOnArray(NMS, 0, SD_len, SD_up, 0, MODE_UPPER, i);
  112.           else if(NMS[i] < 0) SD[i] = iBandsOnArray(NMS, 0, SD_len, SD_dn, 0, MODE_LOWER, i);
  113.           }
  114.         else
  115.           {
  116.           if(NMS[i] == 0)
  117.             {
  118.             if(Show_SD_2lines_up) SD[i] = 0;
  119.             if(Show_SD_2lines_dn) SD2[i] = 0;
  120.             }
  121.           else
  122.             {
  123.             if(Show_SD_2lines_up) SD[i] = iBandsOnArray(NMS, 0, SD_len, SD_up, 0, MODE_UPPER, i);
  124.             if(Show_SD_2lines_dn) SD2[i] = iBandsOnArray(NMS, 0, SD_len, SD_dn, 0, MODE_LOWER, i);
  125.             }
  126.           }
  127.         }
  128.  
  129.   if(Show_NMA)
  130.     for(i = limit; i >= 0; i--)
  131.       if(i < Bars - NMS_period - 1 - NMA_period)
  132.         {
  133.         ratio = 0;
  134.         sum = (NMS[i] - NMS[i+1]) + (NMS[i+1] - NMS[i+2]) * (MathSqrt(2)-1);
  135.         for(ii = 2; ii < NMA_period; ii++)
  136.           sum += (NMS[i+ii] - NMS[i+ii+1]) * (MathSqrt(ii+1) - MathSqrt(ii));
  137.         abssum = MathAbs(sum);
  138.         sum = 0;
  139.         for(ii = 0; ii < NMA_period; ii++)
  140.           sum += MathAbs(NMS[i+ii] - NMS[i+ii+1]);
  141.         if(sum != 0) ratio = abssum / sum;
  142.         NMS_NMA[i] = NMS_NMA[i+1] + (NMS[i] - NMS_NMA[i+1]) * ratio;
  143.         }
  144.  
  145.   if(Show_FastNMA)
  146.     for(i = limit; i >= 0; i--)
  147.       if(i < Bars - NMS_period - 1 - NMA_period)
  148.         {
  149.         maxnms = 0; ratio = 0;
  150.         int NMA_LB_max;
  151.         for(ii = 1; ii <= NMA_period; ii++)
  152.           {
  153.           nmsnum = (NMS[i] - NMS[i+ii]) / MathSqrt(ii);
  154.           if(MathAbs(nmsnum) > maxnms) { maxnms = MathAbs(nmsnum); NMA_LB_max = ii; }
  155.           }
  156.         if(NMA_LB_max < NMA_LB_min) NMA_LB_max = NMA_LB_min;
  157.         sum = (NMS[i] - NMS[i+1]) + (NMS[i+1] - NMS[i+2]) * (MathSqrt(2)-1);
  158.         for(ii = 2; ii < NMA_LB_max; ii++)
  159.           sum += (NMS[i+ii] - NMS[i+ii+1]) * (MathSqrt(ii+1) - MathSqrt(ii));
  160.         abssum = MathAbs(sum);
  161.         sum = 0;
  162.         for(ii = 0; ii < NMA_LB_max; ii++)
  163.           sum += MathAbs(NMS[i+ii] - NMS[i+ii+1]);
  164.         if(sum != 0) ratio = abssum / sum;
  165.         NMS_FastNMA[i] = NMS_FastNMA[i+1] + (NMS[i] - NMS_FastNMA[i+1]) * ratio;
  166.         }
  167.  
  168.   return(0);
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement