SHARE
TWEET

Untitled

a guest Dec 21st, 2016 277 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. int LastAlertDirection = 0;
  38. //---- buffers
  39. double    ExtADXBuffer[];
  40. double    ExtPDIBuffer[];
  41. double    ExtNDIBuffer[];
  42. double    ExtPDBuffer[];
  43. double    ExtNDBuffer[];
  44. double    ExtTmpBuffer[];
  45. //--- global variables
  46. int       ExtADXPeriod;
  47. //+------------------------------------------------------------------+
  48. //| Custom indicator initialization function                         |
  49. //+------------------------------------------------------------------+
  50. void OnInit()
  51.   {
  52. //--- check for input parameters
  53.    if(InpPeriodADX>=100 || InpPeriodADX<=0)
  54.      {
  55.       ExtADXPeriod=14;
  56.       printf("Incorrect value for input variable Period_ADX=%d. Indicator will use value=%d for calculations.",InpPeriodADX,ExtADXPeriod);
  57.      }
  58.    else ExtADXPeriod=InpPeriodADX;
  59. //---- indicator buffers
  60.    SetIndexBuffer(0,ExtADXBuffer);
  61.    SetIndexBuffer(1,ExtPDIBuffer);
  62.    SetIndexBuffer(2,ExtNDIBuffer);
  63.    SetIndexBuffer(3,ExtPDBuffer,INDICATOR_CALCULATIONS);
  64.    SetIndexBuffer(4,ExtNDBuffer,INDICATOR_CALCULATIONS);
  65.    SetIndexBuffer(5,ExtTmpBuffer,INDICATOR_CALCULATIONS);
  66. //--- indicator digits
  67.    IndicatorSetInteger(INDICATOR_DIGITS,2);
  68. //--- set draw begin
  69.    PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,ExtADXPeriod<<1);
  70.    PlotIndexSetInteger(1,PLOT_DRAW_BEGIN,ExtADXPeriod);
  71.    PlotIndexSetInteger(2,PLOT_DRAW_BEGIN,ExtADXPeriod);
  72. //--- indicator short name
  73.    string short_name="ADX("+string(ExtADXPeriod)+")";
  74.    IndicatorSetString(INDICATOR_SHORTNAME,short_name);
  75. //--- change 1-st index label
  76.    PlotIndexSetString(0,PLOT_LABEL,short_name);
  77. //---- end of initialization function
  78.   }
  79. //+------------------------------------------------------------------+
  80. //| Custom indicator iteration function                              |
  81. //+------------------------------------------------------------------+
  82. int OnCalculate(const int rates_total,
  83.                 const int prev_calculated,
  84.                 const datetime &time[],
  85.                 const double &open[],
  86.                 const double &high[],
  87.                 const double &low[],
  88.                 const double &close[],
  89.                 const long &tick_volume[],
  90.                 const long &volume[],
  91.                 const int &spread[])
  92.   {
  93. //--- checking for bars count
  94.    if(rates_total<ExtADXPeriod)
  95.       return(0);
  96. //--- detect start position
  97.    int start;
  98.    if(prev_calculated>1) start=prev_calculated-1;
  99.    else
  100.      {
  101.       start=1;
  102.       ExtPDIBuffer[0]=0.0;
  103.       ExtNDIBuffer[0]=0.0;
  104.       ExtADXBuffer[0]=0.0;
  105.      }
  106. //--- main cycle
  107.    for(int i=start;i<rates_total && !IsStopped();i++)
  108.      {
  109.       //--- get some data
  110.       double Hi    =high[i];
  111.       double prevHi=high[i-1];
  112.       double Lo    =low[i];
  113.       double prevLo=low[i-1];
  114.       double prevCl=close[i-1];
  115.       //--- fill main positive and main negative buffers
  116.       double dTmpP=Hi-prevHi;
  117.       double dTmpN=prevLo-Lo;
  118.       if(dTmpP<0.0)   dTmpP=0.0;
  119.       if(dTmpN<0.0)   dTmpN=0.0;
  120.       if(dTmpP>dTmpN) dTmpN=0.0;
  121.       else
  122.         {
  123.          if(dTmpP<dTmpN) dTmpP=0.0;
  124.          else
  125.            {
  126.             dTmpP=0.0;
  127.             dTmpN=0.0;
  128.            }
  129.         }
  130.       //--- define TR
  131.       double tr=MathMax(MathMax(MathAbs(Hi-Lo),MathAbs(Hi-prevCl)),MathAbs(Lo-prevCl));
  132.       //---
  133.       if(tr!=0.0)
  134.         {
  135.          ExtPDBuffer[i]=100.0*dTmpP/tr;
  136.          ExtNDBuffer[i]=100.0*dTmpN/tr;
  137.         }
  138.       else
  139.         {
  140.          ExtPDBuffer[i]=0.0;
  141.          ExtNDBuffer[i]=0.0;
  142.         }
  143.       //--- fill smoothed positive and negative buffers
  144.       ExtPDIBuffer[i]=ExponentialMA(i,ExtADXPeriod,ExtPDIBuffer[i-1],ExtPDBuffer);
  145.       ExtNDIBuffer[i]=ExponentialMA(i,ExtADXPeriod,ExtNDIBuffer[i-1],ExtNDBuffer);
  146.       //--- fill ADXTmp buffer
  147.       double dTmp=ExtPDIBuffer[i]+ExtNDIBuffer[i];
  148.       if(dTmp!=0.0)
  149.          dTmp=100.0*MathAbs((ExtPDIBuffer[i]-ExtNDIBuffer[i])/dTmp);
  150.       else
  151.          dTmp=0.0;
  152.       ExtTmpBuffer[i]=dTmp;
  153.       //--- fill smoothed ADX buffer
  154.       ExtADXBuffer[i]=ExponentialMA(i,ExtADXPeriod,ExtADXBuffer[i-1],ExtTmpBuffer);
  155.      }
  156. //---- OnCalculate done. Return new prev_calculated.
  157. if (((TriggerCandle > 0) && (time[rates_total - 1] > LastAlertTime)) || (TriggerCandle == 0))
  158. {
  159.     string Text;
  160.     // Above Zero Alert
  161.     if (((ExtPDIBuffer[rates_total - 1 - TriggerCandle] > 20) && (ExtPDIBuffer[rates_total - 2 - TriggerCandle] <= 40)) && ((TriggerCandle > 0) || ((TriggerCandle == 0) && (LastAlertDirection != 1))))
  162.     {
  163.         Text = AlertText + "ExtPDIBuffer: " + Symbol() + " - " + EnumToString(Period()) + " - Above Zero.";
  164.         if (EnableNativeAlerts) Alert(Text);
  165.         if (EnableSoundAlerts) PlaySound(SoundFileName);
  166.         LastAlertTime = time[rates_total - 1];
  167.         LastAlertDirection = 1;
  168.     }
  169.     // Below Zero Alert
  170.     if (((ExtPDIBuffer[rates_total - 1 - TriggerCandle] < 20) && (ExtPDIBuffer[rates_total - 2 - TriggerCandle] >= 40)) && ((TriggerCandle > 0) || ((TriggerCandle == 0) && (LastAlertDirection != -1))))
  171.     {
  172.         Text = AlertText + "ExtPDIBuffer: " + Symbol() + " - " + EnumToString(Period()) + " - Below Zero.";
  173.         if (EnableNativeAlerts) Alert(Text);
  174.         if (EnableSoundAlerts) PlaySound(SoundFileName);
  175.         LastAlertTime = time[rates_total - 1];
  176.         LastAlertDirection = -1;
  177.     }
  178. }
  179.    return(rates_total);
  180.   }
  181. //+------------------------------------------------------------------+
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
 
Top