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 can I access TickSize (or equivalent) from strategy Parameter section?

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

    How can I access TickSize (or equivalent) from strategy Parameter section?

    Is there a way to access the tick size of the current instrument from within the parameter set{} section?

    #2
    Hello,

    Thank you for the question.

    Are you referring to a properties set?

    or

    Code:
    private double period;
    
    [Description("Period")]
    [GridCategory("Period")]
    public double Period
    {
    	get { return period; }
    	set { period = value; }
    }
    If so, yes you can use TickSize in the set for this, here is a simple example:

    Code:
    private double period;
    
    [Description("Period")]
    [GridCategory("Period")]
    public double Period
    {
    	get { return period + 1 * TickSize; }
    	set { period = value; }
    }
    This would return the value that was set, plus 1 tick.

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

    Comment


      #3
      The problem is in get_ticks_dollar_str(). The way it is right now causes an exception to be thrown. If I switch the comments so TickSize is not used all works just fine, except that I don't get the tick size associated with the instrument.

      Code:
      [Description("Enter ticks only.  Ticks and dollars will be displayed.")]
      [GridCategory("Parameters -- Order Handling")]
      [Gui.Design.DisplayName ("011      1st scale out -- ticks")]
      public string First_scale_out__ticks_str
      {
          get { return user__1st_scale_out__ticks_str; 	}
          set {
                      _1st_scale_out__ticks = get_ticks( value );
      			
                      if(  _1st_scale_out__ticks <= 100 )	 
      		        user__1st_scale_out__ticks_str   = get_ticks_dollar_str( _1st_scale_out__ticks );
      	   }
      }
      
      // Typical  arg = "10 ticks = $50.00"
      //-----------------------------------------------------------------------------------
      private int get_ticks( string str  )
      {
          char[] 	delimiters = { ' ' };
          string[] 	words 	= str.Split( delimiters );
      			
          return	Int32.Parse( words[ 0 ] );
      }
      		
      // Create tick dollar string to display in parameters
      //-----------------------------------------------------------------------------------
      private string get_ticks_dollar_str( int ticks )
      {
      //  double 	scale_dollars	= ticks * 5;			// This works
          double 	scale_dollars	= ticks * TickSize;		// This throws an exception
      				
          return ticks.ToString() + " ticks \t= " + scale_dollars.ToString("C");
      }

      Comment


        #4
        update:
        When trying to access TickSize in the way I illustrated I get a Message dialog when opening the strategy dialog.
        The message:
        Strategy 'MinjaTrader.Strategy.bcSr' could not be deserialized: There is an error in XML document (1.24059).

        Comment


          #5
          Hello,

          Thank you for providing this.

          I re created the variables for this and tried it, I am getting a few various errors in your get_ticks method.

          For this, to accurately figure out the reason I would need a sample for the input on this. it seems get_ticks takes a string input and parses the string. Can you provide an example of the string you are passing into this method?

          I will work on the get_ticks first, the property is trivial once the method is working correctly but for now it seems this is the cause.

          The message: 'MinjaTrader.Strategy.bcSr' could not be deserialized: is most likely caused by this property not currently working correctly, simple types like String and Int can be serialized but only if the Get/Set work for the property. You would get errors like this if you try to serialize a more complicate object like a color also.

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

          Comment


            #6
            All three of these work fine:
            ticks = get_ticks( "10 ticks = $50.00" );
            ticks = get_ticks( value );
            ticks = 10;

            When I replace TickSize with a literal or variable all works just fine. Functionally correct and no exceptions. The user enters a numerical tick count in the strategy parameter dialog box. Tick and dollar amount are then displayed.

            Your example is a little different in that your usage of TickSize is in get{} and mine is in set{}. Can't see why that would matter although it might.

            This snippet illustrates the issue. The only line of code that will cause the failure is the one with TickSize.

            [
            Code:
            Description("Enter ticks only.  Ticks and dollars will be displayed.")]
            [GridCategory("Parameters -- Order Handling")]
            [Gui.Design.DisplayName ("011      1st scale out -- ticks")]
            public string Ticks_str
            {
                get { return ticks_str; 	}
                set {
            		ticks      = 10;
            			
            //		ticks_str = ticks .ToString() + " ticks \t= " + (ticks * TickSize).ToString("C");  // This does not work
            		ticks_str = ticks .ToString() + " ticks \t= " + (ticks *            5).ToString("C");  // This works
            	}
            }

            Comment


              #7
              Hello,

              I am not sure I see exactly what was wrong in your existing example, I replaced what you had with a slightly different approach which adds some error handing in case you do not enter a number.

              I believe what you are trying to do would be the following:

              Code:
              private int ticks = 0;
              private string ticks_str = "";
              		
              [Description("Enter ticks only. Ticks and dollars will be displayed.")]
              [GridCategory("Parameters -- Order Handling")]
              [Gui.Design.DisplayName ("011 1st scale out -- ticks")]
              public string Ticks_str
              {
              	get { return ticks_str; 	}
              	set {	
              		ticks = 0;
              		int.TryParse(value, out ticks);
              		ticks_str = ticks.ToString() + " ticks \t= " + (ticks * TickSize).ToString("C");
              	}
              }
              In the properties, when you enter a int tick size, it will replace the user input with your string and show the value while the ticks variable is still set to the value the user had entered leaving it open to use in logic.

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

              Comment


                #8
                Not sure what is going on here, but when I replace my set{} with the example you provided I get the same error I get with my version. All I have to do is replace TickSize with a number and the exceptions go away and all works fine. This is true with your version and mine.

                If I use TickSize in get{} or set{} I get the exceptions.

                This is from the TickSize help:
                "NOTE: This property should NOT be accessed within the Initialize() method."
                I assume because it is not valid or initialized yet.

                The get{} set{} of parameters are certainly accessed before initialize(). So maybe what I am trying to do cannot be done with TickSize directly.

                Comment


                  #9
                  Hello,

                  Have you completely remove all instances of the script from the Control Center Strategies tab and any charts and then re applied it?

                  This could be a case where the changes have not yet been applied because the script was not completely removed and re added, this generally only happens when changes are made in Initialize.

                  I had no exceptions using this, if you still are after the above process, can you create a test script using the property and do a File -> Utilities -> Export NinjaScript so I can just import and run exactly what you are trying?

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

                  Comment


                    #10
                    The attached script illustrates what I am experiencing perfectly. Make sure you have your output window open and visible when loading script. If comment the line with TickSize and uncomment the line with the literal (5) all works fine.
                    Attached Files

                    Comment


                      #11
                      Hello,

                      Thank you for providing the example.

                      It looks like this is strategy specific, the example does work using when inheriting from indicator base but not strategy base. I believe this is due to the way the instance of the strategy is created vs the indicator when it is added.

                      Instead, you can use the following which works in both a strategy or an indicator:

                      Code:
                      private int ticks = 0;
                      private string ticks_str = "";
                      
                      [Description("Enter ticks only. Ticks and dollars will be displayed.")]
                      [GridCategory("Parameters -- Order Handling")]
                      [Gui.Design.DisplayName("011      1st scale out -- ticks")]
                      public string TestTicks
                      {
                      	get { return ticks_str; }
                      	set
                      	{
                      		int.TryParse(value, out ticks);
                      		ticks_str = ticks.ToString() + " ticks \t= " + (ticks * Instrument.MasterInstrument.TickSize).ToString("C");
                      	}
                      }
                      It looks like TickSize is not yet set causing the error, so instead you can use the Instrument which is loaded or:

                      Instrument.MasterInstrument.TickSize

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

                      Comment


                        #12
                        Before I started this thread I tried accessing Instrument.MasterInstrument. It did not work then and it does not work now.

                        There is a difference in error profiles from trying to access TickSize and trying to access anything from Instrument.MasterInstrument. When trying to access TickSize exceptions are thrown that appear in the output window and I get the deserialization dialog that I mentioned before. When trying to access anything from Instrument.MasterInstrument I get the deserialization dialog only. The calculation appears to be correct though.

                        The attached script includes your suggestions and illustrates the error. In order to experience the error, you must try to edit the strategy.
                        Attached Files

                        Comment


                          #13
                          Hello,

                          Yes this would not be able to be serialized due to it using Instrument before it will be available.

                          This can be ignored from serialization so the property would still work and allow the strategy its self to be serialized.

                          If this is simply a cosmetic item for the property, I would recommend instead just making this an int and display the value on screen using a DrawText with the Price. The int could be both serialized and used in logic that way, or manually as you had previously using a specified tick size.

                          If you do want to use this and not serialize it, you could add [XmlIgnore] to the tags on the property and it will not be serialized.

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

                          Comment


                            #14
                            If the property is not serialized its value will not be remembered. Is this correct?

                            Comment


                              #15
                              Hello,

                              Yes that is correct, that 1 specific property would not be remembered but the remainder of the properties and script would be.

                              Unfortunately because of when this property is accessed I don't believe there is a work around to be able to serialize specifically this.

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

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Option Whisperer, Today, 09:55 AM
                              1 response
                              11 views
                              0 likes
                              Last Post bltdavid  
                              Started by port119, Today, 02:43 PM
                              0 responses
                              1 view
                              0 likes
                              Last Post port119
                              by port119
                               
                              Started by Philippe56140, Today, 02:35 PM
                              0 responses
                              3 views
                              0 likes
                              Last Post Philippe56140  
                              Started by 00nevest, Today, 02:27 PM
                              0 responses
                              2 views
                              0 likes
                              Last Post 00nevest  
                              Started by Jonafare, 12-06-2012, 03:48 PM
                              5 responses
                              3,987 views
                              0 likes
                              Last Post rene69851  
                              Working...
                              X