Won't read price value of TrendByAngle line!

 

I'm trying to read the price value of an angled line one bar right of its anchor point using the  ObjectGetValueByShift() function - and it's giving me garbage!  Can't figure out why.

Here is my code from the indicator I'm working on:

============================================================================

    string name = "Angle Line";

    long cID = ChartID();

    ObjectCreate(cID,name,OBJ_TRENDBYANGLE,0,Time[10],0.89,Time[0],0.89);

    ObjectSetDouble(cID,name,OBJPROP_ANGLE,30.0);

    ObjectSetInteger(cID,name,OBJPROP_COLOR,clrBlue); 

    ObjectSetInteger(cID,name,OBJPROP_SELECTABLE,true); 

    ObjectSetInteger(cID,name,OBJPROP_SELECTED,true);

    

    double angle = ObjectGetDouble(cID,name,OBJPROP_ANGLE);

    double Price10 = ObjectGetValueByShift("Angle Line",10);

    double Price9 = ObjectGetValueByShift("Angle Line",9);

    MakeLine("Pnts","Bar[10]: "+DoubleToStr(Price10,Digits)+"  Bar[9]: "+

    DoubleToStr(Price9,Digits)+" Dif: "+DoubleToStr((Price10-Price9)/Point,0)+

    " Angle: "+DoubleToStr(angle,1),LineSize*10);

    ===========================================================================

And this is what I get on the chart:

The value it reads for the Angle Line at Bar[9] is 8900 points LESS than the value it reads at Bar[10] (the anchor point). 

The line is sloped UP at a 30 degree angle, so how could it read a value that is LESS than one bar further left?  

Why does this not work?  Is there anyone who can figure this out for me?  Any help is appreciated.  Thanks.


Trader Mike

 
Trader Mike:

I'm trying to read the price value of an angled line one bar right of its anchor point using the  ObjectGetValueByShift() function - and it's giving me garbage!  Can't figure out why.

Here is my code from the indicator I'm working on:

============================================================================

    string name = "Angle Line";

    long cID = ChartID();

    ObjectCreate(cID,name,OBJ_TRENDBYANGLE,0,Time[10],0.89,Time[0],0.89);

    ObjectSetDouble(cID,name,OBJPROP_ANGLE,30.0);

    ObjectSetInteger(cID,name,OBJPROP_COLOR,clrBlue); 

    ObjectSetInteger(cID,name,OBJPROP_SELECTABLE,true); 

    ObjectSetInteger(cID,name,OBJPROP_SELECTED,true);

    

    double angle = ObjectGetDouble(cID,name,OBJPROP_ANGLE);

    double Price10 = ObjectGetValueByShift("Angle Line",10);

    double Price9 = ObjectGetValueByShift("Angle Line",9);

    MakeLine("Pnts","Bar[10]: "+DoubleToStr(Price10,Digits)+"  Bar[9]: "+

    DoubleToStr(Price9,Digits)+" Dif: "+DoubleToStr((Price10-Price9)/Point,0)+

    " Angle: "+DoubleToStr(angle,1),LineSize*10);

    ===========================================================================

And this is what I get on the chart:

The value it reads for the Angle Line at Bar[9] is 8900 points LESS than the value it reads at Bar[10] (the anchor point). 

The line is sloped UP at a 30 degree angle, so how could it read a value that is LESS than one bar further left?  

Why does this not work?  Is there anyone who can figure this out for me?  Any help is appreciated.  Thanks.


Trader Mike

the angle stays constant after being set .

For instance , if you drag the price you will notice the line maintains its shape .

 
Lorentzos Roussos #:

the angle stays constant after being set .

For instance , if you drag the price you will notice the line maintains its shape .

I am not trying to change the angle, I am trying to determine how many points the price changes from one bar to the next when the angle of the line is a set amount (30 degrees in this case).

 
Trader Mike #:

I am not trying to change the angle, I am trying to determine how many points the price changes from one bar to the next when the angle of the line is a set amount (30 degrees in this case).

i understand . 

what i'm saying is : the points the price changes from one bar to the next , for a trend by angle  , depends on what is in the viewport of the y axis with the prices at that particular time in the chart .
So if you shrink the y axis with the prices ,that will change  because mt4 decides you want that angle at a constant 30 degrees

To improve perspective on this , it does not change only when you drag the price levels . It also changes when new highs or lows are made in the viewport , or , from where you start viewing the chart .
 
Lorentzos Roussos #:

i understand . 

what i'm saying is : the points the price changes from one bar to the next , for a trend by angle  , depends on what is in the viewport of the y axis with the prices at that particular time in the chart .
So if you shrink the y axis with the prices ,that will change  because mt4 decides you want that angle at a constant 30 degrees

To improve perspective on this , it does not change only when you drag the price levels . It also changes when new highs or lows are made in the viewport , or , from where you start viewing the chart .

Thank you for your clarification.  However, I am aware that the price will change based on those things.  But my problem is trying to code an indicator to read whatever the price value is at a point on a trend line (given as a Bar Shift) at the present moment.  I would think that the  ObjectGetValueByShift() function would work for that - but it doesn't!  I'm looking for help in measuring the price difference from one bar to the next on a TRENDBYANGLE line, and can't figure out how to do that?  Does anyone know how to do it?

 
Trader Mike #:

Thank you for your clarification.  However, I am aware that the price will change based on those things.  But my problem is trying to code an indicator to read whatever the price value is at a point on a trend line (given as a Bar Shift) at the present moment.  I would think that the  ObjectGetValueByShift() function would work for that - but it doesn't!  I'm looking for help in measuring the price difference from one bar to the next on a TRENDBYANGLE line, and can't figure out how to do that?  Does anyone know how to do it?

try this :

#property copyright "Discussion"
#property link      "https://www.mql5.com/en/forum/450821"
#property version   "1.00"
#property strict
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- create timer
string name = "Angle Line";
ObjectsDeleteAll(ChartID(),name);

    long cID = ChartID();

    ObjectCreate(cID,name,OBJ_TRENDBYANGLE,0,Time[10],Close[10],Time[0],Close[10]);

    ObjectSetDouble(cID,name,OBJPROP_ANGLE,30.0);

    ObjectSetInteger(cID,name,OBJPROP_COLOR,clrRed); 

    ObjectSetInteger(cID,name,OBJPROP_SELECTABLE,true); 

    ObjectSetInteger(cID,name,OBJPROP_SELECTED,true);
    
    double point0=ObjectGetDouble(cID,name,OBJPROP_PRICE,0);
    double point1=ObjectGetDouble(cID,name,OBJPROP_PRICE,1);
    double point2=ObjectGetDouble(cID,name,OBJPROP_PRICE,2);
    
    Print("price0("+point0+")");
    Print("price1("+point1+")");
    Print("price2("+point2+")");
    
name="Angle Line Guide";

ObjectCreate(cID,name,OBJ_HLINE,0,Time[10],Close[10]);
ObjectSetInteger(cID,name,OBJPROP_COLOR,clrYellow);

calculate_trend_by_angle_by_shift("Angle Line",9,"Angle Line Guide");

//---
   return(INIT_SUCCEEDED);
  }

//calculate value by shift of by angle
bool calculate_trend_by_angle_by_shift(string trend_by_angle_name,
                                       int for_bar,
                                       string hline_name,
                                       bool PRINTS=false){
  double radian=0.0174532925;
//get angle degrees
  double degrees=(double)ObjectGetDouble(ChartID(),trend_by_angle_name,OBJPROP_ANGLE);
  if(PRINTS){Print("Degrees "+degrees);}
//get price min and max 
  double min=(double)ChartGetDouble(ChartID(),CHART_PRICE_MIN,0);
  double max=(double)ChartGetDouble(ChartID(),CHART_PRICE_MAX,0);
  if(PRINTS){Print("Max price ("+max+")");}
  if(PRINTS){Print("Min price ("+min+")");}
//get size of chart in bars (x axis)
  double socib=(double)ChartGetInteger(ChartID(),CHART_WIDTH_IN_BARS,0)+1.0;
  //price per bar , diagonal
  double price_per_bar=((max-min)/_Point)/socib;
  if(PRINTS){Print("Price per bar ("+price_per_bar+")");}
  //get the angle of the screen diagonal
    double ch_width=(double)ChartGetInteger(ChartID(),CHART_WIDTH_IN_PIXELS,0);
    double ch_height=(double)ChartGetInteger(ChartID(),CHART_HEIGHT_IN_PIXELS,0);
    double ch_distance=MathSqrt((ch_width)*(ch_width)+(ch_height)*(ch_height));
    double diagonal_degrees=get_angle(0,(int)ch_height,(int)ch_width,0,ch_distance);
    if(PRINTS){Print("Chart size in bars "+socib);}
    if(PRINTS){Print("Diagonal degrees "+diagonal_degrees);}
    //so price per bar relates to the diagonal angle
    //so i divide by angle find the step per angle in the viewport ?
      double one_degree_y_step=price_per_bar/MathSin(diagonal_degrees*radian);

//sine 
  double sine=MathSin(degrees*radian);
//cosine
  double cosine=MathCos(degrees*radian);
//for 1 step on the x axis with this angle we need 
  if(cosine!=0.0){
  double steps_y=1/cosine;
  double y_step=one_degree_y_step*sine*steps_y;
  if(PRINTS){Print("Y step = "+DoubleToString(y_step,_Digits));}
  double origin=ObjectGetDouble(ChartID(),trend_by_angle_name,OBJPROP_PRICE,0);
  //origin time 
  datetime origin_time=(datetime)ObjectGetInteger(ChartID(),trend_by_angle_name,OBJPROP_TIME,0);
  //origin bar shift
    int origin_bar_shift=iBarShift(_Symbol,_Period,origin_time,true);
  //x step of shift based on origin 
    double x_steps=((double)origin_bar_shift)-((double)for_bar);
  //hline price 
    double hline_price=NormalizeDouble(origin+(x_steps*y_step)*_Point,_Digits);
  ObjectSetDouble(ChartID(),hline_name,OBJPROP_PRICE,0,hline_price);
  }
return(false);
}
//+------------------------------------------------------------------+
double get_angle(int x1,int y1,int x2,int y2,double distance){
  double radian=0.0174532925;
  double arc_tang=MathArctan(distance);
  double angle=arc_tang/radian;
  //cosine
  if(distance!=0)
  {
  double dist_x=x2-x1;
  double dist_y=y1-y2;
  double cosine=dist_x/distance;
  double sine=dist_y/distance;
  double arc_cosine=MathArccos(cosine);
         angle=arc_cosine/radian;
  if(dist_y<0)
    {
    angle=360-angle;
    }
  }
return(angle);
} 
void OnDeinit(const int reason)
  {
  }

void OnTick()
  {
  }
void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
  {
//---
  if(id==CHARTEVENT_CHART_CHANGE){
    calculate_trend_by_angle_by_shift("Angle Line",9,"Angle Line Guide");
    } 
  }
 
Lorentzos Roussos #: the points the price changes from one bar to the next , for a trend by angle  ,
  1. There is no angle from 2 anchor points. An angle requires distance divided by distance; a unit-less number. A chart has price and time. What is the angle of going 30 miles in 45 minutes? Meaningless!

  2. You can get the slope from a trendline: m=ΔPrice÷ΔTime or m=ΔPrice÷Δ(bar index). Changing the price scale, bar width, or window size, changes the apparent angle, the slope is constant. Angle is meaningless.

  3. You can create an angled line using one point and setting the angle (Object Properties - Objects Constants - Standard Constants, Enumerations and Structures - MQL4 Reference.) The line is drawn that angle in pixels. Changing the axes scales does NOT change the angle (and thus is meaningless.)

  4. If you insist, take the two price/time coordinates, convert them to pixel coordinates and then to apparent angle with arctan.
              How to get the angle of a trendline and place a text object to it? - Trend Indicators - MQL4 programming forum - Page 2

 
William Roeder #:
  1. There is no angle from 2 anchor points. An angle requires distance divided by distance; a unit-less number. A chart has price and time. What is the angle of going 30 miles in 45 minutes? Meaningless!

  2. You can get the slope from a trendline: m=ΔPrice÷ΔTime or m=ΔPrice÷Δ(bar index). Changing the price scale, bar width, or window size, changes the apparent angle, the slope is constant. Angle is meaningless.

  3. You can create an angled line using one point and setting the angle (Object Properties - Objects Constants - Standard Constants, Enumerations and Structures - MQL4 Reference.) The line is drawn that angle in pixels. Changing the axes scales does NOT change the angle (and thus is meaningless.)

  4. If you insist, take the two price/time coordinates, convert them to pixel coordinates and then to apparent angle with arctan.
              How to get the angle of a trendline and place a text object to it? - Trend Indicators - MQL4 programming forum - Page 2

Yes the OP is informed . 

 
Lorentzos Roussos #:

try this :

Thank you so much!  This will do it!

 
Trader Mike #:

Thank you so much!  This will do it!

You are welcome . Be careful though , the angle is not grounded in th price action . 

I mean , if this is a product you sell that is going to be one of the serious arguments against it by your users.