Angle of the (DrawTrend)Line

Created at 26 Oct 2021, 11:07
How’s your experience with the cTrader Platform?
Your feedback is crucial to cTrader's development. Please take a few seconds to share your opinion and help us improve your trading experience. Thanks!
HS

hslagter

Joined 28.04.2020

Angle of the (DrawTrend)Line
26 Oct 2021, 11:07


The Chart.DrawTrendLine command allows you to show the angle of the line with showangle. In MT5 you can read the corner value. With which assignment(s) can this be done in cAlgo?


@hslagter
Replies

amusleh
27 Oct 2021, 08:30 ( Updated at: 27 Oct 2021, 08:31 )

Hi,

No, you can't get the angle from API, but you can calculate it if you want to:

        private double GetAngle(ChartTrendLine trendLine)
        {
            var x1 = Bars.OpenTimes.GetIndexByTime(trendLine.Time1);
            var x2= Bars.OpenTimes.GetIndexByTime(trendLine.Time2);

            double xDiff = x2 - x1;
            var yDiff = trendLine.Y2 - trendLine.Y1;

            return Math.Atan2(yDiff, xDiff) * 180.0 / Math.PI;
        }

The above method will return the angle of a trend line in Degree, it will not work properly if your trend line is drawn in future time, because it uses bar indices for x axis.

If you to get the angle from API then please open a thread on forum suggestions section.


@amusleh

hslagter
27 Oct 2021, 20:07

RE:

Thanks for your answer. I opened a thread on the forum suggestions section. I'm going to try your code. All the price movements, the indicators included, are translated in trendlines so you can see immediatly what the status is of the pair. I use to do it in MT5.

amusleh said:

Hi,

No, you can't get the angle from API, but you can calculate it if you want to:

        private double GetAngle(ChartTrendLine trendLine)
        {
            var x1 = Bars.OpenTimes.GetIndexByTime(trendLine.Time1);
            var x2= Bars.OpenTimes.GetIndexByTime(trendLine.Time2);

            double xDiff = x2 - x1;
            var yDiff = trendLine.Y2 - trendLine.Y1;

            return Math.Atan2(yDiff, xDiff) * 180.0 / Math.PI;
        }

The above method will return the angle of a trend line in Degree, it will not work properly if your trend line is drawn in future time, because it uses bar indices for x axis.

If you to get the angle from API then please open a thread on forum suggestions section.

 


@hslagter

newbee
28 Mar 2022, 04:53

RE: Angle of trendline

amusleh said:

Hi,

No, you can't get the angle from API, but you can calculate it if you want to:

        private double GetAngle(ChartTrendLine trendLine)
        {
            var x1 = Bars.OpenTimes.GetIndexByTime(trendLine.Time1);
            var x2= Bars.OpenTimes.GetIndexByTime(trendLine.Time2);

            double xDiff = x2 - x1;
            var yDiff = trendLine.Y2 - trendLine.Y1;

            return Math.Atan2(yDiff, xDiff) * 180.0 / Math.PI;
        }

The above method will return the angle of a trend line in Degree, it will not work properly if your trend line is drawn in future time, because it uses bar indices for x axis.

If you to get the angle from API then please open a thread on forum suggestions section.

 


@newbee

newbee
28 Mar 2022, 04:56

RE: Hello amusleh. Thank you for this. Can you please help me understand how to get this value to use in a cbot. Also can this be used to find the Angle of the EMA instead of a trendline. thank you

amusleh said:

Hi,

No, you can't get the angle from API, but you can calculate it if you want to:

        private double GetAngle(ChartTrendLine trendLine)
        {
            var x1 = Bars.OpenTimes.GetIndexByTime(trendLine.Time1);
            var x2= Bars.OpenTimes.GetIndexByTime(trendLine.Time2);

            double xDiff = x2 - x1;
            var yDiff = trendLine.Y2 - trendLine.Y1;

            return Math.Atan2(yDiff, xDiff) * 180.0 / Math.PI;
        }

The above method will return the angle of a trend line in Degree, it will not work properly if your trend line is drawn in future time, because it uses bar indices for x axis.

If you to get the angle from API then please open a thread on forum suggestions section.

 


@newbee

amusleh
28 Mar 2022, 09:27

RE: RE: Hello amusleh. Thank you for this. Can you please help me understand how to get this value to use in a cbot. Also can this be used to find the Angle of the EMA instead of a trendline. thank you

newbee said:

amusleh said:

Hi,

No, you can't get the angle from API, but you can calculate it if you want to:

        private double GetAngle(ChartTrendLine trendLine)
        {
            var x1 = Bars.OpenTimes.GetIndexByTime(trendLine.Time1);
            var x2= Bars.OpenTimes.GetIndexByTime(trendLine.Time2);

            double xDiff = x2 - x1;
            var yDiff = trendLine.Y2 - trendLine.Y1;

            return Math.Atan2(yDiff, xDiff) * 180.0 / Math.PI;
        }

The above method will return the angle of a trend line in Degree, it will not work properly if your trend line is drawn in future time, because it uses bar indices for x axis.

If you to get the angle from API then please open a thread on forum suggestions section.

 

Hi,

Yes, you can use the code to get angle of a moving average, you just have to change the points (x1, x2, y1, y2), ex:

        private double GetAngle(int x1, double y1, int x2, double y2)
        {
            double xDiff = x2 - x1;
            var yDiff = y2 - y1;

            return Math.Atan2(yDiff, xDiff) * 180.0 / Math.PI;
        }

 


@amusleh

newbee
30 Mar 2022, 01:47

RE: Hello again Amusleh. Been trying to work it out but failed. Don't know how to ge the X1 & X2. Here is what I did can you please assist me further thanks. private double GetAngle(int x1, double y1, int x2, double y2) { double xDiff

amusleh said:

Hi,

No, you can't get the angle from API, but you can calculate it if you want to:

        private double GetAngle(ChartTrendLine trendLine)
        {
            var x1 = Bars.OpenTimes.GetIndexByTime(trendLine.Time1);
            var x2= Bars.OpenTimes.GetIndexByTime(trendLine.Time2);

            double xDiff = x2 - x1;
            var yDiff = trendLine.Y2 - trendLine.Y1;

            return Math.Atan2(yDiff, xDiff) * 180.0 / Math.PI;
        }

The above method will return the angle of a trend line in Degree, it will not work properly if your trend line is drawn in future time, because it uses bar indices for x axis.

If you to get the angle from API then please open a thread on forum suggestions section.

 


@newbee

newbee
30 Mar 2022, 01:51

RE: Hello again Amusleh. Been trying to work it out but failed. Don't know how to get the X1 & X2. Here is what I did can you please assist me further thanks.

 private double GetAngle(int x1, double y1, int x2, double y2)
        {
            double xDiff = x2 - x1;
            var yDiff = ema20.Result.Last(2) - ema20.Result.Last(1);

            return Math.Atan2(yDiff, xDiff) * 180.0 / Math.PI;
        }


@newbee

amusleh
30 Mar 2022, 09:12

RE: RE: Hello again Amusleh. Been trying to work it out but failed. Don't know how to get the X1 & X2. Here is what I did can you please assist me further thanks.

newbee said:

 private double GetAngle(int x1, double y1, int x2, double y2)
        {
            double xDiff = x2 - x1;
            var yDiff = ema20.Result.Last(2) - ema20.Result.Last(1);

            return Math.Atan2(yDiff, xDiff) * 180.0 / Math.PI;
        }

Hi,

You should use the method, you don't have to change the variables inside the method, ex:

using System;
using cAlgo.API;
using cAlgo.API.Indicators;

namespace cAlgo
{
    [Indicator(IsOverlay = false, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class Angle : Indicator
    {
        private ExponentialMovingAverage _ema;

        protected override void Initialize()
        {
            _ema = Indicators.ExponentialMovingAverage(Bars.ClosePrices, 10);
        }

        public override void Calculate(int index)
        {
            // If you are using it on a cBot you can get index like this
            /// var index = Bars.Count - 1;
            // This will give you the angle between current EMA value and 10 previous value
            var angle = GetAngle(index, _ema.Result[index], index - 10, _ema.Result[index - 10]);

            Print(angle);
        }

        private double GetAngle(int x1, double y1, int x2, double y2)
        {
            double xDiff = x2 - x1;
            var yDiff = y2 - y1;

            return Math.Atan2(yDiff, xDiff) * 180.0 / Math.PI;
        }
    }
}

 


@amusleh

newbee
30 Mar 2022, 11:13

Thank you

Thanks amusleh. Thanks for clarifying this and the lesson/note on how it works. Looks good and makes more sense now. I will play around with it. Thanks again 


@newbee