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 make a variable persistent

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

    How to make a variable persistent

    I’m trying to create an indicator that calculates a dynamic upper and lower band which should move with price.

    I set an initial value of the UpperBand and LowerBand and do a check to see if that value is breached. The problem I’m having is that as soon as a bar is completed, the initial values for UpperBand and LowerBand go to zero. How do I make these values retain their previous value?

    The chart I’ve attached shows a 30 sec bar chart. The horizontal lines show the UpperBand and LowerBand. If you run the code, you'll see that the values go to zero.

    Anyone know how I can solve this?

    Thanks,
    Taddypole…
    Attached Files

    #2
    Hi Taddypole,

    You may want to use double here instead of DataSeries for some of these values. DataSeries store a value for every bar, so are most useful when you want index past values or use as input for an indicator. You are not doing that here, so I replaced DataSeries with doubles and ran for a few minutes -- never saw Upperband and LowerBand hit value of zero with this attached version.
    Attached Files
    Ryan M.NinjaTrader Customer Service

    Comment


      #3
      Thanks Ryan,

      That did the trick. Can you explain why dataseries values are not persistent?

      Comment


        #4
        It's a different structure -- you need to set it for every bar. A double value will keep the last value you assigned to it.
        Ryan M.NinjaTrader Customer Service

        Comment


          #5
          TaddyPole - just to clarify further, if you are interested...

          A DataSeries is a type of Array... this means it can store many values, *all of which remain persistent*, until they are deleted, modified, or the array reaches it's maximum size. Generally in c# .NET, an array has a maximum size of 2GB.

          An array stores it's values like this: [first value] [1] [2] [3] [4] [5] [6] [7] [next index]... [last value]

          You can reference any value in an array via it's index, like this: myArray[0] (this will access the first value)

          A NinjaTrader DataSeries is a 'First In First Out (FIFO)' array, meaning that the latest value will have the index [0] and the oldest value will have the index [size-1]...

          You can read more here: http://msdn.microsoft.com/en-us/libr...(v=vs.71).aspx

          If using more than one Bar Period, or Period Type for your charts / indicators, you need to be aware that NinjaTrader DataSeries can also be synchronized with specific members of BarsArray[], though this is beyone the scope of this post.

          Hopefully that's of some help.

          PS - simply put, to set the 'latest' value in the array, use:

          myArray.Set( myValue );

          To retrieve the latest value, and assign it to a variable, use something along the lines of:

          myVariable = myArray[ 0 ];

          J
          Last edited by j2496i; 07-31-2012, 09:38 AM.

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by Haiasi, 04-25-2024, 06:53 PM
          2 responses
          16 views
          0 likes
          Last Post Massinisa  
          Started by Creamers, Today, 05:32 AM
          0 responses
          5 views
          0 likes
          Last Post Creamers  
          Started by Segwin, 05-07-2018, 02:15 PM
          12 responses
          1,786 views
          0 likes
          Last Post Leafcutter  
          Started by poplagelu, Today, 05:00 AM
          0 responses
          3 views
          0 likes
          Last Post poplagelu  
          Started by fx.practic, 10-15-2013, 12:53 AM
          5 responses
          5,407 views
          0 likes
          Last Post Bidder
          by Bidder
           
          Working...
          X