MACD Stochastic Coral

Author: 2009-2020, MetaQuotes Software Corp.
Indicators Used
MACD HistogramStochastic oscillatorMoving average indicator
0 Views
0 Downloads
0 Favorites
MACD Stochastic Coral
ÿþ//+------------------------------------------------------------------+

//|                                        MACD Stochastic Coral.mq5 |

//|                   Copyright 2009-2020, MetaQuotes Software Corp. |

//|                                              http://www.mql5.com |

//+------------------------------------------------------------------+

#property copyright "2009-2020, MetaQuotes Software Corp."

#property link      "http://www.mql5.com"

//--- indicator settings

#property indicator_chart_window

#property indicator_buffers 12

#property indicator_plots   4

#property indicator_type1   DRAW_ARROW

#property indicator_color1  Wheat

#property indicator_width1  2

#property indicator_label1  "MACD Stochastic Coral Sell"

#property indicator_type2   DRAW_ARROW

#property indicator_color2  LightSeaGreen

#property indicator_width2  2

#property indicator_label2  "MACD Stochastic Coral Buy"

//--- plot C

#property indicator_label3  "Coral"

#property indicator_type3   DRAW_COLOR_LINE

#property indicator_color3  clrGreen,clrRed,clrDarkGray

#property indicator_style3  STYLE_SOLID

#property indicator_width3  2

#property indicator_type4   DRAW_LINE

#property indicator_color4  Gold

//---

input group             "---- MACD ----"

input bool               InpMACD=false;               // Off

input int                InpFastEMA=12;               // Fast EMA period

input int                InpSlowEMA=26;               // Slow EMA period

input int                InpSignalSMA=9;              // Signal SMA period

input ENUM_APPLIED_PRICE InpAppliedPrice=PRICE_CLOSE; // Applied price

input group             "---- Stochastic ----"

input bool               InpStochastic=false;         // Off

input int                InpKPeriod=50;               // K period

input int                InpDPeriod=20;               // D period

input int                InpSlowing=10;               // Slowing

input ENUM_MA_METHOD     InpMAMethod=MODE_SMA;        // MA Method

input ENUM_STO_PRICE     InpStoPrice=STO_LOWHIGH;     // PriceCoral

input group             "---- Coral+MA ----"

input bool               InpCoral=true;               // Off

input double             InpCoeff=0.14;               // Coefficient

input ENUM_APPLIED_PRICE InpCoralPrice=PRICE_CLOSE;   // Applied price

input int                InpMAPeriod=14;              // Period

input int                InpMAShift=0;                // Shift

input ENUM_MA_METHOD     InpSMMethod=MODE_SMA;        // Method

input ENUM_APPLIED_PRICE InpSMPrice=PRICE_CLOSE;      // Applied price

input bool               Inpyna=false;                // 1 MA>Coral

input bool               Inpdoy=true;                 // 2 MA<Coral

input bool               Inptrei=false;               // 3 Coral><Coral

//--- indicator buffers

double SellBuffer[];

double BuyBuffer[];

double BufferC[];

double BufferColors[];

double BufferB1[];

double BufferB2[];

double BufferB3[];

double BufferB4[];

double BufferB5[];

double BufferB6[];

double BufferMA[];

double ExtLineBuffer[];

//---

double coeff1;

double coeff2;

int p,P_,OldTrend;

//---

int ExtMacdHandle;

int ExtStoHandle;

int ExtCoralHandle;

int ExtMaHandle;

//+------------------------------------------------------------------+

//| Custom indicator initialization function                         |

//+------------------------------------------------------------------+

void OnInit()

  {

//--- indicator buffers mapping

   SetIndexBuffer(0,SellBuffer,INDICATOR_DATA);

   PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,InpKPeriod+InpDPeriod+InpSlowing);

//--- A>740=85 <5B:8 4;O >B>1@065=8O 2 DataWindow

   PlotIndexSetString(0,PLOT_LABEL,"Sell");

//---- A8<2>; 4;O 8=48:0B>@0

   PlotIndexSetInteger(0,PLOT_ARROW,234);

//---- 8=45:A0F8O M;5<5=B>2 2 1CD5@5 :0: 2 B09<A5@88

   ArraySetAsSeries(SellBuffer,true);

//---- ?@52@0I5=85 48=0<8G5A:>3> <0AA820 2 8=48:0B>@=K9 1CD5@

//---- >ACI5AB2;5=85 A42830 =0G0;0 >BAG5B0 >B@8A>2:8 8=48:0B>@0 2

   SetIndexBuffer(1,BuyBuffer,INDICATOR_DATA);

   PlotIndexSetInteger(1,PLOT_DRAW_BEGIN,InpKPeriod+InpDPeriod+InpSlowing);

//--- A>740=85 <5B:8 4;O >B>1@065=8O 2 DataWindow

   PlotIndexSetString(1,PLOT_LABEL,"Buy");

//---- A8<2>; 4;O 8=48:0B>@0

   PlotIndexSetInteger(1,PLOT_ARROW,233);

//---- 8=45:A0F8O M;5<5=B>2 2 1CD5@5 :0: 2 B09<A5@88

   ArraySetAsSeries(BuyBuffer,true);

//--- set global variables

   coeff1=(InpCoeff<=0.0086 ? 0.0086 : InpCoeff>1 ? 1: InpCoeff);

   coeff2=1.0-coeff1;

//--- indicator buffers mapping

   SetIndexBuffer(2,BufferC,INDICATOR_CALCULATIONS);

   SetIndexBuffer(3,BufferColors,INDICATOR_COLOR_INDEX);

   SetIndexBuffer(4,ExtLineBuffer,INDICATOR_CALCULATIONS);

   SetIndexBuffer(5,BufferB1,INDICATOR_CALCULATIONS);

   SetIndexBuffer(6,BufferB2,INDICATOR_CALCULATIONS);

   SetIndexBuffer(7,BufferB3,INDICATOR_CALCULATIONS);

   SetIndexBuffer(8,BufferB4,INDICATOR_CALCULATIONS);

   SetIndexBuffer(9,BufferB5,INDICATOR_CALCULATIONS);

   SetIndexBuffer(10,BufferB6,INDICATOR_CALCULATIONS);

   SetIndexBuffer(11,BufferMA,INDICATOR_CALCULATIONS);

//--- setting buffer arrays as timeseries

   ArraySetAsSeries(BufferC,true);

   ArraySetAsSeries(BufferColors,true);

   ArraySetAsSeries(ExtLineBuffer,true);

   ArraySetAsSeries(BufferB1,true);

   ArraySetAsSeries(BufferB2,true);

   ArraySetAsSeries(BufferB3,true);

   ArraySetAsSeries(BufferB4,true);

   ArraySetAsSeries(BufferB5,true);

   ArraySetAsSeries(BufferB6,true);

   ArraySetAsSeries(BufferMA,true);

//--- set accuracy

   IndicatorSetInteger(INDICATOR_DIGITS,_Digits+1);

//--- set first bar from what index will be drawn

   PlotIndexSetInteger(11,PLOT_DRAW_BEGIN,InpMAPeriod);

//--- line shifts when drawing

   PlotIndexSetInteger(11,PLOT_SHIFT,InpMAShift);

//--- set drawing line empty value

   PlotIndexSetDouble(11,PLOT_EMPTY_VALUE,0.0);

//--- name for DataWindow and indicator subwindow label

   string short_name=StringFormat("MACD-(%d,%d,%d)Stochastic-(%d,%d,%d)MA-(%d)",InpFastEMA,InpSlowEMA,InpSignalSMA,InpKPeriod,InpDPeriod,InpSlowing,InpMAPeriod);

   IndicatorSetString(INDICATOR_SHORTNAME,short_name);

//--- get Macd Stochastic handles

   ExtMacdHandle=iMACD(NULL,0,InpFastEMA,InpSlowEMA,InpSignalSMA,InpAppliedPrice);

   ExtStoHandle=iStochastic(NULL,0,InpKPeriod,InpDPeriod,InpSlowing,InpMAMethod,InpStoPrice);

   ExtCoralHandle=iMA(NULL,0,1,0,InpMAMethod,InpCoralPrice);

   ExtMaHandle=iMA(NULL,0,InpMAPeriod,InpMAShift,InpSMMethod,InpSMPrice);

  }

//+------------------------------------------------------------------+

//| Stochastic Oscillator                                            |

//+------------------------------------------------------------------+

int OnCalculate(const int rates_total,

                const int prev_calculated,

                const datetime &time[],

                const double &open[],

                const double &high[],

                const double &low[],

                const double &close[],

                const long &tick_volume[],

                const long &volume[],

                const int &spread[])

  {

   int  i,to_copy,limit;

   double STO[],MACD[],STOS[],MACDS[];

   bool StochUp,StochDn,MacdUp,MacdDn,CoralUp,CoralDn;

//--- @>25@:0 8 @0AGQB :>;8G5AB20 ?@>AG8BK205<KE 10@>2

   limit=rates_total-prev_calculated;

   if(limit>1)

     {

      limit=rates_total-2;

      ArrayInitialize(BufferC,EMPTY_VALUE);

      ArrayInitialize(BufferColors,2);

      ArrayInitialize(BufferB1,0);

      ArrayInitialize(BufferB2,0);

      ArrayInitialize(BufferB3,0);

      ArrayInitialize(BufferB4,0);

      ArrayInitialize(BufferB5,0);

      ArrayInitialize(BufferB6,0);

      ArrayInitialize(BufferMA,0);

      ArrayInitialize(ExtLineBuffer,0);

     }

//--- >43>B>2:0 40==KE

   int count=(limit>1 ? rates_total : 1);

   int copied=CopyBuffer(ExtCoralHandle,0,0,count,BufferMA);

   copied=CopyBuffer(ExtMaHandle,0,0,count,ExtLineBuffer);

   if(copied!=count)

      return 0;

//---  0AGQB 8=48:0B>@0

   for(i=limit; i>=0 && !IsStopped(); i--)

     {

      if(i==rates_total-2)

         BufferB1[i]=BufferB2[i]=BufferB3[i]=BufferB4[i]=BufferB5[i]=BufferB6[i]=BufferMA[i];

      else

        {

         BufferB1[i]=coeff1*BufferMA[i]+coeff2*BufferB1[i+1];

         BufferB2[i]=coeff1*BufferB1[i]+coeff2*BufferB2[i+1];

         BufferB3[i]=coeff1*BufferB2[i]+coeff2*BufferB3[i+1];

         BufferB4[i]=coeff1*BufferB3[i]+coeff2*BufferB4[i+1];

         BufferB5[i]=coeff1*BufferB4[i]+coeff2*BufferB5[i+1];

         BufferB6[i]=coeff1*BufferB5[i]+coeff2*BufferB6[i+1];

         BufferC[i]=(-0.064)*BufferB6[i]+0.672*BufferB5[i]-2.352*BufferB4[i]+2.744*BufferB3[i];

         BufferColors[i]=(BufferC[i]>BufferC[i+1] ? 0 : BufferC[i]<BufferC[i+1] ? 1 : 2);

        }

     }

//--- check for bars count

   if(rates_total<=InpKPeriod+InpDPeriod+InpSlowing)

      return(0);

   int  calculated=BarsCalculated(ExtMacdHandle);

   if(calculated<rates_total)

     {

      Print("Not all data of ExtFastMaHandle is calculated (",calculated," bars). Error ",GetLastError());

      return(0);

     }

   calculated=BarsCalculated(ExtStoHandle);

   if(calculated<rates_total)

     {

      Print("Not all data of ExtSlowMaHandle is calculated (",calculated," bars). Error ",GetLastError());

      return(0);

     }

//--- AB0@B>2>3> =><5@0 limit 4;O F8:;0 ?5@5AG5B0 10@>2

   if(prev_calculated>rates_total || prev_calculated<=0)// ?@>25@:0 =0 ?5@2K9 AB0@B @0AG5B0 8=48:0B>@0

     {

      to_copy=rates_total; // @0AG5B=>5 :>;8G5AB2> 2A5E 10@>2

      limit=rates_total-InpKPeriod+InpDPeriod+InpSlowing; // AB0@B>2K9 =><5@ 4;O @0AG5B0 2A5E 10@>2

     }

   else

     {

      to_copy=rates_total-prev_calculated+1; // @0AG5B=>5 :>;8G5AB2> B>;L:> =>2KE 10@>2

      limit=rates_total-prev_calculated; // AB0@B>2K9 =><5@ 4;O @0AG5B0 =>2KE 10@>2

     }

//--- get Fast EMA buffer

   if(IsStopped()) // checking for stop flag

      return(0);

   if(CopyBuffer(ExtStoHandle,0,0,to_copy,STO)<=0)

     {

      Print("Getting fast EMA is failed! Error ",GetLastError());

      return(0);

     }

//--- get SlowSMA buffer

   if(IsStopped()) // checking for stop flag

      return(0);

   if(CopyBuffer(ExtStoHandle,1,0,to_copy,STOS)<=0)

     {

      Print("Getting slow SMA is failed! Error ",GetLastError());

      return(0);

     }

//--- get Fast EMA buffer

   if(IsStopped()) // checking for stop flag

      return(0);

   if(CopyBuffer(ExtMacdHandle,0,0,to_copy,MACD)<=0)

     {

      Print("Getting fast EMA is failed! Error ",GetLastError());

      return(0);

     }

//--- get SlowSMA buffer

   if(IsStopped()) // checking for stop flag

      return(0);

   if(CopyBuffer(ExtMacdHandle,1,0,to_copy,MACDS)<=0)

     {

      Print("Getting slow SMA is failed! Error ",GetLastError());

      return(0);

     }

//--- @>25@:0 8 @0AGQB :>;8G5AB20 ?@>AG8BK205<KE 10@>2

   ArraySetAsSeries(STO,true);

   ArraySetAsSeries(STOS,true);

   ArraySetAsSeries(MACD,true);

   ArraySetAsSeries(MACDS,true);

   ArraySetAsSeries(ExtLineBuffer,true);

   ArraySetAsSeries(high,true);

   ArraySetAsSeries(low,true);

//---- 2>AAB0=02;8205< 7=0G5=8O ?5@5<5==KE

   p=P_;

//---- >A=>2=>9 F8:; @0AG5B0 8=48:0B>@0

   for(i=limit; i>=0; i--)

     {

      //---- 70?><8=05< 7=0G5=8O ?5@5<5==KE ?5@54 ?@>3>=0<8 =0 B5:CI5< 10@5

      if(rates_total!=prev_calculated && i==0)

         P_=p;

      SellBuffer[i]=0.0;

      BuyBuffer[i]=0.0;

      StochDn=STO[i]<STOS[i];

      MacdDn=MACD[i]<MACDS[i];

      CoralDn=((Inpyna && BufferC[i]<ExtLineBuffer[i]) || (Inpdoy && ExtLineBuffer[i]<BufferC[i]) || (Inptrei && BufferC[i]<BufferC[i+1]));

      StochUp=STO[i]>STOS[i];

      MacdUp=MACD[i]>MACDS[i];

      CoralUp=((Inpyna && BufferC[i]>ExtLineBuffer[i]) || (Inpdoy && ExtLineBuffer[i]>BufferC[i]) || (Inptrei && BufferC[i]>BufferC[i+1]));

      if((InpStochastic && StochDn) || (InpMACD && MacdDn) || (InpCoral && CoralDn))

         p = 1;

      if((InpStochastic && StochUp) || (InpMACD && MacdUp) || (InpCoral && CoralUp))

         p = 2;

      if(((InpStochastic && StochDn) || (InpMACD && MacdDn) || (InpCoral && CoralDn)) && (p==1 || p==0))

        {

         if(OldTrend>0)

            SellBuffer[i]=high[i];;

         if(i!=0)

            OldTrend=-1;

        }

      if(((InpStochastic && StochUp) || (InpMACD && MacdUp) || (InpCoral && CoralUp)) && (p==2 || p==0))

        {

         if(OldTrend<0)

            BuyBuffer[i]=low[i];

         if(i!=0)

            OldTrend=+1;

        }

     }

//--- OnCalculate done. Return new prev_calculated.

   return(rates_total);

  }

//+------------------------------------------------------------------+

Comments

Markdown supported. Formatting help

Markdown Formatting Guide

Element Markdown Syntax
Heading # H1
## H2
### H3
Bold **bold text**
Italic *italicized text*
Link [title](https://www.example.com)
Image ![alt text](image.jpg)
Code `code`
Code Block ```
code block
```
Quote > blockquote
Unordered List - Item 1
- Item 2
Ordered List 1. First item
2. Second item
Horizontal Rule ---