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!
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
NinjaTrader
Setting an internal Array as variable
Collapse
X
-
Hello,
Initialize is a NinjaTrader method meaning that it is not absolutely where you have to initialize variables. Initialize is called when the script is starting, this is called when you open a indicator menu or when you add it to a chart.
What it means to Initialize a variable is simply to create the variable so it can be used later in the script, this can be done in most of the scopes in the script.
if you create a variable inside of the Initialize method, that variable can only be used inside of Initialize. If you instead create the variable inside of the class, it can be used by Initialize if needed, and also OnBarUpdate.
For ArrayLists, this is similar to Array in the sense that it is not commonly used any longer, it is similar to array but ultimately List replaces both of these.
You can find more information on ArrayList specifically here: http://www.dotnetperls.com/arraylist
this will take base types like string or int, more advanced objects or NinjaScript specific objects may need casted to work with this, another reason Lists are a better alternative.
I look forward to being of further assistance.
Leave a comment:
-
Thanks a lot Calonious for your explanation, now I understand better the right use of it
Leave a comment:
-
Originally posted by pstrusi View PostOk, I've made a progress, I wasn't using correctly "using System.Collections;", now it's done and at least it's compiling, now I have 2 questions:
1. Why did you say that I must not use that List within Initialize, when all of the rest of variables are always "initialized" for later use OnBarUpdate?
2. Can I assign directly any variable type to this array variable within a loop in a reliable way?
Thanks a lot, looking forward and hopefully not to bother anymore :-)
2. Yes, but I wouldn't recommend that. You generally want to have one Array per type that you are building for. This help prevents mismatch types down the road in your script
Leave a comment:
-
Ok, I've made a progress, I wasn't using correctly "using System.Collections;", now it's done and at least it's compiling, now I have 2 questions:
1. Why did you say that I must not use that Listarray variable "Balance1" within Initialize, when all of the rest of variables are always "initialized" for later use OnBarUpdate?
As you say well, that array variable "Balance1" must be used within OnBarUpdate, thus it have to be properly declared. Having in mind that any variable that I've seen in different scripts are always initialized, now I have a big confusion.
2. Can I assign directly any variable type to this array variable within a loop in a reliable way?
Thanks a lot, looking forward and hopefully not to bother anymore :-)Last edited by pstrusi; 04-16-2015, 12:56 PM.
Leave a comment:
-
Jesse, I appreciate a lot all of your help but I have still some issues.
1. I put "ArrayList Balance1 = new ArrayList();" out of Initialize to put it before like you suggest:
public class TestStrat : Strategy
{
ArrayList Balance1 = new ArrayList();
protected override void Initialize()
{
}
}
Leave a comment:
-
Hello,
you have the correct syntax you just need to move it into the correct scope.
If you are looking at the script I believe you would have this based on the last message:
Code:public class TestStrat : Strategy { protected override void Initialize() { ArrayList Balance1 = new ArrayList(); } }
Code:[B] public class TestStrat : Strategy[/B] { ArrayList Balance1 = new ArrayList(); protected override void Initialize() { } }
If you declare a variable inside of the Initialize method, it can only be used inside of Initialize.
Also don't forget the using statement if you have not already added it, ArrayList is found in System.Collections, or you would need to add to the top of the script:
Code:using System.Collections;
Leave a comment:
-
Yes, Jesse, one little thing more:
I know how to initialize an Arraylist, but I don't know how to declare it as variable, is there any special place or "#region Variables" is ok?
#region Variables
?????????
Initialize:
ArrayList Balance1 = new ArrayList();
Leave a comment:
-
Hello,
Both should be nearly identical in efficiency as far as what you would be able to see.
Lists in C# have basically taken the place of Array because of their multiple uses along with being able to use LINQ to query the list much like it is a database. This means you can do Find, Contains and other search operations on the list removing the need for looping, in this sense they are more efficient.
While arrays still serve a purpose and will work, you need to know the exact index count before hand and are extremely specific on their use.
In Object oriented programming, lists can be a list of specific objects like a string, int, double etc. or they can also be a list of a class meaning that it can have sub objects.
Also lists do not need a index defined, you can add as many entries as you need or remove specific entries. You can also add to a specific index in the middle of the list rather than always appending to the end of the array.
Lists are by far more flexible, if you plan on working with C# or NinjaScript i would highly recommend learning lists as they are much more flexible and absolutely necessary for higher level programming when you start working with objects, IOrders for example.
I look forward to being of further assistance.
Leave a comment:
-
Jesse, a technical question:
List Types variables performance faster than a typical array variable?
I´ll always know the length of the array, so I can use both of them, but I have still that doubt
Thanks
Leave a comment:
-
Hello,
Thank you for the question.
NinjaTrader does not include this type of information in the help guide because this is just standard C# syntax, what you have shown would be a double array which would be a valid type in C# and can be used in NinjaScript.
Arrays are good for things that you know the number of, or the amount of index locations to create.
Another type would be the List type, this does not need a set index and performs very similar to a DataSeries except this can have any number of items and is not synced with the Bars on the chart.
I would recommend reviewing this page as it has a lot of great information. http://www.techotopia.com/index.php/C_Sharp_Essentials
I am unsure on your C# experience, but this page has some information for everyone. If you are a new C# coder, I would recommend reading sections 5 - 9 and then 14 - 15, otherwise if you are already up to speed with the lower level syntax, go ahead and check out sections 14 and 15. These would be the best methods to store lists or arrays of data in C#, I highly recommend using Lists as you do not need to specify a length before hand.
I look forward to being of further assistance.
Leave a comment:
-
Setting an internal Array as variable
Hi Ninjas,
I have the need to set up an internal "Array" as variable to store certain Values. But it's not the typical DataSeries Class which is always linked exactly to the numbers of Bars. What I need it's variable to store internal calculations to access later in other loops, so since I can't find anything regards to this in the NT Help Guide, I was search in the NET and I find this, so I'd like to know if this could work correctly ( not like NT DataSeries ) or either there's a better way to do it.
First declaring the variable:
double[] balance;
Second Initialize the Array:
double[] balance = new double[10];
Hoping to get any hint here
ThanksTags: None
Latest Posts
Collapse
Topics | Statistics | Last Post | ||
---|---|---|---|---|
Started by briansaul, Today, 05:31 AM
|
0 responses
2 views
0 likes
|
Last Post
by briansaul
Today, 05:31 AM
|
||
Started by fwendolynlpxz, Today, 05:19 AM
|
0 responses
4 views
0 likes
|
Last Post Today, 05:19 AM | ||
Started by traderqz, Yesterday, 12:06 AM
|
11 responses
28 views
0 likes
|
Last Post Today, 05:11 AM | ||
Started by PaulMohn, Today, 03:49 AM
|
0 responses
8 views
0 likes
|
Last Post
by PaulMohn
Today, 03:49 AM
|
||
Started by inanazsocial, Today, 01:15 AM
|
1 response
10 views
0 likes
|
Last Post Today, 02:18 AM |
Leave a comment: