Sample code to drag and drop a line on a chart after a bot draws it?

Created at 01 Feb 2021, 12:41
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!
FI

firemyst

Joined 26.03.2019

Sample code to drag and drop a line on a chart after a bot draws it?
01 Feb 2021, 12:41


Hello everyone / @Spotware / @Panagiotis:

I have the following sample code that draws a line on a chart from a cBot:

ChartHorizontalLine c = 
                Chart.DrawHorizontalLine(key + " line", priceAmountToBreakEven, Color.Goldenrod, 2, LineStyle.Dots);

 

My question is, what do I have to do to allow users to be able to drag and drop the line elsewhere on the chart once it's drawn by the cBot?

If this is possible, can someone provide sample code please?

Thank you.


@firemyst
Replies

PanagiotisCharalampous
01 Feb 2021, 14:25

Hi firemyst,

Just set the IsInteractive property of the object to true.

Best Regards,

Panagiotis 

Join us on Telegram

 


@PanagiotisCharalampous

firemyst
01 Feb 2021, 16:08

RE:

PanagiotisCharalampous said:

Hi firemyst,

Just set the IsInteractive property of the object to true.

Best Regards,

Panagiotis 

Join us on Telegram

 

Thank you @Panagiotis!

And how do I capture the events when dragging and dropping the line? I didn't see any examples in the API Guides nor the API documentation itself:

or here:

 

Thank you.


@firemyst

PanagiotisCharalampous
02 Feb 2021, 08:43

Hi firemyst,

Check Chart.Drag and Chart.ObjectsUpdated events.

Best Regards,

Panagiotis 

Join us on Telegram


@PanagiotisCharalampous

firemyst
24 May 2021, 16:58

RE:

PanagiotisCharalampous said:

Hi firemyst,

Check Chart.Drag and Chart.ObjectsUpdated events.

Best Regards,

Panagiotis 

Join us on Telegram

Thanks for that.

Another question. I have the following sample code -- see my question in the commented code:

 

using System;
using System.Collections.Generic;
using cAlgo.API;
using cAlgo.API.Indicators;

namespace cAlgo.Indicators
{
    [Indicator(IsOverlay = true, AccessRights = AccessRights.None)]
    public class Test : Indicator
    {
        private ChartHorizontalLine _chl;
        private double _value;

        protected override void Initialize()
        {
            _value = MarketData.GetBars(Chart.TimeFrame, Symbol.Name).ClosePrices.LastValue;
            _chl = Chart.DrawHorizontalLine("test line", _value, Color.Goldenrod, 2, LineStyle.Solid);
            _chl.IsInteractive = true;

            Chart.ObjectsUpdated += LineUpdated;
        }

        public override void Calculate(int index)
        {
        }

        //When I drag the horizontal line created on the chart, why does this method get called numerous times?
        //Every time I drag the line, if I set a breakpoint on the opening curly brace, this method gets hit for each
        //object on the chart.
        //Is there a way to to reduce this so it only gets called for horizontal line objects?
        private void LineUpdated(ChartObjectsUpdatedEventArgs args)
        {   
            ChartHorizontalLine obj = (ChartHorizontalLine)args.Chart.FindObject("test line");
            if (obj != null && obj.Y != _value)
            {
                _value = obj.Y;
                Chart.DrawStaticText("value", _value.ToString(), VerticalAlignment.Top, HorizontalAlignment.Right, Color.Goldenrod);
            }
        }

    }
}

 


@firemyst

PanagiotisCharalampous
25 May 2021, 07:57

Hi firemyst,

This method is triggered every time any object is updated. You cannot change this behavior. You need to check for the object type inside the event handler before operating on the relevant object.

Best Regards,

Panagiotis 

Join us on Telegram


@PanagiotisCharalampous