Advertisement
Guest User

Untitled

a guest
Nov 19th, 2019
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.81 KB | None | 0 0
  1. //+------------------------------------------------------------------+
  2. //| BBandwidth.mq5 |
  3. //| Copyright Copyright 2010, Investors Haven |
  4. //| http://www.InvestorsHaven.com |
  5. //+------------------------------------------------------------------+
  6. #property copyright "Copyright 2010, Investors Haven"
  7. #property link "http://www.InvestorsHaven.com"
  8. #property version "1.00"
  9. #property indicator_separate_window
  10. #property indicator_buffers 1
  11. #property indicator_plots 1
  12. //--- plot BBBandwidth
  13. #property indicator_label1 "BBandwidth"
  14. #property indicator_type1 DRAW_LINE
  15. #property indicator_color1 Purple
  16. #property indicator_style1 STYLE_SOLID
  17. #property indicator_width1 3
  18. // --- Inputs
  19. input int InpBandsPeriod=20; // Period
  20. input int InpBandsShift=0; // Shift
  21. input double InpBandsDeviations=2.0; // Deviation
  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. // --- BollingerBand handle
  34. int BBHandle;
  35. //--- indicator buffers
  36. double BBandwidthBuffer[];
  37. //+------------------------------------------------------------------+
  38. //| Custom indicator initialization function |
  39. //+------------------------------------------------------------------+
  40. int OnInit()
  41. {
  42. //--- indicator buffers mapping
  43. SetIndexBuffer(0,BBandwidthBuffer,INDICATOR_DATA);
  44. /*
  45. saving processing time by making calls to the iBands indicator to get the upper and
  46. lower band values, then we just perform a simple calculation to output the result
  47. to a single indicator on a seperate chart window.
  48. */
  49. BBHandle=iBands(_Symbol,PERIOD_CURRENT,InpBandsPeriod,InpBandsShift,InpBandsDeviations,PRICE_CLOSE);
  50.  
  51. //---
  52. return(0);
  53. }
  54. //+------------------------------------------------------------------+
  55. //| Custom indicator iteration function |
  56. //+------------------------------------------------------------------+
  57. int OnCalculate(const int rates_total,
  58. const int prev_calculated,
  59. const datetime &time[],
  60. const double &open[],
  61. const double &high[],
  62. const double &low[],
  63. const double &close[],
  64. const long &tick_volume[],
  65. const long &volume[],
  66. const int &spread[])
  67. {
  68. //--- variables
  69. int pos=0;
  70. double upper_band[];
  71. double lower_band[];
  72.  
  73. // --- Turn the arrays into dynamic series arrays
  74. ArraySetAsSeries(lower_band,true);
  75. ArraySetAsSeries(upper_band,true);
  76. ArraySetAsSeries(BBandwidthBuffer,true);
  77.  
  78. //--- check if all data calculated
  79. if(BarsCalculated(BBHandle)<rates_total) return(0);
  80. //--- we can copy all data
  81. if(CopyBuffer(BBHandle,1,0,rates_total,upper_band) <=0) return(0);
  82. if(CopyBuffer(BBHandle,2,0,rates_total,lower_band) <=0) return(0);
  83.  
  84. /*
  85. Now loop through the rates_total to populate the the buffer with the calculation
  86. needed to get the bandwidth of the bollinger bands. Using a switch on the digits
  87. value to determine the decimal calculation offset so that result is consistent.
  88. */
  89. for(int i=pos;i<rates_total -1;i++)
  90. {
  91. switch(_Digits)
  92. {
  93. case 2:
  94. {
  95. BBandwidthBuffer[i]=floor(((upper_band[i]-lower_band[i])/_Point)*0.1);
  96. break;
  97. }
  98. case 3:
  99. {
  100. BBandwidthBuffer[i]=floor(((upper_band[i]-lower_band[i])/_Point)*0.01);
  101. break;
  102. }
  103. case 4:
  104. {
  105. BBandwidthBuffer[i]=floor((upper_band[i]-lower_band[i])/_Point);
  106. break;
  107. }
  108. case 5:
  109. {
  110. BBandwidthBuffer[i]=floor(((upper_band[i]-lower_band[i])/_Point)*0.1);
  111. break;
  112. }
  113. default:
  114. {
  115. BBandwidthBuffer[i]=floor((upper_band[i]-lower_band[i])/_Point);
  116. break;
  117. }
  118. }
  119. }
  120.  
  121. //--- return value of prev_calculated for next call
  122. if (((TriggerCandle > 0) && (time[rates_total - 1] > LastAlertTime)) || (TriggerCandle == 0))
  123. {
  124. string Text;
  125. // Bellow 13.000
  126. if (((BBandwidthBuffer[rates_total - 1 - TriggerCandle] < 0) && (BBandwidthBuffer[TriggerCandle+ 1] >= 0)) && ((TriggerCandle > 0) || ((TriggerCandle == 0) && (LastAlertDirection != -1))))
  127. {
  128. Text = AlertText + "BBandwidthBuffer: " + Symbol() + " - " + EnumToString(Period()) + " - Bellow 13.000.";
  129. if (EnableNativeAlerts) Alert(Text);
  130. if (EnableEmailAlerts) SendMail(AlertEmailSubject + "BBandwidthBuffer Alert", Text);
  131. if (EnableSoundAlerts) PlaySound(SoundFileName);
  132. if (EnablePushAlerts) SendNotification(Text);
  133. LastAlertTime = time[rates_total - 1];
  134. LastAlertDirection = 1;
  135. }
  136. // Below 10.000
  137. if (((BBandwidthBuffer[rates_total - 1 - TriggerCandle] < 0) && (BBandwidthBuffer[TriggerCandle+ 1] >= 0)) && ((TriggerCandle > 0) || ((TriggerCandle == 0) && (LastAlertDirection != -1))))
  138. {
  139. Text = AlertText + "BBandwidthBuffer: " + Symbol() + " - " + EnumToString(Period()) + " - Below 10.000.";
  140. if (EnableNativeAlerts) Alert(Text);
  141. if (EnableEmailAlerts) SendMail(AlertEmailSubject + "BBandwidthBuffer Alert", Text);
  142. if (EnableSoundAlerts) PlaySound(SoundFileName);
  143. if (EnablePushAlerts) SendNotification(Text);
  144. LastAlertTime = time[rates_total - 1];
  145. LastAlertDirection = -1;
  146. }
  147. }
  148. return(rates_total);
  149. }
  150. //+------------------------------------------------------------------+
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement