Announcement

Collapse
No announcement yet.

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 Coyainvestor, Today, 07:32 PM
          0 responses
          3 views
          0 likes
          Last Post Coyainvestor  
          Started by Hyper, Today, 06:14 PM
          1 response
          11 views
          0 likes
          Last Post NinjaTrader_EricB  
          Started by Icus1, Today, 06:26 PM
          0 responses
          3 views
          0 likes
          Last Post Icus1
          by Icus1
           
          Started by djkiwi, Today, 06:24 PM
          1 response
          8 views
          0 likes
          Last Post djkiwi
          by djkiwi
           
          Started by pyraxic, Today, 06:22 PM
          0 responses
          8 views
          0 likes
          Last Post pyraxic
          by pyraxic
           
          Working...
          X