• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

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

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by rickyblah12, Today, 06:19 PM
      0 responses
      3 views
      0 likes
      Last Post rickyblah12  
      Started by ballboy11, Today, 05:22 PM
      0 responses
      3 views
      0 likes
      Last Post ballboy11  
      Started by ballboy11, Today, 04:33 PM
      2 responses
      4 views
      0 likes
      Last Post ballboy11  
      Started by dedouglas, Today, 03:16 PM
      1 response
      7 views
      0 likes
      Last Post NinjaTrader_ChelseaB  
      Started by TradeMyth, Today, 02:45 PM
      3 responses
      26 views
      0 likes
      Last Post NinjaTrader_ChelseaB  
      Working...
      X