Can't draw more than one trendline in custom indicator

Created at 11 Nov 2023, 09:10
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!
IA

ialhamdazeez

Joined 29.10.2023

Can't draw more than one trendline in custom indicator
11 Nov 2023, 09:10


Hi, for learning purpose I am coding a custom indicator. It should draw a trendline on 5 bars every 20 bars. But when it draws new trendline the previous one disapear. Can you help me please?

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)]
    public class testdrawline : Indicator
    {
        [Parameter(DefaultValue = "Hello world!")]
        public string Message { get; set; }

        [Output("Main")]
        public IndicatorDataSeries Result { get; set; }
        
        int period = 20;
        
        
        protected override void Initialize()
        {
            // To learn more about cTrader Automate visit our Help Center:
            // https://help.ctrader.com/ctrader-automate
            
            Print(Message);
        }

        public override void Calculate(int index)
        {
            // Calculate value at specified index
            // Result[index] = 
            if (index % period != 0)return;
            
            Chart.DrawTrendLine("line", Bars[index].OpenTime, Bars[index].Close, Bars[index - 5].OpenTime, Bars[index - 5].Close, "Yellow").IsInteractive = true;
            
        }
    }
}

@ialhamdazeez
Replies

PanagiotisCharalampous
12 Nov 2023, 06:22 ( Updated at: 12 Nov 2023, 06:25 )

Hi there,

It's because you use the same name and the line is overridden. You need to use a unique name for each line

Best regards,

Panagiotis


@PanagiotisCharalampous

applusplus
03 Oct 2024, 10:08 ( Updated at: 03 Oct 2024, 13:05 )

Use

Chart.DrawTrendLine("line" + index, …

to give each drawn object an unique name.

 


@applusplus