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 CarlTrading, 03-31-2026, 09:41 PM
      1 response
      63 views
      0 likes
      Last Post NinjaTrader_ChelseaB  
      Started by CarlTrading, 04-01-2026, 02:41 AM
      0 responses
      35 views
      0 likes
      Last Post CarlTrading  
      Started by CaptainJack, 03-31-2026, 11:44 PM
      0 responses
      54 views
      1 like
      Last Post CaptainJack  
      Started by CarlTrading, 03-30-2026, 11:51 AM
      0 responses
      61 views
      0 likes
      Last Post CarlTrading  
      Started by CarlTrading, 03-30-2026, 11:48 AM
      0 responses
      48 views
      0 likes
      Last Post CarlTrading  
      Working...
      X