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

Reset to default values private variables with public access modifiers

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

    Reset to default values private variables with public access modifiers

    Hello,

    Is it possible to reset to default values private variables with public access modifier set in #region properties, whose values have been changed throughout the code?

    I mean if for instance a private int with a public access modifier with original value of 2 set in State.SetDefaults. If later in OnBarUpdate this value is changed to 3. Is there a way to make it reset to default (2), or another variable needs to be created to store the original value to then restore it?

    Also, if the above variable has been changed throughout the code OnBarUpdate to 3 and it calculates OnBarClose, when a new bar elapses, will the value of this private variable with public access be again 2 (the default value) or it will remain 3 (so it's not affected by its default value set on State.SetDefaults).

    Thanks in advance!

    #2
    Hello roblogic,

    Thank you for the question.

    The way the question is worded is a little confusion so please correct me if I have misunderstood here.


    Is it possible to reset to default values private variables with public access modifier set in #region properties, whose values have been changed throughout the code?
    From what I can tell you are setting the value of a variable in OnBarUpdate, but want it to go back to the default you set in State.SetDefaults, is this correct?

    I mean if for instance a private int with a public access modifier with original value of 2 set in State.SetDefaults. If later in OnBarUpdate this value is changed to 3. Is there a way to make it reset to default (2), or another variable needs to be created to store the original value to then restore it?
    The public/private modifier really wouldn't matter here as you are resetting the value of the variable in runtime. That is what the new value will be just like when it was set in State.SetDefaults in runtime. Another variable would likely be the easiest solution if you need to retain the original value. For example the SMA indicator has a Period input which does not get reset in code, it is just used. If you have logic which uses a input it should also be the same as the SMA and only use the input but not directly change its value.


    Also, if the above variable has been changed throughout the code OnBarUpdate to 3 and it calculates OnBarClose, when a new bar elapses, will the value of this private variable with public access be again 2 (the default value) or it will remain 3 (so it's not affected by its default value set on State.SetDefaults).
    This is a good use case for Prints to further your understanding of how your script is called, I will detail a test you can do below. The quick answer would be, if you are changing the variabe in OnBarUpdate that value would be retained as you mentioned this is a public/private variable being used which exists at the class level. This would persist across multiple OnBarUpdate calls and because SetDefaults is called before OnBarUpdate the value would never reset back to 2.

    In your script, you can add a print to both OnStateChange and OnBarUpdate like the following to better understand the order your script is executed in:

    Code:
    protected override void OnStateChange()
    {
        Print(State);
    }
    
    protected override void OnBarUpdate()
    {
        Print(State + " " + CurrentBar);
    }

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

    Comment


      #3
      Thanks Jesse for the comprehensive information!

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by jaybedreamin, Today, 05:56 PM
      0 responses
      3 views
      0 likes
      Last Post jaybedreamin  
      Started by DJ888, 04-16-2024, 06:09 PM
      6 responses
      18 views
      0 likes
      Last Post DJ888
      by DJ888
       
      Started by Jon17, Today, 04:33 PM
      0 responses
      1 view
      0 likes
      Last Post Jon17
      by Jon17
       
      Started by Javierw.ok, Today, 04:12 PM
      0 responses
      6 views
      0 likes
      Last Post Javierw.ok  
      Started by timmbbo, Today, 08:59 AM
      2 responses
      10 views
      0 likes
      Last Post bltdavid  
      Working...
      X