Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Need help with some C# Counting Code

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

    Need help with some C# Counting Code

    I'm trying to code a counter in Ninjatrader which uses C# as its programming language.

    I would like this counter to commence counting upon the completion of the first counter and once it commences counting to consider only the new condition when counting.

    So, the first counter commences when Close[0] < Close[4]. The second counter commences ONLY after the first counter has reached 9 and its condition for counting is Close[0] <= Low[2].

    I don't know how to write the part of the code where the second counter commences counting and its counts are independent of the first counter reaching 9.


    MyBKExperience
    Last edited by Dwight292; 01-11-2019, 11:52 PM.

    #2
    Not really clear if the 2 conditions need to occur at the same time, but this may give you a pointer to what you want to achieve

    if ( Close[0] < Close[4] )
    {
    Count1++;

    if ( Close[0] <= Low[2] && Count1 >= 9 )
    Count2++;
    }

    Comment


      #3
      Hello Dwight292,

      Thanks for your post and welcome to the NinjaTrader forums!

      You didn't mention what the counter is counting. I suspect you want to count the number of bars that pass after the first condition to then be a condition to then trigger the second counter which again I will assume is counting bars that pass.

      In the example below, the assumptions are that you want to count on bars and that you are using Calculate.OnBarClose. The counters would be declared as private integers and for the purposes of the example will call them counter1 and counter2, which are initialized to 0. In addition, a couple of variables would be declared as bools and initialized as false and are called startcounter1 and startcounter2.

      if (Close[0] < Close[4] && counter1 == 0)
      {
      startcounter1 = true; // set a bool true
      }

      if (Startcounter1)
      {
      counter1++; // increment counter1
      }

      if (counter1 >= 9 && Close[0] <= Low[2]) // check for 2nd trigger
      {
      startcounter2 = true; // start the 2nd counter
      }
      if (startcounter2)
      {
      counter2++; // increment counter2
      }


      Depending on what you are trying to do, you would also likely want to reset the counters and the bools and would need a condition to do that.

      if (some condition to reset counters)
      {
      startcounter1 = false;
      startcounter2 = false;
      counter1 = 0;
      counter2 = 0;
      }


      Paul H.NinjaTrader Customer Service

      Comment


        #4
        Originally posted by Dwight292 View Post
        I'm trying to code a counter in Ninjatrader which uses C# as its programming language.

        I would like this counter to commence counting upon the completion of the first counter and once it commences counting to consider only the new condition when counting.

        So, the first counter commences when Close[0] < Close[4]. The second counter commences ONLY after the first counter has reached 9 and its condition for counting is Close[0] <= Low[2].

        I don't know how to write the part of the code where the second counter commences counting and its counts are independent of the first counter reaching 9.


        https://www.imybkexperience.com/

        When does the first counter reset ?

        If it does not reset, you can store the CurrentBar when Close[0] < Close[4] and add that number to CurrentBar going forward. Then do the same for the second counter.

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by Thamky, Today, 07:28 PM
        0 responses
        4 views
        0 likes
        Last Post Thamky
        by Thamky
         
        Started by Hoffmann, Today, 04:17 PM
        1 response
        5 views
        0 likes
        Last Post NinjaTrader_ShawnB  
        Started by demarcog, Yesterday, 02:24 PM
        8 responses
        30 views
        0 likes
        Last Post NinjaTrader_Kate  
        Started by ErikY, Today, 01:28 PM
        2 responses
        7 views
        0 likes
        Last Post ErikY
        by ErikY
         
        Started by augustfay, Today, 01:07 PM
        1 response
        8 views
        0 likes
        Last Post NinjaTrader_Jesse  
        Working...
        X