Semaphore MAMA

Author: Copyright 2020, MetaQuotes Software Corp.
Indicators Used
Moving average indicator
2 Views
0 Downloads
0 Favorites
Semaphore MAMA
ÿþ//+------------------------------------------------------------------+

//|                                               Semaphore MAMA.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"

#property indicator_chart_window

#property indicator_buffers 4

#property indicator_plots   4

#property indicator_type1   DRAW_ARROW

#property indicator_color1  Wheat

#property indicator_width1  2

#property indicator_label1  "Semaphore MAMA Sell"

#property indicator_type2   DRAW_ARROW

#property indicator_color2  LightSeaGreen

#property indicator_width2  2

#property indicator_label2  "Semaphore MAMA Buy"

#property indicator_width3  2

#property indicator_color3  Red

#property indicator_type3   DRAW_LINE

#property indicator_width4  2

#property indicator_color4  Blue

#property indicator_type4   DRAW_LINE

//--- input parameters

input int                     PeriodMaSlow=21;          //Period slow Ma

input int                     PeriodMaFast=13;          //Pertiod fast Ma

input ENUM_APPLIED_PRICE      PriceMa=PRICE_CLOSE;      //Applied price

input ENUM_MA_METHOD          MethodMa=MODE_SMA;        //Method Ma

//---

double SellBuffer[];

double BuyBuffer[];

double ExtFastMaBuffer[];

double ExtSlowMaBuffer[];

//--- global variables

int    ExtFastMaHandle;

int    ExtSlowMaHandle;

//---

int p,P_,StartBars,OldTrend;

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

//| Custom indicator initialization function                         |

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

int OnInit()

  {

//--- indicator buffers mapping

   StartBars=PeriodMaSlow-1;

//--- indicator buffers mapping

   ArraySetAsSeries(ExtFastMaBuffer,true);

   ArraySetAsSeries(ExtSlowMaBuffer,true);

   SetIndexBuffer(0,SellBuffer,INDICATOR_DATA);

   PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,StartBars);

   PlotIndexSetString(0,PLOT_LABEL,"Semaphore MAMA Sell");

   PlotIndexSetInteger(0,PLOT_ARROW,72);

   ArraySetAsSeries(SellBuffer,true);

   SetIndexBuffer(1,BuyBuffer,INDICATOR_DATA);

   PlotIndexSetInteger(1,PLOT_DRAW_BEGIN,StartBars);

   PlotIndexSetString(1,PLOT_LABEL,"Semaphore MAMA Buy");

   PlotIndexSetInteger(1,PLOT_ARROW,71);

   ArraySetAsSeries(BuyBuffer,true);

//----

   SetIndexBuffer(2,ExtSlowMaBuffer,INDICATOR_CALCULATIONS);

   SetIndexBuffer(3,ExtFastMaBuffer,INDICATOR_CALCULATIONS);

   ArraySetAsSeries(ExtSlowMaBuffer,true);

   ArraySetAsSeries(ExtFastMaBuffer,true);

//----

   ExtFastMaHandle=iMA(NULL,PERIOD_CURRENT,PeriodMaFast,0,MethodMa,PriceMa);

   ExtSlowMaHandle=iMA(NULL,PERIOD_CURRENT,PeriodMaSlow,0,MethodMa,PriceMa);

//----

   IndicatorSetInteger(INDICATOR_DIGITS,_Digits);

//----

   string short_name="Semaphore MAMA";

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

  {

//---

   if(BarsCalculated(ExtFastMaHandle)<rates_total

      || BarsCalculated(ExtSlowMaHandle)<rates_total

      || rates_total<StartBars)

      return(0);

   int to_copy,limit,i;

   bool ExtUp,ExtDn;

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

     }

   ArraySetAsSeries(ExtFastMaBuffer,true);

   ArraySetAsSeries(ExtSlowMaBuffer,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(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;

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

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

      if(ExtDn)

         p = 1;

      if(ExtUp)

         p = 2;

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

        {

         if(OldTrend>0)

            SellBuffer[i]=high[i];;

         if(i!=0)

            OldTrend=-1;

        }

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

        {

         if(OldTrend<0)

            BuyBuffer[i]=low[i];

         if(i!=0)

            OldTrend=+1;

        }

     }

//--- return value of prev_calculated for next call

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