Question about OnDestroy() method

Created at 06 Oct 2023, 05:56
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!
jani's avatar

jani

Joined 05.04.2019

Question about OnDestroy() method
06 Oct 2023, 05:56


Hello,

 

I'm not a programmer, so I do not have a very good understanding of C#. However, I am sufficient enough to write some code in cAlgo.

I tried to use the new method (as instructed in ctrader.com) OnDestroy in an indicator and got an error message:

 

 

 

    protected override void OnDestroy()
        {
           if (SettingsWindow.IsOpened())
           SettingsWindow.Close();
           
           base.OnDestroy();            
        }

 

Could someone explain using OnDestroy in indicators and give some universal examples?

I also do not understand what process keeps executing after the indicator is removed. E.g. how can an indicator that has been removed still take resources?

 

 

============================================================================================

========================================   Indicator code    ========================================

============================================================================================

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

namespace cAlgo
{
    [Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class SwitchSymbol : Indicator
    {
        [Parameter("Symbols", DefaultValue = "EURUSD,GBPUSD,USDJPY,USDCAD,GBPJPY,EURGBP,EURCHF,AUDUSD,USTEC,US500,US2000,XAUEUR,AUDJPY,GBPCHF,EURJPY")]
        public string SymbolsText { get; set; }

        [Parameter("Margin", DefaultValue = 30, MinValue = 0, Step = 5)]
        public int margin { get; set; }


        [Parameter("Symbol Set", DefaultValue = 1, MinValue = 0)]
        public int SymbolSet { get; set; }

        //[Parameter("Broker's Name", DefaultValue = "No Nmae yet")]
        // public string BrokerName { get; set; }




        protected override void Initialize()
        {

            if (Account.BrokerName.Contains("Pepperstone"))
            {

                switch (SymbolSet)
                {
                    case 1:
                       // SymbolsText = ("EURUSD,GBPUSD,USDJPY,USDCAD,GBPJPY,EURGBP,EURCHF,AUDUSD,NAS100,US500,US2000,XAUEUR,AUDJPY,GBPCHF,EURJPY");
                        
                        SymbolsText = ("EURUSD,GBPUSD,USDJPY,GBPJPY,EURGBP,EURJPY,AUDUSD,AUDJPY,EURCAD,EURCHF,NAS100,US500,GER40,US2000,XAUUSD,XAGUSD");
                        break;
                    case 2:
                        SymbolsText = ("Apple_Inc_(AAPL.O),The_Boeing_Company_(BA.N),The_Walt_Disney_Co_(DIS.N),Facebook_Inc_(FB.O),Alphabet_Inc_C_(GOOG.O),IBM_Corporation_(IBM.N),Microsoft_Corp_(MSFT.O),Netflix_Inc_(NFLX.O),Palantir_Tech_(PLTR.O),PayPal_Holdings_Inc_(PYPL.O),Tesla_Inc_(TSLA.O),NAS100,US500,SPDR_S&P500_ETF_Trust_(SPY.P)");
                        break;
                }
            }
            else if (Account.BrokerName.Contains("IC Markets"))
            {
                switch (SymbolSet)
                {
                    case 1:
                        SymbolsText = ("EURUSD,GBPUSD,USDJPY,USDCAD,GBPJPY,EURGBP,EURCHF,AUDUSD,USTEC,US500,US2000,XAUEUR,AUDJPY,GBPCHF,EURJPY");
                        break;
                    case 2:
                        SymbolsText = ("EURUSD,GBPUSD,USDJPY,USDCAD,GBPJPY,EURGBP,EURCHF,AUDUSD,USTEC,US500,US2000,XAUEUR,AUDJPY,GBPCHF,EURJPY");
                        break;
                }
            }
            else if (Account.BrokerName.Contains("TopFX"))
            {
                SymbolsText = ("EURUSD,GBPUSD,USDJPY,USDCAD,GBPJPY,EURGBP,EURCHF,AUDUSD,US TECH 100,US 500,US 30,XAUEUR,AUDJPY,GBPCHF,EURJPY");
            }
            else if (Account.BrokerName.Contains("VARIANSE"))
            {

                switch (SymbolSet)
                {
                    case 1:
                        SymbolsText = ("EURUSD,GBPUSD,USDJPY,USDCAD,GBPJPY,EURGBP,EURCHF,AUDUSD,UT100.v,US500.v,US30.v,XAUUSD,AUDJPY,GBPCHF,EURJPY");
                        break;
                    case 2:
                        SymbolsText = ("AAPL.OQ.x,AMAT.OQ.x,AMD.OQ.x,AMGN.OQ.x,AMZN.OQ.x,FB.OQ.x,GOOG.OQ.x,INTC.OQ.x,LULU.OQ.x,MSFT.OQ.x,NFLX.OQ.x,NVDA.OQ.x,PTON.OQ.x,TSLA.OQ.x,UT100.v,US500.v");

                        break;
                }
            }



            // Check Symbol  and Broker Name


            //  Chart.DrawStaticText("Broker", " \t\t\n" + "Broker's Name:  " + Account.BrokerName + " // Symbol:  " + SymbolName + "   // Start:" + Symbol.MarketHours.Sessions[0].StartTime + "   // Stop:" + Symbol.MarketHours.Sessions[0].EndTime + "   // Is Market open?: " + Symbol.MarketHours.IsOpened(), VerticalAlignment.Top, HorizontalAlignment.Left, Color.Yellow);

            // Account.BrokerName.EndsWith("xxx")

            //Account.BrokerName..StartsWith("xxx")

            var symbolNames = SymbolsText.Split(new[] 
            {
                ','
            }).Select(s => s.Trim()).Where(s => string.IsNullOrWhiteSpace(s) == false).ToArray();

            var stackPanel = new StackPanel 
            {
                VerticalAlignment = VerticalAlignment.Bottom,
                HorizontalAlignment = HorizontalAlignment.Left,
                Margin = margin,
                //StackPanel.Width = 300,
                Orientation = Orientation.Vertical



                //  dynamicStackPanel.Height = 200;  
                //  dynamicStackPanel.Background = new SolidColorBrush(Colors.LightBlue);  
                //   dynamicStackPanel.Orientation = Orientation.Horizontal; 

            };

            foreach (var symbolName in symbolNames)
            {
                var button = new Button 
                {
                    Text = symbolName,
                    Margin = 2,
                    IsEnabled = symbolName != SymbolName,
                    FontSize = 9
                };
                button.Click += Button_Click;
                stackPanel.AddChild(button);
            }

            Chart.AddControl(stackPanel);
        }

        private void Button_Click(ButtonClickEventArgs obj)
        {
            var symbolName = obj.Button.Text;
            Chart.TryChangeTimeFrameAndSymbol(TimeFrame, symbolName);
        }

        public override void Calculate(int index)
        {
            // ChartObjects.DrawText("Testing14", "\n\n\n Account.BrokerName; " + Account.BrokerName + " || xxxxxx " + 0, StaticPosition.TopCenter, Colors.Red);

        }
        
        
        protected override void OnDestroy()
        {
           if (SettingsWindow.IsOpened())
           SettingsWindow.Close();
           
           base.OnDestroy();            
        }
        
        
        
        
    }
}


@jani