Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

How to get Input Variable to Class Scope

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

    How to get Input Variable to Class Scope

    The code below displays this in the indicator Parameters window (i.e. the indicator wizard when right clicking the chart):

    myInt == 4

    myString == 0


    How to get it to display both as:

    myInt == 4

    myString == 4



    Code:
    namespace NinjaTrader.NinjaScript.Indicators
    {
    public class myind : Indicator
    {​
    
    private int myInt;
    ​

    Code:
    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {​
    
    myInt = 4;​
    
    myString = myInt.ToString();
    ​

    Code:
    protected void CreateWPFControls()
    {​
    ...
    switch(i)
    {
    case 0:
    MyStrings = myInt.ToString();
    
    
    ...
    switch(i)
    {
    case 0:
    Myints = myInt.ToString();​
    ​

    Code:
    Properties
    
    [NinjaScriptProperty]
    [Range(0, int.MaxValue)]
    [Display(Name="MyInt", Order=0, GroupName="Parameters")]
    public int MyInt
    {
    get { return myInt; }
    
    set { myInt= value; }
    }​
    
    
    [NinjaScriptProperty]
    [Display(Name="myString", Order=1, GroupName="Parameters")]
    public string myString
    { get; set; }​

    #2
    Hello PaulMohn,

    To set a default for a input you would need to do that in State.SetDefaults like the code you posted.

    Based on the code I don't see a problem. You should be able to set the string to a default value from SetDefaults. You may want to try using a Print to make sure the value you are assigning is valid.

    Comment


      #3
      Originally posted by NinjaTrader_Jesse View Post
      Hello PaulMohn,

      To set a default for a input you would need to do that in State.SetDefaults like the code you posted.

      Based on the code I don't see a problem. You should be able to set the string to a default value from SetDefaults. You may want to try using a Print to make sure the value you are assigning is valid.
      Just tested with this minimal code and it works, but not on the production code with the 2 switches.
      Is there something the 2 switches prevents?

      Code:
      #region Using declarations
      using System;
      using System.Collections.Generic;
      using System.ComponentModel;
      using System.ComponentModel.DataAnnotations;
      using System.Linq;
      using System.Text;
      using System.Threading.Tasks;
      using System.Windows;
      using System.Windows.Input;
      using System.Windows.Media;
      using System.Xml.Serialization;
      using NinjaTrader.Cbi;
      using NinjaTrader.Gui;
      using NinjaTrader.Gui.Chart;
      using NinjaTrader.Gui.SuperDom;
      using NinjaTrader.Gui.Tools;
      using NinjaTrader.Data;
      using NinjaTrader.NinjaScript;
      using NinjaTrader.Core.FloatingPoint;
      using NinjaTrader.NinjaScript.DrawingTools;
      #endregion
      
      //This namespace holds Indicators in this folder and is required. Do not change it.
      namespace NinjaTrader.NinjaScript.Indicators
      {
          public class classscopevariableuserinput : Indicator
          {
              private int myInt;
              
              protected override void OnStateChange()
              {
                  if (State == State.SetDefaults)
                  {
                      Description                                    = @"Enter the description for your new custom Indicator here.";
                      Name                                        = "classscopevariableuserinput";
                      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;
      
                      myInt = 4;​
      
                      myString = myInt.ToString();
                  }
                  else if (State == State.Configure)
                  {
                  }
              }
      
              protected override void OnBarUpdate()
              {
                  //Add your custom indicator logic here.
              }
              
              #region Properties
      
              [NinjaScriptProperty]
              [Range(0, int.MaxValue)]
              [Display(Name="MyInt", Order=0, GroupName="Parameters")]
              public int MyInt
              {
              get { return myInt; }
      
              set { myInt= value; }
              }​
      
      
              [NinjaScriptProperty]
              [Display(Name="myString", Order=1, GroupName="Parameters")]
              public string myString
              { get; set; }​
              
              #endregion
          }
      }
      
      #region NinjaScript generated code. Neither change nor remove.
      
      namespace NinjaTrader.NinjaScript.Indicators
      {
          public partial class Indicator : NinjaTrader.Gui.NinjaScript.IndicatorRenderBase
          {
              private classscopevariableuserinput[] cacheclassscopevariableuserinput;
              public classscopevariableuserinput classscopevariableuserinput(int myInt, string myString)
              {
                  return classscopevariableuserinput(Input, myInt, myString);
              }
      
              public classscopevariableuserinput classscopevariableuserinput(ISeries<double> input, int myInt, string myString)
              {
                  if (cacheclassscopevariableuserinput != null)
                      for (int idx = 0; idx < cacheclassscopevariableuserinput.Length; idx++)
                          if (cacheclassscopevariableuserinput[idx] != null && cacheclassscopevariableuserinput[idx].MyInt == myInt && cacheclassscopevariableuserinput[idx].myString == myString && cacheclassscopevariableuserinput[idx].EqualsInput(input))
                              return cacheclassscopevariableuserinput[idx];
                  return CacheIndicator<classscopevariableuserinput>(new classscopevariableuserinput(){ MyInt = myInt, myString = myString }, input, ref cacheclassscopevariableuserinput);
              }
          }
      }
      
      namespace NinjaTrader.NinjaScript.MarketAnalyzerColumns
      {
          public partial class MarketAnalyzerColumn : MarketAnalyzerColumnBase
          {
              public Indicators.classscopevariableuserinput classscopevariableuserinput(int myInt, string myString)
              {
                  return indicator.classscopevariableuserinput(Input, myInt, myString);
              }
      
              public Indicators.classscopevariableuserinput classscopevariableuserinput(ISeries<double> input , int myInt, string myString)
              {
                  return indicator.classscopevariableuserinput(input, myInt, myString);
              }
          }
      }
      
      namespace NinjaTrader.NinjaScript.Strategies
      {
          public partial class Strategy : NinjaTrader.Gui.NinjaScript.StrategyRenderBase
          {
              public Indicators.classscopevariableuserinput classscopevariableuserinput(int myInt, string myString)
              {
                  return indicator.classscopevariableuserinput(Input, myInt, myString);
              }
      
              public Indicators.classscopevariableuserinput classscopevariableuserinput(ISeries<double> input , int myInt, string myString)
              {
                  return indicator.classscopevariableuserinput(input, myInt, myString);
              }
          }
      }
      
      #endregion

      Comment


        #4
        Hello PaulMohn,

        If that works but your other code does not that means the other code has a problem with whatever you are doing in that script. You would need to debug your other code to make sure a valid value is being passed as a default. If you have a default template you would also need to remove that if you previously saved a default with other values.

        Comment


          #5
          Seems fixed this way:

          The State.SetDefaults variables order matters (the int variable must come before the string)

          This does not work / returns

          myInt == 4

          myString == 0​


          protected override void OnStateChange()
          {
          if (State == State.SetDefaults)
          {​

          myString = myInt.ToString();​​​

          myInt = 4;​


          This works / returns

          myInt == 4

          myString == 4​​

          protected override void OnStateChange()
          {
          if (State == State.SetDefaults)
          {​

          myInt = 4;​

          myString = myInt.ToString();​

          Comment


            #6
            Hello PaulMohn,

            Unfortunately I couldn't say what is happening in your specific script. The code you provided now does not make sense, you need to assign a value to a variable before it can be read. The default value for an int is 0 so that should result in a 0 value unless you have a value saved in a default template.

            myint will start at 0
            Code is executed line by line top to bottom

            Comment


              #7
              The script is working with the given order.

              Attached Files

              Comment


                #8
                Hello PaulMohn,

                That is not the same code that you originally asked about and is a full script so I wouldn't be able to comment on that. The code you previously provided is in the wrong order, myInt would need to be assigned a value before you could convert it to a string that represents its value. You can learn about how C# executes code in external C# learning resources if that would be helpful to you.

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                0 responses
                608 views
                0 likes
                Last Post Geovanny Suaza  
                Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                0 responses
                355 views
                1 like
                Last Post Geovanny Suaza  
                Started by Mindset, 02-09-2026, 11:44 AM
                0 responses
                105 views
                0 likes
                Last Post Mindset
                by Mindset
                 
                Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                0 responses
                560 views
                1 like
                Last Post Geovanny Suaza  
                Started by RFrosty, 01-28-2026, 06:49 PM
                0 responses
                561 views
                1 like
                Last Post RFrosty
                by RFrosty
                 
                Working...
                X