Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

how to use observable collection.

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    how to use observable collection.

    i added a observable collection but the event is not firing up. Need some help here
    Code:
    #region Using declarations
    using System;
    using System.ComponentModel;
    using System.Diagnostics;
    using System.Drawing;
    using System.Drawing.Drawing2D;
    using System.Xml.Serialization;
    using NinjaTrader.Cbi;
    using NinjaTrader.Data;
    using NinjaTrader.Gui.Chart;
    #endregion
    
    using System.Collections.ObjectModel;
    using System.Collections.Specialized;
    // This namespace holds all indicators and is required. Do not change it.
    namespace NinjaTrader.Indicator
    {
        /// <summary>
        /// Enter the description of your new custom indicator here
        /// </summary>
        [Description("Enter the description of your new custom indicator here")]
        public class IndicatorObservablecolleciton : Indicator
        {
            #region Variables
            // Wizard generated variables
                private int myInput0 = 1; // Default setting for MyInput0
            // User defined variables (add any user defined variables below)
    		ObservableCollection<string> mylist = new ObservableCollection<string>();
            #endregion
    
            /// <summary>
            /// This method is used to configure the indicator and is called once before any bar data is loaded.
            /// </summary>
            protected override void Initialize()
            {
                Add(new Plot(Color.FromKnownColor(KnownColor.Orange), PlotStyle.Line, "Plot0"));
                Overlay				= false;
    			 CalculateOnBarClose = false;
    			 mylist.CollectionChanged  += names_CollectionChanged;
            }
    
            /// <summary>
            /// Called on each bar update event (incoming tick)
            /// </summary>
            protected override void OnBarUpdate()
            {
                // Use this method for calculating your indicator values. Assign a value to each
                // plot below by replacing 'Close[0]' with your own formula.
                Plot0.Set(Close[0]);
    			if(FirstTickOfBar)
    			{
    				mylist.Add(CurrentBar.ToString());
    			}
    			
            }
    		private  void names_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
    {
    	Print("Change type: " + e.Action);
        if (e.NewItems != null)
        {
            Print("Items added: ");
            foreach (var item in e.NewItems)
            {
                Print(item.ToString());
            }
        }
     
        if (e.OldItems != null)
        {
            Print("Items removed: ");
            foreach (var item in e.OldItems)
            {
                Print(item.ToString());
            }
        }
    }
    
    
            #region Properties
            [Browsable(false)]	// this line prevents the data series from being displayed in the indicator properties dialog, do not remove
            [XmlIgnore()]		// this line ensures that the indicator can be saved/recovered as part of a chart template, do not remove
            public DataSeries Plot0
            {
                get { return Values[0]; }
            }
    		
    		 public ObservableCollection<string>  customvalues
            {
                get { return mylist; }
            }
    
            [Description("")]
            [GridCategory("Parameters")]
            public int MyInput0
            {
                get { return myInput0; }
                set { myInput0 = Math.Max(1, value); }
            }
            #endregion
        }
    	
     
    	
    
    }
    
    #region NinjaScript generated code. Neither change nor remove.
    // This namespace holds all indicators and is required. Do not change it.
    namespace NinjaTrader.Indicator
    {
        public partial class Indicator : IndicatorBase
        {
            private IndicatorObservablecolleciton[] cacheIndicatorObservablecolleciton = null;
    
            private static IndicatorObservablecolleciton checkIndicatorObservablecolleciton = new IndicatorObservablecolleciton();
    
            /// <summary>
            /// Enter the description of your new custom indicator here
            /// </summary>
            /// <returns></returns>
            public IndicatorObservablecolleciton IndicatorObservablecolleciton(int myInput0)
            {
                return IndicatorObservablecolleciton(Input, myInput0);
            }
    
            /// <summary>
            /// Enter the description of your new custom indicator here
            /// </summary>
            /// <returns></returns>
            public IndicatorObservablecolleciton IndicatorObservablecolleciton(Data.IDataSeries input, int myInput0)
            {
                if (cacheIndicatorObservablecolleciton != null)
                    for (int idx = 0; idx < cacheIndicatorObservablecolleciton.Length; idx++)
                        if (cacheIndicatorObservablecolleciton[idx].MyInput0 == myInput0 && cacheIndicatorObservablecolleciton[idx].EqualsInput(input))
                            return cacheIndicatorObservablecolleciton[idx];
    
                lock (checkIndicatorObservablecolleciton)
                {
                    checkIndicatorObservablecolleciton.MyInput0 = myInput0;
                    myInput0 = checkIndicatorObservablecolleciton.MyInput0;
    
                    if (cacheIndicatorObservablecolleciton != null)
                        for (int idx = 0; idx < cacheIndicatorObservablecolleciton.Length; idx++)
                            if (cacheIndicatorObservablecolleciton[idx].MyInput0 == myInput0 && cacheIndicatorObservablecolleciton[idx].EqualsInput(input))
                                return cacheIndicatorObservablecolleciton[idx];
    
                    IndicatorObservablecolleciton indicator = new IndicatorObservablecolleciton();
                    indicator.BarsRequired = BarsRequired;
                    indicator.CalculateOnBarClose = CalculateOnBarClose;
    #if NT7
                    indicator.ForceMaximumBarsLookBack256 = ForceMaximumBarsLookBack256;
                    indicator.MaximumBarsLookBack = MaximumBarsLookBack;
    #endif
                    indicator.Input = input;
                    indicator.MyInput0 = myInput0;
                    Indicators.Add(indicator);
                    indicator.SetUp();
    
                    IndicatorObservablecolleciton[] tmp = new IndicatorObservablecolleciton[cacheIndicatorObservablecolleciton == null ? 1 : cacheIndicatorObservablecolleciton.Length + 1];
                    if (cacheIndicatorObservablecolleciton != null)
                        cacheIndicatorObservablecolleciton.CopyTo(tmp, 0);
                    tmp[tmp.Length - 1] = indicator;
                    cacheIndicatorObservablecolleciton = tmp;
                    return indicator;
                }
            }
        }
    }
    
    // This namespace holds all market analyzer column definitions and is required. Do not change it.
    namespace NinjaTrader.MarketAnalyzer
    {
        public partial class Column : ColumnBase
        {
            /// <summary>
            /// Enter the description of your new custom indicator here
            /// </summary>
            /// <returns></returns>
            [Gui.Design.WizardCondition("Indicator")]
            public Indicator.IndicatorObservablecolleciton IndicatorObservablecolleciton(int myInput0)
            {
                return _indicator.IndicatorObservablecolleciton(Input, myInput0);
            }
    
            /// <summary>
            /// Enter the description of your new custom indicator here
            /// </summary>
            /// <returns></returns>
            public Indicator.IndicatorObservablecolleciton IndicatorObservablecolleciton(Data.IDataSeries input, int myInput0)
            {
                return _indicator.IndicatorObservablecolleciton(input, myInput0);
            }
        }
    }
    
    // This namespace holds all strategies and is required. Do not change it.
    namespace NinjaTrader.Strategy
    {
        public partial class Strategy : StrategyBase
        {
            /// <summary>
            /// Enter the description of your new custom indicator here
            /// </summary>
            /// <returns></returns>
            [Gui.Design.WizardCondition("Indicator")]
            public Indicator.IndicatorObservablecolleciton IndicatorObservablecolleciton(int myInput0)
            {
                return _indicator.IndicatorObservablecolleciton(Input, myInput0);
            }
    
            /// <summary>
            /// Enter the description of your new custom indicator here
            /// </summary>
            /// <returns></returns>
            public Indicator.IndicatorObservablecolleciton IndicatorObservablecolleciton(Data.IDataSeries input, int myInput0)
            {
                if (InInitialize && input == null)
                    throw new ArgumentException("You only can access an indicator with the default input/bar series from within the 'Initialize()' method");
    
                return _indicator.IndicatorObservablecolleciton(input, myInput0);
            }
        }
    }
    #endregion

    #2
    Hello,

    I tried the script and see the event working:

    Change type: Add
    Items added:
    5038
    Change type: Add
    Items added:
    5039
    Change type: Add
    Items added:
    5040
    Change type: Add
    Items added:
    5041


    Can you tell me, are you seeing any errors or the script is just not printing anything for you? Have you tried completely removing the instance of the script and re adding it to a chart as well?

    Additionally, this would require a reference to WindowsBase.dll, how was this reference added and also had you restarted the platform after adding the reference?

    I look forward to being of further assistance.
    JesseNinjaTrader Customer Service

    Comment


      #3
      yes it works. i was not using the right indicator when i tested it

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by stafe, Today, 12:34 PM
      2 responses
      19 views
      0 likes
      Last Post stafe
      by stafe
       
      Started by asmmbillah, Today, 11:18 AM
      1 response
      17 views
      0 likes
      Last Post NinjaTrader_ChrisL  
      Started by nickse2003, Today, 09:56 AM
      1 response
      26 views
      0 likes
      Last Post NinjaTrader_ChristopherJ  
      Started by ATMtrader0001, Today, 10:08 AM
      5 responses
      37 views
      0 likes
      Last Post ATMtrader0001  
      Started by WHICKED, Today, 09:47 AM
      1 response
      26 views
      0 likes
      Last Post NinjaTrader_Spencer  
      Working...
      X