NinjaTrader Support Forum  

Go Back   NinjaTrader Support Forum > NinjaScript Development Support > Indicator Development

Indicator Development Support for the development of custom indicators using NinjaScript.

Reply
 
Thread Tools Display Modes
Old 07-06-2012, 12:49 PM   #1
Taddypole
Senior Member
 
Join Date: Mar 2007
Location: , ,
Posts: 240
Thanks: 1
Thanked 2 times in 2 posts
Default 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 Images
File Type: png UpperBand LowerBand Indicator.png (32.9 KB, 15 views)
Attached Files
File Type: cs RemkoSupremeChannelOne.cs (8.5 KB, 4 views)
Taddypole is offline  
Reply With Quote
Old 07-06-2012, 01:11 PM   #2
NinjaTrader_RyanM
NinjaTrader Customer Service
 
NinjaTrader_RyanM's Avatar
 
Join Date: Sep 2009
Location: Denver, CO
Posts: 8,117
Thanks: 249
Thanked 418 times in 415 posts
Default

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
File Type: zip RemkoSupremeChannelOne2.zip (2.3 KB, 6 views)
NinjaTrader_RyanM is offline  
Reply With Quote
Old 07-06-2012, 02:05 PM   #3
Taddypole
Senior Member
 
Join Date: Mar 2007
Location: , ,
Posts: 240
Thanks: 1
Thanked 2 times in 2 posts
Default

Thanks Ryan,

That did the trick. Can you explain why dataseries values are not persistent?
Taddypole is offline  
Reply With Quote
Old 07-06-2012, 02:11 PM   #4
NinjaTrader_RyanM
NinjaTrader Customer Service
 
NinjaTrader_RyanM's Avatar
 
Join Date: Sep 2009
Location: Denver, CO
Posts: 8,117
Thanks: 249
Thanked 418 times in 415 posts
Default

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.
NinjaTrader_RyanM is offline  
Reply With Quote
Old 07-31-2012, 09:32 AM   #5
j2496i
Junior Member
 
Join Date: Dec 2011
Posts: 15
Thanks: 2
Thanked 2 times in 2 posts
Default

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 at 09:38 AM.
j2496i is offline  
Reply With Quote
The following user says thank you to j2496i for this post:
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Creating Persistent Indicator Eubie Indicator Development 5 10-07-2011 09:12 AM
Persistent Forex position astrolobe Version 7 Beta General Questions & Bug Reports 5 11-01-2010 05:57 AM
Is there a way to draw a persistent marker on a bar? tradefaz General Programming 1 09-18-2008 08:17 AM
Is the Zen-fire Connection Persistent? mgbloomfield Connecting 2 06-02-2008 11:50 AM
Persistent Orders That Do Not Require Renewal whitmark Suggestions And Feedback 1 08-02-2007 06:17 AM


All times are GMT -6. The time now is 02:57 PM.