Get the Y value of a horizontal line

Created at 03 Feb 2022, 14:51
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!
CT

ctid1980098

Joined 03.02.2022

Get the Y value of a horizontal line
03 Feb 2022, 14:51


Trying to get the y value of a horizontal line on the chart. 

Create the line:

var hline= Chart.DrawHorizontalLine("HorizontalLine", (ask + 0.1), Color.Yellow, 2);

Trying to get the y value from the horizontal line:

var obj = Chart.FindObject("HorizontalLine");
Print(obj.Y);

the error i get:

'cAlgo.API.ChartObject' does not contain a definition for 'Y' and no extension method 'Y' accepting a first argument of type 'cAlgo.API.ChartObject' could be found (are you missing a using directive or an assembly reference?)

So there seems to be no extension method available. What can one do to work around this? 

thanks


@ctid1980098
Replies

amusleh
04 Feb 2022, 08:55

Hi,

The Chart FindObject method returns a ChartObject which is the base class of all other chart objects.

ChartObject class has no y value property, you should cast it to ChartHorizontalLine class, ex:

using cAlgo.API;

namespace cAlgo
{
    [Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class NewIndicator : Indicator
    {
        protected override void Initialize()
        {
            // The line is a ChartHorizontalLine object
            var line = Chart.DrawHorizontalLine("myLine", Bars.LowPrices[Chart.LastVisibleBarIndex], Color.Red);

            // Now to get the line back
            var myLine = Chart.FindObject("myLine") as ChartHorizontalLine;

            if (myLine != null)
            {
                var yValue = myLine.Y;

                Print(yValue);
            }
        }

        public override void Calculate(int index)
        {
        }
    }
}

 


@amusleh

ctid1980098
04 Feb 2022, 10:47

RE:

amusleh said:

Hi,

The Chart FindObject method returns a ChartObject which is the base class of all other chart objects.

ChartObject class has no y value property, you should cast it to ChartHorizontalLine class, ex:

using cAlgo.API;

namespace cAlgo
{
    [Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class NewIndicator : Indicator
    {
        protected override void Initialize()
        {
            // The line is a ChartHorizontalLine object
            var line = Chart.DrawHorizontalLine("myLine", Bars.LowPrices[Chart.LastVisibleBarIndex], Color.Red);

            // Now to get the line back
            var myLine = Chart.FindObject("myLine") as ChartHorizontalLine;

            if (myLine != null)
            {
                var yValue = myLine.Y;

                Print(yValue);
            }
        }

        public override void Calculate(int index)
        {
        }
    }
}

 

Hi amusleh

Ah, I understand now. I was not aware of that you could cast to a class ChartHorizontalLine. Appreciate your assistance. Thank you


@ctid1980098

ctid1980098
31 Mar 2022, 00:36

RE: RE:

Ctrader updated yesterday and this no longer works for some reason. Any thoughts? Trying to get it working. 

If i get it to work. I will post here

 


@ctid1980098

amusleh
31 Mar 2022, 10:38

RE: RE: RE:

ctid1980098 said:

Ctrader updated yesterday and this no longer works for some reason. Any thoughts? Trying to get it working. 

If i get it to work. I will post here

 

Hi,

I just tested it on cTrader 4.2 Spotware beta and it works fine, on which version it's not working?


@amusleh

ctid1980098
31 Mar 2022, 13:55

RE: RE: RE: RE:

amusleh said:

ctid1980098 said:

Ctrader updated yesterday and this no longer works for some reason. Any thoughts? Trying to get it working. 

If i get it to work. I will post here

 

Hi,

I just tested it on cTrader 4.2 Spotware beta and it works fine, on which version it's not working?

Hi,

Tried on 4.2 beta and couldn't get it to work. However my broker still uses 4.1, so i reinstalled and it is working for now. Will try again later with 4.2 beta. 


@ctid1980098

amusleh
31 Mar 2022, 14:22

RE: RE: RE: RE: RE:

ctid1980098 said:

amusleh said:

ctid1980098 said:

Ctrader updated yesterday and this no longer works for some reason. Any thoughts? Trying to get it working. 

If i get it to work. I will post here

 

Hi,

I just tested it on cTrader 4.2 Spotware beta and it works fine, on which version it's not working?

Hi,

Tried on 4.2 beta and couldn't get it to work. However my broker still uses 4.1, so i reinstalled and it is working for now. Will try again later with 4.2 beta. 

Hi,

If there is any error please post, it works fine on 4.2 and 4.1.


@amusleh

ctid1980098
31 Mar 2022, 14:40

RE: RE: RE: RE: RE: RE:

amusleh said:

ctid1980098 said:

amusleh said:

ctid1980098 said:

Ctrader updated yesterday and this no longer works for some reason. Any thoughts? Trying to get it working. 

If i get it to work. I will post here

 

Hi,

I just tested it on cTrader 4.2 Spotware beta and it works fine, on which version it's not working?

Hi,

Tried on 4.2 beta and couldn't get it to work. However my broker still uses 4.1, so i reinstalled and it is working for now. Will try again later with 4.2 beta. 

Hi,

If there is any error please post, it works fine on 4.2 and 4.1.

Hi, 

Will do. Thank you


@ctid1980098