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 charlesugo_1, 05-26-2026, 05:03 PM
      0 responses
      65 views
      0 likes
      Last Post charlesugo_1  
      Started by DannyP96, 05-18-2026, 02:38 PM
      1 response
      149 views
      0 likes
      Last Post NinjaTrader_ChelseaB  
      Started by CarlTrading, 05-11-2026, 05:56 AM
      0 responses
      162 views
      0 likes
      Last Post CarlTrading  
      Started by CarlTrading, 05-10-2026, 08:12 PM
      0 responses
      99 views
      0 likes
      Last Post CarlTrading  
      Started by Hwop38, 05-04-2026, 07:02 PM
      0 responses
      286 views
      0 likes
      Last Post Hwop38
      by Hwop38
       
      Working...
      X