ChartObjectAddedEventArgs Example

Created at 25 Sep 2018, 20:44
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!
YI

yisrgg

Joined 02.04.2016

ChartObjectAddedEventArgs Example
25 Sep 2018, 20:44


Hello everyone,

could someone post a code example using ChartObjectAddedEventArgs? Thank you very much in advance.


@yisrgg
Replies

PanagiotisCharalampous
26 Sep 2018, 10:10

Hi fcomanjoncabeza,

Here it is

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(DefaultValue = 0.0)]
        public double Parameter { get; set; }

        protected override void OnStart()
        {
            Chart.ObjectAdded += OnChartObjectAdded;
            var highPrice = MarketSeries.High.LastValue;
            var openTime = MarketSeries.OpenTime.LastValue;
            var text = Chart.DrawText("text1", "High is here", openTime, highPrice, Color.Red);
            text.VerticalAlignment = VerticalAlignment.Bottom;
            text.HorizontalAlignment = HorizontalAlignment.Center;
        }

        void OnChartObjectAdded(ChartObjectAddedEventArgs obj)
        {
            Print("Object Added: " + obj.ChartObject.ObjectType.ToString());
        }

        protected override void OnTick()
        {
            // Put your core logic here
        }

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

Best Regards,

Panagiotis


@PanagiotisCharalampous

yisrgg
27 Sep 2018, 19:27

RE:

Panagiotis Charalampous said:

Hi fcomanjoncabeza,

Here it is

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(DefaultValue = 0.0)]
        public double Parameter { get; set; }

        protected override void OnStart()
        {
            Chart.ObjectAdded += OnChartObjectAdded;
            var highPrice = MarketSeries.High.LastValue;
            var openTime = MarketSeries.OpenTime.LastValue;
            var text = Chart.DrawText("text1", "High is here", openTime, highPrice, Color.Red);
            text.VerticalAlignment = VerticalAlignment.Bottom;
            text.HorizontalAlignment = HorizontalAlignment.Center;
        }

        void OnChartObjectAdded(ChartObjectAddedEventArgs obj)
        {
            Print("Object Added: " + obj.ChartObject.ObjectType.ToString());
        }

        protected override void OnTick()
        {
            // Put your core logic here
        }

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

Best Regards,

Panagiotis

Thank you very much. But I still get the same error as, when I tried writing my own robot: Error CS0246: The type or namespace name 'ChartObjectAddedEventArgs' could not be found (are you missing a using directive or an assembly reference?) Any idea why?

Thank you very much in advance.


@yisrgg

PanagiotisCharalampous
28 Sep 2018, 10:06

Hi fcomanjoncabeza,

This functionality is available only in cTrader 3.3. Do you use Spotware Beta?

Best Regards,

Panagiotis


@PanagiotisCharalampous

yisrgg
19 Dec 2018, 18:04

Thank you very much for your answer. Now I would like to get the price level of an added horizontal line in the OnChartObjectAdded method. Is it possible?

Thank you very much in advance.


@yisrgg

PanagiotisCharalampous
20 Dec 2018, 09:30

Hi Pep,

Here it is

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(DefaultValue = 0.0)]
        public double Parameter { get; set; }

        protected override void OnStart()
        {
            Chart.ObjectAdded += OnChartObjectAdded;
        }

        void OnChartObjectAdded(ChartObjectAddedEventArgs obj)
        {
            if (obj.ChartObject is ChartHorizontalLine)
                Print("Object Added: " + (obj.ChartObject as ChartHorizontalLine).Y);
        }

        protected override void OnTick()
        {
            // Put your core logic here
        }

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

Best Regards,

Panagiotis


@PanagiotisCharalampous