Interactive price line indicator

Created at 27 May 2022, 23:40
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!
WA

waym77

Joined 22.07.2021

Interactive price line indicator
27 May 2022, 23:40


Hi,

I'm trying  to create a simple indicator that shows an interactive Priceline with a static text that displays the distance from the price but I am experiencing 2 problems:

On activating the price line, it should start on the close price at the time of start, but activates on random spots.

The second problem is that if I switch the display back to No, the object is not deleted.

Thanks,

 

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

namespace cAlgo
{
    [Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class InteractivePriceLine : Indicator
    {
        [Parameter("Show Interactive Price Line?", Group = "Chart", DefaultValue = false)]
        public bool _show_interactive_price_line { get; set; }

        ChartStaticText _price_info;
        ExponentialMovingAverage _fast;
        ChartHorizontalLine _price_line;
        bool _price_line_exists;

        protected override void Initialize()
        {
            _fast = Indicators.ExponentialMovingAverage(Bars.ClosePrices, 12);
            _price_info = Chart.DrawStaticText("_price_info", string.Empty, VerticalAlignment.Top, HorizontalAlignment.Center, Color.Chocolate);
            _price_line_exists = false;
        }

        public override void Calculate(int index)
        {
            if (_show_interactive_price_line)
            {
                double _current_pips_from_line;
                if (!_price_line_exists)
                {
                    _price_line = Chart.DrawHorizontalLine("_price_line", Bars.ClosePrices.LastValue, Color.White, 2, LineStyle.LinesDots);
                    _price_line_exists = true;
                }

                _price_line.IsInteractive = true;
                _price_info.Color = Color.White;

                if (_price_line.Y > Bars.ClosePrices.LastValue)
                {
                    _price_line.Color = Color.Blue;
                    _current_pips_from_line = Math.Round((_price_line.Y - Bars.ClosePrices.LastValue) / Symbol.PipSize, 1);
                    _price_info.Text = string.Format("Price Lower Than Line By {0} Pips. | Price: {1}", _current_pips_from_line, Bars.ClosePrices.LastValue);
                }

                if (_price_line.Y < Bars.ClosePrices.LastValue)
                {
                    _price_line.Color = Color.Yellow;
                    _current_pips_from_line = Math.Round((Bars.ClosePrices.LastValue - _price_line.Y) / Symbol.PipSize, 1);
                    _price_info.Text = string.Format("Price Higher Than Line By {0} Pips. | Price: {1}", _current_pips_from_line, Bars.ClosePrices.LastValue);
                }
            }

            if (!_show_interactive_price_line)
            {
                if (_price_line != null)
                {
                    Chart.RemoveObject("_price_line");
                    _price_line_exists = false;
                }
            }
        }
    }
}

 


@waym77
Replies

amusleh
30 May 2022, 09:22

Hi,

Try this:

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

namespace cAlgo
{
    [Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class InteractivePriceLine : Indicator
    {
        [Parameter("Show Interactive Price Line?", Group = "Chart", DefaultValue = false)]
        public bool _show_interactive_price_line { get; set; }

        private ChartStaticText _price_info;
        private ExponentialMovingAverage _fast;
        private ChartHorizontalLine _price_line;
        private bool _price_line_exists;

        protected override void Initialize()
        {
            _fast = Indicators.ExponentialMovingAverage(Bars.ClosePrices, 12);
            _price_info = Chart.DrawStaticText("_price_info", string.Empty, VerticalAlignment.Top, HorizontalAlignment.Center, Color.Chocolate);
            _price_line_exists = false;

            if (!_show_interactive_price_line)
            {
                Chart.RemoveObject("_price_line");
            }
        }

        public override void Calculate(int index)
        {
            if (!IsLastBar) return;

            if (_show_interactive_price_line)
            {
                double _current_pips_from_line;
                if (!_price_line_exists)
                {
                    _price_line = Chart.DrawHorizontalLine("_price_line", Bars.ClosePrices.LastValue, Color.White, 2, LineStyle.LinesDots);
                    _price_line_exists = true;
                }

                _price_line.IsInteractive = true;
                _price_info.Color = Color.White;

                if (_price_line.Y > Bars.ClosePrices.LastValue)
                {
                    _price_line.Color = Color.Blue;
                    _current_pips_from_line = Math.Round((_price_line.Y - Bars.ClosePrices.LastValue) / Symbol.PipSize, 1);
                    _price_info.Text = string.Format("Price Lower Than Line By {0} Pips. | Price: {1}", _current_pips_from_line, Bars.ClosePrices.LastValue);
                }

                if (_price_line.Y < Bars.ClosePrices.LastValue)
                {
                    _price_line.Color = Color.Yellow;
                    _current_pips_from_line = Math.Round((Bars.ClosePrices.LastValue - _price_line.Y) / Symbol.PipSize, 1);
                    _price_info.Text = string.Format("Price Higher Than Line By {0} Pips. | Price: {1}", _current_pips_from_line, Bars.ClosePrices.LastValue);
                }
            }

            if (!_show_interactive_price_line)
            {
                if (_price_line != null)
                {
                    Chart.RemoveObject("_price_line");
                    _price_line_exists = false;
                }
            }
        }
    }
}

 


@amusleh