Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Instantiate indicator with non [NinjaScriptProperty] parameters

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

    Instantiate indicator with non [NinjaScriptProperty] parameters

    While referencing indicator from another indicator or strategy I sometimes find myself in need of changing some optional parameters that are not included in constructor (ie. properties that don't have [NinjaScriptProperty] decorator).

    That's where the problem starts as there seem to be only two options to solve it:
    1. Add the decorator to the property... but then you need to change every place it was already referenced which sometimes is a lot
    2. Add another copy of the indicator this time with added parameter... but this can cause a mess with multiple versions of the same indicators.

    What I would like to do is instead be able to do something like:
    Code:
     
    myIndy = new BestIndicator()
    {
        InputType = InputType,
        DecompositionLevel = DecompositionLevel,
        ShowApproximation = ShowApproximation,
        ShowMirrorImage = ShowMirrorImage,
        SmoothedDetails = "0"
    };
    where I could change all parameters if needed.

    Is there any other way of achieving it?

    #2
    Hello gregid,

    Thank you for your post.

    You could pass the parameters that are public but not tagged with [NinjaScriptProperty] to the instantiated object before adding it to the chart.

    For example:

    In the strategy:
    Code:
    	public class MyStrategy : Strategy
    	{
    		private MyIndicator myIndi;
    		...
    			if (State == State.Configure)
    			{				
    				
    				myIndi= TMyIndicator();
    				[B]myIndi.MyVariable = 10; // here I pass 10 to my public variable[/B]
    				AddChartIndicator(myIndi);
    			}
    In the indicator:
    Code:
    	public class MyIndicator : Indicator
    	{
    		public int 	MyVariable	= 1;
    		...
    		protected override void OnBarUpdate()
    		{
    			Print("");
    			Print(Time[0]);
    			Print(MyVariable);
    		}

    Comment


      #3
      Thanks Patrick for your assistance but I am afraid this is not solving my problem.
      I need to instantiate the indicator with additional property and not to pass variable to instantiated indicator. In effect what I need is different constructor
      eg. assuming I have two properties in the indicator I want to instantiate:
      Code:
      [FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2][[/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#2b91af][FONT=Consolas][SIZE=2][COLOR=#2b91af][FONT=Consolas][SIZE=2][COLOR=#2b91af]NinjaScriptProperty[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2]][/SIZE][/FONT]
      [SIZE=2][FONT=Consolas][[/FONT][/SIZE][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#2b91af][FONT=Consolas][SIZE=2][COLOR=#2b91af][FONT=Consolas][SIZE=2][COLOR=#2b91af]Display[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2]([/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2]Name = [/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#a31515][FONT=Consolas][SIZE=2][COLOR=#a31515][FONT=Consolas][SIZE=2][COLOR=#a31515]"Period1"[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2], GroupName = [/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#a31515][FONT=Consolas][SIZE=2][COLOR=#a31515][FONT=Consolas][SIZE=2][COLOR=#a31515]"Parameters"[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2], Order = 0)][/SIZE][/FONT]
      [/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff]public [/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2] Period1[/SIZE][/FONT]
      [SIZE=2][FONT=Consolas]{ [/FONT][/SIZE][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff]get[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2]; [/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff]set[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2]; }[/SIZE][/FONT][/SIZE][/FONT]
      [FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2][COLOR=#008000][FONT=Consolas][SIZE=2][COLOR=#008000][FONT=Consolas][SIZE=2][COLOR=#008000]// and this one without [NinjaScriptProperty]:[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2]
      [SIZE=2][FONT=Consolas][[/FONT][/SIZE][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#2b91af][FONT=Consolas][SIZE=2][COLOR=#2b91af][FONT=Consolas][SIZE=2][COLOR=#2b91af]Display[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2]([/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2]Name = [/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#a31515][FONT=Consolas][SIZE=2][COLOR=#a31515][FONT=Consolas][SIZE=2][COLOR=#a31515]"Period2"[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2], GroupName = [/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#a31515][FONT=Consolas][SIZE=2][COLOR=#a31515][FONT=Consolas][SIZE=2][COLOR=#a31515]"OptParameters"[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2], Order = 0)][/SIZE][/FONT]
      [/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff]public [/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2] Period2[/SIZE][/FONT]
      [SIZE=2][FONT=Consolas]{ [/FONT][/SIZE][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff]get[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2]; [/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff]set[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2]; }[/SIZE][/FONT][/SIZE][/FONT]
      [/SIZE][/FONT][/SIZE][/FONT]
      NT will generate wrapper only for Period1 and in few situations I want to also have a constructor with Period2. To see the problem imagine an indicator with let's say 50 properties. In 95% I only need constructor with let's say property A, B and C (so I flag A, B and C with [NinjaScriptProperty]) but the remaining 5% it can be any of the other 47 properties.
      If I try to change the property in the similar fashion to what you suggested the change will only happen after configuration of the indicator is complete (ie. after State.Configure). Is there a way to write additional constructor to the default NT generated as this could solve my problem?

      Comment


        #4
        You can add to the NinjaTrader Generated code region. For example:
        Code:
        namespace NinjaTrader.NinjaScript.Indicators
        {
        	public class ExampleConstruct : Indicator
        	{
        		private int Test = 10;
        		protected override void OnStateChange()
        		{
        			if (State == State.SetDefaults)
        			{
        				Description							= @"Enter the description for your new custom Indicator here.";
        				Name								= "ExampleConstruct";
        				Calculate							= Calculate.OnBarClose;
        				IsOverlay							= false;
        				DisplayInDataBox					= true;
        				DrawOnPricePanel					= true;
        				DrawHorizontalGridLines				= true;
        				DrawVerticalGridLines				= true;
        				PaintPriceMarkers					= true;
        				ScaleJustification					= NinjaTrader.Gui.Chart.ScaleJustification.Right;
        				//Disable this property if your indicator requires custom values that cumulate with each new market data event. 
        				//See Help Guide for additional information.
        				IsSuspendedWhileInactive			= true;
        			}
        			else if (State == State.Configure)
        			{
        			}
        		}
        
        		protected override void OnBarUpdate()
        		{
        			//Add your custom indicator logic here.
        		}
        	}
        }
        
        [B]namespace NinjaTrader.NinjaScript.Indicators
        {
        	public partial class Indicator : NinjaTrader.Gui.NinjaScript.IndicatorRenderBase
        	{
        		public ExampleConstruct ExampleConstruct (int Test)
        		{
        			return ExampleConstruct (Input);
        		}
        	}
        }[/B]

        Comment


          #5
          Thanks Patrick.

          I was under the impression the wrapper is generated automatically and if any change related to properties occurs NT will regenerate, ie. remove existing and replace with the new wrapper. Will this not cause me losing my custom addition to the wrapper?

          Comment


            #6
            Hello gregid,

            You would not write in the NinjaScript generated code. I have added mine just above. In general, it is not recommended to play with the constructors in this way as it could cause unforeseen items.

            Comment


              #7
              OK, I will play with this approach. At the same time I think it would be good if there was a safe way to create custom constructors.
              Even better if we could simply use object initializer (https://msdn.microsoft.com/en-us/library/bb384062.aspx) instead of creating multiple overloads of the constructors.
              Please consider it as a feature request. Are there any technical obstacles to implement this?

              Comment


                #8
                gregid,

                I am not sure on the obstacles, but I will forward to development.

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by bortz, 11-06-2023, 08:04 AM
                47 responses
                1,606 views
                0 likes
                Last Post aligator  
                Started by jaybedreamin, Today, 05:56 PM
                0 responses
                9 views
                0 likes
                Last Post jaybedreamin  
                Started by DJ888, 04-16-2024, 06:09 PM
                6 responses
                19 views
                0 likes
                Last Post DJ888
                by DJ888
                 
                Started by Jon17, Today, 04:33 PM
                0 responses
                6 views
                0 likes
                Last Post Jon17
                by Jon17
                 
                Started by Javierw.ok, Today, 04:12 PM
                0 responses
                15 views
                0 likes
                Last Post Javierw.ok  
                Working...
                X