SHARE
TWEET

Untitled

a guest Jun 3rd, 2019 58 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //+------------------------------------------------------------------+
  2. //|                                                          CCI.mq4 |
  3. //|                   Copyright 2005-2014, MetaQuotes Software Corp. |
  4. //|                                              http://www.mql4.com |
  5. //+------------------------------------------------------------------+
  6. #property copyright   "2005-2014, MetaQuotes Software Corp."
  7. #property link        "http://www.mql4.com"
  8. #property description "Commodity Channel Index"
  9. #property strict
  10.  
  11. #include <MovingAverages.mqh>
  12.  
  13. #property indicator_separate_window
  14. #property indicator_buffers    1
  15. #property indicator_color1     LightSeaGreen
  16. #property indicator_level1    -100.0
  17. #property indicator_level2     100.0
  18. #property indicator_levelcolor clrSilver
  19. #property indicator_levelstyle STYLE_DOT
  20. //--- input parameter
  21. input int InpCCIPeriod=14; // CCI Period
  22. input int    TriggerCandle  = 1;
  23. input bool   EnableNativeAlerts = true;
  24. input bool   EnableSoundAlerts  = true;
  25. input bool   EnableEmailAlerts  = true;
  26. input bool   EnablePushAlerts  = true;
  27. input string AlertEmailSubject  = "";
  28. input string AlertText      = "";
  29. input string SoundFileName  = "alert.wav";
  30.  
  31. datetime LastAlertTime = D'01.01.1970';
  32. int LastAlertDirection = 0;
  33. //--- buffers
  34. double ExtCCIBuffer[];
  35. double ExtPriceBuffer[];
  36. double ExtMovBuffer[];
  37. //+------------------------------------------------------------------+
  38. //| Custom indicator initialization function                         |
  39. //+------------------------------------------------------------------+
  40. int OnInit(void)
  41.   {
  42.    string short_name;
  43. //--- 2 additional buffers are used for counting.
  44.    IndicatorBuffers(3);
  45.    SetIndexBuffer(1,ExtPriceBuffer);
  46.    SetIndexBuffer(2,ExtMovBuffer);
  47. //--- indicator line
  48.    SetIndexStyle(0,DRAW_LINE);
  49.    SetIndexBuffer(0,ExtCCIBuffer);
  50. //--- check for input parameter
  51.    if(InpCCIPeriod<=1)
  52.      {
  53.       Print("Wrong input parameter CCI Period=",InpCCIPeriod);
  54.       return(INIT_FAILED);
  55.      }
  56. //---
  57.    SetIndexDrawBegin(0,InpCCIPeriod);
  58. //--- name for DataWindow and indicator subwindow label
  59.    short_name="CCI("+IntegerToString(InpCCIPeriod)+")";
  60.    IndicatorShortName(short_name);
  61.    SetIndexLabel(0,short_name);
  62. //--- initialization done
  63.    return(INIT_SUCCEEDED);
  64.   }
  65. //+------------------------------------------------------------------+
  66. //| Commodity Channel Index                                          |
  67. //+------------------------------------------------------------------+
  68. int OnCalculate(const int rates_total,
  69.                 const int prev_calculated,
  70.                 const datetime &time[],
  71.                 const double &open[],
  72.                 const double &high[],
  73.                 const double &low[],
  74.                 const double &close[],
  75.                 const long &tick_volume[],
  76.                 const long &volume[],
  77.                 const int &spread[])
  78.   {
  79.    int    i,k,pos;
  80.    double dSum,dMul;
  81. //---
  82.    if(rates_total<=InpCCIPeriod || InpCCIPeriod<=1)
  83.       return(0);
  84. //--- counting from 0 to rates_total
  85.    ArraySetAsSeries(ExtCCIBuffer,false);
  86.    ArraySetAsSeries(ExtPriceBuffer,false);
  87.    ArraySetAsSeries(ExtMovBuffer,false);
  88.    ArraySetAsSeries(high,false);
  89.    ArraySetAsSeries(low,false);
  90.    ArraySetAsSeries(close,false);
  91. //--- initial zero
  92.    if(prev_calculated<1)
  93.      {
  94.       for(i=0; i<InpCCIPeriod; i++)
  95.         {
  96.          ExtCCIBuffer[i]=0.0;
  97.          ExtPriceBuffer[i]=(high[i]+low[i]+close[i])/3;
  98.          ExtMovBuffer[i]=0.0;
  99.         }
  100.      }
  101. //--- calculate position
  102.    pos=prev_calculated-1;
  103.    if(pos<InpCCIPeriod)
  104.       pos=InpCCIPeriod;
  105. //--- typical price and its moving average
  106.    for(i=pos; i<rates_total; i++)
  107.      {
  108.       ExtPriceBuffer[i]=(high[i]+low[i]+close[i])/3;
  109.       ExtMovBuffer[i]=SimpleMA(i,InpCCIPeriod,ExtPriceBuffer);
  110.      }
  111. //--- standard deviations and cci counting
  112.    dMul=0.015/InpCCIPeriod;
  113.    pos=InpCCIPeriod-1;
  114.    if(pos<prev_calculated-1)
  115.       pos=prev_calculated-2;
  116.    i=pos;
  117.    while(i<rates_total)
  118.      {
  119.       dSum=0.0;
  120.       k=i+1-InpCCIPeriod;
  121.       while(k<=i)
  122.         {
  123.          dSum+=MathAbs(ExtPriceBuffer[k]-ExtMovBuffer[i]);
  124.          k++;
  125.         }
  126.       dSum*=dMul;
  127.       if(dSum==0.0)
  128.          ExtCCIBuffer[i]=0.0;
  129.       else
  130.          ExtCCIBuffer[i]=(ExtPriceBuffer[i]-ExtMovBuffer[i])/dSum;
  131.       i++;
  132.      }
  133. //---
  134. //--- adding alert
  135. if (((TriggerCandle > 0) && (Time[0] > LastAlertTime)) || (TriggerCandle == 0))
  136. {
  137.     string Text;
  138.     // Above Zero Alert
  139.     if (((ExtCCIBuffer[TriggerCandle] > 0) && (ExtCCIBuffer[TriggerCandle+ 1] <= 0)) && ((TriggerCandle > 0) || ((TriggerCandle == 0) && (LastAlertDirection != 1))))
  140.     {
  141.         Text = AlertText + "cci: " + Symbol() + " - " + EnumToString((ENUM_TIMEFRAMES)Period()) + " - Above Zero.";
  142.         if (EnableNativeAlerts) Alert(Text);
  143.         if (EnableEmailAlerts) SendMail(AlertEmailSubject + "buy signal", Text);
  144.         if (EnableSoundAlerts) PlaySound(SoundFileName);
  145.         if (EnablePushAlerts) SendNotification(Text);
  146.         LastAlertTime = Time[0];
  147.         LastAlertDirection = 1;
  148.     }
  149.     // Below Zero Alert
  150.     if (((ExtCCIBuffer[TriggerCandle] < 0) && (ExtCCIBuffer[TriggerCandle+ 1] >= 0)) && ((TriggerCandle > 0) || ((TriggerCandle == 0) && (LastAlertDirection != -1))))
  151.     {
  152.         Text = AlertText + "cci: " + Symbol() + " - " + EnumToString((ENUM_TIMEFRAMES)Period()) + " - Below Zero.";
  153.         if (EnableNativeAlerts) Alert(Text);
  154.         if (EnableEmailAlerts) SendMail(AlertEmailSubject + "sell signal", Text);
  155.         if (EnableSoundAlerts) PlaySound(SoundFileName);
  156.         if (EnablePushAlerts) SendNotification(Text);
  157.         LastAlertTime = Time[0];
  158.         LastAlertDirection = -1;
  159.     }
  160. }
  161.  
  162.  
  163. //---  
  164.    return(rates_total);
  165.   }
  166. //+------------------------------------------------------------------+
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top