Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

Bars Since a Condition in a Strategy

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

    Bars Since a Condition in a Strategy

    Hello

    I've read a lot about this subject on the forum, but it's still not clear to me what's the simplest way to use this concept in a strategy. The MRO method doesn't seem to be the way to go (at least not for what I have in mind, which is something very basic btw). The int Variables BarsSince & TriggerBar seem more promising, but I don't know how to make a bool flag with them, which is what I want to do.

    I'd like to add a bool flag to one of the criteria in the strategy that becomes true only if 5 or more bars have closed since the condition was last true.

    The bool flag is called "Barcount". Here's what I've got so far:

    if (conditions here && Barcount)
    {
    Barcount = false;
    }


    if ("this is what I still need to figure out")
    {
    Barcount = true;
    }

    Any help is much appreciated.

    Thanks

    #2
    Hello laocoon,

    BarsSince could be done with a counter. The snippet below assumes COBC = true.

    Code:
     
    if(Conditions)
    {
    BarsSince = 0;
    Barcount = false;
    }
     
    else
    BarsSince++;
     
     
    if(BarsSince >= 5)
    Barcount = true;
    Last edited by NinjaTrader_RyanM1; 10-07-2010, 08:44 AM.
    Ryan M.NinjaTrader Customer Service

    Comment


      #3
      Hi RyanM

      Thanks a lot for your reply.
      I get what you're suggesting, but I'm having an issue with this part:

      else
      BarsSince++

      What does BarsSince++ mean?
      It also won't compile, whereas if I leave it out it compiles OK.

      Thanks

      Comment


        #4
        That statement increments BarsSince value by one. To compile: most likely just needs the semicolon at end of statement ;

        Fixed the snippet above.
        Ryan M.NinjaTrader Customer Service

        Comment


          #5
          Got it, thanks a lot RyanM!

          Comment


            #6
            RyanM,

            While I'm at it, can a bool flag have a second condition or is it limited to a single one?
            The reason I'm asking is that I tried to expand the condition set with an "||" but it won't let me.

            This is the code:

            ...
            else
            BarsSince++;

            if(BarsSince >= 15) || Low [1] < Low [5]
            BarCount = true;

            Thanks
            Last edited by laocoon; 10-07-2010, 09:25 AM.

            Comment


              #7
              Yes, that should work as well.

              Are you getting compile errors or when running it? Check log tab of control center for any error messages.
              Ryan M.NinjaTrader Customer Service

              Comment


                #8
                I'm getting a compile error. It says: Invalid Expression Term "||".
                I replaced the || with a && but it doesn't work either.

                Thanks

                Comment


                  #9
                  Are you missing the closing parentheses?

                  if(BarsSince >= 5 || Low [1] < Low [5])
                  Barcount =
                  true;
                  Ryan M.NinjaTrader Customer Service

                  Comment


                    #10
                    Indeed, I forgot the closing parenthesis and got confused by the error message pointing to another error source.

                    Sorry for that & thanks again.

                    Comment


                      #11
                      Glad to hear it was something simple. No worries - you pointed out mine earlier
                      Ryan M.NinjaTrader Customer Service

                      Comment


                        #12
                        Hi RyanM,

                        One last question: I have a couple of different signals in my strategy and I added the code snippet you helped me with to all of them. I noticed that when I add it once (for only one signal) to the strategy it works perfectly. When I add it more than once, it doesn't work for all signals, hence my idea to use a numbering system to avoid that the different bits get mixed up. Example:

                        if ( conditions here && BarCount1)

                        {
                        BarsSince = 0;
                        BarCount1 = false;
                        }

                        else
                        BarsSince++;

                        if(BarsSince >= 15 || High [1] > High [5])
                        BarCount1 = true;

                        I then use the same code again but BarCount1 gets replaced by BarCount2 etc.

                        Should this work in your opinion or is there a conceptual error in there?

                        Thanks

                        Comment


                          #13
                          Yeah, that should help to make these variables unique per signal then. For accuracy here you would probably have to do this with all of the tracking variables: BarsSinceCondition1, BarsSinceCondition2, etc.
                          Ryan M.NinjaTrader Customer Service

                          Comment


                            #14
                            Hi RyanM

                            Thanks a lot for your help on this, my project is almost finished.
                            The last thing I'm struggling with is the following: when my strategy generates a signal, it draws a dot below the candle where all conditions are true (the "Signal Candle"). The dot stays on the chart until the next candle is closed and only if a higher high than the high of the Signal Candle is made. If that's not the case the signal is invalidated and the dot is removed from the chart once the next candle has closed.

                            Now, if the signal has been invalidated, I obviously want the "BarsSinceCondition1, 2, etc" to be true, but for some reason this does not work.

                            The code below draws the dot, removes it if the condition is invalidated and waits 10 candles before the condition is set to true again. The only thing it does not do is "resetting" the BarsSinceCondition function if the signal is invalidated and thus the dot removed. I've been looking at this from different angles but I don't see where I made I mistake. Could I ask you to have a last look at it?

                            Thanks a lot.



                            if (conditions here && BarCount1)
                            {
                            DrawDot("Long" + CurrentBar, false, 0, Low[0]- 2*(TickSize), Color.Black);
                            BarsSinceCondition1 = 0;
                            BarCount1 = false;
                            }

                            else
                            BarsSinceCondition1++;

                            if(BarsSinceCondition1 >= 10 || (High[0] <= High[1]))
                            BarCount1 = true;

                            if (High[0] <= High[1])
                            {
                            RemoveDrawObject("DotBlackLongFading" + (CurrentBar -1));
                            }
                            Last edited by laocoon; 10-15-2010, 08:25 AM.

                            Comment


                              #15
                              Can you clarify what do you mean by reset? The BarsSinceCondition is zero when your conditions are true. For each bar that it's not true, it is increased by one. This statement below is not connected to any of the previous evaluations and may be where you want to start looking:

                              if (High[0] <= High[1])
                              {
                              RemoveDrawObject("DotBlackLongFading" + (CurrentBar -1));
                              }
                              Ryan M.NinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by JoMoon2024, Today, 06:56 AM
                              0 responses
                              6 views
                              0 likes
                              Last Post JoMoon2024  
                              Started by Haiasi, 04-25-2024, 06:53 PM
                              2 responses
                              17 views
                              0 likes
                              Last Post Massinisa  
                              Started by Creamers, Today, 05:32 AM
                              0 responses
                              5 views
                              0 likes
                              Last Post Creamers  
                              Started by Segwin, 05-07-2018, 02:15 PM
                              12 responses
                              1,786 views
                              0 likes
                              Last Post Leafcutter  
                              Started by poplagelu, Today, 05:00 AM
                              0 responses
                              3 views
                              0 likes
                              Last Post poplagelu  
                              Working...
                              X