Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Confirmation by a slower Timeframe

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

    Confirmation by a slower Timeframe

    Hello

    I've developed a strategy that uses only one timeframe and it works perfectly.
    What I want to do now is to add the confirmation of a slower timeframe before triggering a signal.
    For a Long signal the last closed candle (candle [1]) of the slower timeframe must be green, for a short is must be red. That's it. I posted the code snippet I developed for this in another thread but the only reply I got was that I should debug it using Print commands. I did that but it got me nowhere. This is why I'm trying my luck again and I'm addresing this request to the wider community. How would you go about it?

    Thanks

    #2
    Hi laocoon,

    Thank you for your post.

    You'll want to add a second time series to your strategy. There are a few examples worth taking a look at.

    Multiple Time-Frames: http://www.ninjatrader.com/support/h...nstruments.htm

    Adding Granularity: http://www.ninjatrader.com/support/f...ead.php?t=6652
    TimNinjaTrader Customer Service

    Comment


      #3
      Hi Tim

      Thanks for your reply. I'm already using a second time series.
      Here's what I did:


      Add (PeriodType.Volume,500); //This is the slower timeframe

      protected override void OnBarUpdate()

      {
      ifBarsInProgress ==0 //Code for faster timeframe (2500+ lines of code that work perfectly)
      {
      Code
      }

      ifBarsInProgress ==1 //Code for slower timeframe. This is where I check whether the last closed candle is Green or Red.
      {
      if Open[1] <Close [1]
      SlowTimeframeLong = true
      }

      {
      if Open[1] >Close [1]
      SlowTimeframeShort = true
      }
      }

      I then add the SlowTimeframeLong = true criteria to the criteria list of my Long entry signals (same for shorts) and that's it.

      Unfortunately it doesn't work. I've read all tutorials about how to add another timeframe to a strategy but I'm not aware of a conceptual mistake I made.

      Comment


        #4
        Hi laocoon,

        I agree with the previous advice, you'll need to start by checking if you conditions are being met, so for example:
        {
        if Open[1] <Close [1]
        SlowTimeframeLong = true
        Print("Condition 1 met")
        }

        Then look for that in the Output window.
        Also check the Log tab for any errors.
        TimNinjaTrader Customer Service

        Comment


          #5
          You enter each BarsInprogress only on the end of the bar. So maybe what you want is Close[0] and Open[0].

          Comment


            #6
            Thanks for your replies, Tim & Baruch.
            Tim: I already did what you suggested and it works fine. The output window shows the desired output. This is why I said in my first post that debugging isn't going to help me here.

            Baruch: you're right, it is indeed Close[0] & Open[0], BUT here's the problem:

            Let's say I use 100 contract Volume bars (fast timeframe) and 500 contract Volume bars (slow timeframe).
            The confirmation criteria for the stragegy is that for a Long entry on the fast timeframe, the slow timeframe has to display a closed Green (Up) bar (=> Close[0] > Open[0])

            Each 500 Volume bar obviously contains 5 x100 Volume bars, with the fifth 100 Volume bar closing at EXACTLY the same time as the 500 Volume bar. If this happens (20% probability), then the strategy won't generate a signal because for some reason the Open[0] <Close [0] criteria of the slow timeframe doesn't update fast enough or for some other reason.

            How can I fix this?

            Comment


              #7
              Check this condition on a faster BarsINProgress. Like Closes[1][0]. You even don't need a second TF. You just need to know that you got 500 Volume and than to calculete. One way to do it is acumulating volume and asking if the number mod (%) 500 = 0.

              Comment


                #8
                OK, I went down the debug route but it lead nowhere so I decided to write a very simple code and post it here in its entirety, hoping that someone will be kind enough to highlight the mistake I made.

                The code is applied to a 1 minute chart and puts a red dot above a red (short) candle and a green dot below a green (long) candle but ONLY if the most recent closed candle in the 10 min chart is either red (for shorts) or green (for longs). With the code as it is right now, I'm getting red & green dots on every red/green candle, which means that the code doesn't take into account the color of the last 10 min candle.

                Thanks for helping me out here.

                public class TwoTimeframes : Strategy
                {
                #region Variables
                // Wizard generated variables
                // User defined variables (add any user defined variables below)

                bool CandleLong;
                bool CandleShort;
                bool SlowTimeframeLong;
                bool SlowTimeframeShort;


                #endregion

                /// <summary>
                /// This method is used to configure the strategy and is called once before any strategy method is called.
                /// </summary>
                protected override void Initialize()
                {
                CalculateOnBarClose = true;
                AllowRemovalOfDrawObjects = true;
                Add(PeriodType.Minute, 10);


                }


                protected override void OnStartUp()
                {

                }

                /// <summary>
                /// Called on each bar update event (incoming tick)
                /// </summary>
                protected override void OnBarUpdate()
                {

                if (BarsInProgress == 0) //Code for faster timeframe
                {

                if(Open[0]<Close[0])
                {
                CandleLong = true;
                }
                else
                {
                CandleLong = false;
                }

                if(Close[0] < Open[0])
                {
                CandleShort = true;
                }
                else
                {
                CandleShort = false;
                }
                }

                if (BarsInProgress == 1) //Code for slower timeframe

                {
                if (Open[1] <Close [1])
                {
                SlowTimeframeLong = true;
                }


                if (Open[1] >Close [1])
                {

                SlowTimeframeShort = true;
                }

                }

                //LONG
                if (CandleLong == true && SlowTimeframeLong == true)
                {

                DrawDot("DotLong" + CurrentBar, false, 0, Low[0]- 2*(TickSize), Color.Lime);

                }

                //SHORT
                if (CandleShort == true && SlowTimeframeShort == true)
                {

                DrawDot("DotShort" + CurrentBar, false, 0, High[0]+ 2*(TickSize), Color.Red);

                }


                }

                #region Properties
                #endregion
                }
                }

                Comment


                  #9
                  why
                  if (BarsInProgress == 1) //Code for slower timeframe

                  {
                  if (Open[1] <Close [1])
                  and not if (Open[0] <Close [0]) ?
                  I would write this strategy like this:

                  public class TwoTimeframes : Strategy
                  {
                  #region Variables
                  // Wizard generated variables
                  // User defined variables (add any user defined variables below)

                  bool CandleLong;
                  bool CandleShort;
                  bool SlowTimeframeLong;
                  bool SlowTimeframeShort;


                  #endregion

                  /// <summary>
                  /// This method is used to configure the strategy and is called once before any strategy method is called.
                  /// </summary>
                  protected override void Initialize()
                  {
                  CalculateOnBarClose = true;
                  AllowRemovalOfDrawObjects = true;
                  Add(PeriodType.Minute, 10);


                  }


                  protected override void OnStartUp()
                  {

                  }

                  /// <summary>
                  /// Called on each bar update event (incoming tick)
                  /// </summary>
                  protected override void OnBarUpdate()
                  {

                  if (BarsInProgress == 0) //Code for current!! timeframe
                  {

                  //LONG
                  if (Close[0] > Open[0] && Closes[0] > Opens[0])
                  {

                  DrawDot("DotLong" + CurrentBar, false, 0, Low[0]- 2*(TickSize), Color.Lime);

                  }

                  //SHORT
                  else if (Close[0] < Open[0] && Closes[0] < Opens[0])
                  {

                  DrawDot("DotShort" + CurrentBar, false, 0, High[0]+ 2*(TickSize), Color.Red);

                  }


                  }

                  #region Properties
                  #endregion
                  }
                  }

                  Comment


                    #10
                    Thanks a lot for your quick reply Baruch.

                    I don't understand this part however: Closes[0] > Opens[0]).
                    What does "Closes" (instead of "Close") mean and how does it reference to the 10 min chart?

                    Furthermore, if I want to add, let's say, a second variable from the 10 min chart, for example whether or not the MACD Line is below 0, (MACD(12, 26, 9)[0] <0), how does the strategy know that I want to check this condition on the 10 min chart? (This is why I added the if (BarsInProgress == 1) part, as per the Ninja tutorial, but you seem not to use it. (It doesn't appear in your code snippet).

                    Thanks again.

                    Comment


                      #11
                      Sorry my mistake.
                      It should be Closes[1][0]
                      For MACD
                      MACD(12, 26, 9)[1][0] <0

                      Comment


                        #12
                        Hello,

                        You would need to use Closes as shown here;



                        Closes[1][0]


                        The above Closes statement tells NinjaTrader the first [1] is referencing the secondary series and then second [0] is referencing the current bar of the secondary series.

                        I would have said: Closes[0][0] if I just wanted the primary series last close.

                        If you do BarsInProgress filtering however the above is not needed, and you can simply use Close[0] as it will reference the current Bar in Progress. This is for if you want to access the Close price of another dataseries from within another data series OnBarUpdate()
                        Last edited by NinjaTrader_Brett; 12-23-2011, 08:56 AM.
                        BrettNinjaTrader Product Management

                        Comment


                          #13
                          Brett,
                          Its the oposite
                          Closes[int barSeriesIndex][int barsAgo]

                          Comment


                            #14
                            Good catch, get the two mixed up occasionally. Will correct.
                            BrettNinjaTrader Product Management

                            Comment


                              #15
                              Thanks a lot to both of you, will try it out later today.

                              Have a great day.

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by argusthome, Yesterday, 10:06 AM
                              0 responses
                              20 views
                              0 likes
                              Last Post argusthome  
                              Started by NabilKhattabi, 03-06-2026, 11:18 AM
                              0 responses
                              18 views
                              0 likes
                              Last Post NabilKhattabi  
                              Started by Deep42, 03-06-2026, 12:28 AM
                              0 responses
                              14 views
                              0 likes
                              Last Post Deep42
                              by Deep42
                               
                              Started by TheRealMorford, 03-05-2026, 06:15 PM
                              0 responses
                              9 views
                              0 likes
                              Last Post TheRealMorford  
                              Started by Mindset, 02-28-2026, 06:16 AM
                              0 responses
                              38 views
                              0 likes
                              Last Post Mindset
                              by Mindset
                               
                              Working...
                              X