Advertisement
Aphelele

Sochastic x8 with Alert

May 23rd, 2018
291
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.06 KB | None | 0 0
  1. //+------------------------------------------------------------------+
  2. //| Stochastic-X8.mq5 |
  3. //| Copyright © 2010, lukas1 |
  4. //+------------------------------------------------------------------+
  5. #property copyright "Copyright © 2010, lukas1"
  6. #property link ""
  7. #property description "Stochastic-X8"
  8. //---- indicator version
  9. #property version "1.00"
  10. //---- drawing the indicator in a separate window
  11. #property indicator_separate_window
  12. //+-----------------------------------+
  13. //| Declaration of constants |
  14. //+-----------------------------------+
  15. #define LINES_TOTAL 5 // The constant for the number of the indicator lines
  16. #define RESET 0 // The constant for getting the command for the indicator recalculation back to the terminal
  17. //---- number of indicator buffers
  18. #property indicator_buffers LINES_TOTAL
  19. //---- number of graphical plots
  20. #property indicator_plots LINES_TOTAL
  21. //+-----------------------------------+
  22. //| Indicators drawing parameters |
  23. //+-----------------------------------+
  24. //---- drawing the oscillators as lines
  25. #property indicator_type1 DRAW_LINE
  26. //---- selection of the lines colors
  27. #property indicator_color1 BlueViolet
  28. //---- lines are dott-dash curves
  29. #property indicator_style1 STYLE_SOLID
  30. //---- lines 1 width
  31. #property indicator_width1 1
  32. //+-----------------------------------+
  33. //| Indicator input parameters |
  34. //+-----------------------------------+
  35. //---- indicator parameters
  36. input int Stoch_K=12; // the K period (the number of bars) for %K line calculation
  37. input int Stoch_D=8; // smoothing period for %D line calculation
  38. input int Stoch_S=5; // Deceleration value
  39. input double Koef=1.2; // Periods additional multiplying ratio
  40. input ENUM_MA_METHOD ma_method=MODE_LWMA; // Smoothing type
  41. input ENUM_STO_PRICE price_field=STO_CLOSECLOSE; // Stochastic calculation method
  42. //+-----------------------------------+
  43. //---Adding Alert Code
  44. input int TriggerCandle = 1;
  45. input bool EnableNativeAlerts = true;
  46. input bool EnableSoundAlerts = true;
  47. input bool EnableEmailAlerts = true;
  48. input bool EnablePushAlerts = true;
  49. input string AlertEmailSubject = "";
  50. input string AlertText = "";
  51. input string SoundFileName = "alert.wav";
  52. datetime LastAlertTime = D'01.01.1970';
  53. int LastAlertDirection = 0;
  54. //+-----------------------------------+
  55. //---- declaration of integer variables for the indicators handles
  56. int Stoch_Handle[LINES_TOTAL];
  57. //---- declaration of the integer variables for the start of data calculation
  58. int min_rates_total;
  59. //+------------------------------------------------------------------+
  60. //| Variables arrays for the indicator buffers creation |
  61. //+------------------------------------------------------------------+
  62. class CIndicatorsBuffers
  63. {
  64. public: double IndBuffer[];
  65. };
  66. //+------------------------------------------------------------------+
  67. //| Indicator buffers creation |
  68. //+------------------------------------------------------------------+
  69. CIndicatorsBuffers Ind[LINES_TOTAL];
  70. //+------------------------------------------------------------------+
  71. //| Custom indicator initialization function |
  72. //+------------------------------------------------------------------+
  73. void OnInit()
  74. {
  75. //---- initialization of variables of the start of data calculation
  76. min_rates_total=int((Stoch_K+Stoch_D+Stoch_S)*MathPow(Koef,LINES_TOTAL-1));
  77.  
  78. //----
  79. for(int numb=0; numb<LINES_TOTAL; numb++)
  80. {
  81. double K=MathPow(Koef,numb);
  82. int period=int(Stoch_K*K);
  83. int Kperiod=int(Stoch_D*K);
  84. int Dperiod=int(Stoch_S*K);
  85.  
  86. //---- getting handle of the iStochastic indicator
  87. Stoch_Handle[numb]=iStochastic(NULL,0,period,Kperiod,Dperiod,ma_method,price_field);
  88. if(Stoch_Handle[numb]==INVALID_HANDLE)Print(" Failed to get handle of the iStochastic indicator ",numb);
  89.  
  90. string shortname="";
  91. StringConcatenate(shortname,"Stochastic X",LINES_TOTAL,"*",DoubleToString(Koef,1),"(",period,Kperiod,Dperiod,")");
  92. //--- creation of the name to be displayed in a separate sub-window and in a tooltip
  93. PlotIndexSetString(numb,PLOT_LABEL,shortname);
  94. //---- setting the indicator values that won't be visible on a chart
  95. PlotIndexSetDouble(numb,PLOT_EMPTY_VALUE,EMPTY_VALUE);
  96. //---- performing the shift of the beginning of the indicator drawing
  97. PlotIndexSetInteger(numb,PLOT_DRAW_BEGIN,min_rates_total);
  98. //---- set dynamic arrays as indicator buffers
  99. SetIndexBuffer(numb,Ind[numb].IndBuffer,INDICATOR_DATA);
  100. //---- indexing the elements in buffers as timeseries
  101. ArraySetAsSeries(Ind[numb].IndBuffer,true);
  102. //--- set levels
  103. IndicatorSetInteger(INDICATOR_LEVELS,2);
  104. IndicatorSetDouble(INDICATOR_LEVELVALUE,0,20);
  105. IndicatorSetDouble(INDICATOR_LEVELVALUE,1,80);
  106. //---- copying the indicator first line parameters for all the rest ones
  107. PlotIndexSetInteger(numb,PLOT_DRAW_TYPE,PlotIndexGetInteger(0,PLOT_DRAW_TYPE));
  108. PlotIndexSetInteger(numb,PLOT_LINE_STYLE,PlotIndexGetInteger(0,PLOT_LINE_STYLE));
  109. PlotIndexSetInteger(numb,PLOT_LINE_WIDTH,PlotIndexGetInteger(0,PLOT_LINE_WIDTH));
  110. PlotIndexSetInteger(numb,PLOT_LINE_COLOR,PlotIndexGetInteger(0,PLOT_LINE_COLOR));
  111. }
  112. //---- initializations of a variable for the indicator short name
  113. string shortname;
  114. StringConcatenate(shortname,"Stochastic X",LINES_TOTAL,"*",
  115. DoubleToString(Koef,1),"(",EnumToString(ma_method),", ",EnumToString(price_field),")");
  116. //---- creating a name for displaying in a separate sub-window and in a tooltip
  117. IndicatorSetString(INDICATOR_SHORTNAME,shortname);
  118.  
  119. //---- determination of accuracy of displaying the indicator values
  120. IndicatorSetInteger(INDICATOR_DIGITS,_Digits+1);
  121. //---- initialization end
  122. }
  123. //+------------------------------------------------------------------+
  124. //| Custom indicator iteration function |
  125. //+------------------------------------------------------------------+
  126. int OnCalculate(const int rates_total, // number of bars in history at the current tick
  127. const int prev_calculated,// number of bars calculated at previous call
  128. const datetime &time[],
  129. const double &open[],
  130. const double &high[],
  131. const double &low[],
  132. const double &close[],
  133. const long &tick_volume[],
  134. const long &volume[],
  135. const int &spread[])
  136. {
  137. //---- checking the number of bars to be enough for the calculation
  138. if(rates_total<min_rates_total) return(0);
  139. for(int numb=0; numb<LINES_TOTAL; numb++) if(BarsCalculated(Stoch_Handle[numb])<min_rates_total) return(0);
  140.  
  141. //---- declarations of local variables
  142. int to_copy;
  143.  
  144. //---- calculation of the necessary amount of data to be copied
  145. if(prev_calculated>rates_total || prev_calculated<=0) // checking for the first start of the indicator calculation
  146. to_copy=rates_total-1;
  147. else to_copy=rates_total-prev_calculated+1;
  148.  
  149. //---- copy newly appeared data in the indicator buffers
  150. for(int numb=0; numb<LINES_TOTAL; numb++) if(CopyBuffer(Stoch_Handle[numb],0,0,to_copy,Ind[numb].IndBuffer)<=0) return(0);
  151. //----
  152. //+----------------------------------------------+
  153. //---Adding conditions of the alert
  154. if (((TriggerCandle > 0) && (time[rates_total - 1] > LastAlertTime)) || (TriggerCandle == 0))
  155. {
  156. string Text;
  157. // Stochastic Main line reversal above 80 level
  158. if ((Ind[numb].IndBuffer[rates_total - 1 - TriggerCandle] > 80)
  159. && ((TriggerCandle > 0) || ((TriggerCandle == 0) && (LastAlertDirection != 1))))
  160. {
  161. Text = AlertText + "Sell: " + Symbol() + " - " + EnumToString(Period()) + " - Reversal above 80 level";
  162. if (EnableNativeAlerts) Alert(Text);
  163. if (EnableEmailAlerts) SendMail(AlertEmailSubject + "Sochastic Reversal", Text);
  164. if (EnableSoundAlerts) PlaySound(SoundFileName);
  165. if (EnablePushAlerts) SendNotification(Text);
  166. LastAlertTime =time[rates_total - 1];
  167. LastAlertDirection = 1;
  168. }
  169. // Stochastic Main line reversal below 20 level
  170. if ((Ind[numb].IndBuffer[rates_total - 1 - TriggerCandle] < 20)
  171. && ((TriggerCandle > 0) || ((TriggerCandle == 0) && (LastAlertDirection != 1))))
  172. {
  173. Text = AlertText + "Buy: " + Symbol() + " - " + EnumToString(Period()) + " - Reversal below 20 level..";
  174. if (EnableNativeAlerts) Alert(Text);
  175. if (EnableEmailAlerts) SendMail(AlertEmailSubject + "Sochastic Reversal", Text);
  176. if (EnableSoundAlerts) PlaySound(SoundFileName);
  177. if (EnablePushAlerts) SendNotification(Text);
  178. LastAlertTime = time[rates_total - 1];
  179. LastAlertDirection = -1;
  180. }
  181. }
  182. //+----------------------------------------------+
  183. return(rates_total);
  184. }
  185. //+------------------------------------------------------------------+
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement