Simple Coding Help - No Promise.

Romeo1998

Well-Known Member
Hi

Lets say I draw and Free Line or Trend Line on chart, is it possible to print the angle of trend line on chart?

For example After I draw a Trend Line, it should print the angle of line on the chart itself

View attachment 39529

By angle I mean degree angle or in degrees

Sir just use this code and double click on drawn line and give study id as ZZ, you will see angle of drawn line in Title. :)

Code:
drawnline = Study( "ZZ", GetChartID() );

x0 = LastValue( ValueWhen( ExRem( drawnline, 0 ), BarIndex() ) );
x1 = LastValue( ValueWhen( drawnline, BarIndex() ) );
y0 = LastValue( ValueWhen( ExRem( drawnline, 0 ), drawnline ) );
y1 = LastValue( ValueWhen( drawnline, drawnline ) );

if( x0 != 0 )
{
    slope = ( y1 - y0 ) / ( x1 - x0 );
    angle = atan( slope );
    angle_in_degrees = angle * 180 / 3.14 ;

    Title = StrFormat( "{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ) + "\nAngle of drawn line is " + angle_in_degrees + " °";
}

Good Luck :)
 
Sir just use this code and double click on drawn line and give study id as ZZ, you will see angle of drawn line in Title. :)

Code:
drawnline = Study( "ZZ", GetChartID() );

x0 = LastValue( ValueWhen( ExRem( drawnline, 0 ), BarIndex() ) );
x1 = LastValue( ValueWhen( drawnline, BarIndex() ) );
y0 = LastValue( ValueWhen( ExRem( drawnline, 0 ), drawnline ) );
y1 = LastValue( ValueWhen( drawnline, drawnline ) );

if( x0 != 0 )
{
    slope = ( y1 - y0 ) / ( x1 - x0 );
    angle = atan( slope );
    angle_in_degrees = angle * 180 / 3.14 ;

    Title = StrFormat( "{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ) + "\nAngle of drawn line is " + angle_in_degrees + " °";
}

Good Luck :)
:up:

Instead of Title we can also use Gfx for display


.
 

MSN1979

Well-Known Member
Sir just use this code and double click on drawn line and give study id as ZZ, you will see angle of drawn line in Title. :)

Code:
drawnline = Study( "ZZ", GetChartID() );

x0 = LastValue( ValueWhen( ExRem( drawnline, 0 ), BarIndex() ) );
x1 = LastValue( ValueWhen( drawnline, BarIndex() ) );
y0 = LastValue( ValueWhen( ExRem( drawnline, 0 ), drawnline ) );
y1 = LastValue( ValueWhen( drawnline, drawnline ) );

if( x0 != 0 )
{
    slope = ( y1 - y0 ) / ( x1 - x0 );
    angle = atan( slope );
    angle_in_degrees = angle * 180 / 3.14 ;

    Title = StrFormat( "{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ) + "\nAngle of drawn line is " + angle_in_degrees + " °";
}

Good Luck :)

Thank you so much, you are a genius
 

yusi

Well-Known Member
Code:
    slope = ( y1 - y0 ) / ( x1 - x0 );
Got to wondering if this was actionable. A Rs 300 stock rising to 310 over 11 bars would be 45 degrees. So would a Rs 1000 scrip rising to 1010 over the same period.

Perhaps an angle in terms of percentages is more appropriate (given that the angle is for x and y in different units) :

Code:
    slope = ( someMultiple * ( y1 - y0 ) / y0 ) / ( x1 - x0 );
 

trash

Well-Known Member
Sir just use this code and double click on drawn line and give study id as ZZ, you will see angle of drawn line in Title. :)

Code:
drawnline = Study( "ZZ", GetChartID() );

x0 = LastValue( ValueWhen( ExRem( drawnline, 0 ), BarIndex() ) );
x1 = LastValue( ValueWhen( drawnline, BarIndex() ) );
y0 = LastValue( ValueWhen( ExRem( drawnline, 0 ), drawnline ) );
y1 = LastValue( ValueWhen( drawnline, drawnline ) );

if( x0 != 0 )
{
    slope = ( y1 - y0 ) / ( x1 - x0 );
    angle = atan( slope );
    angle_in_degrees = angle * 180 / 3.14 ;

    Title = StrFormat( "{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ) + "\nAngle of drawn line is " + angle_in_degrees + " °";
}

Good Luck :)

Still copying material from other websites and making it look like as it is based from your brain, you piece of a joke??

https://www.amibroker.com/kb/2006/03/07/getting-x-y-co-ordinates-of-study/
https://forum.amibroker.com/t/atan-returning-radians-and-i-need-degrees/4973
etc....
 

Similar threads