ChartTrendLineAdapter

Created at 31 May 2022, 18: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!
SE

seankiaa

Joined 02.02.2022

ChartTrendLineAdapter
31 May 2022, 18:07


Hi, 

I am creating a bot so I can export bars within two trend lines. Here is my code to read TrendLine objects:

 var trendLineObjects = Chart.Objects.Where(chartObject => chartObject.ObjectType == ChartObjectType.TrendLine).ToList();

   if (trendLineObjects != null)
            {
                var l1 = trendLineObjects[0];
                var l2 = trendLineObjects[1];
               // ChartTrendLineAdapter
                var exportItem = CreateExportItem((ChartTrendLine)l1, (ChartTrendLine)l2);
                SaveExportItem(exportItem);
            }

I have got two trend line objects on the chart for the sake of testing. 

When I try to cast the l1 to ChartTrendLine type I get cast exception. After debugging I realized type of l1 object is ChartTrendLineAdapter hence casting is not possible. See the image below:

And I cannot cast it to ChartTrendLineAdapter despite having a reference to CtraderAutomate 1.0.1

I am using .NET 6 version. 

What am I missing? 


@seankiaa
Replies

amusleh
01 Jun 2022, 10:28

Hi,

This works fine for me:

using System.Linq;
using cAlgo.API;

namespace cAlgo
{
    [Indicator(IsOverlay = true, AccessRights = AccessRights.None)]
    public class TrendLineTest : Indicator
    {
        protected override void Initialize()
        {
            foreach (var i in Enumerable.Range(0, 10))
            {
                _ = Chart.DrawTrendLine(i.ToString(), Chart.FirstVisibleBarIndex, Chart.BottomY, Chart.LastVisibleBarIndex, Chart.TopY, Color.Red);
            }

            var trendLines = Chart.Objects.Where(chartObject => chartObject is ChartTrendLine).Select(chartObject => chartObject as ChartTrendLine).ToArray();

            foreach (var trendLine in trendLines)
            {
                trendLine.Color = Color.Yellow;

                Print(trendLine.Name);
            }
        }

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

 


@amusleh