SHARE
TWEET

Untitled

a guest Jun 26th, 2017 502 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //+------------------------------------------------------------------+
  2. //|                                                   Stochastic.mq4 |
  3. //|                   Copyright 2005-2014, MetaQuotes Software Corp. |
  4. //|                                              http://www.mql4.com |
  5. //+------------------------------------------------------------------+
  6. #property copyright   "2005-2014, MetaQuotes Software Corp."
  7. #property link        "http://www.mql4.com"
  8. #property description "Stochastic Oscillator"
  9. #property strict
  10.  
  11. #property indicator_separate_window
  12. #property indicator_minimum    0
  13. #property indicator_maximum    100
  14. #property indicator_buffers    2
  15. #property indicator_color1     LightSeaGreen
  16. #property indicator_color2     Red
  17. #property indicator_level1     20.0
  18. #property indicator_level2     80.0
  19. #property indicator_levelcolor clrSilver
  20. #property indicator_levelstyle STYLE_DOT
  21. //--- input parameters
  22. input int InpKPeriod=5; // K Period
  23. input int InpDPeriod=3; // D Period
  24. input int InpSlowing=3; // Slowing
  25. input int    TriggerCandle  = 1;
  26. input bool   EnableNativeAlerts = true;
  27. input bool   EnableSoundAlerts  = true;
  28. input bool   EnableEmailAlerts  = true;
  29. input string AlertEmailSubject  = "";
  30. input string AlertText      = "";
  31. input string SoundFileName  = "alert.wav";
  32.  
  33. datetime LastAlertTime = D'01.01.1970';
  34. int LastAlertDirection = 0;
  35. //--- buffers
  36. double ExtMainBuffer[];
  37. double ExtSignalBuffer[];
  38. double ExtHighesBuffer[];
  39. double ExtLowesBuffer[];
  40. //---
  41. int draw_begin1=0;
  42. int draw_begin2=0;
  43. //+------------------------------------------------------------------+
  44. //| Custom indicator initialization function                         |
  45. //+------------------------------------------------------------------+
  46. int OnInit(void)
  47.   {
  48.    string short_name;
  49. //--- 2 additional buffers are used for counting.
  50.    IndicatorBuffers(4);
  51.    SetIndexBuffer(2, ExtHighesBuffer);
  52.    SetIndexBuffer(3, ExtLowesBuffer);
  53. //--- indicator lines
  54.    SetIndexStyle(0,DRAW_LINE);
  55.    SetIndexBuffer(0, ExtMainBuffer);
  56.    SetIndexStyle(1,DRAW_LINE);
  57.    SetIndexBuffer(1, ExtSignalBuffer);
  58. //--- name for DataWindow and indicator subwindow label
  59.    short_name="Sto("+IntegerToString(InpKPeriod)+","+IntegerToString(InpDPeriod)+","+IntegerToString(InpSlowing)+")";
  60.    IndicatorShortName(short_name);
  61.    SetIndexLabel(0,short_name);
  62.    SetIndexLabel(1,"Signal");
  63. //---
  64.    draw_begin1=InpKPeriod+InpSlowing;
  65.    draw_begin2=draw_begin1+InpDPeriod;
  66.    SetIndexDrawBegin(0,draw_begin1);
  67.    SetIndexDrawBegin(1,draw_begin2);
  68. //--- initialization done
  69.    return(INIT_SUCCEEDED);
  70.   }
  71. //+------------------------------------------------------------------+
  72. //| Stochastic oscillator                                            |
  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.    int    i,k,pos;
  86. //--- check for bars count
  87.    if(rates_total<=InpKPeriod+InpDPeriod+InpSlowing)
  88.       return(0);
  89. //--- counting from 0 to rates_total
  90.    ArraySetAsSeries(ExtMainBuffer,false);
  91.    ArraySetAsSeries(ExtSignalBuffer,false);
  92.    ArraySetAsSeries(ExtHighesBuffer,false);
  93.    ArraySetAsSeries(ExtLowesBuffer,false);
  94.    ArraySetAsSeries(low,false);
  95.    ArraySetAsSeries(high,false);
  96.    ArraySetAsSeries(close,false);
  97. //---
  98.    pos=InpKPeriod-1;
  99.    if(pos+1<prev_calculated)
  100.       pos=prev_calculated-2;
  101.    else
  102.      {
  103.       for(i=0; i<pos; i++)
  104.         {
  105.          ExtLowesBuffer[i]=0.0;
  106.          ExtHighesBuffer[i]=0.0;
  107.         }
  108.      }
  109. //--- calculate HighesBuffer[] and ExtHighesBuffer[]
  110.    for(i=pos; i<rates_total && !IsStopped(); i++)
  111.      {
  112.       double dmin=1000000.0;
  113.       double dmax=-1000000.0;
  114.       for(k=i-InpKPeriod+1; k<=i; k++)
  115.         {
  116.          if(dmin>low[k])
  117.             dmin=low[k];
  118.          if(dmax<high[k])
  119.             dmax=high[k];
  120.         }
  121.       ExtLowesBuffer[i]=dmin;
  122.       ExtHighesBuffer[i]=dmax;
  123.      }
  124. //--- %K line
  125.    pos=InpKPeriod-1+InpSlowing-1;
  126.    if(pos+1<prev_calculated)
  127.       pos=prev_calculated-2;
  128.    else
  129.      {
  130.       for(i=0; i<pos; i++)
  131.          ExtMainBuffer[i]=0.0;
  132.      }
  133. //--- main cycle
  134.    for(i=pos; i<rates_total && !IsStopped(); i++)
  135.      {
  136.       double sumlow=0.0;
  137.       double sumhigh=0.0;
  138.       for(k=(i-InpSlowing+1); k<=i; k++)
  139.         {
  140.          sumlow +=(close[k]-ExtLowesBuffer[k]);
  141.          sumhigh+=(ExtHighesBuffer[k]-ExtLowesBuffer[k]);
  142.         }
  143.       if(sumhigh==0.0)
  144.          ExtMainBuffer[i]=100.0;
  145.       else
  146.          ExtMainBuffer[i]=sumlow/sumhigh*100.0;
  147.      }
  148. //--- signal
  149.    pos=InpDPeriod-1;
  150.    if(pos+1<prev_calculated)
  151.       pos=prev_calculated-2;
  152.    else
  153.      {
  154.       for(i=0; i<pos; i++)
  155.          ExtSignalBuffer[i]=0.0;
  156.      }
  157.    for(i=pos; i<rates_total && !IsStopped(); i++)
  158.      {
  159.       double sum=0.0;
  160.       for(k=0; k<InpDPeriod; k++)
  161.          sum+=ExtMainBuffer[i-k];
  162.       ExtSignalBuffer[i]=sum/InpDPeriod;
  163.      }
  164. //--- Alert RP.
  165. if (((TriggerCandle > 0) && (Time[0] > LastAlertTime)) || (TriggerCandle == 0))
  166. {
  167.     string Text;
  168.     // Above Zero Alert
  169.     if (((ExtSignalBuffer[TriggerCandle] > 80) && (ExtSignalBuffer[TriggerCandle+ 1] <= 80)) && ((TriggerCandle > 0) || ((TriggerCandle == 0) && (LastAlertDirection != 1))))
  170.     {
  171.         Text = AlertText + "Stochastic: " + Symbol() + " - " + EnumToString((ENUM_TIMEFRAMES)Period()) + " - Above Eighty.";
  172.         if (EnableNativeAlerts) Alert(Text);
  173.         if (EnableEmailAlerts) SendMail(AlertEmailSubject + "Stochastic Alert", Text);
  174.         if (EnableSoundAlerts) PlaySound(SoundFileName);
  175.         LastAlertTime = Time[0];
  176.         LastAlertDirection = 1;
  177.     }
  178.     // Below Zero Alert
  179.     if (((ExtSignalBuffer[TriggerCandle] < 20) && (ExtSignalBuffer[TriggerCandle+ 1] >= 20)) && ((TriggerCandle > 0) || ((TriggerCandle == 0) && (LastAlertDirection != -1))))
  180.     {
  181.         Text = AlertText + "Stochastic: " + Symbol() + " - " + EnumToString((ENUM_TIMEFRAMES)Period()) + " - Below Twenty.";
  182.         if (EnableNativeAlerts) Alert(Text);
  183.         if (EnableEmailAlerts) SendMail(AlertEmailSubject + "Stochastic Alert", Text);
  184.         if (EnableSoundAlerts) PlaySound(SoundFileName);
  185.         LastAlertTime = Time[0];
  186.         LastAlertDirection = -1;
  187.     }
  188. }
  189. //--- OnCalculate done. Return new prev_calculated.
  190.    return(rates_total);
  191.   }
  192. //+------------------------------------------------------------------+
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