Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Count signals on previous 12 bars & alert, then reset

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

    Count signals on previous 12 bars & alert, then reset

    Hello,
    I am trying to write some code for NT8 so that at the close of each bar, the code looks back over the previous 12 bars, and there may be anything from 0 to 2 signs on each bar. If the number of signs counted are > 10, then a diamond is drawn to the chart. The problem is that I am getting too many diamonds because the count does not appear to be resetting to 0 on the first loop after the close of a bar. The code is also written so that there should not be more than one bar without a sign on it, and no 2 consecutive bars must be without a sign. Can anyone help to identify where I have gone wrong, in particular in resetting the count.

    Thank you.

    Code:
    int barsToCheck = Math.Min(CurrentBar, 12); //CurrentBar is 0, so it will look back to 11 to make 12.
                            countSigns = 0;
                            for (int i = 0; i < barsToCheck; i++)
                            {
                                        if(variousSigns[0]==false && variousSigns [1]==false) //2 bars with no sign, reset count to 0.
                                        {countSigns =0;}
                                        if(sign1[0]==true)
                                        {countSigns ++;}
                                        if(sign2[0]==true)
                                        {countSigns ++;}
                                        if(sign3[0]==true)
                                        {countSigns ++;}                                 
                            }
    
                            if(countSigns > 10)                                         
                            {
                                        Diamond myDiamond = Draw.Diamond(this, "MyDiamond" + CurrentBar, false, 0, high0 + 6*TickSize, Brushes.DarkOrange);
                                        myDiamond.OutlineBrush = Brushes.Black;                          
                            }

    #2
    Hello GeorgeW,

    Diamond myDiamond = Draw.Diamond(this, "MyDiamond" + CurrentBar, false, 0, high0 + 6*TickSize, Brushes.DarkOrange);

    Draw.Diamond(NinjaScriptBase owner, string tag, bool isAutoScale, int barsAgo, double y, Brush brush)

    This code appears to draw a new object on new bars by using unique tag names.

    If you want to move and update the existing object, do not use a unique tag and instead use the same tag.

    Or you can use RemoveDrawObject() to remove an object by its tag name.

    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Thanks for your response ChelseaB.

      I don't think I made it clear what I was looking for. The multiple signals are occurring because the count is not being reset properly. Say during the lookback the count reaches 10 when it reaches 8 bars back, but if 2 of the bars in that 8 have no signs, then a signal should not be registered. The maximum that can have no signs is 1 bar. The use of unique tag names to draw a new object on a new bar is deliberately, as long as the count and number of bars without a sign is correct.

      Comment


        #4
        Hello GeorgeW,

        To confirm, you are asking about your logic and why the countSigns variable does not have the value you are expecting when you are expecting it?

        Add prints to the script in each condition's action block that changes the value of the countSigns variable and print the time and the value of countSigns along with some indication of which condition set the print is coming from.
        At what point does the value of countSigns become unexpected?
        Is the condition that triggered the print evaluating as true unexpectedly?
        Print the values of the variables used in that condition set. Which variable has an unexpected value?

        You can also contact a professional NinjaScript Consultant who would be eager to create or modify this script at your request or assist you with your script. The NinjaTrader Ecosystem has affiliate contacts who provide educational as well as consulting services. Please let me know if you would like our business development follow up with you with a list of affiliate consultants who would be happy to create this script or any others at your request.
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          To assist anyone who may view this thread, this issue has been resolved. Basically I set up a <Series> integer to do the counting, which meant I could restart the count at any point, not just at 0, by using something like:
          if(???)
          countSeries[0] = countSeries[5] - countSeries[1]
          here it resets to only those signs counted since countSeries[4] inclusive.

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by Geovanny Suaza, 02-11-2026, 06:32 PM
          0 responses
          571 views
          0 likes
          Last Post Geovanny Suaza  
          Started by Geovanny Suaza, 02-11-2026, 05:51 PM
          0 responses
          331 views
          1 like
          Last Post Geovanny Suaza  
          Started by Mindset, 02-09-2026, 11:44 AM
          0 responses
          101 views
          0 likes
          Last Post Mindset
          by Mindset
           
          Started by Geovanny Suaza, 02-02-2026, 12:30 PM
          0 responses
          549 views
          1 like
          Last Post Geovanny Suaza  
          Started by RFrosty, 01-28-2026, 06:49 PM
          0 responses
          550 views
          1 like
          Last Post RFrosty
          by RFrosty
           
          Working...
          X