OsMA

楼主  收藏   举报   帖子创建时间:  2019-05-05 05:02 回复:0 关注量:706
EURGBPH1.png

  1. //+------------------------------------------------------------------+
  2. //|                                                         OsMA.mq4 |
  3. //|                      Copyright ?2004, metaQuotes Software Corp. |
  4. //|                                       http://www.metaquotes.net/ |
  5. //|                                    Copyright ?2006, Robert Hill |
  6. //|                                    Copyright ?2008, Linuxser    |
  7. //+------------------------------------------------------------------+
  8. #property  copyright "Copyright ?2006, Robert Hill"
  9. #property  copyright "Copyright ?2008, Linuxser and Forex-TSD"
  10. #property  link      "http://www.metaquotes.net/"
  11. //---- indicator settings
  12. #property  indicator_separate_window
  13. #property  indicator_buffers 2
  14. #property  indicator_color1  DarkSeaGreen
  15. #property  indicator_color2  Tomato

  16. //---- indicator parameters
  17. extern bool SoundON=true;
  18. extern bool EmailON=false;
  19. extern bool HistogramAlarm=false;
  20. extern bool ZeroLineAlarm=false;
  21. extern int FastEMA=12;
  22. extern int SlowEMA=26;
  23. extern int SignalSMA=9;

  24. //---- indicator buffers
  25. double     OsmaBuffer[];
  26. double     MacdBuffer[];
  27. double     SignalBuffer[];
  28. double HistogramBufferUp[];
  29. double HistogramBufferDown[];
  30. bool HistAboveZero = false;
  31. bool HistBelowZero = false;
  32. bool MACDAboveZero = false;
  33. bool MACDBelowZero = false;

  34. //+------------------------------------------------------------------+
  35. //| Custom indicator initialization function                         |
  36. //+------------------------------------------------------------------+
  37. int init()
  38.   {
  39. //---- 2 additional buffers are used for counting.
  40.    IndicatorBuffers(5);
  41. //---- drawing settings
  42.    SetIndexDrawBegin(0,SignalSMA);
  43.    SetIndexStyle(0,DRAW_HISTOGRAM,STYLE_SOLID);
  44.    SetIndexBuffer(0,HistogramBufferUp);
  45.    SetIndexStyle(1,DRAW_HISTOGRAM,STYLE_SOLID);
  46.    SetIndexBuffer(1,HistogramBufferDown);
  47.    IndicatorDigits(Digits+2);
  48. //---- 3 indicator buffers mapping
  49.    SetIndexBuffer(2,OsmaBuffer);
  50.    SetIndexBuffer(3,MacdBuffer);
  51.    SetIndexBuffer(4,SignalBuffer);
  52.    SetIndexBuffer(0,HistogramBufferUp);
  53.    SetIndexBuffer(1,HistogramBufferDown);
  54.    
  55. //---- name for DataWindow and indicator subwindow label
  56.    IndicatorShortName("OsMA("+FastEMA+","+SlowEMA+","+SignalSMA+")");
  57. //---- initialization done
  58.    return(0);
  59.   }
  60. //+------------------------------------------------------------------+
  61. //| Moving Average of Oscillator                                     |
  62. //+------------------------------------------------------------------+
  63. int start()
  64.   {

  65.    double current, prev;
  66.    int limit;
  67.    int counted_bars=IndicatorCounted();
  68. //---- last counted bar will be recounted
  69.    if(counted_bars>0) counted_bars--;
  70.    limit=Bars-counted_bars;
  71. //---- macd counted in the 1-st additional buffer
  72.    for(int i=0; i<limit; i++)
  73.       MacdBuffer[i]=iMA(NULL,0,FastEMA,0,MODE_EMA,PRICE_CLOSE,i)-iMA(NULL,0,SlowEMA,0,MODE_EMA,PRICE_CLOSE,i);
  74. //---- signal line counted in the 2-nd additional buffer
  75.    for(i=0; i<limit; i++)
  76.       SignalBuffer[i]=iMAonArray(MacdBuffer,Bars,SignalSMA,0,MODE_SMA,i);
  77. //---- main loop
  78.    for(i=0; i<limit; i++)
  79.       OsmaBuffer[i]=MacdBuffer[i]-SignalBuffer[i];

  80.    for(i=0; i<limit; i++)
  81.    {
  82.       HistogramBufferUp[i] = 0;
  83.       HistogramBufferDown[i] = 0;
  84.       current = MacdBuffer[i] - SignalBuffer[i];
  85.       prev = MacdBuffer[i+1] - SignalBuffer[i+1];
  86.       if (current > prev)
  87.       {
  88.         HistogramBufferUp[i] = current;
  89.         HistogramBufferDown[i] = 0.0;
  90.       }
  91.       else
  92.       {
  93.         HistogramBufferDown[i] = current;
  94.         HistogramBufferUp[i] = 0.0;
  95.       }
  96.       
  97.       if (MACDAboveZero) Comment ("nThe trend has changed to UP");
  98.       if (MACDBelowZero) Comment ("nThe trend has changed to DOWN");
  99.       
  100.       if (i == 1)
  101.       {
  102. // Check for Histogram Change Color
  103.         if  (HistogramAlarm==true)
  104.         {
  105.         if (HistogramBufferUp[i] > HistogramBufferDown[i + 1])
  106.         {
  107. // Cross up
  108.          if (HistAboveZero == false)
  109.          {
  110.            HistAboveZero=true;
  111.            HistBelowZero=false;
  112.            if (SoundON) alert("OSMA is Positive","n Time=",TimeToStr(CurTime(),TIME_DATE)," ",TimeHour(CurTime()),":",TimeMinute(CurTime()),"n Symbol=",Symbol()," Period=",Period());
  113.            if (EmailON) SendMail("OSMA is Positive", "MACD Crossed up, Date="+TimeToStr(CurTime(),TIME_DATE)+" "+TimeHour(CurTime())+":"+TimeMinute(CurTime())+" Symbol="+Symbol()+" Period="+Period());
  114.          }
  115.         }
  116.         else if (HistogramBufferDown[i] <  HistogramBufferUp[i + 1] > 0)
  117.         {
  118. // Cross down
  119.          if (HistBelowZero == false)
  120.          {
  121.           HistBelowZero=true;
  122.           HistAboveZero=false;
  123.           if (SoundON) alert("OSMA is Negative","n Date=",TimeToStr(CurTime(),TIME_DATE)," ",TimeHour(CurTime()),":",TimeMinute(CurTime()),"n Symbol=",Symbol()," Period=",Period());
  124.           if (EmailON) SendMail("OSMA is Negative","MACD Crossed Down, Date="+TimeToStr(CurTime(),TIME_DATE)+" "+TimeHour(CurTime())+":"+TimeMinute(CurTime())+" Symbol="+Symbol()+" Period="+Period());
  125.          }
  126.         }
  127.         }
  128. // Check for MACD Signal line crossing 0 line
  129.         if (ZeroLineAlarm==true)
  130.         {
  131.         if (OsmaBuffer[i] > 0 && OsmaBuffer[i + 1] < 0)
  132.         {
  133. // Cross up
  134.          if (MACDAboveZero == false)
  135.          {
  136.            MACDAboveZero=true;
  137.            MACDBelowZero=false;
  138.            if (SoundON) alert("Histogram is Above Zero Line","n Time=",TimeToStr(CurTime(),TIME_DATE)," ",TimeHour(CurTime()),":",TimeMinute(CurTime()),"n Symbol=",Symbol()," Period=",Period());
  139.            if (EmailON) SendMail("Histogram is Above Zero Line", "OSMA Crossed up, Date="+TimeToStr(CurTime(),TIME_DATE)+" "+TimeHour(CurTime())+":"+TimeMinute(CurTime())+" Symbol="+Symbol()+" Period="+Period());
  140.          }
  141.         }
  142.         else if (OsmaBuffer[i] < 0 && OsmaBuffer[i + 1] > 0)
  143.         {
  144. // Cross down
  145.          if (MACDBelowZero == false)
  146.          {
  147.           MACDBelowZero=true;
  148.           MACDAboveZero=false;
  149.           if (SoundON) alert("Histogram is Below Zero Line","n Date=",TimeToStr(CurTime(),TIME_DATE)," ",TimeHour(CurTime()),":",TimeMinute(CurTime()),"n Symbol=",Symbol()," Period=",Period());
  150.           if (EmailON) SendMail("Histogram is Below Zero Line","OSMA Crossed Down, Date="+TimeToStr(CurTime(),TIME_DATE)+" "+TimeHour(CurTime())+":"+TimeMinute(CurTime())+" Symbol="+Symbol()+" Period="+Period());
  151.          }
  152.         }
  153.       }
  154.     }  
  155.    }
  156.       
  157. //---- done
  158.    return(0);
  159.   }
打赏