Advertisement
micclly

TRIX

Feb 16th, 2014
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.63 KB | None | 0 0
  1. /*
  2.  * License: GNU General Public License Version 3
  3.  */
  4. #property strict
  5. #property indicator_chart_window
  6.  
  7. #property indicator_buffers    3
  8. #property indicator_color1     clrRed
  9. #property indicator_style1     STYLE_SOLID
  10. #property indicator_color2     clrGreen
  11. #property indicator_style2     STYLE_SOLID
  12. #property indicator_color3     clrPurple
  13. #property indicator_style3     STYLE_SOLID
  14.  
  15. input int i_EMAPeriod = 15;
  16.  
  17. double g_emaSingleSmoothed[];
  18. double g_emaDoubleSmoothed[];
  19. double g_emaTripleSmoothed[];
  20.  
  21. void calculateEMA(int rates_total, int prev_calculated, const double& price[], double& indexBuffer[])
  22. {
  23.     int    limit;
  24.     double smoothFactor = 2.0 / (1.0 + i_EMAPeriod);
  25.  
  26.     if (prev_calculated == 0)
  27.     {
  28.         limit = i_EMAPeriod;
  29.         indexBuffer[0] = price[0];
  30.  
  31.         for(int i = 1; i < limit; i++) {
  32.             indexBuffer[i] = price[i] * smoothFactor + indexBuffer[i-1] * (1.0 - smoothFactor);
  33.         }
  34.     }
  35.     else {
  36.         limit = prev_calculated - 1;
  37.     }
  38.  
  39.    for(int i = limit; i < rates_total; i++) {
  40.       if (IsStopped()) {
  41.         break;
  42.       }
  43.  
  44.       indexBuffer[i] = price[i] * smoothFactor + indexBuffer[i-1] * (1.0 - smoothFactor);
  45.    }
  46. }
  47.  
  48. int OnInit()
  49. {
  50.     IndicatorShortName("TRIX" + string(i_EMAPeriod) + ")");
  51.     IndicatorDigits(Digits);
  52.  
  53.     SetIndexBuffer(0, g_emaSingleSmoothed);
  54.     SetIndexBuffer(1, g_emaDoubleSmoothed);
  55.     SetIndexBuffer(2, g_emaTripleSmoothed);
  56.  
  57.     return INIT_SUCCEEDED;
  58. }
  59.  
  60. int OnCalculate(const int rates_total,
  61.                 const int prev_calculated,
  62.                 const datetime &time[],
  63.                 const double &open[],
  64.                 const double &high[],
  65.                 const double &low[],
  66.                 const double &close[],
  67.                 const long &tick_volume[],
  68.                 const long &volume[],
  69.                 const int &spread[])
  70. {
  71.     if (rates_total < i_EMAPeriod - 1) {
  72.         return 0;
  73.     }
  74.  
  75.     ArraySetAsSeries(g_emaSingleSmoothed, false);
  76.     ArraySetAsSeries(g_emaDoubleSmoothed, false);
  77.     ArraySetAsSeries(g_emaTripleSmoothed, false);
  78.     ArraySetAsSeries(close,false);
  79.  
  80.     if (prev_calculated == 0) {
  81.         ArrayInitialize(g_emaSingleSmoothed, EMPTY_VALUE);
  82.         ArrayInitialize(g_emaDoubleSmoothed, EMPTY_VALUE);
  83.         ArrayInitialize(g_emaTripleSmoothed, EMPTY_VALUE);
  84.     }
  85.  
  86.  
  87.     calculateEMA(rates_total, prev_calculated, close, g_emaSingleSmoothed);
  88.     calculateEMA(rates_total, prev_calculated, g_emaSingleSmoothed, g_emaDoubleSmoothed);
  89.     calculateEMA(rates_total, prev_calculated, g_emaDoubleSmoothed, g_emaTripleSmoothed);
  90.  
  91.     return rates_total;
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement