SHARE
TWEET

ADX

a guest Dec 21st, 2016 166 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //+------------------------------------------------------------------+
  2. //|                                                          ADX.mq5 |
  3. //|                        Copyright 2009, MetaQuotes Software Corp. |
  4. //|                                              http://www.mql5.com |
  5. //+------------------------------------------------------------------+
  6. #property copyright   "2009, MetaQuotes Software Corp."
  7. #property link        "http://www.mql5.com"
  8. #property description "Average Directional Movement Index"
  9. #include <MovingAverages.mqh>
  10.  
  11. #property indicator_separate_window
  12. #property indicator_buffers 6
  13. #property indicator_plots   3
  14. #property indicator_type1   DRAW_LINE
  15. #property indicator_color1  LightSeaGreen
  16. #property indicator_style1  STYLE_SOLID
  17. #property indicator_width1  1
  18. #property indicator_type2   DRAW_LINE
  19. #property indicator_color2  YellowGreen
  20. #property indicator_style2  STYLE_DOT
  21. #property indicator_width2  1
  22. #property indicator_type3   DRAW_LINE
  23. #property indicator_color3  Wheat
  24. #property indicator_style3  STYLE_DOT
  25. #property indicator_width3  1
  26. #property indicator_label1  "ADX"
  27. #property indicator_label2  "+DI"
  28. #property indicator_label3  "-DI"
  29. //--- input parameters
  30. input int InpPeriodADX=14; // Period
  31. input int    TriggerCandle  = 0;
  32. input bool   EnableNativeAlerts = true;
  33. input bool   EnableSoundAlerts  = true;
  34. input string AlertText      = "ADX_Crossing";
  35. input string SoundFileName  = "alert.wav";
  36. datetime LastAlertTime = D'01.01.1970';
  37. //---- buffers
  38. double    ExtADXBuffer[];
  39. double    ExtPDIBuffer[];
  40. double    ExtNDIBuffer[];
  41. double    ExtPDBuffer[];
  42. double    ExtNDBuffer[];
  43. double    ExtTmpBuffer[];
  44. //--- global variables
  45. int       ExtADXPeriod;
  46. //+------------------------------------------------------------------+
  47. //| Custom indicator initialization function                         |
  48. //+------------------------------------------------------------------+
  49. void OnInit()
  50.   {
  51. //--- check for input parameters
  52.    if(InpPeriodADX>=100 || InpPeriodADX<=0)
  53.      {
  54.       ExtADXPeriod=14;
  55.       printf("Incorrect value for input variable Period_ADX=%d. Indicator will use value=%d for calculations.",InpPeriodADX,ExtADXPeriod);
  56.      }
  57.    else ExtADXPeriod=InpPeriodADX;
  58. //---- indicator buffers
  59.    SetIndexBuffer(0,ExtADXBuffer);
  60.    SetIndexBuffer(1,ExtPDIBuffer);
  61.    SetIndexBuffer(2,ExtNDIBuffer);
  62.    SetIndexBuffer(3,ExtPDBuffer,INDICATOR_CALCULATIONS);
  63.    SetIndexBuffer(4,ExtNDBuffer,INDICATOR_CALCULATIONS);
  64.    SetIndexBuffer(5,ExtTmpBuffer,INDICATOR_CALCULATIONS);
  65. //--- indicator digits
  66.    IndicatorSetInteger(INDICATOR_DIGITS,2);
  67. //--- set draw begin
  68.    PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,ExtADXPeriod<<1);
  69.    PlotIndexSetInteger(1,PLOT_DRAW_BEGIN,ExtADXPeriod);
  70.    PlotIndexSetInteger(2,PLOT_DRAW_BEGIN,ExtADXPeriod);
  71. //--- indicator short name
  72.    string short_name="ADX("+string(ExtADXPeriod)+")";
  73.    IndicatorSetString(INDICATOR_SHORTNAME,short_name);
  74. //--- change 1-st index label
  75.    PlotIndexSetString(0,PLOT_LABEL,short_name);
  76. //---- end of initialization function
  77.   }
  78. //+------------------------------------------------------------------+
  79. //| Custom indicator iteration function                              |
  80. //+------------------------------------------------------------------+
  81. int OnCalculate(const int rates_total,
  82.                 const int prev_calculated,
  83.                 const datetime &time[],
  84.                 const double &open[],
  85.                 const double &high[],
  86.                 const double &low[],
  87.                 const double &close[],
  88.                 const long &tick_volume[],
  89.                 const long &volume[],
  90.                 const int &spread[])
  91.   {
  92. //--- checking for bars count
  93.    if(rates_total<ExtADXPeriod)
  94.       return(0);
  95. //--- detect start position
  96.    int start;
  97.    if(prev_calculated>1) start=prev_calculated-1;
  98.    else
  99.      {
  100.       start=1;
  101.       ExtPDIBuffer[0]=0.0;
  102.       ExtNDIBuffer[0]=0.0;
  103.       ExtADXBuffer[0]=0.0;
  104.      }
  105. //--- main cycle
  106.    for(int i=start;i<rates_total && !IsStopped();i++)
  107.      {
  108.       //--- get some data
  109.       double Hi    =high[i];
  110.       double prevHi=high[i-1];
  111.       double Lo    =low[i];
  112.       double prevLo=low[i-1];
  113.       double prevCl=close[i-1];
  114.       //--- fill main positive and main negative buffers
  115.       double dTmpP=Hi-prevHi;
  116.       double dTmpN=prevLo-Lo;
  117.       if(dTmpP<0.0)   dTmpP=0.0;
  118.       if(dTmpN<0.0)   dTmpN=0.0;
  119.       if(dTmpP>dTmpN) dTmpN=0.0;
  120.       else
  121.         {
  122.          if(dTmpP<dTmpN) dTmpP=0.0;
  123.          else
  124.            {
  125.             dTmpP=0.0;
  126.             dTmpN=0.0;
  127.            }
  128.         }
  129.       //--- define TR
  130.       double tr=MathMax(MathMax(MathAbs(Hi-Lo),MathAbs(Hi-prevCl)),MathAbs(Lo-prevCl));
  131.       //---
  132.       if(tr!=0.0)
  133.         {
  134.          ExtPDBuffer[i]=100.0*dTmpP/tr;
  135.          ExtNDBuffer[i]=100.0*dTmpN/tr;
  136.         }
  137.       else
  138.         {
  139.          ExtPDBuffer[i]=0.0;
  140.          ExtNDBuffer[i]=0.0;
  141.         }
  142.       //--- fill smoothed positive and negative buffers
  143.       ExtPDIBuffer[i]=ExponentialMA(i,ExtADXPeriod,ExtPDIBuffer[i-1],ExtPDBuffer);
  144.       ExtNDIBuffer[i]=ExponentialMA(i,ExtADXPeriod,ExtNDIBuffer[i-1],ExtNDBuffer);
  145.       //--- fill ADXTmp buffer
  146.       double dTmp=ExtPDIBuffer[i]+ExtNDIBuffer[i];
  147.       if(dTmp!=0.0)
  148.          dTmp=100.0*MathAbs((ExtPDIBuffer[i]-ExtNDIBuffer[i])/dTmp);
  149.       else
  150.          dTmp=0.0;
  151.       ExtTmpBuffer[i]=dTmp;
  152.       //--- fill smoothed ADX buffer
  153.       ExtADXBuffer[i]=ExponentialMA(i,ExtADXPeriod,ExtADXBuffer[i-1],ExtTmpBuffer);
  154.      }
  155. //---- OnCalculate done. Return new prev_calculated.
  156. if (((TriggerCandle > 0) && (time[rates_total - 1] > LastAlertTime)) || (TriggerCandle == 0))
  157. {
  158.     string Text;
  159.     // Above Zero Alert
  160.     if ((ExtPDIBuffer[rates_total - 1 - TriggerCandle] > 20) && (ExtPDIBuffer[rates_total - 2 - TriggerCandle] <= 40))
  161.     {
  162.         Text = AlertText + "ExtPDIBuffer: " + Symbol() + " - " + EnumToString(Period()) + " - Above Zero.";
  163.         if (EnableNativeAlerts) Alert(Text);
  164.         if (EnableSoundAlerts) PlaySound(SoundFileName);
  165.         LastAlertTime = time[rates_total - 1];
  166.     }
  167.     // Below Zero Alert
  168.     if ((ExtPDIBuffer[rates_total - 1 - TriggerCandle] < 20) && (ExtPDIBuffer[rates_total - 2 - TriggerCandle] >= 40))
  169.     {
  170.         Text = AlertText + "ExtPDIBuffer: " + Symbol() + " - " + EnumToString(Period()) + " - Below Zero.";
  171.         if (EnableNativeAlerts) Alert(Text);
  172.         if (EnableSoundAlerts) PlaySound(SoundFileName);
  173.         LastAlertTime = time[rates_total - 1];
  174.     }
  175. }
  176.    return(rates_total);
  177.   }
  178. //+------------------------------------------------------------------+
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