Trend OBJ_REGRESSION 2

Author: Copyright © 2020-2021, Vladimir Karputov
0 Views
0 Downloads
0 Favorites
Trend OBJ_REGRESSION 2
ÿþ//+------------------------------------------------------------------+

//|                                       Trend OBJ_REGRESSION 2.mq5 |

//|                         Copyright © 2020-2021, Vladimir Karputov |

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

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

#property copyright "Copyright © 2020-2021, Vladimir Karputov"

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

#property version   "2.002"

#property indicator_chart_window

#property indicator_buffers 0

#property indicator_plots   0

//--- input parameters

input group             "Fast object"

input uchar                InpFastChannelBars   = 12;                // Channel Bars

input string               InpFastName          = "Fast Regression"; // Channel name

input color                InpFastColor         = clrDeepPink;       // Channel color

input ENUM_LINE_STYLE      InpFastStyle         = STYLE_DASH;        // Style of channel lines

input int                  InpFastWidth         = 2;                 // Width of channel lines

input bool                 InpFastFill          = true;              // Filling the channel with color

input bool                 InpFastBack          = true;              // Background channel

input bool                 InpFastSelection     = false;             // Highlight to move

input bool                 InpFastHidden        = false;             // Hidden in the object list

input long                 InpFastZOrder        = 0;                 // Priority for mouse click

input group             "Slow object"

input uchar                InpSlowChannelBars   = 30;                // Channel Bars

input string               InpSlowName          = "Slow Regression"; // Channel name

input color                InpSlowColor         = clrTurquoise;      // Channel color

input ENUM_LINE_STYLE      InpSlowStyle         = STYLE_DASH;        // Style of channel lines

input int                  InpSlowWidth         = 2;                 // Width of channel lines

input bool                 InpSlowFill          = true;              // Filling the channel with color

input bool                 InpSlowBack          = true;              // Background channel

input bool                 InpSlowSelection     = false;             // Highlight to move

input bool                 InpSlowHidden        = false;             // Hidden in the object list

input long                 InpSlowZOrder        = 0;                 // Priority for mouse click

//---

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

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

//| Custom indicator initialization function                         |

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

int OnInit()

  {

   RegressionDelete(ChartID(),InpFastName);

   RegressionDelete(ChartID(),InpSlowName);

//--- name for DataWindow

   IndicatorSetString(INDICATOR_SHORTNAME,"Trend 2("+IntegerToString(InpFastChannelBars)+","+IntegerToString(InpSlowChannelBars)+")");

//---

   return(INIT_SUCCEEDED);

  }

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

//| Indicator deinitialization function                              |

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

void OnDeinit(const int reason)

  {

//---

   RegressionDelete(ChartID(),InpFastName);

   RegressionDelete(ChartID(),InpSlowName);

  }

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

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

  {

   /*

      time[0]              -> D'2018.01.02 00:00:00'

      time[rates_total-1]  -> D'2018.12.31 22:00:00'

   */

//---

   if(rates_total<InpSlowChannelBars)

      return(0);

   int i=rates_total-1;

   if(prev_calculated==0)

     {

      RegressionCreate(0,InpFastName,0,time[i-(InpFastChannelBars-1)],time[i],InpFastColor,InpFastStyle,InpFastWidth,

                       InpFastFill,InpFastBack,InpFastSelection,false,true,InpFastHidden,InpFastZOrder);

      RegressionCreate(0,InpSlowName,0,time[i-(InpSlowChannelBars-1)],time[i],InpSlowColor,InpSlowStyle,InpSlowWidth,

                       InpSlowFill,InpSlowBack,InpSlowSelection,false,true,InpSlowHidden,InpSlowZOrder);

     }

//---

   if(ExtPrevBars!=time[i])

     {

      ExtPrevBars=time[i];

      if(!RegressionPointChange(0,InpFastName,0,time[i-(InpFastChannelBars-1)]) || !RegressionPointChange(0,InpFastName,1,time[i]))

        {

         ExtPrevBars=0;

         return(0);

        }

      if(!RegressionPointChange(0,InpSlowName,0,time[i-(InpSlowChannelBars-1)]) || !RegressionPointChange(0,InpSlowName,1,time[i]))

        {

         ExtPrevBars=0;

         return(0);

        }

     }

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

   return(rates_total);

  }

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

//| Create Linear Regression Channel by the given coordinates        |

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

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

                      const string          name="Regression", // channel name

                      const int             sub_window=0,      // subwindow index

                      datetime              time1=0,           // first point time

                      datetime              time2=0,           // second point time

                      const color           clr=clrRed,        // channel color

                      const ENUM_LINE_STYLE style=STYLE_SOLID, // style of channel lines

                      const int             width=1,           // width of channel lines

                      const bool            fill=false,        // filling the channel with color

                      const bool            back=false,        // in the background

                      const bool            selection=true,    // highlight to move

                      const bool            ray_left=false,    // channel's continuation to the left

                      const bool            ray_right=false,   // channel's continuation to the right

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

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

  {

//--- set anchor points' coordinates if they are not set

   ChangeRegressionEmptyPoints(time1,time2);

//--- reset the error value

   ResetLastError();

//--- create a channel by the given coordinates

   if(!ObjectCreate(chart_ID,name,OBJ_REGRESSION,sub_window,time1,0,time2,0))

     {

      Print(__FUNCTION__,

            ": failed to create linear regression channel! Error code = ",GetLastError());

      return(false);

     }

//--- set channel color

   ObjectSetInteger(chart_ID,name,OBJPROP_COLOR,clr);

//--- set style of the channel lines

   ObjectSetInteger(chart_ID,name,OBJPROP_STYLE,style);

//--- set width of the channel lines

   ObjectSetInteger(chart_ID,name,OBJPROP_WIDTH,width);

//--- enable (true) or disable (false) the mode of filling the channel

   ObjectSetInteger(chart_ID,name,OBJPROP_FILL,fill);

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

   ObjectSetInteger(chart_ID,name,OBJPROP_BACK,back);

//--- enable (true) or disable (false) the mode of highlighting the channel for moving

//--- when creating a graphical object using ObjectCreate function, the object cannot be

//--- highlighted and moved by default. Inside this method, selection parameter

//--- is true by default making it possible to highlight and move the object

   ObjectSetInteger(chart_ID,name,OBJPROP_SELECTABLE,selection);

   ObjectSetInteger(chart_ID,name,OBJPROP_SELECTED,selection);

//--- enable (true) or disable (false) the mode of continuation of the channel's display to the left

   ObjectSetInteger(chart_ID,name,OBJPROP_RAY_LEFT,ray_left);

//--- enable (true) or disable (false) the mode of continuation of the channel's display to the right

   ObjectSetInteger(chart_ID,name,OBJPROP_RAY_RIGHT,ray_right);

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

  }

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

//| Move the channel's anchor point                                  |

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

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

                           const string name="Channel", // channel name

                           const int    point_index=0,  // anchor point index

                           datetime     time=0)         // anchor point time coordinate

  {

//--- if point time is not set, move the point to the current bar

   if(!time)

      time=TimeCurrent();

//--- reset the error value

   ResetLastError();

//--- move the anchor point

   if(!ObjectMove(chart_ID,name,point_index,time,0))

     {

      Print(__FUNCTION__,

            ": failed to move the anchor point! Error code = ",GetLastError());

      return(false);

     }

//--- successful execution

   return(true);

  }

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

//| Delete the channel                                               |

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

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

                      const string name="Channel") // channel name

  {

//--- reset the error value

   ResetLastError();

//--- delete the channel

   if(!ObjectDelete(chart_ID,name))

     {

      Print(__FUNCTION__,

            ": failed to delete the channel! Error code = ",GetLastError());

      return(false);

     }

//--- successful execution

   return(true);

  }

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

//| Check the values of the channel's anchor points and set default values  |

//| for empty ones                                                          |

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

void ChangeRegressionEmptyPoints(datetime &time1,datetime &time2)

  {

//--- if the second point's time is not set, it will be on the current bar

   if(!time2)

      time2=TimeCurrent();

//--- if the first point's time is not set, it is located 9 bars left from the second one

   if(!time1)

     {

      //--- array for receiving the open time of the last 10 bars

      datetime temp[10];

      CopyTime(Symbol(),Period(),time2,10,temp);

      //--- set the first point 9 bars left from the second one

      time1=temp[0];

     }

  }

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

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