Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Bars Since a Condition in a Strategy

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

    #16
    RyanM,

    The code snippet you're referring to is simply there to remove the dot if no higher high than the high of the signal candle has been made in the candle following the signal candle => Invalidated Signal. This works well. What I'm trying to do is to make the code treat an invalidated signal like a "non-event", ie if the candle counter was already at 5 and a dot is drawn just to be removed at the next candle because it was invalidated, the counter shouldn't start over at 0 again but move on to 6, 7 etc. until it reaches 10 (the value where the condition is set to true again). This is the part that I don't manage to get to work.

    Thanks

    Comment


      #17
      Sorry, I don't follow what you're looking for. The idea of BarsSince is that its value is zero when the conditions are true and then the counter increments for every bar that it's not true. This may not be what you're looking for. I suggest simplifying and working on one element at a time.

      If you're looking for professional coding help, please see our NinjaScript consultants here:
      Ryan M.NinjaTrader Customer Service

      Comment


        #18
        RyanM

        The BarsSince command is exactly what I'm looking for and I'm grateful that you showed me how to use it (it works perfectly).

        The only thing I was trying to explain in my last post is the following:
        Once the strategy has produced a signal the Barcount command is set to false and the BarsSince command has to count 10 bars before it is set to true again. So far so good.

        Now, what I'm trying to add to this routine is a small loop that takes care of "Invalidated Entry Signals". When such a signal appears, I want the code to ignore it, ie I don't want the BarsSince command to restart at 0 but I want it to continue from where it was. Example:

        Bar 1: Valid Signal => BarsSince starts to count until 10
        Bar 2: No Signal => BarsSince =1
        Bar 3: No Signal => BarsSince =2
        Bar 4: Invalidated Signal => BarsSince =3 and NOT 0. The counter doesn't start over at 0 but continues the loop it had started before because the signal was invalidated.

        The code that defines an invalidated signal is the following:
        if (High[0] <= High[1])
        {
        RemoveDrawObject("DotBlackLongFading" + (CurrentBar -1));
        }

        The dot that appears on the chart is removed again at the open of the next bar because the signal is invalidated. A (long) signal is invalidated if the bar that follows it doesn't make a higher high than the high of the bar where the signal has been generated. I hope this makes things a little clearer. (What I'm trying to do is really simple, it's just a little hard to explain).

        Thanks
        Last edited by laocoon; 10-17-2010, 06:00 AM.

        Comment


          #19
          Bar 1: Valid Signal => BarsSince starts to count until 10
          The counter is only increased when your conditions are not true. It's only reset to zero when your conditions are true.

          You're saying that you would like it do something different after a particular sequence has occurred (BarsSince > 10)?

          Can you add BarsSince < 10 to the if statement that resets the value to zero? It then only resets the value during the initial count up but once it hits 10 it ignores.

          You'll then probably need an additional branch later on that properly resets BarsSince to zero.
          Ryan M.NinjaTrader Customer Service

          Comment


            #20
            Bars Since - Trying to Limit Trades

            Hi Ryan -
            I am trying to limit the number of trades my medium to high frequency system does by attempting to prohibit a new trade if a trade has been generated within a certain amount of bars.
            Example:
            Bar 1: All conditions = True. Trade signal generated. Trade exited. Loss.
            Bar 4: All conditions = True. Trade signal generated. Trade exited. Loss.
            Bar 8: All conditions = True. Trade signal generated. Trade exited. Gain.
            Bar 13: All conditions = True. Trade signal generated. Trade exited. Loss.
            Actual % range of all these trades, 0.2-0.3% of each other. The system looks to find reversals and breakouts so the "chop" that is created is costing alot of PnL. Instead, I would prefer to add the filter I mentioned above, so that there will be no trade signal generated IF there was a trade within a variable amount of bars, lets say 10 for sake of example. What is the best way to do this? Can you provide sample code? Many thanks, MKT

            Comment


              #21
              MKTTHOTS,

              What you want to do is the easy part (thanks RyanM), here's the sample code:

              if (your conditions here && BarCount1)
              {
              EnterLong etc...
              BarsSinceCondition1 = 0;
              BarCount1 = false;
              }

              else
              BarsSinceCondition1++;

              if(BarsSinceCondition1 >= 10)
              BarCount1 = true;

              What's more difficult is to ensure that if a Buy Signal was generated but not executed (=>Invalidated Signal), the BarsSince command should not restart at 0 but continue from where it was before. An Invalidated Signal might appear while the BarsSince counter is at 6, and since it is invalidated the counter should move on to 7 and not restart at 0.

              The invalidated signal will depend on your trade entry logic: in my case I don't enter at market once the signal is generated but only at the open of the next bar IF the new bar makes a higher high than the high of the bar where the signal was generated. If the new bar doesn't make a higher high, the entry is invalidated and the visual trade entry signal removed from the chart once this new bar has closed.

              I still don't know how to do that.
              Last edited by laocoon; 10-18-2010, 06:23 AM.

              Comment


                #22
                Thanks Laocoon. But, how do I setup:
                BarCount1 and BarsSinceCondition1?

                Comment


                  #23
                  In the Variables section of the code, just add:

                  bool BarCount1;
                  bool Barcount2;

                  int BarsSinceCondition1 = 0;
                  int BarsSinceCondition2 = 0;

                  To avoid any confusion, I would use a unique BarCount variable (BarCount1, BarCount2, BarCount3, etc) and a unique BarsSinceCondition variable (BarsSinceCondition1, BarsSinceCondition2, BarsSinceCondition3, etc) for every buy and sell signal in your code.
                  Last edited by laocoon; 10-18-2010, 07:06 AM.

                  Comment


                    #24
                    I understand and will give it a try. I assume the BarsSinceCondition=X should be a private int in the variables section right?

                    Comment


                      #25
                      I have them as "int" in my code. Not sure about the difference between "int" and "private int".

                      Comment


                        #26
                        Okay, as an alternative to the kind code you have provided, I have also dug around and found that:
                        BarsSinceEntry() > 10 //or// BarsSinceEntry() > BarsSinceCondition1
                        Works basically the same way, but with less complicated code; do you agree with this?

                        Comment


                          #27
                          I would have to test it to see if the result is the same.

                          Comment


                            #28
                            Would be interesting to see what you think for sure.

                            Comment


                              #29
                              MKTTHOTS,

                              Yes, you can use BarsSinceEntry() as an enter condition. One thing you should be aware of is if all your entry conditions have this, you will never see an entry.
                              Ryan M.NinjaTrader Customer Service

                              Comment


                                #30
                                Hi Ryan, I don't understand. Do you mean that I need to have another condition that says "IF i have already entered 1 (one) trade, then "....make the barssinceentry condition true" ?? Also, i am using a multi-time frame strategy and CANNOT get the barssincentry to work, like, i've tried everything: can you give me the right code or solution for this:
                                // Condition set 1
                                if (BarsInProgress == 0)
                                return;

                                if (High[0] >= Pivots(PivotRange.Daily, HLCCalculationMode.CalcFromIntradayData, 20).R1[0] + -20 * TickSize
                                && High[
                                0] < Pivots(PivotRange.Daily, HLCCalculationMode.CalcFromIntradayData, 500).R1[0] + 20 * TickSize
                                && SMA(BarsArray[
                                1], 200)[0] > SMA(BarsArray[0], 20)[0]/* && BarsSinceEntry(int BarsInProgress == 0, string "", int BarsSinceCondition1)*/)

                                Comment

                                Latest Posts

                                Collapse

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