Confusion handling ChartObjects

Created at 02 Dec 2023, 00:12
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

ctid4921325

Joined 18.11.2023

Confusion handling ChartObjects
02 Dec 2023, 00:12


Hi there

Can someone help clarify for me:

Should this line return null if the horizontal line  named “Insight” can't be found and set the value of insightLine to null?:


insightLine = Chart.FindObject("Insight") as ChartHorizontalLine;

If i do:
 

  insightLine = Chart.DrawHorizontalLine("Insight", _linePrice, Color.Red);
  Chart.RemoveObject("Insight");

 

Should I expect that  insightLine.IsAlive should now be False? (but insightLine is ≠ null)

If I do:

  insightLine = Chart.DrawHorizontalLine("Insight", _linePrice, Color.Red);
  Chart.RemoveObject("Insight");
  insightLine = Chart.FindObject("Insight") as ChartHorizontalLine;

 

insightLine should now be null?

 

What I get is, that remove object doesn't seem to change the IsAlive property to false, and once it exists FindObject never seems to return a null, mostly as established with DrawStaticText, but I thought I should clarify whats actually expected behaviour.

 

THanks

 

Glenn


@ctid4921325
Replies

PanagiotisCharalampous
02 Dec 2023, 07:54

Hi Glenn,

Should this line return null if the horizontal line  named “Insight” can't be found and set the value of insightLine to null?:

Yes

Should I expect that  insightLine.IsAlive should now be False? (but insightLine is ≠ null)

Yes

insightLine should now be null?

Yes

What I get is, that remove object doesn't seem to change the IsAlive property to false, and once it exists FindObject never seems to return a null, mostly as established with DrawStaticText, but I thought I should clarify whats actually expected behaviour.

Works fine for me. See my code below

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using cAlgo.API;
using cAlgo.API.Collections;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;

namespace cAlgo
{
    [Indicator(AccessRights = AccessRights.None, IsOverlay = true)]
    public class NewIndicator : Indicator
    {
        [Parameter(DefaultValue = "Hello world!")]
        public string Message { get; set; }

        [Output("Main")]
        public IndicatorDataSeries Result { get; set; }
        
        ChartHorizontalLine insightLine;
        protected override void Initialize()
        {
              insightLine = Chart.DrawHorizontalLine("Insight", 1, Color.Red);
              insightLine = Chart.FindObject("Insight") as ChartHorizontalLine;
              Print(insightLine.IsAlive);
              Chart.RemoveObject("Insight");
              Print(insightLine.IsAlive);
        }

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

Best regards,

Panagiotis


@PanagiotisCharalampous

ctid4921325
02 Dec 2023, 08:51

RE: Confusion handling ChartObjects

PanagiotisCharalampous said: 

Hi Glenn,

Should this line return null if the horizontal line  named “Insight” can't be found and set the value of insightLine to null?:

Yes

Should I expect that  insightLine.IsAlive should now be False? (but insightLine is ≠ null)

Yes

insightLine should now be null?

Yes

What I get is, that remove object doesn't seem to change the IsAlive property to false, and once it exists FindObject never seems to return a null, mostly as established with DrawStaticText, but I thought I should clarify whats actually expected behaviour.

Works fine for me. See my code below

using System;using System.Collections.Generic;using System.Linq;using System.Text;using cAlgo.API;using cAlgo.API.Collections;using cAlgo.API.Indicators;using cAlgo.API.Internals;namespace cAlgo{    [Indicator(AccessRights = AccessRights.None, IsOverlay = true)]    public class NewIndicator : Indicator    {        [Parameter(DefaultValue = "Hello world!")]        public string Message { get; set; }        [Output("Main")]        public IndicatorDataSeries Result { get; set; }                ChartHorizontalLine insightLine;        protected override void Initialize()        {              insightLine = Chart.DrawHorizontalLine("Insight", 1, Color.Red);              insightLine = Chart.FindObject("Insight") as ChartHorizontalLine;              Print(insightLine.IsAlive);              Chart.RemoveObject("Insight");              Print(insightLine.IsAlive);        }        public override void Calculate(int index)        {                }    }}

Best regards,

Panagiotis

Awesome, thanks Panagiotis, now I know what should happen… :)


@ctid4921325