Is there a way to hide the indicator from the button I created on the chart?

Created at 21 Apr 2022, 04:57
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!
yomm0401's avatar

yomm0401

Joined 11.04.2020

Is there a way to hide the indicator from the button I created on the chart?
21 Apr 2022, 04:57


Hello guys

I am looking for a way to hide the indicator from the button I created on the chart.
I have created the button on the chart, but do not know where to go from there.
Can someone please tell me how to do 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, AutoRescale = false, AccessRights = AccessRights.None)]
    public class SampleSMA : Indicator
    {
        [Parameter("Source")]
        public DataSeries Source { get; set; }

        [Parameter(DefaultValue = 14)]
        public int Periods { get; set; }

        [Output("Main")]
        public IndicatorDataSeries Result { get; set; }

        private ToggleButton togglebutton;
        private bool hideshow = true;

        protected override void Initialize()
        {
            buttonDraw();
        }

        private void buttonDraw()
        {
            var stackPanel = new StackPanel 
            {
                HorizontalAlignment = HorizontalAlignment.Center,
                VerticalAlignment = VerticalAlignment.Bottom,
                Margin = 20
            };

            togglebutton = new ToggleButton 
            {
                Text = "Hide",
                Width = 80
            };
            togglebutton.Click += togglebuttonclick;

            stackPanel.AddChild(togglebutton);

            Chart.AddControl(stackPanel);


        }
        private void togglebuttonclick(ToggleButtonEventArgs arg)
        {
            if (togglebutton.IsChecked)
            {
                hideshow = false;
                togglebutton.Text = "Show";
            }
            else
            {
                hideshow = true;
                togglebutton.Text = "Hide";
            }
        }

        public override void Calculate(int index)
        {
            if (hideshow)
            {
                double sum = 0.0;

                for (int i = index - Periods + 1; i <= index; i++)
                {
                    sum += Source[i];
                }
                Result[index] = sum / Periods;
            }
        }
    }
}


@yomm0401
Replies

amusleh
21 Apr 2022, 10:42

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, AutoRescale = false, AccessRights = AccessRights.None)]
    public class SampleSMA : Indicator
    {
        [Parameter("Source")]
        public DataSeries Source { get; set; }

        [Parameter(DefaultValue = 14)]
        public int Periods { get; set; }

        [Output("Main")]
        public IndicatorDataSeries Result { get; set; }

        private ToggleButton togglebutton;
        private bool hideshow = true;

        protected override void Initialize()
        {
            buttonDraw();
        }

        private void buttonDraw()
        {
            var stackPanel = new StackPanel 
            {
                HorizontalAlignment = HorizontalAlignment.Center,
                VerticalAlignment = VerticalAlignment.Bottom,
                Margin = 20
            };

            togglebutton = new ToggleButton 
            {
                Text = "Hide",
                Width = 80
            };
            togglebutton.Click += togglebuttonclick;

            stackPanel.AddChild(togglebutton);

            Chart.AddControl(stackPanel);


        }
        private void togglebuttonclick(ToggleButtonEventArgs arg)
        {
            if (togglebutton.IsChecked)
            {
                hideshow = false;
                togglebutton.Text = "Show";
                
                for (int i = 0; i < Result.Count; i++)
                {
                    Result[i] = double.NaN;
                }
            }
            else
            {
                hideshow = true;
                togglebutton.Text = "Hide";
                
                for (int i = 0; i < Result.Count; i++)
                {
                    Calculate(i);
                }
            }
        }

        public override void Calculate(int index)
        {
            if (hideshow)
            {
                double sum = 0.0;

                for (int i = index - Periods + 1; i <= index; i++)
                {
                    sum += Source[i];
                }
                Result[index] = sum / Periods;
            }
        }
    }
}

 


@amusleh

yomm0401
21 Apr 2022, 11:14

RE:

amusleh said:

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, AutoRescale = false, AccessRights = AccessRights.None)]
    public class SampleSMA : Indicator
    {
        [Parameter("Source")]
        public DataSeries Source { get; set; }

        [Parameter(DefaultValue = 14)]
        public int Periods { get; set; }

        [Output("Main")]
        public IndicatorDataSeries Result { get; set; }

        private ToggleButton togglebutton;
        private bool hideshow = true;

        protected override void Initialize()
        {
            buttonDraw();
        }

        private void buttonDraw()
        {
            var stackPanel = new StackPanel 
            {
                HorizontalAlignment = HorizontalAlignment.Center,
                VerticalAlignment = VerticalAlignment.Bottom,
                Margin = 20
            };

            togglebutton = new ToggleButton 
            {
                Text = "Hide",
                Width = 80
            };
            togglebutton.Click += togglebuttonclick;

            stackPanel.AddChild(togglebutton);

            Chart.AddControl(stackPanel);


        }
        private void togglebuttonclick(ToggleButtonEventArgs arg)
        {
            if (togglebutton.IsChecked)
            {
                hideshow = false;
                togglebutton.Text = "Show";
                
                for (int i = 0; i < Result.Count; i++)
                {
                    Result[i] = double.NaN;
                }
            }
            else
            {
                hideshow = true;
                togglebutton.Text = "Hide";
                
                for (int i = 0; i < Result.Count; i++)
                {
                    Calculate(i);
                }
            }
        }

        public override void Calculate(int index)
        {
            if (hideshow)
            {
                double sum = 0.0;

                for (int i = index - Periods + 1; i <= index; i++)
                {
                    sum += Source[i];
                }
                Result[index] = sum / Periods;
            }
        }
    }
}

 

 

Dear amusleh

you are great!

Thank you very much.


@yomm0401