Moving average MTF

Author: Copyright 2020, Forex RM
Indicators Used
Moving average indicator
0 Views
0 Downloads
0 Favorites
Moving average MTF
ÿþ//+------------------------------------------------------------------+

//|                                           Moving average MTF.mq4 |

//|                                         Copyright 2020, Forex RM |

//|                          https://www.mql5.com/ru/users/omutmoren |

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

#property copyright "Copyright 2020, Forex RM"

#property link      "https://www.mql5.com/ru/users/omutmoren"

#property version   "1.1"

#property strict

#include  "frm_enum.mqh"



#property indicator_chart_window

#property indicator_buffers 3

#property indicator_color1 clrRed

#property indicator_color2 clrGreen

#property indicator_color3 clrBlue

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

//| Parameters                                                       |

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

input string               TextMA1        =  "- - - - - - - - -"; // Moving average #1

input int                  PeriodMA1      =  50;                  // Period

input ENUM_MA_METHOD       TypeMA1        =  MODE_SMA;            // Mode

input ENUM_APPLIED_PRICE   PriceMA1       =  PRICE_CLOSE;         // Price

input string               TextMA2        =  "- - - - - - - - -"; // Moving average #2

input bool                 ShowMA2        =  true;                // Show MA?

input int                  PeriodMA2      =  100;                 // Period

input ENUM_MA_METHOD       TypeMA2        =  MODE_SMA;            // Mode

input ENUM_APPLIED_PRICE   PriceMA2       =  PRICE_CLOSE;         // Price

input string               TextMA3        =  "- - - - - - - - -"; // Moving average #3

input bool                 ShowMA3        =  true;                // Show MA?

input int                  PeriodMA3      =  200;                 // Period

input ENUM_MA_METHOD       TypeMA3        =  MODE_SMA;            // Mode

input ENUM_APPLIED_PRICE   PriceMA3       =  PRICE_CLOSE;         // Price

input string               TextMA4        =  "- - - - - - - - -"; // Other parameters

input int                  Indent         =  5;                   // Indent

input int                  CountBar       =  15;                  // Count bar for show MA

input ENUM_KEY             KeyTFup        =  VK_Q;                // Key for TF up

input ENUM_KEY             KeyTFdn        =  VK_W;                // Key for TF dn

input ENUM_KEY             KeySwitch      =  VK_A;                // Key for switch removes/displays the label



//---- indicator buffers

double ExtBufferMA1[];

double ExtBufferMA2[];

double ExtBufferMA3[];



//---- other

ENUM_TIMEFRAMES curTF = ENUM_TIMEFRAMES(_Period);

bool   show      = false;



string label     = "frm_Moving average MTF",

       labelshow = "frm_Moving average MTF show",

       labelma1  = "frm_label_ma1",

       labelma2  = "frm_label_ma2",

       labelma3  = "frm_label_ma3";



int    text_size = 8;                   // Font size

string text_font = "Calibry";           // Font



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

//| Custom indicator initialization function                         |

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

int OnInit()

  {

//--- timeframe

//curTF = TimeFrame;

//--- indicator buffers mapping

   int bar = CountBar;

   if(bar < 2)

      bar = 2;

   if(bar > Bars)

      bar = Bars;



//----

   SetIndexDrawBegin(0,Bars-bar);

   SetIndexDrawBegin(1,Bars-bar);

   SetIndexDrawBegin(2,Bars-bar);



//----

   SetIndexBuffer(0,ExtBufferMA1);

   SetIndexBuffer(1,ExtBufferMA2);

   SetIndexBuffer(2,ExtBufferMA3);



//----

   SetIndexEmptyValue(0,0.0);

   SetIndexEmptyValue(1,0.0);

   SetIndexEmptyValue(2,0.0);



//---- index labels

   SetIndexLabel(0,"MA" + IntegerToString(PeriodMA1) + "");

   SetIndexLabel(1,"MA" + IntegerToString(PeriodMA2) + "");

   SetIndexLabel(2,"MA" + IntegerToString(PeriodMA3) + "");



//----

   if(ObjectFind(label) == -1)

     {

      SetText(label, TFtoString(), clrNONE, TimeCurrent(), 0, text_size);

      SetText(labelshow,  "false", clrNONE, TimeCurrent(), 0, text_size);

     }

   else

     {

      curTF = StringtoTF(ObjectGetString(0, label, OBJPROP_TEXT, 0));

      show  = ((ObjectGetString(0, labelshow, OBJPROP_TEXT, 0) == "true") ? true : false);

     }

   return(INIT_SUCCEEDED);

  }

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

//| Custom indicator deinitialization function                       |

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

void OnDeinit(const int reason)

  {

   Print(__FUNCTION__,"_UninitReason = ",getUninitReasonText(_UninitReason));

   if(reason == REASON_REMOVE)

     {

      ObjectDelete(label);

      ObjectDelete(labelshow);

      ObjectDelete(labelma1);

      ObjectDelete(labelma2);

      ObjectDelete(labelma3);

     }

   if(reason == REASON_PARAMETERS || reason == REASON_CHARTCHANGE)

     {

      ObjectDelete(labelma1);

      ObjectDelete(labelma2);

      ObjectDelete(labelma3);

     }

  }

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

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

  {

//---

   Calculated(0);

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

   return(rates_total);

  }

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

//|                                                                  |

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

void Calculated(int a)

  {

   int limit;

   int counted_bars = 0;

   if(a == 0)

      counted_bars = IndicatorCounted();

   else

      counted_bars = CountBar;

//---- check for possible errors

   if(counted_bars < 0)

      return;

//---- last counted bar will be recounted

   if(counted_bars > 0)

      counted_bars--;

   limit = Bars - counted_bars;

//---- main loop

   for(int i = 0; i < limit; i++)

     {

      //---- ma_shift set to 0 because SetIndexShift called abowe

      int bar_shift   = iBarShift(NULL, curTF, Time[i]);

      //if (ShowMA1)

      ExtBufferMA1[i] = iMA(NULL, curTF, PeriodMA1, 0, TypeMA1, PriceMA1, bar_shift);

      if(ShowMA2)

         ExtBufferMA2[i] = iMA(NULL, curTF, PeriodMA2, 0, TypeMA2, PriceMA2, bar_shift);

      if(ShowMA3)

         ExtBufferMA3[i] = iMA(NULL, curTF, PeriodMA3, 0, TypeMA3, PriceMA3, bar_shift);

     }



   ExtBufferMA1[CountBar] = 0.0;

   ExtBufferMA2[CountBar] = 0.0;

   ExtBufferMA3[CountBar] = 0.0;



   if(show)

     {

      SetText(labelma1, DoubleToString(ExtBufferMA1[0], (int)SymbolInfoInteger(Symbol(), SYMBOL_DIGITS)) + " (" + TFtoString() + ")",

              indicator_color1, Time[0], ExtBufferMA1[0], text_size);

      if(ShowMA2)

         SetText(labelma2, DoubleToString(ExtBufferMA2[0], (int)SymbolInfoInteger(Symbol(), SYMBOL_DIGITS)) + " (" + TFtoString() + ")",

                 indicator_color2, Time[0], ExtBufferMA2[0], text_size);

      if(ShowMA3)

         SetText(labelma3, DoubleToString(ExtBufferMA3[0], (int)SymbolInfoInteger(Symbol(), SYMBOL_DIGITS)) + " (" + TFtoString() + ")",

                 indicator_color3, Time[0], ExtBufferMA3[0], text_size);

     }

   else

     {

      ObjectDelete(labelma1);

      ObjectDelete(labelma2);

      ObjectDelete(labelma3);

     }

  }

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

//| ChartEvent function                                              |

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

void OnChartEvent(const int id,

                  const long &lparam,

                  const double &dparam,

                  const string &sparam)

  {

//--- pressing a button on the keyboard

   if(id == CHARTEVENT_KEYDOWN)

     {

      // The KeyTFup increases the timeframe

      if(lparam == KeyTFup)

        {

         upTF();

         ObjectSetString(0,label,OBJPROP_TEXT,TFtoString());

         Calculated(1);

         ChartRedraw();

        }



      // The KeyTFup decreases the timeframe

      if(lparam == KeyTFdn)

        {

         dnTF();

         ObjectSetString(0,label,OBJPROP_TEXT,TFtoString());

         Calculated(1);

         ChartRedraw();

        }



      // The KeySwitch removes / displays the price

      if(lparam == KeySwitch)

        {

         if(show)

           {

            show = false;

            ObjectSetString(0,labelshow,OBJPROP_TEXT,"false");

           }

         else

           {

            show = true;

            ObjectSetString(0,labelshow,OBJPROP_TEXT,"true");

           }

         Calculated(1);

        }

     }

  }

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

//| TF up                                                            |

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

void upTF()

  {

   switch(curTF)

     {

      case PERIOD_M1  :

         curTF = PERIOD_M5;

         break;

      case PERIOD_M5  :

         curTF = PERIOD_M15;

         break;

      case PERIOD_M15 :

         curTF = PERIOD_M30;

         break;

      case PERIOD_M30 :

         curTF = PERIOD_H1;

         break;

      case PERIOD_H1  :

         curTF = PERIOD_H4;

         break;

      case PERIOD_H4  :

         curTF = PERIOD_D1;

         break;

      case PERIOD_D1  :

         curTF = PERIOD_W1;

         break;

      case PERIOD_W1  :

         curTF = PERIOD_MN1;

         break;

      case PERIOD_MN1 :

         curTF = PERIOD_MN1;

         break;

     }

  }

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

//| TF down                                                          |

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

void dnTF()

  {

   switch(curTF)

     {

      case PERIOD_M1  :

         curTF = PERIOD_M1;

         break;

      case PERIOD_M5  :

         curTF = PERIOD_M1;

         break;

      case PERIOD_M15 :

         curTF = PERIOD_M5;

         break;

      case PERIOD_M30 :

         curTF = PERIOD_M15;

         break;

      case PERIOD_H1  :

         curTF = PERIOD_M30;

         break;

      case PERIOD_H4  :

         curTF = PERIOD_H1;

         break;

      case PERIOD_D1  :

         curTF = PERIOD_H4;

         break;

      case PERIOD_W1  :

         curTF = PERIOD_D1;

         break;

      case PERIOD_MN1 :

         curTF = PERIOD_W1;

         break;

     }

  }

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

//| Set text                                                         |

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

bool SetText(string nm, string tx, color cl, datetime time, double price, int fs)

  {

   time += Indent * Period() * 60;

   ResetLastError();

//--- Create object "Text"

   if(ObjectFind(nm) < 0)

      ObjectCreate(0, nm, OBJ_TEXT, 0, time, price);



   ObjectMove(0, nm, 0, time, price);

   ObjectSetString(0, nm, OBJPROP_TEXT,      tx);

   ObjectSetString(0, nm, OBJPROP_FONT,      text_font);

   ObjectSetInteger(0, nm, OBJPROP_FONTSIZE,  fs);

   ObjectSetDouble(0, nm, OBJPROP_ANGLE,     0.0);

   ObjectSetInteger(0, nm, OBJPROP_ANCHOR,    ANCHOR_LEFT_LOWER);

   ObjectSetInteger(0, nm, OBJPROP_COLOR,     cl);

   ObjectSetInteger(0, nm, OBJPROP_BACK,      false);

   ObjectSetInteger(0, nm, OBJPROP_SELECTABLE,false);

   ObjectSetInteger(0, nm, OBJPROP_SELECTED,  false);

   ObjectSetInteger(0, nm, OBJPROP_HIDDEN,    true);



   return (true);

  }

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

//|                                                                  |

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

string TFtoString()

  {

   string res = "";

   if(curTF == PERIOD_CURRENT)

      curTF = (ENUM_TIMEFRAMES)_Period;

   switch(curTF)

     {

      case PERIOD_M1  :

         res = "M1";

         break;

      case PERIOD_M5  :

         res = "M5";

         break;

      case PERIOD_M15 :

         res = "M15";

         break;

      case PERIOD_M30 :

         res = "M30";

         break;

      case PERIOD_H1  :

         res = "H1";

         break;

      case PERIOD_H4  :

         res = "H4";

         break;

      case PERIOD_D1  :

         res = "D1";

         break;

      case PERIOD_W1  :

         res = "W1";

         break;

      case PERIOD_MN1 :

         res = "MN1";

         break;

     }

   return res;

  }

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

//|                                                                  |

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

ENUM_TIMEFRAMES StringtoTF(string txt)

  {

   ENUM_TIMEFRAMES res = (ENUM_TIMEFRAMES)_Period;

   if(txt == "M1")

      res = PERIOD_M1;

   if(txt == "M5")

      res = PERIOD_M5;

   if(txt == "M15")

      res = PERIOD_M15;

   if(txt == "M30")

      res = PERIOD_M30;

   if(txt == "H1")

      res = PERIOD_H1;

   if(txt == "H4")

      res = PERIOD_H4;

   if(txt == "D1")

      res = PERIOD_D1;

   if(txt == "W1")

      res = PERIOD_W1;

   if(txt == "MN1")

      res = PERIOD_MN1;



   return res;

  }

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