Cant find and use trendline angle - page 3

 
zemo:
My take is measuring the difference between the upperBB and lowerBB and display it as an indicator then applying the angle measurement on this indicator.
 
Tom Sasson:

I am sorry to bring this post up, I just couldn't find the answer here and it bothered me to see answers like "it can't be done" etc.

So, it CAN be done, using my example below.

First I will explain what needs to be done, since we all agree that the angle cannot be determined if your coordinates are (price,time), so :

1. First challenge is that you need to change them to the chart's (x,y), so we'll get what we see visually.

2. Second challenge is that (x,y) are also not on the same scale, so you need to multiply your resulted slope by the width/height ratio.

So the code should be something like:

i have tested it ! it runs very well in the real-time chart,but failed in the stratege tester.it resulted in a critical error as the file below, i don't know why!

Files:
meafu2.jpg  11 kb
 
Tom Sasson: I just couldn't find the answer here and it bothered me to see answers like "it can't be done" etc. you need to multiply your resulted slope by the width/height ratio.

These three images with two lines are the same chart. Are the angles the same? No, because of the chart scaling. Angles are meaningless.

Multiplying slope by the height and width scale means you get different answers for the same slope - meaningless.




 

Hello Tom,

Your solution is important to me.

I'm a newbie and wish to do the same angle calculation. I copy-pasted the whole of what you shared here just to study how it works and how to write proper code. I put the code inside the "void OnTick()" event handler. After i clicked compiled. i'm getting a "double semicolon expected" error pointing to the line "double GetAngle".  I checked the lines and found all of them do not seem to have any missing semicolon.

sorry for this newbie question. i'm totally lost. 



Tom Sasson:

I am sorry to bring this post up, I just couldn't find the answer here and it bothered me to see answers like "it can't be done" etc.

So, it CAN be done, using my example below.

First I will explain what needs to be done, since we all agree that the angle cannot be determined if your coordinates are (price,time), so :

1. First challenge is that you need to change them to the chart's (x,y), so we'll get what we see visually.

2. Second challenge is that (x,y) are also not on the same scale, so you need to multiply your resulted slope by the width/height ratio.

So the code should be something like:

 

Hi,

I'm a complete newbie and just trying to experiment to understand the below MQL5 code.

Why is it that if i change the subwindow integer to '0' zero, it gives me "comma expected" error. But if i change  it to 'w', it works! shouldnt the subwindow be a number instead of a letter?!?


bool ChartTimePriceToXY( 

   long           ChartID,             // Chart ID 
   int            w,                // The number of the subwindow 
   datetime       time,          // Time on the chart 
   double         PRICE_CLOSE,         // Price on the chart 
   int           x,              // The X coordinate for the time on the chart 
   int           y                // The Y coordinates for the price on the chart 

   ); 
 

Hi,

I know this is an old topic, still I ended up here looking for answers for a client who needed to get the Angle for his EA, because he wanted an angle, because that's what he was used to act on.

I ended up calculating the Angle by the help on this topic and other topics, given the fact that the chart was set to a fixed scale (One to One), it did give consistent enough results; to set to fixed scale: right click your chart, properties, common, 'Scale fix One to One'.

There was one more problem and that's when backtesting, there is no visual chart (except in visual mode), so the angle would not be correctly calculated. I ended up wring down the X- and Y scale on the chart (the amount of pixels based on time (X) and price movement (Y)) when demo trading and using these scales to calculate the angle when backtesting without a visual chart. Here's some demo code I used to get this working.


input double AngleXScale = 0;
input double AngleYScale = 0;

void OnTick()
{
   _ea.HandleTick();

   double currentSlopeLineValue;
   double previousSlopeLineValue;
   double height;
   double degrees;
   string comment;

   currentSlopeLineValue = iCustom(Symbol(),PERIOD_CURRENT,"Slope Direction Line Alert",SlopeDirectionLineAlert_period_sdl,SlopeDirectionLineAlert_SDLAlertUp_method,SlopeDirectionLineAlert_SDLAlertUp_price,SlopeDirectionLineAlert_SDLAlertUp_EmailON,SlopeDirectionLineAlert_SDLAlertUp_SoundON,0,0);
   previousSlopeLineValue = iCustom(Symbol(),PERIOD_CURRENT,"Slope Direction Line Alert",SlopeDirectionLineAlert_period_sdl,SlopeDirectionLineAlert_SDLAlertUp_method,SlopeDirectionLineAlert_SDLAlertUp_price,SlopeDirectionLineAlert_SDLAlertUp_EmailON,SlopeDirectionLineAlert_SDLAlertUp_SoundON,0,1)

   if (IsTesting())
   {

      height = ((previousSlopeLineValue-currentSlopeLineValue)*100)/AngleYScale;
      degrees = MathArctan(height/AngleXScale)*((double)360/((double)2*M_PI));
      comment += "\n\n Angle = "+DoubleToString(degrees,1);
      Comment(comment);
   }

   if (!IsTesting())
   {
      int x1,y1,x0,y0;
      ChartTimePriceToXY(0,0,Time[1],previousSlopeLineValue,x1,y1);
      ChartTimePriceToXY(0,0,Time[0],currentSlopeLineValue,x0,y0);

      double base    = x0-x1;
      height = y1-y0;
      degrees = MathArctan(height/base)*((double)360/((double)2*M_PI));

      int commentIndex = 1;
      string commentLabel = StringConcatenate("CommentLabel", commentIndex);  

      ObjectCreate(commentLabel, OBJ_LABEL, 0, 0, 0 );
      ObjectSet(commentLabel, OBJPROP_CORNER, 0);
      ObjectSet(commentLabel, OBJPROP_XDISTANCE, 5);
      ObjectSet(commentLabel, OBJPROP_YDISTANCE, 15 + (commentIndex * 15) );
      ObjectSetText(commentLabel, "Angle: " + DoubleToString(NormalizeDouble(degrees, 3)), 12, "Tahoma", clrAntiqueWhite );   

      comment = "\n\n\n\n\n Angle = "+DoubleToString(degrees,1);
      comment += "\n\n Base = "+DoubleToString(base,1);
      comment += "\n\n Height = "+DoubleToString(height,1);
      comment += "\n\n X0 = "+DoubleToString(x0,1);
      comment += "\n\n X1 = "+DoubleToString(x1,1);
      comment += "\n\n Y0 = "+DoubleToString(y0,1);
      comment += "\n\n Y1 = "+DoubleToString(y1,1);

      double valueDifferenceSL = currentSlopeLineValue < previousSlopeLineValue ? previousSlopeLineValue - currentSlopeLineValue : currentSlopeLineValue - previousSlopeLineValue;
      comment += "\n\n valueDifference SL: " + DoubleToString(valueDifferenceSL);
      double verticalDifferenceXY = y0 < y1 ? y1 - y0 : y0 - y1;
      comment += "\n\n verticalDifference XY: " + DoubleToString(verticalDifferenceXY);

      if (verticalDifferenceXY == 0)
      {
         Comment(comment);
         return;
      }

      comment += "\n\n Scale: " + DoubleToString((valueDifferenceSL * 100) / verticalDifferenceXY);
      comment += "\n\n Backtest AngleXScale: " + DoubleToString(base,1);
      comment += "\n\n Backtest AngleYScale: " + DoubleToString(NormalizeDouble(((valueDifferenceSL * 100) / verticalDifferenceXY), 4));
      Comment(comment);
   }
}


Reason: