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
multidimensional array - initialize and declare only once
Collapse
X
-
multidimensional array - initialize and declare only once
I need to use a multidimensional array to store certain values and calculations that are done ONLY ONCE at beginning, when the automated strategy is launched. The challenge is how to initialize and declare the array once without having to repeat it each iteration of the "protected override void OnBarUpdate()" loop. Currently, my strategy does it in every loop through. Since I have "CalculateOnBarClose = false;" that is a lot of extra horsepower wasted. Kindly advise what I should do. Thank you. -
Hello wolfgang239,
Thank you for your post.
If you have something that only needs to be done once on start up you can use the OnStartUp() method: http://www.ninjatrader.com/support/h.../onstartup.htm
-
Thank you Patrick, but that will not work because you cannot initialize and declare an Array in the OnStartUp() and have it accessible in the rest of the program. The error message is:
The name 'theArrayName' does not exist in the current context.
This error message will be displayed corresponding to a line in the body of loop
"OnBarUpdate()"
Comment
-
-
The following snippet shows that you cannot initialize an Array in OnStartUp() and have it accessible in the main section of the program:
#region Using declarations
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Xml.Serialization;
using NinjaTrader.Cbi;
using NinjaTrader.Data;
using NinjaTrader.Indicator;
using NinjaTrader.Gui.Chart;
using NinjaTrader.Strategy;
#endregion
namespace NinjaTrader.Strategy
{
[Description("")]
public class TestArray2 : Strategy
{
#region Variables
#endregion
protected override void Initialize()
{ CalculateOnBarClose = true;
}
protected override void OnStartUp()
{ ClearOutputWindow();
int[] theArrayFour = new int[5] { 555, 666, 777, 888, 999 };
for (int i = 0; i < theArrayFour.Length; i++)
{
Print("theArrayFour(" + i + ") " + theArrayFour[i]); //WORKS FINE!
}
}
protected override void OnBarUpdate()
{
// the following produces error: The name 'theArrayFour' does not exist in the current context
// for (int i = 0; i < theArrayFour.Length; i++)
// {
// Print("Inside the OnBarUpdate() loop - theArrayFour(" + i + ") " + theArrayFour[i]);
// }
}
}
}
Comment
-
Wolfgang,
Please declare the new array in the variables section
Code:#region variables int[] myArray = new int[5]; // simple array int[ , , , ] my multidimArray = new int [10, 10, 10, 10]; /four-dimensional array #endregion
Alternatively you can also use lists and add values to the list in OnStartUp() or OnBarUpdate() as needed.
Comment
-
Brilliant! Thank you Harry. Works great.
For anyone that may be having a similar problem, here is a simple snippet that shows Harry's solution. You can access the array in the main loop but it needs to be declared in the Variables region and initialized in the OnStartUp()
#region Using declarations
using System;
using System.ComponentModel;
using NinjaTrader.Cbi;
using NinjaTrader.Strategy;
#endregion
namespace NinjaTrader.Strategy
{
[Description("")]
public class TestArray4 : Strategy
{
#region Variables
int[,] myMultidimArray = new int [10,4]; //two-dimensional array
#endregion
protected override void Initialize()
{
CalculateOnBarClose = true;
}
protected override void OnStartUp()
{ ClearOutputWindow();
for (int i = 0; i < myMultidimArray.GetLength(0); i++)
{
myMultidimArray [i,0] = 100;
myMultidimArray [i,1] = 200;
myMultidimArray [i,2] = 300;
myMultidimArray [i,3] = 400;
Print("INITIALIZING index: " + i + "\t" + myMultidimArray[i,0] + "\t" + myMultidimArray[i,1] + "\t" + myMultidimArray[i,2] + "\t" + myMultidimArray[i,3]);
}
Print("finished INITIALIZING");
Print("");
for (int i = 0; i < myMultidimArray.GetLength(0); i++)
{
Print("in OnStartUp section index: " + i + "\t" + myMultidimArray[i,0] + "\t" + myMultidimArray[i,1] + "\t" + myMultidimArray[i,2] + "\t" + myMultidimArray[i,3]);
}
Print("finished OnStartUp section");
Print("");
}
protected override void OnBarUpdate()
{
for (int i = 0; i < myMultidimArray.GetLength(0); i++)
{
myMultidimArray[i,0]++;
myMultidimArray[i,1]++;
myMultidimArray[i,2]++;
myMultidimArray[i,3]++;
Print("in OnBarUpdate() loop: " + i + myMultidimArray[i,0] + "\t" + myMultidimArray[i,1] + "\t" + myMultidimArray[i,2] + "\t" + myMultidimArray[i,3]);
}
Print("");Print("");
}
}
}
Comment
-
Originally posted by wolfgang239 View PostThe following snippet shows that you cannot initialize an Array in OnStartUp() and have it accessible in the main section of the program:
#region Using declarations
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Xml.Serialization;
using NinjaTrader.Cbi;
using NinjaTrader.Data;
using NinjaTrader.Indicator;
using NinjaTrader.Gui.Chart;
using NinjaTrader.Strategy;
#endregion
namespace NinjaTrader.Strategy
{
[Description("")]
public class TestArray2 : Strategy
{
#region Variables
#endregion
protected override void Initialize()
{ CalculateOnBarClose = true;
}
protected override void OnStartUp()
{ ClearOutputWindow();
int[] theArrayFour = new int[5] { 555, 666, 777, 888, 999 };
for (int i = 0; i < theArrayFour.Length; i++)
{
Print("theArrayFour(" + i + ") " + theArrayFour[i]); //WORKS FINE!
}
}
protected override void OnBarUpdate()
{
// the following produces error: The name 'theArrayFour' does not exist in the current context
// for (int i = 0; i < theArrayFour.Length; i++)
// {
// Print("Inside the OnBarUpdate() loop - theArrayFour(" + i + ") " + theArrayFour[i]);
// }
}
}
}
Comment
Latest Posts
Collapse
Topics | Statistics | Last Post | ||
---|---|---|---|---|
Started by arvidvanstaey, Today, 02:19 PM
|
2 responses
6 views
0 likes
|
Last Post Today, 03:12 PM | ||
Started by jordanq2, Today, 03:10 PM
|
0 responses
2 views
0 likes
|
Last Post
by jordanq2
Today, 03:10 PM
|
||
Started by traderqz, Today, 12:06 AM
|
10 responses
18 views
0 likes
|
Last Post
by traderqz
Today, 02:49 PM
|
||
Started by algospoke, 04-17-2024, 06:40 PM
|
5 responses
46 views
0 likes
|
Last Post Today, 02:36 PM | ||
Started by mmckinnm, Today, 01:34 PM
|
3 responses
5 views
0 likes
|
Last Post Today, 02:12 PM |
Comment