Intersection two iMA Text

Author: Copyright © 2020, Vladimir Karputov
Indicators Used
Moving average indicator
0 Views
0 Downloads
0 Favorites
Intersection two iMA Text
ÿþ//+------------------------------------------------------------------+

//|                                    Intersection two iMA Text.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.000"

#property indicator_chart_window

#property indicator_buffers 2

#property indicator_plots   0

//--- input parameters

input group           "Text"

input string            InpFont        = "Lucida Console";  // Font

input int               InpFontSize    = 10;                // Font size

input color             InpColorUp     = clrBlue;           // Color Intersection up

input color             InpColorDown   = clrRed;            // Color Intersection down

input double            InpAngle       = 90.0;              // Slope angle in degrees

input group           "MA Fast"

input int                  Inp_MA_Fast_ma_period      = 12;          // 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_SMA;    // MA Fast: smoothing type

input ENUM_APPLIED_PRICE   Inp_MA_Fast_applied_price  = PRICE_CLOSE; // MA Fast: type of price

input group           "MA Slow"

input int                  Inp_MA_Slow_ma_period      = 26;          // 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_SMA;    // MA Slow: smoothing type

input ENUM_APPLIED_PRICE   Inp_MA_Slow_applied_price  = PRICE_CLOSE; // MA Slow: type of price

//--- indicator buffers

double   iMAFastBuffer[];

double   iMASlowBuffer[];

//---

int      handle_iMA_Fast;                       // variable for storing the handle of the iMA indicator

int      handle_iMA_Slow;                       // variable for storing the handle of the iMA indicator



string   m_prefix="ITIT "; // prefix for object

double   m_adjusted_point;                      // point value adjusted for 3 or 5 points

datetime m_prev_bars                = 0;        // "0" -> D'1970.01.01 00:00';

int      m_start;

int      bars_calculated=0;                     // we will keep the number of values in the Moving Average indicator

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

//| Custom indicator initialization function                         |

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

int OnInit()

  {

//--- indicator buffers mapping

   SetIndexBuffer(0,iMAFastBuffer,INDICATOR_CALCULATIONS);

   SetIndexBuffer(1,iMASlowBuffer,INDICATOR_CALCULATIONS);

//--- create handle of the indicator iMA

   handle_iMA_Fast=iMA(Symbol(),Period(),Inp_MA_Fast_ma_period,Inp_MA_Fast_ma_shift,

                       Inp_MA_Fast_ma_method,Inp_MA_Fast_applied_price);

//--- if the handle is not created

   if(handle_iMA_Fast==INVALID_HANDLE)

     {

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

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

                  Symbol(),

                  EnumToString(Period()),

                  GetLastError());

      //--- the indicator is stopped early

      return(INIT_FAILED);

     }

//--- create handle of the indicator iMA

   handle_iMA_Slow=iMA(Symbol(),Period(),Inp_MA_Slow_ma_period,Inp_MA_Slow_ma_shift,

                       Inp_MA_Slow_ma_method,Inp_MA_Slow_applied_price);

//--- if the handle is not created

   if(handle_iMA_Slow==INVALID_HANDLE)

     {

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

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

                  Symbol(),

                  EnumToString(Period()),

                  GetLastError());

      //--- the indicator is stopped early

      return(INIT_FAILED);

     }

//---

   m_start=Inp_MA_Fast_ma_period+Inp_MA_Fast_ma_shift;

   if(m_start<Inp_MA_Slow_ma_period+Inp_MA_Slow_ma_shift)

      m_start=Inp_MA_Slow_ma_period+Inp_MA_Slow_ma_shift;

   m_start++;

//---

   return(INIT_SUCCEEDED);

  }

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

//| Custom indicator deinitialization function                       |

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

void OnDeinit(const int reason)

  {

   ObjectsDeleteAll(0,m_prefix,0,OBJ_TEXT);

  }

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

//| 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(rates_total<m_start)

      return(0);

//--- number of values copied from the iMA indicator

   int values_to_copy;

//--- determine the number of values calculated in the indicator

   int calculated_fast=BarsCalculated(handle_iMA_Fast);

   if(calculated_fast<=0)

     {

      PrintFormat("BarsCalculated(handle_iMA_Fast) returned %d, error code %d",calculated_fast,GetLastError());

      return(0);

     }

   int calculated_slow=BarsCalculated(handle_iMA_Slow);

   if(calculated_slow<=0)

     {

      PrintFormat("BarsCalculated(handle_iMA_Slow) returned %d, error code %d",calculated_slow,GetLastError());

      return(0);

     }

   if(calculated_fast!=calculated_slow)

      return(0);

   int calculated=calculated_fast;

//--- if it is the first start of calculation of the indicator or if the number of values in the iMA indicator changed

//---or if it is necessary to calculated the indicator for two or more bars (it means something has changed in the price history)

   if(prev_calculated==0 || calculated!=bars_calculated || rates_total>prev_calculated+1)

     {

      //--- if the iMABuffer array is greater than the number of values in the iMA indicator for symbol/period, then we don't copy everything

      //--- otherwise, we copy less than the size of indicator buffers

      if(calculated>rates_total)

         values_to_copy=rates_total;

      else

         values_to_copy=calculated;

     }

   else

     {

      //--- it means that it's not the first time of the indicator calculation, and since the last call of OnCalculate()

      //--- for calculation not more than one bar is added

      values_to_copy=(rates_total-prev_calculated)+1;

     }

//--- fill the iMABuffer array with values of the Moving Average indicator

//--- if FillArrayFromBuffer returns false, it means the information is nor ready yet, quit operation

   if(!FillArrayFromBuffer(iMAFastBuffer,Inp_MA_Fast_ma_shift,handle_iMA_Fast,values_to_copy))

      return(0);

   if(!FillArrayFromBuffer(iMASlowBuffer,Inp_MA_Slow_ma_shift,handle_iMA_Slow,values_to_copy))

      return(0);

//--- memorize the number of values in the Moving Average indicator

   bars_calculated=calculated;

//--- main loop

   int limit=prev_calculated-1;

   if(prev_calculated==0)

      limit=m_start;

//--- we work only at the time of the birth of new bar

   datetime time_0=time[rates_total-1];

   if(time_0==m_prev_bars)

      return(rates_total);

   m_prev_bars=time_0;

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

     {

      if(iMAFastBuffer[i-1]<iMASlowBuffer[i-1] && iMAFastBuffer[i]>iMASlowBuffer[i])

         TextCreate(0,m_prefix+(string)time[i],0,time[i],high[i]," UP",InpFont,InpFontSize,InpColorUp,InpAngle,ANCHOR_LEFT);

      else

         if(iMAFastBuffer[i-1]>iMASlowBuffer[i-1] && iMAFastBuffer[i]<iMASlowBuffer[i])

            TextCreate(0,m_prefix+(string)time[i],0,time[i],low[i],"DOWN ",InpFont,InpFontSize,InpColorDown,InpAngle,ANCHOR_RIGHT);

     }

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

   return(rates_total);

  }

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

//| Filling indicator buffers from the MA indicator                  |

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

bool FillArrayFromBuffer(double &values[],   // indicator buffer of Moving Average values

                         int shift,          // shift

                         int ind_handle,     // handle of the iMA indicator

                         int amount          // number of copied values

                        )

  {

//--- reset error code

   ResetLastError();

//--- fill a part of the iMABuffer array with values from the indicator buffer that has 0 index

   if(CopyBuffer(ind_handle,0,-shift,amount,values)<0)

     {

      //--- if the copying fails, tell the error code

      PrintFormat("Failed to copy data from the iMA indicator, error code %d",GetLastError());

      //--- quit with zero result - it means that the indicator is considered as not calculated

      return(false);

     }

//--- everything is fine

   return(true);

  }

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

//| Creating Text object                                             |

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

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

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

                const int               sub_window=0,             // subwindow index

                datetime                time=0,                   // anchor point time

                double                  price=0,                  // anchor point price

                const string            text="Text",              // the text itself

                const string            font="Arial",             // font

                const int               font_size=10,             // font size

                const color             clr=clrRed,               // color

                const double            angle=0.0,                // text slope

                const ENUM_ANCHOR_POINT anchor=ANCHOR_LEFT_UPPER, // anchor type

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

   if(!ObjectCreate(chart_ID,name,OBJ_TEXT,sub_window,time,price))

     {

      Print(__FUNCTION__,

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

      return(false);

     }

//--- set the text

   ObjectSetString(chart_ID,name,OBJPROP_TEXT,text);

//--- set text font

   ObjectSetString(chart_ID,name,OBJPROP_FONT,font);

//--- set font size

   ObjectSetInteger(chart_ID,name,OBJPROP_FONTSIZE,font_size);

//--- set the slope angle of the text

   ObjectSetDouble(chart_ID,name,OBJPROP_ANGLE,angle);

//--- set anchor type

   ObjectSetInteger(chart_ID,name,OBJPROP_ANCHOR,anchor);

//--- set color

   ObjectSetInteger(chart_ID,name,OBJPROP_COLOR,clr);

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

  }

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

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