how to code a trendline

 

Hi, guys, I'm trying to write a code for trendline, but I don't know, where to start, is there anybody could help me and give me some clues. what approach should I follow?

thanks in advance.

 
337252 :

Hi, guys, I'm trying to write a code for trendline, but I don't know, where to start, is there anybody could help me and give me some clues. what approach should I follow?

thanks in advance.

An example of how to create an OBJ_TREND object

Documentation on MQL5: Constants, Enumerations and Structures / Objects Constants / Object Types / OBJ_TREND
Documentation on MQL5: Constants, Enumerations and Structures / Objects Constants / Object Types / OBJ_TREND
  • www.mql5.com
OBJ_TREND - Object Types - Objects Constants - Constants, Enumerations and Structures - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Search in the reference (Editor: F1) for ObjectCreate() and OBJ_TREND.
 
I don't have problem with trendline function, I'm trying to write an E.A to draw a trendline automatically. I had an idea, and it was a line that it was turning so that could touch two point(iLow's), in that algorithm I needed value of OBG_TRENDBYANGLE, but apparently mt5 has a bug in this function. so I have to use another algorithm.
 
337252:
I don't have problem with trendline function, I'm trying to write an E.A to draw a trendline automatically. I had an idea, and it was a line that it was turning so that could touch two point(iLow's), in that algorithm I needed value of OBG_TRENDBYANGLE, but apparently mt5 has a bug in this function. so I have to use another algorithm.

A pictorial representation would help in explaining this, don't know about others but I couldn't make, what out you were asking 

 
337252:
I don't have problem with trendline function, I'm trying to write an E.A to draw a trendline automatically. I had an idea, and it was a line that it was turning so that could touch two point(iLow's), in that algorithm I needed value of OBG_TRENDBYANGLE, but apparently mt5 has a bug in this function. so I have to use another algorithm.

What kind of bug? The angle is diffrent about the chaet scale

 
amando:

What kind of bug? The angle is diffrent about the chaet scale

Hi, you could refer to below topic that I asked before.

"problem with value of ObjectGetValueByTime for OBJ_TRENDBYANGLE"


MQL5 forum
MQL5 forum
  • www.mql5.com
MQL5: Forum on automated trading systems and strategy testing
 
Thank-god Avwerosuoghene Odukudu:

A pictorial representation would help in explaining this, don't know about others but I couldn't make out you were asking 

I want to know how to code an automatic trendline, what are the steps? i want by using trendline and finding breakout

for example are these below steps reasonable?

1- find the two highest between 1 to 200 candle.

2-draw a line that passes these two points.

3- if price is above the line, delete the object.(it is not valid)

4- if not, it could be a trendline.(that I can use it in my E.A and in next step I will find breakout the trendline for trade).

5-repeat the process 

or other ways for example using fractals, supports and resistance, and so on.

 
337252:

Hi, guys, I'm trying to write a code for trendline, but I don't know, where to start, is there anybody could help me and give me some clues. what approach should I follow?

thanks in advance.

bool Trend_Line_By_Angle(const long           chart_ID=0,        //Chart ID
                        const string          name="TrendLine",  // Line Name
                        const int             sub_window=0,      // Window Num
                        datetime              time=0,            // First Time Point
                        double                price=0,           // First Price Point
                        const double          angle=45.0,        // Angle value
                        const color           clr=clrRed,        // Line Color
                        const ENUM_LINE_STYLE style=STYLE_SOLID, // Line Style
                        const int             width=1,           // Line Width
                        const bool            back=false,        // back
                        const bool            selection=true,    // Selection
                        const bool            ray_right=true,    // ray
                        const bool            hidden=true,       // Hidden
                        const long            z_order=0)         // Priority
  {
   datetime time2=0;
   double   price2=0;
   ResetLastError();
   if(ObjectCreate(chart_ID,name,OBJ_TRENDBYANGLE,sub_window,time,price,time2,price2))
     {
      ObjectSetDouble(chart_ID,name,OBJPROP_ANGLE,angle);
      ObjectSetInteger(chart_ID,name,OBJPROP_COLOR,clr);
      ObjectSetInteger(chart_ID,name,OBJPROP_STYLE,style);
      ObjectSetInteger(chart_ID,name,OBJPROP_WIDTH,width);
      ObjectSetInteger(chart_ID,name,OBJPROP_BACK,back);
      ObjectSetInteger(chart_ID,name,OBJPROP_SELECTABLE,selection);
      ObjectSetInteger(chart_ID,name,OBJPROP_SELECTED,selection);
      ObjectSetInteger(chart_ID,name,OBJPROP_RAY_RIGHT,ray_right);
      ObjectSetInteger(chart_ID,name,OBJPROP_HIDDEN,hidden);
      ObjectSetInteger(chart_ID,name,OBJPROP_ZORDER,z_order);
      ChartRedraw();
      return(true);
        }
     else {
      Print(__FUNCTION__,
             ": failed to create => ",name," object! Error code = ",GetLastError());
      return(false);
          }
  } 
 
Mohsen Bjp:
Thank you, but this function is not something that i'm looking for, in this function user should define two points, after that function will draws a line passing from points.
 
337252:
Thank you, but this function is not something that i'm looking for, in this function user should define two points, after that function will draws a line passing from points.

at first find your points with iHigh and ...

it should be like

time0, price0 ,time1,price1

then draw a trendline between this points with:

ObjectCreate("name",OBJ_TRENDBYANGLE,0);       // (is not complete)

then check your conditions

if it was false you want to delete trendline

you can do that with:

ObjectDelete("trend line name");

if the conditions were true

you want to check value of trendline for each point for checking breakout

you can get the values by

 ObjectGetShiftByValue();
 ObjectGetValueByShift();
 ObjectGetTimeByValue();
 ObjectGetValueByTime();
 

with this codes you can get value of trendline...