Author: Copyright 2020, MetaQuotes Software Corp.
Indicators Used
Moving average indicatorStochastic oscillator
2 Views
0 Downloads
0 Favorites
BrainMacd
ÿþ//+------------------------------------------------------------------+

//|                                                    BrainMacd.mq5 |

//|                        Copyright 2020, MetaQuotes Software Corp. |

//|                                             https://www.mql5.com |

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

#property copyright "Copyright 2020, MetaQuotes Software Corp."

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

#property version   "1.00"

//--- indicator settings

#property indicator_chart_window

#property indicator_buffers 16

#property indicator_plots   2

#property indicator_type1   DRAW_ARROW

#property indicator_color1  Wheat

#property indicator_width1  2

#property indicator_label1  "BrainMacd Sell"

#property indicator_type2   DRAW_ARROW

#property indicator_color2  LightSeaGreen

#property indicator_width2  2

#property indicator_label2  "BrainMacd Buy"

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

//| E>4=K5 ?0@0<5B@K 8=48:0B>@0                 |

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

double             InpCoeff=1;                  // Coefficient

ENUM_APPLIED_PRICE InpCoralPrice=PRICE_CLOSE;   // Coral Applied price

input int                InpMaPeriod=14;              // Moving period

input ENUM_MA_METHOD     MA_Method=MODE_SMA;          // Moving average method

input ENUM_APPLIED_PRICE InpMaPrice=PRICE_CLOSE;      // Moving Applied price

input int                InpFastEMA=10;               // Fast EMA period

input int                InpSlowEMA=15;               // Slow EMA period

input int                InpSignalSMA=13;             // Signal SMA period

input ENUM_APPLIED_PRICE InpAppliedPrice=PRICE_CLOSE; // Applied price

input int                InpKPeriod=50;               // K period

input int                InpDPeriod=20;               // D period

input int                InpSlowing=10;               // Slowing

//---- >1JO2;5=85 48=0<8G5A:8E <0AA82>2, :>B>@K5 1C4CB 2

//---- 40;L=59H5< 8A?>;L7>20=K 2 :0G5AB25 8=48:0B>@=KE 1CD5@>2

double SellBuffer[];

double BuyBuffer[];

double ExtFastMaBuffer[];

double ExtSlowMaBuffer[];

double ExtHighesBuffer[];

double ExtLowesBuffer[];

double ExtTRBuffer[];

//--- indicator buffers

double BufferC[];

double BufferColors[];

double BufferB1[];

double BufferB2[];

double BufferB3[];

double BufferB4[];

double BufferB5[];

double BufferB6[];

double BufferMA[];

//--- global variables

int    ExtFastMaHandle;

int    ExtSlowMaHandle;

int    ExtFasStoHandle;

int    ExtSloStoHandle;

int    ExtPerMaHandle;

int    ExtSMAMaHandle;

//---

double coeff1,coeff2;

int p,P_,StartBars,OldTrend;

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

//| Custom indicator initialization function                         |

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

int OnInit()

  {

//---- 8=8F80;870F8O 3;>10;L=KE ?5@5<5==KE

   StartBars=InpSignalSMA-1;

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

   coeff2=1.0-coeff1;

//---- ?@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 1

//--- indicator buffers mapping

   SetIndexBuffer(0,SellBuffer,INDICATOR_DATA);

   SetIndexBuffer(1,BuyBuffer,INDICATOR_DATA);

   SetIndexBuffer(2,BufferC,INDICATOR_DATA);

   SetIndexBuffer(3,BufferColors,INDICATOR_COLOR_INDEX);

   SetIndexBuffer(4,ExtFastMaBuffer,INDICATOR_CALCULATIONS);

   SetIndexBuffer(5,ExtSlowMaBuffer,INDICATOR_CALCULATIONS);

   SetIndexBuffer(6,ExtHighesBuffer,INDICATOR_CALCULATIONS);

   SetIndexBuffer(7,ExtLowesBuffer,INDICATOR_CALCULATIONS);

   SetIndexBuffer(8,ExtTRBuffer,INDICATOR_CALCULATIONS);

   SetIndexBuffer(9,BufferB1,INDICATOR_CALCULATIONS);

   SetIndexBuffer(10,BufferB2,INDICATOR_CALCULATIONS);

   SetIndexBuffer(11,BufferB3,INDICATOR_CALCULATIONS);

   SetIndexBuffer(12,BufferB4,INDICATOR_CALCULATIONS);

   SetIndexBuffer(13,BufferB5,INDICATOR_CALCULATIONS);

   SetIndexBuffer(14,BufferB6,INDICATOR_CALCULATIONS);

   SetIndexBuffer(15,BufferMA,INDICATOR_CALCULATIONS);

//--- setting buffer arrays as timeseries

   ArraySetAsSeries(BufferC,true);

   ArraySetAsSeries(BufferColors,true);

   ArraySetAsSeries(BufferB1,true);

   ArraySetAsSeries(BufferB2,true);

   ArraySetAsSeries(BufferB3,true);

   ArraySetAsSeries(BufferB4,true);

   ArraySetAsSeries(BufferB5,true);

   ArraySetAsSeries(BufferB6,true);

   ArraySetAsSeries(BufferMA,true);

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

   PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,StartBars);

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

   PlotIndexSetString(0,PLOT_LABEL,"BrainMacd 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

   PlotIndexSetInteger(1,PLOT_DRAW_BEGIN,StartBars);

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

   PlotIndexSetString(1,PLOT_LABEL,"BrainMacd 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);

//--- get MA handles

   ExtFastMaHandle=iMA(NULL,PERIOD_CURRENT,InpFastEMA,0,MODE_EMA,InpAppliedPrice);

   ExtSlowMaHandle=iMA(NULL,PERIOD_CURRENT,InpSlowEMA,0,MODE_EMA,InpAppliedPrice);

   ExtFasStoHandle=iStochastic(NULL,PERIOD_CURRENT,InpKPeriod,InpDPeriod,InpSlowing,MODE_SMA,STO_LOWHIGH);

   ExtSloStoHandle=iStochastic(NULL,PERIOD_CURRENT,InpKPeriod,InpSlowing,InpDPeriod,MODE_SMA,STO_LOWHIGH);

   ExtPerMaHandle=iMA(NULL,PERIOD_CURRENT,InpMaPeriod,0,MA_Method,InpMaPrice);

   ExtSMAMaHandle=iMA(NULL,PERIOD_CURRENT,1,0,MODE_SMA,InpCoralPrice);

//---- CAB0=>2:0 D>@<0B0 B>G=>AB8 >B>1@065=8O 8=48:0B>@0

   IndicatorSetInteger(INDICATOR_DIGITS,_Digits);

//---- 8<O 4;O >:>= 40==KE 8 ;M910 4;O AC1J>:>=

   string short_name="BrainMacd";

   IndicatorSetString(INDICATOR_SHORTNAME,short_name);

//---

   return(INIT_SUCCEEDED);

  }

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

//| Custom indicator iteration function                              |

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

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[])

  {

//---- ?@>25@:0 :>;8G5AB20 10@>2 =0 4>AB0B>G=>ABL 4;O @0AG5B0

   if(BarsCalculated(ExtFastMaHandle)<rates_total

      || BarsCalculated(ExtSlowMaHandle)<rates_total

      || BarsCalculated(ExtFasStoHandle)<rates_total

      || BarsCalculated(ExtSloStoHandle)<rates_total

      || BarsCalculated(ExtPerMaHandle)<rates_total

      || BarsCalculated(ExtSMAMaHandle)<rates_total

      || rates_total<StartBars)

      return(0);

   int to_copy,limit,bar;

//--- @>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);

     }

//--- setting buffer arrays as timeseries

   ArraySetAsSeries(BufferC,true);

   ArraySetAsSeries(BufferColors,true);

   ArraySetAsSeries(BufferB1,true);

   ArraySetAsSeries(BufferB2,true);

   ArraySetAsSeries(BufferB3,true);

   ArraySetAsSeries(BufferB4,true);

   ArraySetAsSeries(BufferB5,true);

   ArraySetAsSeries(BufferB6,true);

   ArraySetAsSeries(BufferMA,true);

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

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

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

   if(copied!=count)

      return 0;

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

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

     {

      if(bar==rates_total-2)

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

      else

        {

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

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

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

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

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

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

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

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

        }

     }

//---- >1JO2;5=8O ;>:0;L=KE ?5@5<5==KE

   bool ExtUp,ExtDn,StoUp,StoDn,ColUp,ColDn;

//---- @0AG5BK =5>1E>48<>3> :>;8G5AB20 :>?8@C5<KE 40==KE 8

//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-StartBars; // 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(ExtFastMaHandle,0,0,to_copy,ExtFastMaBuffer)<=0)

     {

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

      return(0);

     }

//--- get SlowSMA buffer

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

      return(0);

   if(CopyBuffer(ExtSlowMaHandle,0,0,to_copy,ExtSlowMaBuffer)<=0)

     {

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

      return(0);

     }

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

      return(0);

   if(CopyBuffer(ExtFasStoHandle,0,0,to_copy,ExtHighesBuffer)<=0)

     {

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

      return(0);

     }

//--- get SlowSMA buffer

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

      return(0);

   if(CopyBuffer(ExtSloStoHandle,0,0,to_copy,ExtLowesBuffer)<=0)

     {

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

      return(0);

     }

//--- get SlowSMA buffer

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

      return(0);

   if(CopyBuffer(ExtPerMaHandle,0,0,to_copy,ExtTRBuffer)<=0)

     {

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

      return(0);

     }

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

   ArraySetAsSeries(ExtFastMaBuffer,true);

   ArraySetAsSeries(ExtSlowMaBuffer,true);

   ArraySetAsSeries(ExtHighesBuffer,true);

   ArraySetAsSeries(ExtLowesBuffer,true);

   ArraySetAsSeries(ExtTRBuffer,true);

   ArraySetAsSeries(BufferC,true);

   ArraySetAsSeries(open,true);

   ArraySetAsSeries(high,true);

   ArraySetAsSeries(low,true);

   ArraySetAsSeries(close,true);

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

   p=P_;

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

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

     {

      //---- 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 && bar==0)

         P_=p;

      SellBuffer[bar]=0.0;

      BuyBuffer[bar]=0.0;

      ExtDn=ExtFastMaBuffer[bar]<ExtSlowMaBuffer[bar];

      ExtUp=ExtFastMaBuffer[bar]>ExtSlowMaBuffer[bar];

      StoDn=ExtLowesBuffer[bar]<ExtHighesBuffer[bar];

      StoUp=ExtLowesBuffer[bar]>ExtHighesBuffer[bar];

      ColDn=BufferC[bar]<ExtTRBuffer[bar];

      ColUp=BufferC[bar]>ExtTRBuffer[bar];

      if(ExtDn && StoDn && ColDn)

         p = 1;

      if(ExtUp && StoUp && ColUp)

         p = 2;

      if(ExtDn && StoDn && ColDn && (p==1 || p==0))

        {

         if(OldTrend>0)

            SellBuffer[bar]=high[bar];;

         if(bar!=0)

            OldTrend=-1;

        }

      if(ExtUp && StoUp && ColUp && (p==2 || p==0))

        {

         if(OldTrend<0)

            BuyBuffer[bar]=low[bar];

         if(bar!=0)

            OldTrend=+1;

        }

     }

//----

   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 ---