//+------------------------------------------------------------------+ //| Test_Regression.mq5 | //| Copyright 2018, MetaQuotes Software Corp. | //| https://www.mql5.com | //+------------------------------------------------------------------+ #property copyright "Copyright 2018, MetaQuotes Software Corp." #property link "https://www.mql5.com" #property version "1.00" #property indicator_chart_window //+------------------------------------------------------------------+ //| Custom indicator initialization function | //+------------------------------------------------------------------+ int OnInit() { //--- indicator buffers mapping //--- 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[]) { //--- ResetLastError(); ChartRedraw(); RegressionCreate(0,"Regress",0,time[rates_total-1]-(10*60),time[rates_total-1]); Print(ObjectGetValueByTime(0,"Regress",time[rates_total-1])," ",time[rates_total-1]," ",GetLastError()); //--- return value of prev_calculated for next call return(rates_total); } //+------------------------------------------------------------------+ //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ 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=false,// 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]; } } //+------------------------------------------------------------------+