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 samish18, 04-17-2024, 08:57 AM
          17 responses
          64 views
          0 likes
          Last Post NinjaTrader_BrandonH  
          Started by rocketman7, Today, 02:12 AM
          2 responses
          16 views
          0 likes
          Last Post rocketman7  
          Started by briansaul, Today, 05:31 AM
          1 response
          12 views
          0 likes
          Last Post NinjaTrader_Jesse  
          Started by PaulMohn, Today, 03:49 AM
          1 response
          12 views
          0 likes
          Last Post NinjaTrader_BrandonH  
          Started by frslvr, 04-11-2024, 07:26 AM
          6 responses
          106 views
          1 like
          Last Post NinjaTrader_BrandonH  
          Working...
          X