Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

NBarsUp Modification

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

    NBarsUp Modification

    Can you tell me why this code is not correctly counting the # of up bars? I'm trying to follow the logic before modifying it. Thanks in advance!

    protected override void OnBarUpdate()
    {
    int BarsupCnt;

    BarsupCnt = 0;
    //Barup = false;

    if (CurrentBar < BarCount)
    {
    Value[0] = 0;
    }
    else
    {
    bool gotBars = false;

    Print(Time[3].ToString() + " 3 close " + Close[3]);
    Print(Time[3].ToString() + " 3 open " + Open[3]);
    Print(Time[2].ToString() + " 2 close " + Close[2]);
    Print(Time[2].ToString() + " 2 open " + Open[2]);
    Print(Time[1].ToString() + " 1 close " + Close[1]);
    Print(Time[2].ToString() + " 1 open " + Open[1]);
    Print(Time[0].ToString() + " 0 close " + Close[0]);
    Print(Time[0].ToString() + " 0 open " + Open[0]);

    for (int i = 0; i < BarCount + 1; i++)
    {
    if (i == BarCount)
    {
    gotBars = true;
    break;
    }

    if ((Close[i] > Open[i]))
    {
    BarsupCnt = BarsupCnt + 1;
    }

    Print("BarsupCnt: " + BarsupCnt);

    }

    Value[0] = gotBars ? 1 : 0;
    }
    }

    #2
    NBarsUp Modification

    Hello GibbsB613,

    Thanks for writing in to our Support team.

    You are setting your BarsupCnt = 0 each time OnBarUpdate() is called. You will want to define this variable outside of the OnBarUpdate() method so it is only changing with respect to your logic defined in your else condition.

    Please let me know if I may be of any further assistance.
    Alan S.NinjaTrader Customer Service

    Comment


      #3
      Just so I understand this clearly:

      1. Once that last bar closes, isn't onbarupdate only being called once?
      2. Were you able to test your recommendation?

      Thanks!!

      Comment


        #4
        Hello GibbsB613,

        Thanks for your reply.

        1. OnBarUpdate is called for every bar in the collection. Your code sets the variable to 0 on every bar. You then attempt to increment the value if the Close is greater than the Open. So your only possible values are 1 or 0 at any time. This code will never increment to a full count of the up bars as is.

        2. The logic you are using is flawed at the basic level of assigning values. Testing our suggestion would be to test basic programming concepts.

        Please let me know if you have any questions.
        Alan S.NinjaTrader Customer Service

        Comment


          #5
          Ok, I'll give it a try. My expectation was that once the bar closed it would run once and only reset the variable to zero once.

          Thanks!!

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by sidlercom80, 10-28-2023, 08:49 AM
          168 responses
          2,262 views
          0 likes
          Last Post sidlercom80  
          Started by Barry Milan, Yesterday, 10:35 PM
          3 responses
          10 views
          0 likes
          Last Post NinjaTrader_Manfred  
          Started by WeyldFalcon, 12-10-2020, 06:48 PM
          14 responses
          1,429 views
          0 likes
          Last Post Handclap0241  
          Started by DJ888, 04-16-2024, 06:09 PM
          2 responses
          9 views
          0 likes
          Last Post DJ888
          by DJ888
           
          Started by jeronymite, 04-12-2024, 04:26 PM
          3 responses
          41 views
          0 likes
          Last Post jeronymite  
          Working...
          X