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

Help with identifying the opening of larger bar interval, using Calculate.OnEachTick.

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

    Help with identifying the opening of larger bar interval, using Calculate.OnEachTick.

    Hello.

    I am attempting to identify different bar interval openings on a chart. For example, the primary bar series is an M15 EUR/USD chart. On that chart, I would like to identify the Daily Open and the 60 min openings on that M15 EUR/USD chart.

    Also of note, is that I will be creating this using Calculate.OnEachTick. I have read the guidance HERE, but am still having trouble. I have tried using additional bar series, but the opening event seems to be off, and I'm wondering if I can get the 60 min opening some way other than using additional bar series?

    For the Daily Open, I can find it this way, that does NOT require an additional bar series.

    Code:
                    if( Bars.IsFirstBarOfSession  )    //Daily Open 
                    {    
                        if (IsFirstTickOfBar)    //ONLY process this ONE TIME as a bar opens. HISTORICALLY, this block is processed I think on the close? 
                        {
                            Draw.VerticalLine(this, "D1"+CurrentBar, 0, Brushes.Green, DashStyleHelper.Dot, 1);
                            d1OpenBar = CurrentBar;
                            d1OpenPrice= Open[0];
                        }                    
                    }

    For the M60 Opening, is there another possible way of doing this reliably w/o needing to add an additional bar series to try and time the opening event?













    #2
    Hello forrestang,

    Thanks for your post.

    On historical bars, it will not run as Calculate.OnEachTick because an historical bar only has 1 tick that drives the onBarUpdate() and this is the end of bar.

    Is the issue you are facing with historical bars or real time?

    Are you trying to show the opening range in live data and want to see the line as the bars develop (meaning the line changes) or are you wanting to see the line only at the end of the 60 minute period?

    Do you know what time your session begins and have you considered using a time check to trigger drawing the line or just checking for for the range?



    Paul H.NinjaTrader Customer Service

    Comment


      #3
      Thanks NinjaTrader_PaulH So I got it sorted mostly after I posted.

      The issue I was having wasn't with onTick, it was with just getting a way to time the event of a 60min(or any sub-daily) chart's Open time on top of say a 15min chart. I initially tried to do it by adding additional bar series, but couldn't get that timed right. So this post was just really, what type of "if" statement would be used to know the OPEN of a a 60 min.

      So ur questions, about the sessions is a good one. What I have done is take the open time(Bars.IsFirstBarOfSession), creating a list/array with that initial time, and populating the rest of that array with the initial time + whatever TF I have as a user input. So each value in the list is incremented by n-minutes. I also have an index associated with that Array, so that each time:

      Code:
      Time[0]  >= myArray[nextIndex]
      It will plot the hourly line, and advance the index.

      So, to find the first bar of session, which is the first hourly bar, I use:
      Code:
                  if( Bars.IsFirstBarOfSession  )    //Daily Open
                  {    
                      if (IsFirstTickOfBar)    //ONLY process this ONE TIME as a bar opens. HISTORICALLY, this block is processed I think on the close?
                      {
                           Draw.VerticalLine(this, "D1"+CurrentBar, 0, Brushes.Green, DashStyleHelper.Dot, 1);
                           //Do some stuff ONLY one time as the new bar opens
                           //Calculate Array of Times for the day
                      }    
      
                      //Do other stuff down here on every tick of first bar of session, to include storing the hourly open
      
                  }

      Then to know if a new 60 minute bar has opened, I use

      Code:
                  else if( !Bars.IsFirstBarOfSession )    
                  {
                  //Indicates that we have found the opening of the H1 bar    
                      if (IsFirstTickOfBar && Time[0] >= TableBig[nextBigBarNDX] )    
                      {
      //                  Draw.VerticalLine(this, "H1"+CurrentBar, 0, Brushes.Orange, DashStyleHelper.Dot, 1);
                          nextBigBarNDX++;    //Advancing to the NEXT openTime once found
      
                      }
      
                      //Do other stuff down here that is NOT first tick of bar
                  }

      Attached u can see the image of the green line the plots the open of the Daily, and the orange that plots the Open of the Hourly, this is a 15min chart. Obviously, the hourly can be changed to any number that greater than the bar interval on the chart, and less than the daily.

      I checked this using market replay as well as watching it live today and it all seems to function properly.

      Not sure if this explanation is clear? But thanks for the help.
      Attached Files
      Last edited by forrestang; 03-31-2020, 03:47 PM.

      Comment


        #4
        Also, the function that populates the list that is used to store the times of the bar is:

        Code:
                public void PopulateTableBig( int myTF)
                {
                    int numOfElements = 1440/myTF + 1;
        
                    for (int i=0; i < numOfElements; i++)    //Exit on all but primary bar events, Not needed for loop over every series
                    {
                            TableBig.Add(Time[0].AddMinutes(i*BigTF)  ); //adding traded = false
                    }    
                }
        Last edited by forrestang; 03-31-2020, 03:50 PM.

        Comment


          #5
          Hello forrestang,

          Thanks for your replies.

          Glad you found a solution.

          Another approach might be:

          Code:
                  private int sessionBar;
                  private double maxHigh, maxLow;
                  private DateTime startTime;
          
                  protected override void OnBarUpdate()
                  {
                      if (Bars.IsFirstBarOfSession && IsFirstTickOfBar)
                      {
                          maxHigh = double.MinValue;
                          maxLow = double.MaxValue;
                          Draw.VerticalLine(this, "Sessionstart"+CurrentBar, 0, Brushes.Green);
                          startTime = Time[0];
                          sessionBar = CurrentBar;  // used for the line label to retain historical labels
                      }
          
                      if (Time[0] >= startTime && Time[0] <= startTime.AddHours(1))
                      {
                          maxHigh = High[0] > maxHigh ? High[0] : maxHigh;
                          maxLow = Low[0] < maxLow ? Low[0] : maxLow;    
                          Draw.Line(this, "high"+sessionBar, true, startTime, maxHigh, Time[0], maxHigh, Brushes.Red, DashStyleHelper.Solid,2);
                          Draw.Line(this, "low"+sessionBar, true, startTime, maxLow, Time[0], maxLow, Brushes.CornflowerBlue, DashStyleHelper.Solid,2);
                      }
                  }

          Click image for larger version

Name:	Forrest-1.PNG
Views:	291
Size:	27.1 KB
ID:	1092572
          Paul H.NinjaTrader Customer Service

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by judysamnt7, 03-13-2023, 09:11 AM
          4 responses
          55 views
          0 likes
          Last Post DynamicTest  
          Started by ScottWalsh, Today, 06:52 PM
          4 responses
          35 views
          0 likes
          Last Post ScottWalsh  
          Started by olisav57, Today, 07:39 PM
          0 responses
          7 views
          0 likes
          Last Post olisav57  
          Started by trilliantrader, Today, 03:01 PM
          2 responses
          19 views
          0 likes
          Last Post helpwanted  
          Started by cre8able, Today, 07:24 PM
          0 responses
          7 views
          0 likes
          Last Post cre8able  
          Working...
          X