How to detect user drag on chart object

13 Mar 2019, 06:51How to detect user drag on chart object#1
noppanonposts: 17since: 17 May 2017

Hi All,

   I would like user to interact with horizontal line by draging up or down. Base on sample code below, I can only detect  when the line is selected. However, when user move mosue away from the line it will not get the unselected event. It will get unselect event only if user click on empty area on chart or select other object on the chart.

Questions

1. How can I get the event (or what event) when user release mouse button or when move away from the line?

2. How can I get the Y value of the line when user release the mouse button?

Thanks in advance

 

Noppanon

 

 

using System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class DragLine : Robot
    {
        [Parameter("Distance 1", DefaultValue = 10.0, MinValue = 1.0, Step = 1.0)]
        public double Distance1Param { get; set; }

        ChartHorizontalLine TargetLine1;
        

        // Chart decoration information when alert is triggered
        private string ChartID1;
        private readonly Color AlertColor1 = Color.Khaki;
        private readonly LineStyle AlertLineStyle = LineStyle.LinesDots;
        private readonly int AlertLineThick = 2;        

        protected override void OnStart()
        {
            ChartID1 = "Target1";
            double distance1;

            distance1 = (Symbol.Bid + Symbol.Ask) / 2 + (Distance1Param * Symbol.PipSize);

            Print("Distance 1 = {0}", distance1);

            if (RunningMode != RunningMode.Optimization)
            {
                TargetLine1 = Chart.DrawHorizontalLine(ChartID1, distance1, AlertColor1, AlertLineThick, AlertLineStyle);
                TargetLine1.IsInteractive = true;
                TargetLine1.Comment = "line 1";                
                Chart.ObjectSelectionChanged += OnObjectSelected;
            }

        }

        protected override void OnStop()
        {
            Chart.RemoveObject(TargetLine1.Name);
        }

        private void OnObjectSelected(ChartObjectSelectionChangedEventArgs args)
        {
            ChartObject line = args.ChartObject;
            if (args.IsObjectSelected )
            {
                if (line.Name == TargetLine1.Name)
                {
                    Print("Selecte Target Line 1  {0}", args.ChartObject.Comment);
                }
            }
            else
            {
                if (line.Name == TargetLine1.Name)
                {
                    Print("UNselect Target Line 1  {0}", args.ChartObject.Comment);
                }
            }
        }
    }
}

 

13 Mar 2019, 08:17#2
noppanonposts: 17since: 17 May 2017

Hi all,

   Say it in another word, I would like user to interact with horizental line similar to Take profit / Stop Loss line in position.

   How should the code look like?

 

Regards,

Noppanon

13 Mar 2019, 09:19#3
Panagiotis Charalampousposts: 2446since: 13 Jan 2017

Hi Noppanon,

You can try Chart.ObjectUpdated event.

Best Regards,

Panagiotis


Head of Community Management at cTrader
13 Mar 2019, 15:32#4
noppanonposts: 17since: 17 May 2017

Hi Panagiotis,

   Thank you for your suggestion. It works. 

 

Regards

Noppanon

14 Mar 2019, 16:16#5
bienve.pfposts: 23since: 19 Sep 2018

How can we detect if an object is selected?
This is useful to avoid handling it while the user is modifying it

 

for example:

ChartTrendLine line = obj as ChartTrendLine;
if( line.IsSelected ){


}

 

15 Mar 2019, 10:00#6
Panagiotis Charalampousposts: 2446since: 13 Jan 2017

Hi bienve.pf,

There is no Selected state for the objects in the chart. When you say modify, do you mean when objects are dragged on the chart? If yes, you can use DragStart and  DragEnd events.

Best Regards,

Panagiotis


Head of Community Management at cTrader
16 Mar 2019, 03:00#7
noppanonposts: 17since: 17 May 2017

Hi Panagiotis,

   I tried to use DrgStart and DragEnd. But it does not work. When I drag a line, there is no event called back. But I drag a chart, the drag evetns get call. see the image and modified code below.

   Do I misundertand anything?

Regards,

Noppanon

 

using System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class DragLine2 : Robot
    {
        [Parameter("Distance 1", DefaultValue = 10.0, MinValue = 1.0, Step = 1.0)]
        public double Distance1Param { get; set; }

        ChartHorizontalLine TargetLine1;


        // Chart decoration information when alert is triggered
        private string ChartID1;
        private readonly Color AlertColor1 = Color.Khaki;
        private readonly LineStyle AlertLineStyle = LineStyle.LinesDots;
        private readonly int AlertLineThick = 2;

        protected override void OnStart()
        {
            ChartID1 = "Target1";
            double distance1;

            distance1 = (Symbol.Bid + Symbol.Ask) / 2 + (Distance1Param * Symbol.PipSize);

            Print("Distance 1 = {0}", distance1);

            if (RunningMode != RunningMode.Optimization)
            {
                TargetLine1 = Chart.DrawHorizontalLine(ChartID1, distance1, AlertColor1, AlertLineThick, AlertLineStyle);
                TargetLine1.IsInteractive = true;
                TargetLine1.Comment = "line 1";
                Chart.ObjectSelectionChanged += OnObjectSelected;                
                Chart.DragStart += OnDragStart;
                Chart.Drag += OnDrag;
                Chart.DragEnd += OnDragEnd;
            }

        }

        protected override void OnStop()
        {
            Chart.RemoveObject(TargetLine1.Name);
        }

        private void OnDragStart(ChartDragEventArgs args)
        {
            Print("Start drag line {0} Y = {1}", args.ToString(), args.YValue);
        }

        private void OnDrag(ChartDragEventArgs args)
        {
            Print("Dragig line {0} Y = {1}", args.ToString(), args.YValue);
        }
        private void OnDragEnd(ChartDragEventArgs args)
        {
            Print("End drag line {0} Y = {1}", args.ToString(), args.YValue);
        }

        private void OnObjectSelected(ChartObjectSelectionChangedEventArgs args)
        {
            ChartObject line = args.ChartObject;
            if (args.IsObjectSelected)
            {
                if (line.Name == TargetLine1.Name)
                {
                    Print("Selecte Target Line 1  {0}", args.ChartObject.Comment);
                }
            }
            else
            {
                if (line.Name == TargetLine1.Name)
                {
                    Print("UNselect Target Line 1  {0}", args.ChartObject.Comment);
                }
            }
        }
    }
}

 

19 Mar 2019, 11:09#8
Panagiotis Charalampousposts: 2446since: 13 Jan 2017

Hi noppanon,

I think I misunderstood what you are trying to do. The workaround below is closer to what you need, even though not perfect. It detects when an object is hovered.

using System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class NewcBot : Robot
    {
        [Parameter("Distance 1", DefaultValue = 10.0, MinValue = 1.0, Step = 1.0)]
        public double Distance1Param { get; set; }

        ChartHorizontalLine TargetLine1;
        ChartObject _selectedObject;
        // Chart decoration information when alert is triggered
        private string ChartID1;
        private readonly Color AlertColor1 = Color.Khaki;
        private readonly LineStyle AlertLineStyle = LineStyle.LinesDots;
        private readonly int AlertLineThick = 2;
        protected override void OnStart()
        {
            ChartID1 = "Target1";
            double distance1;

            distance1 = (Symbol.Bid + Symbol.Ask) / 2 + (Distance1Param * Symbol.PipSize);
            TargetLine1 = Chart.DrawHorizontalLine(ChartID1, distance1, AlertColor1, AlertLineThick, AlertLineStyle);
            TargetLine1.IsInteractive = true;
            TargetLine1.Comment = "line 1";
            Chart.ObjectHoverChanged += OnChartObjectHoverChanged;
        }

        void OnChartObjectHoverChanged(ChartObjectHoverChangedEventArgs obj)
        {
            if (obj.IsObjectHovered)
                _selectedObject = obj.ChartObject;
            else
                _selectedObject = null;
        }

        protected override void OnTick()
        {
            if (_selectedObject != null)
            {
                Print(_selectedObject.Name + " is selected");
            }
        }

        protected override void OnStop()
        {
            // Put your deinitialization logic here
        }
    }
}

Best Regards,

Panagiotis


Head of Community Management at cTrader
22 Mar 2019, 05:07#9
noppanonposts: 17since: 17 May 2017

Hi Panagiotis,

   Thank you for suggestion. But it is not what I am looking. Anyway, it is what cTrader Chart programing model. I will think another way to deal. 

regards

Noppanon