Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Changing from "int" to Series<int>

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Changing from "int" to Series<int>

    I have an indicator where I want to store values for each bar so I want to use a Series<int>.

    When I use "int" values for ema200SlopeA it works fine. But when I try to switch to Series<int> for ema200SlopeA, I get this error

    "Indicator 'Stack': Error on calling 'OnBarUpdate' method on bar 300: Object reference not set to an instance of an object."

    below is the code where I initialize and then calculate the values for ema200SlopeA. When I use just an "int" it works fine. When I switch to Series I get the error.

    I have this statement in so that I don't get an out of range error " if (CurrentBar < 300) return; "

    it seems simple, but I'm missing something

    thanks


    PHP Code:
    public class Stack : Indicator
        {
    
            private Series<int>        ema200SlopeA;
            private int             ema50SlopeA;//, ema200SlopeA;
            private int             ema50SlopeB, ema200SlopeB;&#8203; 
    

    PHP Code:
            protected override void OnBarUpdate()
            {
                if(DateTime.Today > new DateTime(2023, 09, 01, 0, 0, 0)) return;    //  filter to stop running if old version
    
                ema50A = EMA(BarsArray[1],50);           // these are type EMA
                ema50B = EMA(BarsArray[2],50);
                ema200A = EMA(BarsArray[1],200);
                ema200B = EMA(BarsArray[2],200);
    
    
                if (CurrentBar < 300) return;        
    
                trendDirection = 0;
    
                if(ema50A[0] > (ema50A[lookBack] + TickSize/s50))
                    ema50SlopeA = 1;
                else if(ema50A[0] < (ema50A[lookBack] - TickSize/s50))
                    ema50SlopeA = -1;
                else
                    ema50SlopeA = 0;
    
                if(ema200A[0] > ema200A[lookBack] + TickSize/s200)
                    ema200SlopeA[0] = 1;    
                else if(ema200A[0] < ema200A[lookBack] - TickSize/200)
                    ema200SlopeA[0] = -1;    
                else
                    ema200SlopeA[0] = 0;
    &#8203; 
    

    #2
    Hello cr8able,

    Thanks for your post.

    When creating a custom Series object, you would need to make sure you are instantiating the custom Series object in OnStateChange() when the State == State.DataLoaded.

    See this help guide page for more information about working with custom Series objects: https://ninjatrader.com/support/help...t8/seriest.htm
    <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

    Comment


      #3
      Got it. Thank you

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by Geovanny Suaza, 02-11-2026, 06:32 PM
      0 responses
      599 views
      0 likes
      Last Post Geovanny Suaza  
      Started by Geovanny Suaza, 02-11-2026, 05:51 PM
      0 responses
      344 views
      1 like
      Last Post Geovanny Suaza  
      Started by Mindset, 02-09-2026, 11:44 AM
      0 responses
      103 views
      0 likes
      Last Post Mindset
      by Mindset
       
      Started by Geovanny Suaza, 02-02-2026, 12:30 PM
      0 responses
      558 views
      1 like
      Last Post Geovanny Suaza  
      Started by RFrosty, 01-28-2026, 06:49 PM
      0 responses
      557 views
      1 like
      Last Post RFrosty
      by RFrosty
       
      Working...
      X