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

Need help with indicator that plots highs and lows during the hour

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

    Need help with indicator that plots highs and lows during the hour

    I have an indicator that almost works like I would like, but I can't figure out how to do what I want. I am not well versed in coding, but trying to find a solution here. I attached a screenshot of how the indicator functions currently and one of what I would like it to do. I only want it to plot the highest and lowest points during that hour, but currently it plots every new high and low during the hour. Using AddPlot(), but not sure if there is a different method.

    Hope someone can help.

    Code:
                protected override void OnBarUpdate()
            {
                int hour = Time[0].Hour;
                double currentHigh = High[0];
                double currentLow = Low[0];
    
                if (hour != currentHour)
                {
                    // Start of a new hour, reset high and low
                    currentHour = hour;
                    hourHighs[hour] = currentHigh;
                    hourLows[hour] = currentLow;
                }
                else
                {
                    // Update high and low if necessary
                    if (currentHigh > hourHighs[hour])
                        hourHighs[hour] = currentHigh;
    
                    if (currentLow < hourLows[hour])
                        hourLows[hour] = currentLow;
                }
    
                // Plot high and low for the entire hour
                Values[0][0] = hourHighs[hour];
                Values[1][0] = hourLows[hour];
            }​​
            }​
    Attached Files

    #2
    Hello i2ogu3,

    Does this have to be a plot and not a line?

    You want to wait for the hour to end, and then set the previous bar plot values within the previous hour to that high?

    Code:
    if (Time[0].Minute == 0 && Time[0].Second == 0)
    {
    int hourBeginBarsAgo = CurrentBar - Bars.GetBar(Time[0].AddHours(-1));
    if (hourBeginBarsAgo > 0)
    {
    double highestHigh = MAX(High, hourBeginBarsAgo)[0];
    for (int index = 0; index < hourBeginBarsAgo; index++)
    Value[index] = highestHigh;
    }
    }
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      I want the line to stay updated during the current hour as well. Keeping the line along the newest high and low. It works that way currently but plots each new high and low. I only would like the highest and lowest bar of that current hour. Not every new high.

      Comment


        #4
        Hello i2ogu3,

        Remove the condition that the Time[0].Minute and Time[0].Second must be 0.

        Use the Time[0] minus the Time[0].Minute to get the start of the hour.

        Loop from 0, until the index is less than or equal to the start bar.

        Code:
        int hourBeginBarsAgo = CurrentBar - Bars.GetBar(Time[0].AddMinutes(-1 * Time[0].Minute));
        if (hourBeginBarsAgo > 0)
        {
        double highestHigh = MAX(High, hourBeginBarsAgo)[0];
        ​for (int index = 0; index <= hourBeginBarsAgo; index++)
        Value[index] = highestHigh;
        }
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Originally posted by NinjaTrader_ChelseaB View Post
          Hello i2ogu3,

          Remove the condition that the Time[0].Minute and Time[0].Second must be 0.

          Use the Time[0] minus the Time[0].Minute to get the start of the hour.

          Loop from 0, until the index is less than or equal to the start bar.

          Code:
          int hourBeginBarsAgo = CurrentBar - Bars.GetBar(Time[0].AddMinutes(-1 * Time[0].Minute));
          if (hourBeginBarsAgo > 0)
          {
          double highestHigh = MAX(High, hourBeginBarsAgo)[0];
          ​for (int index = 0; index <= hourBeginBarsAgo; index++)
          Value[index] = highestHigh;
          }
          You are absolutely amazing. Works just like I was hoping now. I always see your name around fixing so much code. You made my day. Thank you.

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by Pep de lou, Today, 08:14 PM
          2 responses
          18 views
          0 likes
          Last Post bltdavid  
          Started by fingers, 12-15-2023, 08:44 AM
          6 responses
          136 views
          0 likes
          Last Post bltdavid  
          Started by algospoke, 05-13-2024, 06:53 PM
          4 responses
          33 views
          0 likes
          Last Post algospoke  
          Started by dtl-saw, 12-29-2022, 09:12 AM
          46 responses
          3,194 views
          1 like
          Last Post sonia0101  
          Started by AlphaOptions, 06-18-2013, 08:24 AM
          7 responses
          2,186 views
          0 likes
          Last Post NinjaTrader_Manfred  
          Working...
          X