Three information charts MA

Author: Copyright © 2020, Vladimir Karputov
0 Views
0 Downloads
0 Favorites
Three information charts MA
ÿþ//+------------------------------------------------------------------+

//|                                  Three information charts MA.mq5 |

//|                              Copyright © 2020, Vladimir Karputov |

//|                     https://www.mql5.com/ru/market/product/43516 |

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

#property copyright "Copyright © 2020, Vladimir Karputov"

#property link      "https://www.mql5.com/ru/market/product/43516"

#property version   "1.002"

#property indicator_chart_window

#property indicator_buffers 0

#property indicator_plots   0

//--- input parameters

input int                  InpMAPeriod=13;         // Period

input int                  InpMAShift=0;           // Shift

input ENUM_MA_METHOD       InpMAMethod=MODE_SMMA;  // Method

input color                InpColor=clrBlue;       // Color

input int                  InpWidth=1;             // Width

input group             "MA Fast"

input int                  Inp_MA_Fast_ma_period      = 5;              // MA Fast: averaging period

input int                  Inp_MA_Fast_ma_shift       = 0;              // MA Fast: horizontal shift

input ENUM_MA_METHOD       Inp_MA_Fast_ma_method      = MODE_EMA;       // MA Fast: smoothing type

input color                Inp_MA_Fast_Color          = clrBlue;        // MA Fast Color

input int                  Inp_MA_Fast_Width          = 5;              // MA Fast Width

input group             "MA Slow"

input int                  Inp_MA_Slow_ma_period      = 25;             // MA Slow: averaging period

input int                  Inp_MA_Slow_ma_shift       = 0;              // MA Slow: horizontal shift

input ENUM_MA_METHOD       Inp_MA_Slow_ma_method      = MODE_EMA;       // MA Slow: smoothing type

input color                Inp_MA_Slow_Color          = clrRed;         // MA Slow Color

input int                  Inp_MA_Slow_Width          = 5;              // MA Slow Width

//---

int         array_handles[];                             // array for storing the handles of the iCustom indicators

string      array_obj_charts_name[];

string      m_prefix="Three information charts MA ";

int         m_x_size=300;

int         m_y_size=146;

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

//| Indicator deinitialization function                                 |

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

void OnDeinit(const int reason)

  {

//---

   ObjectsDeleteAll(ChartID(),m_prefix,0,OBJ_CHART);

  }

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

//| Custom indicator initialization function                         |

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

int OnInit()

  {

   ENUM_TIMEFRAMES array_timeframes[3]= {PERIOD_M5,PERIOD_H1,PERIOD_D1};

   int total_array_timeframes=ArraySize(array_timeframes);

   ArrayResize(array_obj_charts_name,total_array_timeframes);

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

     {

      //--- create handle of the indicator iCustom

      int handle_iCustom_Fast=iCustom(Symbol(),array_timeframes[i],"Custom Moving Average Inputs",

                                      Inp_MA_Fast_ma_period,

                                      Inp_MA_Fast_ma_shift,

                                      Inp_MA_Fast_ma_method,

                                      Inp_MA_Fast_Color,

                                      Inp_MA_Fast_Width);

      //--- if the handle is not created

      if(handle_iCustom_Fast==INVALID_HANDLE)

        {

         //--- tell about the failure and output the error code

         PrintFormat("Failed to create handle of the iCustom indicator ('Fast') for the symbol %s/%s, error code %d",

                     Symbol(),

                     EnumToString(array_timeframes[i]),

                     GetLastError());

         //--- the indicator is stopped early

         return(INIT_FAILED);

        }

      //--- create handle of the indicator iCustom

      int handle_iCustom_Slow=iCustom(Symbol(),array_timeframes[i],"Custom Moving Average Inputs",

                                      Inp_MA_Slow_ma_period,

                                      Inp_MA_Slow_ma_shift,

                                      Inp_MA_Slow_ma_method,

                                      Inp_MA_Slow_Color,

                                      Inp_MA_Slow_Width);

      //--- if the handle is not created

      if(handle_iCustom_Slow==INVALID_HANDLE)

        {

         //--- tell about the failure and output the error code

         PrintFormat("Failed to create handle of the iCustom indicator ('Slow') for the symbol %s/%s, error code %d",

                     Symbol(),

                     EnumToString(array_timeframes[i]),

                     GetLastError());

         //--- the indicator is stopped early

         return(INIT_FAILED);

        }

      //---

      int total_array_handles=ArraySize(array_handles);

      ArrayResize(array_handles,total_array_handles+2);

      array_handles[total_array_handles]=handle_iCustom_Fast;

      array_handles[total_array_handles+1]=handle_iCustom_Slow;

      //--- set Chart object coordinates and its size

      int x=0;

      int y=0;

      string m_chart_name=m_prefix+EnumToString(array_timeframes[i]);

      //--- create Chart object

      if(!ObjectChartCreate(ChartID(),m_chart_name,0,Symbol(),array_timeframes[i],x,y,m_x_size,m_y_size))

        {

         return(INIT_FAILED);

        }

      //---

      long chart_id=ObjectChartGetID(ChartID(),m_chart_name);

      if(chart_id>-1)

         array_obj_charts_name[i]=m_chart_name;

      else

         return(INIT_FAILED);

      color back_color=ChartBackColorGet(0);

      ChartShowPeriodSepapatorSet(false,chart_id); // disables displaying of vertical separators between adjacent periods

      ChartShowGridSet(false,chart_id);            // disables displaying of grid on chart

      ChartShowVolumesSet(false,chart_id);         // sets mode of displaying volumes on chart

      ChartBackColorSet(back_color,chart_id);      // chart background color

      ChartUpColorSet(back_color,chart_id);        // sets the color of up bar, shadow and border of a bullish candlestick's body

      ChartDownColorSet(back_color,chart_id);      // sets the color of down bar, shadow and border of a bearish candlestick's body

      ChartLineColorSet(back_color,chart_id);      // sets the color of chart line and Doji candlesticks

      ChartBullColorSet(back_color,chart_id);      // sets the color of bullish candlestick's body

      ChartBearColorSet(back_color,chart_id);      // sets the color of bearish candlestick's body*/

      //---

      ChartIndicatorAdd(chart_id,0,array_handles[total_array_handles]);

      ChartIndicatorAdd(chart_id,0,array_handles[total_array_handles+1]);

     }

//---

   ArrayPrint(array_handles);

   ArrayPrint(array_obj_charts_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[])

  {

//---

//--- Initialize the generator of random numbers

   MathSrand(GetTickCount());

   string m_chart_name=IntegerToString(rand(),5);

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

   return(rates_total);

  }

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

//| ChartEvent function                                              |

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

void OnChartEvent(const int id,

                  const long &lparam,

                  const double &dparam,

                  const string &sparam)

  {

//--- event of resizing the chart or modifying the chart properties using the properties dialog window

   if(id==CHARTEVENT_CHART_CHANGE)

     {

      long chart_id=ChartID();

      //--- chart window size

      long x_distance;

      long y_distance;

      //--- set window size

      if(!ChartGetInteger(0,CHART_WIDTH_IN_PIXELS,0,x_distance))

        {

         Print("Failed to get the chart width! Error code = ",GetLastError());

         return;

        }

      if(!ChartGetInteger(0,CHART_HEIGHT_IN_PIXELS,0,y_distance))

        {

         Print("Failed to get the chart height! Error code = ",GetLastError());

         return;

        }

      //--- set Chart object coordinates and its size

      int x=(int)x_distance-m_x_size-10;

      int y=15;

      //---

      int total=ArraySize(array_obj_charts_name);

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

        {

         ObjectChartMove(chart_id,array_obj_charts_name[i],x,y);

         y=y+m_y_size+5;

        }

     }

  }

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

//| Creating Chart object                                            |

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

bool ObjectChartCreate(const long              chart_ID=0,               // chart's ID

                       const string            name="Chart",             // object name

                       const int               sub_window=0,             // subwindow index

                       const string            symbol="EURUSD",          // symbol

                       const ENUM_TIMEFRAMES   period=PERIOD_H1,         // period

                       const int               x=0,                      // X coordinate

                       const int               y=0,                      // Y coordinate

                       const int               width=300,                // width

                       const int               height=200,               // height

                       const ENUM_BASE_CORNER  corner=CORNER_LEFT_UPPER, // anchoring corner

                       const color             clr=clrRed,               // border color when highlighted

                       const ENUM_LINE_STYLE   style=STYLE_SOLID,        // line style when highlighted

                       const int               point_width=1,            // move point size

                       const bool              back=false,               // in the background

                       const bool              selection=false,          // highlight to move

                       const bool              hidden=true,              // hidden in the object list

                       const long              z_order=0)                // priority for mouse click

  {

//--- reset the error value

   ResetLastError();

//--- create Chart object

   if(!ObjectCreate(chart_ID,name,OBJ_CHART,sub_window,0,0))

     {

      Print(__FUNCTION__,

            ": failed to create \"Chart\" object! Error code = ",GetLastError());

      return(false);

     }

//--- set object coordinates

   ObjectSetInteger(chart_ID,name,OBJPROP_XDISTANCE,x);

   ObjectSetInteger(chart_ID,name,OBJPROP_YDISTANCE,y);

//--- set object size

   ObjectSetInteger(chart_ID,name,OBJPROP_XSIZE,width);

   ObjectSetInteger(chart_ID,name,OBJPROP_YSIZE,height);

//--- set the chart's corner, relative to which point coordinates are defined

   ObjectSetInteger(chart_ID,name,OBJPROP_CORNER,corner);

//--- set the symbol

   ObjectSetString(chart_ID,name,OBJPROP_SYMBOL,symbol);

//--- set the period

   ObjectSetInteger(chart_ID,name,OBJPROP_PERIOD,period);

//--- set the border color when object highlighting mode is enabled

   ObjectSetInteger(chart_ID,name,OBJPROP_COLOR,clr);

//--- set the border line style when object highlighting mode is enabled

   ObjectSetInteger(chart_ID,name,OBJPROP_STYLE,style);

//--- set a size of the anchor point for moving an object

   ObjectSetInteger(chart_ID,name,OBJPROP_WIDTH,point_width);

//--- display in the foreground (false) or background (true)

   ObjectSetInteger(chart_ID,name,OBJPROP_BACK,back);

//--- enable (true) or disable (false) the mode of moving the label by mouse

   ObjectSetInteger(chart_ID,name,OBJPROP_SELECTABLE,selection);

   ObjectSetInteger(chart_ID,name,OBJPROP_SELECTED,selection);

//--- hide (true) or display (false) graphical object name in the object list

   ObjectSetInteger(chart_ID,name,OBJPROP_HIDDEN,hidden);

//--- set the priority for receiving the event of a mouse click in the chart

   ObjectSetInteger(chart_ID,name,OBJPROP_ZORDER,z_order);

//--- successful execution

   return(true);

  }

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

//| Sets the symbol and time frame of the Chart object               |

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

bool ObjectChartSetSymbolAndPeriod(const long            chart_ID=0,       // chart's ID (not Chart object's one)

                                   const string          name="Chart",     // object name

                                   const string          symbol="EURUSD",  // symbol

                                   const ENUM_TIMEFRAMES period=PERIOD_H1) // time frame

  {

//--- reset the error value

   ResetLastError();

//--- set Chart object's symbol and time frame

   if(!ObjectSetString(chart_ID,name,OBJPROP_SYMBOL,symbol))

     {

      Print(__FUNCTION__,

            ": failed to set a symbol for \"Chart\" object! Error code = ",GetLastError());

      return(false);

     }

   if(!ObjectSetInteger(chart_ID,name,OBJPROP_PERIOD,period))

     {

      Print(__FUNCTION__,

            ": failed to set a period for \"Chart\" object! Error code = ",GetLastError());

      return(false);

     }

//--- successful execution

   return(true);

  }

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

//| Move Chart object                                                |

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

bool ObjectChartMove(const long   chart_ID=0,   // chart's ID (not Chart object's one)

                     const string name="Chart", // object name

                     const int    x=0,          // X coordinate

                     const int    y=0)          // Y coordinate

  {

//--- reset the error value

   ResetLastError();

//--- move the object

   if(!ObjectSetInteger(chart_ID,name,OBJPROP_XDISTANCE,x))

     {

      Print(__FUNCTION__,

            ": failed to move X coordinate of \"Chart\" object! Error code = ",GetLastError());

      return(false);

     }

   if(!ObjectSetInteger(chart_ID,name,OBJPROP_YDISTANCE,y))

     {

      Print(__FUNCTION__,

            ": failed to move Y coordinate of \"Chart\" object! Error code = ",GetLastError());

      return(false);

     }

//--- successful execution

   return(true);

  }

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

//| Change Chart object size                                         |

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

bool ObjectChartChangeSize(const long   chart_ID=0,   // chart's ID (not Chart object's one)

                           const string name="Chart", // object name

                           const int    width=300,    // width

                           const int    height=200)   // height

  {

//--- reset the error value

   ResetLastError();

//--- change the object size

   if(!ObjectSetInteger(chart_ID,name,OBJPROP_XSIZE,width))

     {

      Print(__FUNCTION__,

            ": failed to change the width of \"Chart\" object! Error code = ",GetLastError());

      return(false);

     }

   if(!ObjectSetInteger(chart_ID,name,OBJPROP_YSIZE,height))

     {

      Print(__FUNCTION__,

            ": failed to change the height of \"Chart\" object! Error code = ",GetLastError());

      return(false);

     }

//--- successful execution

   return(true);

  }

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

//| Return Chart object's ID                                         |

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

long ObjectChartGetID(const long   chart_ID=0,   // chart's ID (not Chart object's one)

                      const string name="Chart") // object name

  {

//--- prepare the variable to get Chart object's ID

   long id=-1;

//--- reset the error value

   ResetLastError();

//--- get ID

   if(!ObjectGetInteger(chart_ID,name,OBJPROP_CHART_ID,0,id))

     {

      Print(__FUNCTION__,

            ": failed to get \"Chart\" object's ID! Error code = ",GetLastError());

     }

//--- return the result

   return(id);

  }

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

//| Delete Chart object                                              |

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

bool ObjectChartDelete(const long   chart_ID=0,   // chart's ID (not Chart object's one)

                       const string name="Chart") // object name

  {

//--- reset the error value

   ResetLastError();

//--- delete the button

   if(!ObjectDelete(chart_ID,name))

     {

      Print(__FUNCTION__,

            ": failed to delete \"Chart\" object! Error code = ",GetLastError());

      return(false);

     }

//--- successful execution

   return(true);

  }

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

//| Enables/disables displaying of vertical separators between adjacent periods |

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

bool ChartShowPeriodSepapatorSet(const bool value,const long chart_ID=0)

  {

//--- reset the error value

   ResetLastError();

//--- set property value

   if(!ChartSetInteger(chart_ID,CHART_SHOW_PERIOD_SEP,0,value))

     {

      //--- display the error message in Experts journal

      Print(__FUNCTION__+", Error Code = ",GetLastError());

      return(false);

     }

//--- successful execution

   return(true);

  }

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

//| Enables/disables displaying of grid on chart                     |

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

bool ChartShowGridSet(const bool value,const long chart_ID=0)

  {

//--- reset the error value

   ResetLastError();

//--- set the property value

   if(!ChartSetInteger(chart_ID,CHART_SHOW_GRID,0,value))

     {

      //--- display the error message in Experts journal

      Print(__FUNCTION__+", Error Code = ",GetLastError());

      return(false);

     }

//--- successful execution

   return(true);

  }

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

//| Sets mode of displaying volumes on chart                         |

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

bool ChartShowVolumesSet(const long value,const long chart_ID=0)

  {

//--- reset the error value

   ResetLastError();

//--- set property value

   if(!ChartSetInteger(chart_ID,CHART_SHOW_VOLUMES,value))

     {

      //--- display the error message in Experts journal

      Print(__FUNCTION__+", Error Code = ",GetLastError());

      return(false);

     }

//--- successful execution

   return(true);

  }

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

//| Gets the background color of chart                               |

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

color ChartBackColorGet(const long chart_ID=0)

  {

//--- prepare the variable to receive the color

   long result=clrNONE;

//--- reset the error value

   ResetLastError();

//--- receive chart background color

   if(!ChartGetInteger(chart_ID,CHART_COLOR_BACKGROUND,0,result))

     {

      //--- display the error message in Experts journal

      Print(__FUNCTION__+", Error Code = ",GetLastError());

     }

//--- return the value of the chart property

   return((color)result);

  }

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

//| Sets the background color of chart                               |

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

bool ChartBackColorSet(const color clr,const long chart_ID=0)

  {

//--- reset the error value

   ResetLastError();

//--- set the chart background color

   if(!ChartSetInteger(chart_ID,CHART_COLOR_BACKGROUND,clr))

     {

      //--- display the error message in Experts journal

      Print(__FUNCTION__+", Error Code = ",GetLastError());

      return(false);

     }

//--- successful execution

   return(true);

  }

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

//| Sets the color of axes, scale and OHLC line                      |

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

bool ChartForeColorSet(const color clr,const long chart_ID=0)

  {

//--- reset the error value

   ResetLastError();

//--- set the color of axes, scale and OHLC line

   if(!ChartSetInteger(chart_ID,CHART_COLOR_FOREGROUND,clr))

     {

      //--- display the error message in Experts journal

      Print(__FUNCTION__+", Error Code = ",GetLastError());

      return(false);

     }

//--- successful execution

   return(true);

  }

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

//| Sets the color of up bar, shadow and border of a bullish candlestick's body |

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

bool ChartUpColorSet(const color clr,const long chart_ID=0)

  {

//--- reset the error value

   ResetLastError();

//--- set the color of up bar, its shadow and border of body of a bullish candlestick

   if(!ChartSetInteger(chart_ID,CHART_COLOR_CHART_UP,clr))

     {

      //--- display the error message in Experts journal

      Print(__FUNCTION__+", Error Code = ",GetLastError());

      return(false);

     }

//--- successful execution

   return(true);

  }

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

//| Sets the color of down bar, shadow and border of a bearish candlestick's body |

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

bool ChartDownColorSet(const color clr,const long chart_ID=0)

  {

//--- reset the error value

   ResetLastError();

//--- set the color of down bar, its shadow and border of bearish candlestick's body

   if(!ChartSetInteger(chart_ID,CHART_COLOR_CHART_DOWN,clr))

     {

      //--- display the error message in Experts journal

      Print(__FUNCTION__+", Error Code = ",GetLastError());

      return(false);

     }

//--- successful execution

   return(true);

  }

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

//| Sets the color of chart line and Doji candlesticks               |

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

bool ChartLineColorSet(const color clr,const long chart_ID=0)

  {

//--- reset the error value

   ResetLastError();

//--- set color of the chart line and Doji candlesticks

   if(!ChartSetInteger(chart_ID,CHART_COLOR_CHART_LINE,clr))

     {

      //--- display the error message in Experts journal

      Print(__FUNCTION__+", Error Code = ",GetLastError());

      return(false);

     }

//--- successful execution

   return(true);

  }

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

//| Sets the color of bullish candlestick's body                     |

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

bool ChartBullColorSet(const color clr,const long chart_ID=0)

  {

//--- reset the error value

   ResetLastError();

//--- set the color of bullish candlestick's body

   if(!ChartSetInteger(chart_ID,CHART_COLOR_CANDLE_BULL,clr))

     {

      //--- display the error message in Experts journal

      Print(__FUNCTION__+", Error Code = ",GetLastError());

      return(false);

     }

//--- successful execution

   return(true);

  }

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

//| Sets the color of bearish candlestick's body                     |

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

bool ChartBearColorSet(const color clr,const long chart_ID=0)

  {

//--- reset the error value

   ResetLastError();

//--- set the color of bearish candlestick's body

   if(!ChartSetInteger(chart_ID,CHART_COLOR_CANDLE_BEAR,clr))

     {

      //--- display the error message in Experts journal

      Print(__FUNCTION__+", Error Code = ",GetLastError());

      return(false);

     }

//--- successful execution

   return(true);

  }

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

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