Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Counters are being reset to zero when running strategy

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

    Counters are being reset to zero when running strategy

    Hello everyone I need some help here. For a couple of days I been trying to learn Ninjascript but here is the problem.

    if I do the following:
    Code:
    int counter = 0;
    
    if (conditions go here) counter++;
    The counter will be set to 1 but then it will be resent to zero when the conditions are no longer met!!! In other words it is not storing the correct count.

    Another example:

    Code:
    int currentbar = 0;
    
    if (conditions go here)
    {
    EnterShortLimit();
    currentbar = CurrentBar;
    }
    The variable currentbar gets reset to zero every time the strategy runs. So I am not able to store variables. Any ideas on what I am doing wrong?

    #2
    Hello Kaotik,

    Thanks for your post.

    The variable is being reset on every run of the method it is in (such as OnBarUpdate).

    If you initialize the variable outside of that method (in the Variables region) but within the class as a private variable, then it won't be reset on the next run of the containing method.

    For example:

    Code:
    public class SMA : Indicator
    {
    #region Variables
    
    private int	count = 0;
    
    #endregion
    
    protected override void Initialize()
    {
    }
    
    protected override void OnBarUpdate()
    {
    if (Close[0] > Close[1])
    {
    count++;
    }
    }
    
    }
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Thank you ChelseaB, that answers my question. I really appreciate your quick reply.

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by Geovanny Suaza, 02-11-2026, 06:32 PM
      0 responses
      648 views
      0 likes
      Last Post Geovanny Suaza  
      Started by Geovanny Suaza, 02-11-2026, 05:51 PM
      0 responses
      369 views
      1 like
      Last Post Geovanny Suaza  
      Started by Mindset, 02-09-2026, 11:44 AM
      0 responses
      108 views
      0 likes
      Last Post Mindset
      by Mindset
       
      Started by Geovanny Suaza, 02-02-2026, 12:30 PM
      0 responses
      572 views
      1 like
      Last Post Geovanny Suaza  
      Started by RFrosty, 01-28-2026, 06:49 PM
      0 responses
      573 views
      1 like
      Last Post RFrosty
      by RFrosty
       
      Working...
      X