Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Evaluating bars on a 5min chart

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

    Evaluating bars on a 5min chart

    Hello,

    I'm working on a strategy that evaluates the first few opening bars(6:30am PST to 7:00am PST, 5 minute chart) on the S&P500. The code below does work. However, I'd like to evaluate the bars at 6:59:00AM. When I change the time to 6:59:00, the strategy won't detect any patterns, it does detect several of them when I leave the time at 7:00:00.

    I've tried adding a second data series
    Code:
    AddDataSeries("^SP500", BarsPeriodType.Minute, 1);
    . And changing the 'DateTime now' to [CODE][DateTime now = Times[1][0];/CODE]. But when I do that I get an out-of-range error.

    So I would like to evaluate the bars on the 5-minute chart, but at 6:59am. Is there any way to do this?

    Thank you in advance.

    -Madhava







    Full code
    Code:
    namespace NinjaTrader.NinjaScript.Strategies
    {
        public class DEV_unlocked : Strategy
        {
    
    protected override void OnStateChange()
    {
        if (State == State.SetDefaults)
        {
            Description = @"Enter the description for your strategy here.";
            Name = "Dev_unlocked"; // Give your strategy a unique name
    
            // Set other defaults as needed
        }
        else if (State == State.Configure)
        {
            
        }
    }
    
    protected override void OnBarUpdate()
    {
        // Set up time for checking the pattern
        DateTime now = Times[0][0];
        DateTime checkVPatternTime = new DateTime(now.Year, now.Month, now.Day, 7, 00, 00);
    
        // Check if it's the correct time to evaluate the pattern
        if (now.TimeOfDay == checkVPatternTime.TimeOfDay)
        {
            // Index of the first bar
            int firstBarIndex = 5;
            // Capture high and low from the first bar at 6:30 AM
            double firstCandleHigh = High[firstBarIndex];
            double firstCandleLow = Low[firstBarIndex];
    
            // Paint the first bar Ivory white
            BarBrushes[firstBarIndex] = Brushes.Ivory;
    
            // Define the allowable deviation
            double allowableDeviation = 1.5;
    
            // Boolean to track if a V pattern is detected
            bool vPatternDetected = false;
    
            // Variable to track if there was at least one or two closes above the first candle's high
            bool closeAboveFirstCandleHigh = false;
    
            // Iterate over the next candles to check for a V pattern
            for (int i = firstBarIndex - 1; i >= 0; i--)
            {
                // Check if the current candle closes above the first candle's high
                if (Close[i] > firstCandleHigh)
                {
                    closeAboveFirstCandleHigh = true;
                    // Continue to the next iteration to only check for retracement after this condition is met
                    continue;
                }
    
                // If a candle closed above the first candle's high, look for the retracement
                if (closeAboveFirstCandleHigh && Low[i] <= firstCandleLow + allowableDeviation && Low[i] >= firstCandleLow - allowableDeviation)
                {
                    // If retracement to near the low of the first candle is found after a higher close
                    vPatternDetected = true;
    
                    // Shade the bar where V pattern is confirmed
                    BarBrushes[i] = Brushes.Yellow;
                    Print("V pattern detected on " + now.ToString("MM/dd/yy "));
    
                    break;
                }
            }
        }
    }
    }
    }
    ​

    #2
    Hello madhava,

    Thanks for your post.

    You could add a 1-Minute data series to your script, check if BarsInProgress == 1 is processing, and create a Time Comparison condition to see if the time (Times[1][0]) is equal to 6:59 AM.

    I see that you do not have a CurrentBars check in the script to ensure you have enough bars processing for the data you are trying to access.

    Add a CurrentBars check to the script to make sure you have enough bars for the data you are accessing.

    See the help guide documentation below for more information.

    CurrentBars: https://ninjatrader.com/support/help...urrentbars.htm
    Make sure you have enough bars: https://ninjatrader.com/support/help...nough_bars.htm
    BarsInProgress: https://ninjatrader.com/support/help...inprogress.htm
    Time Comparison Conditions: https://ninjatrader.com/support/help...imeComparisons
    Working with Multi-TimeFrame/Multi-Instrument NinjaScripts: https://ninjatrader.com/support/help...nstruments.htm
    <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

    Comment


      #3
      Hi Brandon,

      Thanks for the quick reply, and the good advice.

      So it does start to look for the pattern. But now instead of checking the 6:30:00am PST candle to the 7:00:00am candle, it's checking 5 minutes before. See the attached pic.

      When I set the strategy to execute at 7:00am, Close[5] would be the 6:30am candle. If it's now set to execute at 6:59:00am Close[0][5] (primary index) it's now the 13:00pm candle.

      Since the 5-minute candle closing at 7:00 isn't fully formed, and I execute at 6:59, does that affect the index numbers of Close[ ], High [ ], etc.

      I hope that's clear.

      Thanks again,
      Madhava


      Click image for larger version

Name:	Screenshot 2024-04-15 144808.png
Views:	205
Size:	7.6 KB
ID:	1299668



      Full strategy code:​
      Code:
      namespace NinjaTrader.NinjaScript.Strategies
      {
          public class DEV_unlocked : Strategy
          {
              protected override void OnStateChange()
              {
                  if (State == State.SetDefaults)
                  {
                      Description = @"Enter the description for your strategy here.";
                      Name = "Dev_unlocked"; // Give your strategy a unique name
      
                      // Set other defaults as needed
                  }
                  else if (State == State.Configure)
                  {
                      AddDataSeries("^SP500", BarsPeriodType.Minute, 1);
                      AddDataSeries("^SP500", BarsPeriodType.Minute, 5); // Add 5-minute series
                  }
              }
      
              protected override void OnBarUpdate()
              {
                  // Check if BarsInProgress is processing the 1-minute series
                  if (BarsInProgress == 1)
                  {
                      // Set up time for checking the pattern
                      DateTime now = Times[1][0];
      
                      // Check if it's 6:59 AM
                      if (now.Hour == 6 && now.Minute == 59)
                      {
                          Print(now.TimeOfDay + " line 37");
      
                          // Continue processing only if we have enough bars on the 5-minute chart
                          if (CurrentBars[0] < 6) return;
      
                          // Index of the first bar
                          int firstBarIndex = 5;
                          // Capture high and low from the first bar at 6:30 AM on the 5-minute chart
                          double firstCandleHigh = Highs[0][firstBarIndex];
                          double firstCandleLow = Lows[0][firstBarIndex];
      
                          // Paint the first bar Ivory white
                          BarBrushes[firstBarIndex] = Brushes.Ivory;
      
                          // Define the allowable deviation
                          double allowableDeviation = 1.5;
      
                          // Boolean to track if a V pattern is detected
                          bool vPatternDetected = false;
      
                          // Variable to track if there was at least one or two closes above the first candle's high
                          bool closeAboveFirstCandleHigh = false;
      
                          // Iterate over the next candles to check for a V pattern
                          for (int i = firstBarIndex - 1; i >= 0; i--)
                          {
                              // Check if the current candle closes above the first candle's high
                              if (Closes[0][i] > firstCandleHigh)
                              {
                                  closeAboveFirstCandleHigh = true;
                                  // Continue to the next iteration to only check for retracement after this condition is met
                                  continue;
                              }
      
                              // If a candle closed above the first candle's high, look for the retracement
                              if (closeAboveFirstCandleHigh && Lows[0][i] <= firstCandleLow + allowableDeviation && Lows[0][i] >= firstCandleLow - allowableDeviation)
                              {
                                  // If retracement to near the low of the first candle is found after a higher close
                                  vPatternDetected = true;
      
                                  // Shade the bar where V pattern is confirmed
                                  BarBrushes[i] = Brushes.Yellow;
                                  Print("V pattern detected on " + now.ToString("MM/dd/yy "));
      
                                  break;
                              }
                          }
                      }
                  }
              }
          }
      }
      ​

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by NullPointStrategies, Yesterday, 05:17 AM
      0 responses
      54 views
      0 likes
      Last Post NullPointStrategies  
      Started by argusthome, 03-08-2026, 10:06 AM
      0 responses
      130 views
      0 likes
      Last Post argusthome  
      Started by NabilKhattabi, 03-06-2026, 11:18 AM
      0 responses
      72 views
      0 likes
      Last Post NabilKhattabi  
      Started by Deep42, 03-06-2026, 12:28 AM
      0 responses
      44 views
      0 likes
      Last Post Deep42
      by Deep42
       
      Started by TheRealMorford, 03-05-2026, 06:15 PM
      0 responses
      49 views
      0 likes
      Last Post TheRealMorford  
      Working...
      X