Advertisement
Guest User

NMA_Price_Osc

a guest
Apr 6th, 2012
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.60 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     NMA_period  = 40;
  10. extern bool    FastNMA     = true;
  11. extern int     NMA_LB_min  = 8;
  12. extern bool    Show_SD     = true;
  13. extern int     SD_len      = 30;
  14. extern double  SD_up       = 2;
  15. extern double  SD_dn       = 2;
  16.  
  17. double Price_Osc[];
  18. double SD[];
  19. double NMA[];
  20.  
  21. int init()
  22. {
  23.   string nmaname = "NMA_Price_Osc(" + NMA_period + ")";
  24.   if(FastNMA) nmaname = "Fast_NMA_Price_Osc(" + NMA_period + ", " + NMA_LB_min + ")";
  25.   string sdname = "NMA_Price_Osc_SD(" + SD_len + ")";
  26.   IndicatorShortName(nmaname);
  27.   IndicatorBuffers(3);
  28.   SetIndexBuffer(0, Price_Osc);
  29.   SetIndexLabel(0, nmaname);
  30.   SetIndexBuffer(1, SD);
  31.   SetIndexLabel(1, sdname);
  32.   SetIndexBuffer(2, NMA);
  33.   return(0);
  34. }
  35.  
  36. int start()
  37. {
  38.   int limit, i, ii, counted_bars = IndicatorCounted();
  39.  
  40.   if(Bars <= NMA_period) return(0);
  41.   if(counted_bars < 0) counted_bars = 0;
  42.   if(counted_bars > NMA_period) limit = Bars - counted_bars;
  43.   else                          limit = Bars - NMA_period - 1;
  44.  
  45.   for(i = limit; i >= 0; i--)
  46.     {
  47.     double nmmnum, maxnmm = 0, sum, abssum, ratio = 0;
  48.     int NMA_LB_max;
  49.     if(FastNMA)
  50.       {
  51.       for(ii = 1; ii <= NMA_period; ii++)
  52.         {
  53.         nmmnum = (MathLog(Close[i]) - MathLog(Close[i+ii])) / MathSqrt(ii);
  54.         if(MathAbs(nmmnum) > maxnmm) { maxnmm = MathAbs(nmmnum); NMA_LB_max = ii; }
  55.         }
  56.       if(NMA_LB_max < NMA_LB_min) NMA_LB_max = NMA_LB_min;
  57.       }
  58.     else NMA_LB_max = NMA_period;
  59.     sum = (MathLog(Close[i]) - MathLog(Close[i+1])) + (MathLog(Close[i+1]) - MathLog(Close[i+2])) * (MathSqrt(2)-1);
  60.     for(ii = 2; ii < NMA_LB_max; ii++)
  61.       sum += (MathLog(Close[i+ii]) - MathLog(Close[i+ii+1])) * (MathSqrt(ii+1) - MathSqrt(ii));
  62.     abssum = MathAbs(sum);
  63.     sum = 0;
  64.     for(ii = 0; ii < NMA_LB_max; ii++)
  65.       sum += MathAbs(MathLog(Close[i+ii]) - MathLog(Close[i+ii+1]));
  66.     if(sum != 0) ratio = abssum / sum;
  67.     NMA[i] = NMA[i+1] + (Close[i] - NMA[i+1]) * ratio;
  68.     Price_Osc[i] = Close[i] - NMA[i];
  69.     }
  70.  
  71.   if(Show_SD)
  72.     for(i = limit; i >= 0; i--)
  73.       if(i < Bars - NMA_period - 1 - SD_len)
  74.         {
  75.         if(Price_Osc[i] == 0)     SD[i] = 0;
  76.         else if(Price_Osc[i] > 0) SD[i] = iBandsOnArray(Price_Osc, 0, SD_len, SD_up, 0, MODE_UPPER, i);
  77.         else if(Price_Osc[i] < 0) SD[i] = iBandsOnArray(Price_Osc, 0, SD_len, SD_dn, 0, MODE_LOWER, i);
  78.         }
  79.  
  80.   return(0);
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement