Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Count WilliamsR in Row

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

    Count WilliamsR in Row

    Hi,

    I am new to coding as of a few days ago and just downloaded my trial of NT. So far having fun trying to create indicators but am getting caught up in various places.

    Could someone help me with the below code? The first part of the code works fine. It's when I add the 2nd paragraph, with red highlighted number, when it stops producing values on the chart.

    I'm trying to count the number of Williams Bars under -89.5 in a row. The first paragraph does that well. I want to ultimately find the max of the number in row at time 0 and the number in row at time t-1. Clearly, my syntax is wrong. Could someone help me with how to make this work?

    Also, if I want to limit it to only look back say a maximum of 3 periods is there an easy way to do that? Thanks so much!!

    {if (WilliamsR(10)[0] < -89.5)
    {williamsvariable = williamsvariable + 1;}
    else
    {williamsvariable = 0;}

    {if (WilliamsR(10)[1] < -89.5)
    {williamsvariablebef = williamsvariablebef + 1;}
    else
    {williamsvariablebef = 0;}

    {finalwilliams = Math.Max(williamsvariable, williamsvariablebef);}
    {Value[0] = finalwilliams;}
    }}

    #2
    Hello GoodTradingFun,

    Thank you for your post.

    I can't tell from the code snippet you provided, but are you checking that at least one bar is on the chart before you try to look back to the previous bars' values?

    For example, if you place this at the beginning of OnBarUpdate, you wouldn't get a index error due to trying to look back one bar on the first bar of the chart:

    if(CurrentBar < 1) return;



    If you're looking back further than 1 bar, you'd need to increase the number you're comparing CurrentBar to in that statement accordingly. So if the furthest you look back at bars in the script is 5, you'd want to use if(CurrentBar < 5) return; instead.

    Also, if I want to limit it to only look back say a maximum of 3 periods is there an easy way to do that?
    Can you clarify, by 3 periods do you mean look back 3 bars?

    Thanks in advance; I look forward to assisting you further.

    Comment


      #3
      Wow, can't believe I missed that! I was having several indicators just show blanks the last couple of days and that was definitely the issue. I tried that and it worked. Really appreciate the help on it!

      Yes, I mean 3 bars in that example. For example, the next indicator I'm going to count the # of WilliamsR under -89.5 within the last 11 trading bars. I'm wondering how to specify a maximum of 11 bars, as an example, to count through only. Thanks again!

      Comment


        #4
        Originally posted by GoodTradingFun View Post
        Hi,

        I am new to coding as of a few days ago and just downloaded my trial of NT. So far having fun trying to create indicators but am getting caught up in various places.

        Could someone help me with the below code? The first part of the code works fine. It's when I add the 2nd paragraph, with red highlighted number, when it stops producing values on the chart.

        I'm trying to count the number of Williams Bars under -89.5 in a row. The first paragraph does that well. I want to ultimately find the max of the number in row at time 0 and the number in row at time t-1. Clearly, my syntax is wrong. Could someone help me with how to make this work?

        Also, if I want to limit it to only look back say a maximum of 3 periods is there an easy way to do that? Thanks so much!!

        {if (WilliamsR(10)[0] < -89.5)
        {williamsvariable = williamsvariable + 1;}
        else
        {williamsvariable = 0;}

        {if (WilliamsR(10)[1] < -89.5)
        {williamsvariablebef = williamsvariablebef + 1;}
        else
        {williamsvariablebef = 0;}

        {finalwilliams = Math.Max(williamsvariable, williamsvariablebef);}
        {Value[0] = finalwilliams;}
        }}
        Maybe calling for a historical value, WR()[1], that is not there yet.

        Perhaps adding the following two lines first under OnBarUpdate() may help.

        Code:
        if (CurrentBar) < 1)
        return;
        Cheers!

        Comment


          #5
          Hello GoodTradingFun,

          Thank you for your reply.

          You could create a user input variable for the number of bars you want to look back through, and then use that to loop through however many bars you want.

          Let's say I've created an int type user input called LookBackBars and that it's set to 11.

          I could use a for loop to cycle through those bars and keep a running count of how many bars the WilliamsR() is below your specified value:

          int Counter = 0;

          for(int i = 0; i < LookBackBars; i++)
          {
          if(WilliamsR(10)[i] < -89.5)
          {
          Counter += 1;
          }

          }

          Print("Bars below -89.5: " + Counter);

          Please let us know if we may be of further assistance to you.

          Comment


            #6
            Thanks Kate and Alligator. I got everything to work and really appreciate it!!

            Comment


              #7
              Kate, can I use something like that to count bars since last entry execution? I've looked at all the examples in the guide and can't get any of them to work. They all refer to multi-series charts. I only have one chart, a 1 minute. I even added that condition in Strategy Builder and copied to code to my unlocked script and it didn't work either. So, is there anyway to tell the script to start a bar count when it enters a trade? I just want to count a bar or two before its allowed to entered another trade (in my case in the same direction.) I really don't want to mess with the BarsSinceEntryExecution code any more. Thanks...

              Comment


                #8
                Hello JoeF1953,

                Thank you for your reply.

                Could you give an example of what you've tried with BarsSinceEntryExecution that doesn't seem to be working for you? It will work on a chart of a single data series, so I'm curious to know what you've got that doesn't seem to be working for you.

                Thanks in advance; I look forward to assisting you further.

                Comment


                  #9
                  I already deleted all that stuff from my code. When I ran it, its said " Strategy Exception... Bars in Progress needed, or not defined, or not referenced, or something like that. Bars in Progress was clearly defined at the top of the code. Anyway, it may take me a day or two to recreate it. I'm not really interested in messing with it though. So, I guess you are telling me that there's no other way to have the strat wait a couple of bars between trades? Thanks...

                  Comment


                    #10
                    Hello JoeF1953,

                    Thank you for your reply.

                    You could do something like I've suggested above for tracking the number of times the WilliamsR() is below a specified value. I'm saying that there's already a method, BarsSinceEntryExecution(), that already does what you're looking for and that it's likely not necessary to reinvent the wheel, so to speak. I'm attaching a very simple example strategy showing how BarsSinceEntryExecution may be used to exit a position after a specified number of bars that demonstrates this.

                    Please let us know if we may be of further assistance to you.
                    Attached Files

                    Comment


                      #11
                      Hey Kate, I appreciate you prodiving the zip. It lets me see where everything goes. This looks exactly like the one I tried from the Strategy builder (I copied and pasted) except I have >= 2 instead of == 10. I'll copy yours into my code and see if it makes a difference. While I'm here, here's the Error I got:

                      ': Error on calling 'OnBarUpdate' method on bar 4982: Strategy 'Joe2TicScalperCode/218761916': You must use the overload that has a 'BarsInProgress' parameter when calling the BarsSinceEntryExecution() method in the context of a multi-time frame and instrument strategy.

                      What's confusing is that I'm not in the context of a multi-time frame and instrument strategy. ???

                      Here's a copy of the last code I tried, copied out of the guide:
                      && (BarsSinceEntryExecution(@"Long") > 2 || BarsSinceEntryExecution(@"Long") == -1)

                      Of course, I got the same error on the other two syntax's I tried as well. BarsSinceEntryExecution() >2, and the one that looks like the one in your zip.
                      I'll copy yours and try again.

                      ** Does it make a difference if it's in a list of conditions that have to be met before it puts an Entry Order in?
                      I'll let you know what happens after I try your code.
                      Thanks for trying to help keep me from reinventing the wheel. Sometimes, reinventing the wheel takes a lot less time than trying to make something that's supposed to work, actually work.

                      Comment


                        #12
                        Hey Kate, I've been testing it for two days now and it looks like its working. Thanks for all your help.... you're GREAT!

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                        0 responses
                        574 views
                        0 likes
                        Last Post Geovanny Suaza  
                        Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                        0 responses
                        333 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
                        553 views
                        1 like
                        Last Post Geovanny Suaza  
                        Started by RFrosty, 01-28-2026, 06:49 PM
                        0 responses
                        551 views
                        1 like
                        Last Post RFrosty
                        by RFrosty
                         
                        Working...
                        X