What I'm trying to do is this;
1. Declare a new class 'clsLevel'.
2. On each bar instantiate an instance of clsLevel and populate it with some data.
3. Store each instance of 'clsLevel' into into a list object named 'Levels'.
4. Later refer to the list and the objects stored in it.
So I can get the necessary objects instantiated. However the 'Levels' list is instantiated when OnStartup() is called and then when I try to refer to the object in OnBarUpdate() using
Levels.Add(candLevel)
then the code fails with this message
'Object reference not set to an instance of an object'
and with that I'm stuck. Can you help me out here?
Thanks
Riskybiz
//Design the class namespace ClassDesign { public class clsLevel { public bool isInitialised; public DateTime startDateTime; public DateTime endDateTime; public int startBar; public int endBar; public double highVal; public double lowVal; public clsLevel() { isInitialised = true; } } } //Instantiate the List object once at the initialization of the indicator protected override void OnStartUp() { // Set any variables or logic you wish to do only once at start of your indicator/strategy List<clsLevel> Levels = new List<clsLevel>(); Levels.Capacity = 10; levelsInitiated = true; Print("Levels Initiated: " + levelsInitiated.ToString()); Print("Capacity " + Levels.Capacity.ToString()); } /// <summary> /// Called on each bar update event (incoming tick) /// </summary> protected override void OnBarUpdate() { // Use this method for calculating your indicator values. Assign a value to each // plot below by replacing 'Close[0]' with your own formula. candLevel = new clsLevel(); Print("Candidate Initiated: " + candLevel.isInitialised.ToString()); candLevel.startDateTime = Time[0]; candLevel.endDateTime = Time[0]; candLevel.startBar = CurrentBar; candLevel.endBar = CurrentBar; candLevel.highVal = High[0]; candLevel.lowVal = Low[0]; Print("StartBar " + candLevel.startBar.ToString());//check to test accessing the object Levels.Add(candLevel); Plot0.Set(High[0]);//just plot something to see that it calculated Plot1.Set(Low[0]);//just plot something to see that it calculated }
Comment