question

FranKDuc-4126 avatar image
0 Votes"
FranKDuc-4126 asked FranKDuc-4126 commented

How to use get set accessor with a list?

Hello,

I am trying to use the result of my list in another class.

  public class NEWSDBA : Indicator
     {
 //should i declare private my listm here??
    
 protected override void OnBarUpdate()
         {
     
              foreach (var item in listm)
  {
  //use the values inside the listm declared in 
                                        //protected override void OnRender
  }
         }
    
    
 protected override void OnRender(ChartControl chartControl, ChartScale chartScale)
         {
    
 var listm = new List<double>();
    
 // use values of listm in protected override void OnBarUpdate()
    
    
         }
    
 }
dotnet-csharp
· 4
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

How to use get set accessor with a list? I am trying to use the result of my list in another class.

The standard programming pattern is passing arguments to a constructor or method input parameters.

Constructor Example

 public class NEWSDBA : Indicator
 {
     private readonly List<double> listm;   
     public NEWSDBA(List<double> list)
     {
         listm = list;
     }
 }

0 Votes 0 ·

Error:

Indicator 'NEWSDBA': Error on calling 'OnRender' method on bar 1975: Object reference not set to an instance of an object.

0 Votes 0 ·

Use the debugger to figure out what's null. Check if you passed a null List<double> to the constructor. Calling the parameterless constructor will certainly cause a null List<double>. The null can also be from unrelated code that we cannot see.

Keep in mind, we can only see what YOU share on this forum. IMHO, responding with the most common programming error on the planet is not a good way to get help. It gives the appearance that you are not trying.

0 Votes 0 ·

@FranKDuc-4126
Obviously, you have modified the code, so please show the current details: this class and the code that uses this class.
Only then can we reproduce your error and then try to solve it

0 Votes 0 ·

1 Answer

FranKDuc-4126 avatar image
0 Votes"
FranKDuc-4126 answered FranKDuc-4126 commented

Timon,

The code is too long. What i can say is if i declare or initialize listm private it will return a different answer.

protected override void OnBarUpdate() and protected override void OnRender are classes coming from the software ninjatrader, charting software.

I want to use AddPlot method to create a line in the chart from the values in listm but AddPlot method will only work in OnBarUpdate().

According to the tech at NT :

A value would need to be assigned to the plot in OnBarUpdate(), then you would call base.OnRender() in OnRender() to ensure Plots are rendered along with custom render logic.

 protected override void OnStateChange()
         {
             if (State == State.SetDefaults)
             {
                 Description = @"new version of SDBA";
                 Name = "NEWSDBA";
                 Calculate = Calculate.OnEachTick;
                 IsOverlay = true;
                 DisplayInDataBox = true;
                 DrawOnPricePanel = true;
                 DrawHorizontalGridLines = true;
                 DrawVerticalGridLines = true;
                 PaintPriceMarkers = true;
                 ScaleJustification = NinjaTrader.Gui.Chart.ScaleJustification.Right;
                 //Disable this property if your indicator requires custom values that cumulate with each new market data event. 
                 //See Help Guide for additional information.
                 IsSuspendedWhileInactive = true;
                 TBPrice                                         = 0;
                 CheckV                                      = true;
                 Volume                                      = 0;
                 AddPlot(Brushes.Yellow, "NEWSDBA");
    
             }
         }
    
         protected override void OnBarUpdate()
         {
               
    
    
         }
    
            
        
    
         protected override void OnRender(ChartControl chartControl, ChartScale chartScale)
         {
              
    
           
             //CMA minute
             var lists = new List<double>();
             var listv = new List<double>();
             double avg = 0;
             double closePrice = 0;
             double volumes = 0;
             double sum1 = 0;
             double sum2 = 0;
         double fibo2 = 0;
                
                
             for (int barIndex = foundIndex; barIndex <= ChartBars.ToIndex; barIndex++)
             {                
                 closePrice = Bars.GetClose(barIndex);
                 volumes = Bars.GetVolume(barIndex);
                    
                 if (CheckV)
                 {
                 avg = Volume;
                 }
                 else 
                 {
                 avg = sum2 / RfoundI;
                 }
                    
                   
                    
                 double clovol = closePrice * volumes;
    
    
                 sum1 += clovol;
                 sum2 += volumes;
    
                 cma = sum1 / sum2;
    
                    
         lists.Add(closePrice);
                 listv.Add(volumes);
                    
             }
                
                
                
                
                
                
             var listm = new List<double>();
             listm.Add(Close.GetValueAt(CurrentBar));
    
 }
 }



If you can make sense of this be my guess.

Thank you

· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

As written, the listm List<double> is a collection with one item. That item is fetched from Close.GetValueAt() within the OnRender() method. IMHO, the design is questionable because I assume the state change is processed/persisted elsewhere. OnRender() is the code block where you get to write code to affect the UI. I'm not a NinjaTrader expert though...

I recommend contacting NinjaTrader support for assistance.

0 Votes 0 ·

Yeah it looks like there is only one element in the list but the code goes on for a thousand lines. There is many more element in the list. base.OnRender() should be introduce somewhere on top of the protected override void OnRender class.
I thought about trying this:

 public class MyIndicator
 {
 private List<double> myList = new List<double>();
 public List<double> GetList()
  return myList;
 }
 }
    
 public class CallingClass
 {
 MyClass myClass = new MyClass();
 public void GetList()
 {
 List<double> calledList = myClass.GetList();
  }
 }

Can public class CallingClass be a subclass of OnBarUpfdate? I am out of idea. Anyway the listm wont return the same answer in private!

0 Votes 0 ·