Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Multiple Timeframe Entry and Exit

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

    Multiple Timeframe Entry and Exit

    I'm using 1 min and 5 min bars. On the entry, I want to use 5 min bar. No problem coding that. For the exit, I want to use the 5 min bars but calculate it every 1 min. My reasoning is that when the price is going up, it can cross over the exit point and go back down before the end of the 5 min bar.

    Here is a sample code:
    protected override void OnBarUpdate()
    {

    if (BarsInProgress == 1) {
    if (Position.MarketPosition == MarketPosition.Flat) {
    if (SMA(BarsArray[1])[0] < 50)
    {
    EnterLong();
    }
    }
    }

    if (BarsInProgress == 0) {
    if (Position.MarketPosition == MarketPosition.Long) {
    BarsArray[1][0] = BarsArray[2][0]; // Assign the last 1 mins bar value to the 5 mins bar (Unable to change the BarsArray value because it's readonly
    if (SMA(BarsArray[1])[0] > 50)
    {
    ExitLong();
    }
    }
    }
    }

    Andy

    #2
    Andy, if you are working in the BarsInProgress of the 1 min series you would see a call to OnBarUpdate() already each minute / each bar directly - so in backtesting you can for example calculate the SMA with a longer length on the 1 min series simulating the higher time frame SMA and then monitor for crossovers here for your exit.

    Comment


      #3
      I have tried your suggestion before, but the output I got was incorrect. I would get the same values for the 5 min bars 5 times. If I output the SMA() values, I would get this.

      45.1 <- 5 min bars
      47.3 <- 1 min bars
      47.3 <- 1 min bars
      47.3 <- 1 min bars
      47.3 <- 1 min bars
      47.3 <- 1 min bars
      85.6 <- 5 min bars
      65.3 <- 1 min bars
      65.3 <- 1 min bars
      65.3 <- 1 min bars
      65.3 <- 1 min bars
      65.3 <- 1 min bars

      Comment


        #4
        What code were you using please? So you were actually calling the SMA on the 1 min series with 5 times the length that would be using on the 5?

        Comment


          #5
          if (BarsInProgress == 0) {
          if (Position.MarketPosition == MarketPosition.Long) {
          Log("Time = " + Time[0] + ", " + Instrument.MasterInstrument.Name + ", RSI = " + RSI(BarsArray[1],2,0)[0], NinjaTrader.Cbi.LogLevel.Information);
          if (RSI(BarsArray[1],2,0)[0] > 75) {
          Log("Time = " + Time[0] + ", Selling " + Position.Quantity + " " + Instrument.MasterInstrument.Name + ", RSI = " + RSI(BarsArray[1],2,0)[0], NinjaTrader.Cbi.LogLevel.Information);
          ExitLong();
          }

          }
          }

          Time = 8/14/2013 9:20:00 AM, TDS, RSI = 92.8511295908075
          Time = 8/14/2013 9:19:00 AM, TDS, RSI = 85.1613014007924
          Time = 8/14/2013 9:18:00 AM, TDS, RSI = 85.1613014007924
          Time = 8/14/2013 9:17:00 AM, TDS, RSI = 85.1613014007924
          Time = 8/14/2013 9:15:00 AM, TDS, RSI = 85.1613014007924
          Time = 8/14/2013 9:14:00 AM, TDS, RSI = 47.5453818303917
          Time = 8/14/2013 9:13:00 AM, TDS, RSI = 47.5453818303917
          Time = 8/14/2013 9:11:00 AM, TDS, RSI = 47.5453818303917
          Time = 8/14/2013 9:10:00 AM, TDS, RSI = 47.5453818303917
          Time = 8/14/2013 9:09:00 AM, TDS, RSI = 47.5453818303917
          Time = 8/14/2013 9:08:00 AM, TDS, RSI = 47.5453818303917
          Time = 8/14/2013 9:07:00 AM, TDS, RSI = 47.5453818303917
          Time = 8/14/2013 9:06:00 AM, TDS, RSI = 47.5453818303917
          Time = 8/14/2013 9:05:00 AM, TDS, RSI = 47.5453818303917
          Time = 8/14/2013 9:04:00 AM, TDS, RSI = 0.0273704928197134
          Time = 8/14/2013 9:03:00 AM, TDS, RSI = 0.0273704928197134
          Time = 8/14/2013 9:01:00 AM, TDS, RSI = 0.0273704928197134
          Time = 8/14/2013 9:00:00 AM, TDS, RSI = 0.0273704928197134
          Time = 8/14/2013 8:59:00 AM, TDS, RSI = 0.0500323437923242
          Time = 8/14/2013 8:58:00 AM, TDS, RSI = 0.0500323437923242
          Time = 8/14/2013 8:57:00 AM, TDS, RSI = 0.0500323437923242
          Last edited by mechinvestor; 08-15-2013, 06:47 AM.

          Comment


            #6
            Ok, so which is primary now? You would need to process in the lower timeframe directly, so if your primary is a 5 min chart and you added in the 1 min, you would process the indicator on the BarsArray[1] data in BarsInProgress == 1, however your length would need to be set longer to reflect calling this from the finer series and simulating the higher timeframe this way. This will not be exact though, but an approximation.

            Comment


              #7
              My primary is 1 min, and I added 5 min bars. My code is already doing exactly what you said. You can see in the output. Here is the Initialize method.

              protected override void Initialize()
              {
              // Add a 5 minute Bars object to the strategy
              Add(PeriodType.Minute, 5);
              }

              Comment


                #8
                Ok, thanks - the issue is that you pass in BarsArray[1] so 5 min datapoints. You would need to run the indicator on actual 1 min data with a longer length to approximate.

                Comment


                  #9
                  I want to calculate the RSI on the 5 min bar but getting the value every 1 min. What do you mean running longer length to approximate ? Increasing the length would not get you the same or similar values. Can you send sample code what you mean ?

                  Comment


                    #10
                    Is there another way to do this ?

                    BarsArray[1][0] = BarsArray[0][0]; // Assign the last 5 min bar the last 1 min bar

                    That would allow me to calculate the RSI every 1 min using the 5 min bar.

                    Comment


                      #11
                      This will unfortunately not work, you could not look inside the bar with this technique for historical data, only for realtime working on CalculateOnBarClose = false.

                      Simulating with longer lengths usually works ok with price based studies like an SMA, but I've checked for your RSI and then unfortunately stronger value differences in the outputs would be seen.

                      Comment

                      Latest Posts

                      Collapse

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