Torrik20

indicator

Jul 13th, 2019
189
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. //+------------------------------------------------------------------+
  3. //| NHNL.mq5 |
  4. //| Copyright 2018, MetaQuotes Software Corp. |
  5. //| https://mql5.com |
  6. //+------------------------------------------------------------------+
  7. #property copyright "Copyright 2018, MetaQuotes Software Corp."
  8. #property link "https://mql5.com"
  9. #property version "1.00"
  10. #property indicator_separate_window
  11. #property indicator_buffers 3
  12. #property indicator_plots 3
  13. //--- plot High
  14. #property indicator_label1 "High Index"
  15. #property indicator_type1 DRAW_LINE
  16. #property indicator_color1 clrBlack
  17. #property indicator_style1 STYLE_SOLID
  18. #property indicator_width1 1
  19. //--- plot Low
  20. #property indicator_label2 "Low Index"
  21. #property indicator_type2 DRAW_LINE
  22. #property indicator_color2 clrBlack
  23. #property indicator_style2 STYLE_SOLID
  24. #property indicator_width2 1
  25. //--- plot High+Low
  26. #property indicator_label3 "Index HL"
  27. #property indicator_type3 DRAW_LINE
  28. #property indicator_color3 clrBlue
  29. #property indicator_style3 STYLE_SOLID
  30. #property indicator_width3 3
  31. //--- input parameters
  32. input uint InpPeriod=20;
  33. input int TriggerCandle = 1;
  34. input bool EnableNativeAlerts = true;
  35. input bool EnableSoundAlerts = true;
  36. input bool EnableEmailAlerts = false;
  37. input bool EnablePushAlerts = true;
  38. input string AlertEmailSubject = "";
  39. input string AlertText = "CROSS";
  40. input string SoundFileName = "alert.wav";
  41.  
  42. datetime LastAlertTime = D'01.01.1970';
  43. int LastAlertDirection = 0;
  44. //--- indicator buffers
  45. double BufferHigh[];
  46. double BufferLow[];
  47. double BufferHL[];
  48. //--- global variables
  49. int period;
  50. //+------------------------------------------------------------------+
  51. //| Custom indicator initialization function |
  52. //+------------------------------------------------------------------+
  53. int OnInit()
  54. {
  55. //--- setting global variables
  56. period=int(InpPeriod<1 ? 1 : InpPeriod);
  57. //--- indicator buffers mapping
  58. SetIndexBuffer(0,BufferHigh,INDICATOR_DATA);
  59. SetIndexBuffer(1,BufferLow,INDICATOR_DATA);
  60. SetIndexBuffer(2,BufferHL,INDICATOR_DATA);
  61. //--- settings indicators parameters
  62. IndicatorSetInteger(INDICATOR_DIGITS,Digits());
  63. IndicatorSetString(INDICATOR_SHORTNAME,"New High, New Low Index("+(string)period+")");
  64. //--- setting buffer arrays as timeseries
  65. ArraySetAsSeries(BufferHigh,true);
  66. ArraySetAsSeries(BufferLow,true);
  67. ArraySetAsSeries(BufferHL,true);
  68. //---
  69. return(INIT_SUCCEEDED);
  70. }
  71. //+------------------------------------------------------------------+
  72. //| Custom indicator iteration function |
  73. //+------------------------------------------------------------------+
  74. int OnCalculate(const int rates_total,
  75. const int prev_calculated,
  76. const datetime &time[],
  77. const double &open[],
  78. const double &high[],
  79. const double &low[],
  80. const double &close[],
  81. const long &tick_volume[],
  82. const long &volume[],
  83. const int &spread[])
  84. {
  85. //--- Проверка на минимальное количество баров для расчёта
  86. if(rates_total<period) return 0;
  87. //--- Установка индексации массивов как таймсерий
  88. ArraySetAsSeries(high,true);
  89. ArraySetAsSeries(low,true);
  90. //--- Проверка и расчёт количества просчитываемых баров
  91. int limit=rates_total-prev_calculated;
  92. if(limit>1)
  93. {
  94. limit=rates_total-period-2;
  95. ArrayInitialize(BufferHigh,EMPTY_VALUE);
  96. ArrayInitialize(BufferLow,EMPTY_VALUE);
  97. ArrayInitialize(BufferHL,EMPTY_VALUE);
  98. }
  99. //--- Расчёт индикатора
  100. for(int i=limit; i>=0 && !IsStopped(); i--)
  101. {
  102. int count_h=0;
  103. int count_l=0;
  104. for(int n=0; n<period; n++)
  105. {
  106. if(high[i+n]>high[i+n+1]) count_h++;
  107. if(low[i+n]<low[i+n+1]) count_l--;
  108. }
  109. BufferHigh[i]=count_h;
  110. BufferLow[i]=count_l;
  111. BufferHL[i]=count_h+count_l;
  112. }
  113. //---- OnCalculate done. Return new prev_calculated.
  114. //--- adding alert
  115. if (((TriggerCandle > 0) && (time[rates_total - 0] > LastAlertTime)) || (TriggerCandle == 0))
  116. {
  117. string Text;
  118. // Above Zero Alert
  119. if (((BufferHL[rates_total - 0 - TriggerCandle] > 0) && (BufferHL[rates_total + 1 - TriggerCandle] <= 0)) && ((TriggerCandle > 0) || ((TriggerCandle == 0) && (LastAlertDirection != 1))))
  120. {
  121. Text = AlertText + "BufferHL: " + Symbol() + " - " + EnumToString(Period()) + " - Above Zero.";
  122. if (EnableNativeAlerts) Alert(Text);
  123. if (EnableEmailAlerts) SendMail(AlertEmailSubject + "BufferL Alert", Text);
  124. if (EnableSoundAlerts) PlaySound(SoundFileName);
  125. if (EnablePushAlerts) SendNotification(Text);
  126. LastAlertTime = time[rates_total - 0];
  127. LastAlertDirection = - 1;
  128. }
  129. // Below Zero Alert
  130. if (((BufferHL[rates_total - 0 - TriggerCandle] < 0) && (BufferHL[rates_total + 1 - TriggerCandle] >= 0)) && ((TriggerCandle > 0) || ((TriggerCandle == 0) && (LastAlertDirection != -1))))
  131. {
  132. Text = AlertText + "BufferHL: " + Symbol() + " - " + EnumToString(Period()) + " - Below Zero.";
  133. if (EnableNativeAlerts) Alert(Text);
  134. if (EnableEmailAlerts) SendMail(AlertEmailSubject + "BufferHL Alert", Text);
  135. if (EnableSoundAlerts) PlaySound(SoundFileName);
  136. if (EnablePushAlerts) SendNotification(Text);
  137. LastAlertTime = time[rates_total - 0];
  138. LastAlertDirection = -1;
  139. }
  140. }
  141.  
  142.  
  143. //--- return value of prev_calculated for next call
  144. return(rates_total);
  145. }
  146. //+------------------------------------------------------------------+
RAW Paste Data