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 PaulMohn, Today, 03:49 AM
          0 responses
          7 views
          0 likes
          Last Post PaulMohn  
          Started by inanazsocial, Today, 01:15 AM
          1 response
          9 views
          0 likes
          Last Post NinjaTrader_Jason  
          Started by rocketman7, Today, 02:12 AM
          0 responses
          10 views
          0 likes
          Last Post rocketman7  
          Started by dustydbayer, Today, 01:59 AM
          0 responses
          4 views
          0 likes
          Last Post dustydbayer  
          Started by trilliantrader, 04-18-2024, 08:16 AM
          5 responses
          23 views
          0 likes
          Last Post trilliantrader  
          Working...
          X