Trendline Breakout Automated Entry

 

Hi everyone,

I am wondering if there is a possibility to code an entry condition where the price is > or < a trendline drawn on the chart.

Is there any possibility to get the value of a trendline in mql5?

Is it possible to compare the price with the value of the trendline on the same date?

Thanks a lot!

 
  1. Of course.
  2. There is no value. There is only a value at a specific datetime.
  3. Of course. Get the price at a datetime. Get the trendline value (№ 2) at that datetime, compare them (№ 1).
 
William Roeder:
  1. Of course.
  2. There is no value. There is only a value at a specific datetime.
  3. Of course. Get the price at a datetime. Get the trendline value (№ 2) at that datetime, compare them (№ 1).

Okay, so first thank you.

After some searching and reading I wrote down this code:

   //get the value of the trendline
   double trendline1=ObjectGetValueByTime(0,"Trendline",iTime(NULL, PERIOD_D1, 1),0);   
   double trendline2=ObjectGetValueByTime(0,"Trendline",iTime(NULL, PERIOD_D1, 2),0);
   
   //Last bar close price
   MqlRates PriceInformation[];
   ArraySetAsSeries(PriceInformation,true);
   int Data=CopyRates(Symbol(),Period(),0,Bars(Symbol(),Period()),PriceInformation);
   double bar1=PriceInformation[1].close;
   double bar2=PriceInformation[2].close;
      
   //buy&sell check
   if(bar1>trendline1 && bar2<trendline2)
   {
      entry="buy";
   }
   if(bar1<trendline1 && bar2>trendline2)
   {
      entry="sell";
   }

I have drawn the trendline on the chart and named it as in my code. But somehow it doesn't work when I test it. Do you have an idea, what am I doing wrong? 

 
Demosfen:

Okay, so first thank you.

After some searching and reading I wrote down this code:

I have drawn the trendline on the chart and named it as in my code. But somehow it doesn't work when I test it. Do you have an idea, what am I doing wrong? 

Your trendline refers to PERIOD_D1 but CopyRates is copying the Current Chart Window bar values. 

If your chart is not on D1, price values for [1] and [2] will be another, and it will not work.


Try specifying the Period like:

int Data=CopyRates(Symbol(),PERIOD_D1,0,3,PriceInformation);
 
rrocchi:

Your trendline refers to PERIOD_D1 but CopyRates is copying the Current Chart Window bar values. 

If your chart is not on D1, price values for [1] and [2] will be another, and it will not work.


Try specifying the Period like:

Thanks, I fixed it.