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

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
    Brandon H.NinjaTrader Customer Service

    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:	50
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


        #4
        Hello Madhava,

        Thanks for your notes.

        When a script's Calculate mode is set to Calculate.OnBarClose, the OnBarUpdate() logic will only process at the close of a bar.

        Calculate: https://ninjatrader.com/support/help.../calculate.htm

        Passing in a BarsAgo value of 0 when Calculate.OnBarClose is used means that you are accessing the value of the last closed bar on the chart, not the currently forming bar.

        So Close[0] would refer to the Close of the last bar that closed, not the Close price of the currently forming bar.

        Ultimately, if a script is not behaving as expected it is necessary to add debugging prints to the script that print out all the values in the script along with the time of the bar and CurrentBar.

        See this NinjaTrader Support article which details Debugging with Print(): https://support.ninjatrader.com/s/ar...nd-TraceOrders
        Brandon H.NinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by nicthe, Today, 02:58 PM
        1 response
        6 views
        0 likes
        Last Post nicthe
        by nicthe
         
        Started by percy3687, Yesterday, 12:28 AM
        3 responses
        30 views
        0 likes
        Last Post percy3687  
        Started by SilverSurfer1, Today, 01:33 PM
        0 responses
        9 views
        0 likes
        Last Post SilverSurfer1  
        Started by ETFVoyageur, Today, 10:27 AM
        2 responses
        18 views
        0 likes
        Last Post ETFVoyageur  
        Started by CommonWhale, Today, 01:12 PM
        0 responses
        9 views
        0 likes
        Last Post CommonWhale  
        Working...
        X