TrendLine Selection

Created at 30 Jul 2021, 15:23
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!
AK

akbarlotfi15168

Joined 07.04.2021

TrendLine Selection
30 Jul 2021, 15:23


Hello.

I want to select an ordinary trend line   by Name  that user draw in the chart . Is there some methods like

_chartTrendLine = SelectObjectByNme("TrendLine_1");

or

_chartTrendLine =  ChartTrendLine("TrendLine_1");

TY for Help.


@akbarlotfi15168
Replies

amusleh
30 Jul 2021, 17:21

Hi,

All chart objects are inside Chart.Objects collection and you can iterate over it to find specific object by using Linq or a foreach loop:

using System;
using cAlgo.API;
using System.Linq;

namespace cAlgo
{
    [Indicator(IsOverlay = false, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class Test : Indicator
    {
        protected override void Initialize()
        {
            // GetTrendLine can return null if there was no matching trend line
            var myTrendLine = GetTrendLine("MyTrendLine");
        }

        private ChartTrendLine GetTrendLine(string name)
        {
            var chartObjects = Chart.Objects.ToArray();

            return chartObjects.FirstOrDefault(chartObject => chartObject.Name.Equals(name, StringComparison.OrdinalIgnoreCase) && chartObject.ObjectType == ChartObjectType.TrendLine) as ChartTrendLine;
        }

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

 


@amusleh

akbarlotfi15168
30 Jul 2021, 17:34

Tank You

Tank You for useful information I read a sample in forum

It solved my problem . I write the code like this:

 var trendLines = Chart.FindAllObjects<ChartTrendLine>();
            foreach (var trendLine in trendLines)
            {
                var chartTrendLine = trendLine as ChartTrendLine;
                _chartTrendLine = chartTrendLine;
            }
            var BarIndex = MarketSeries.Close.Count - 1;
            double trendLineLastValue = _chartTrendLine.CalculateY(BarIndex);

But I think there is something better that I have used for BarIndex .Can you help me?


@akbarlotfi15168

amusleh
31 Jul 2021, 08:22

Hi,

The code you pasted iterates over all available trend lines on your chart, and then you save each trend line reference to _chartTrendLine variable, at the end of the loop it will have a reference to the latest iterated trend line.

Then you get the latest bar index Y (Price) value by using the trend line CalculateY method.

I'm not sure what exactly you are trying to do, but that's what your code does.


@amusleh