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

High between two times with Getbar

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

    High between two times with Getbar

    Hi there,

    I am trying to get the high between two times, say 9:30 and 9:40 using tick charts.

    Can I use Getbar like I have below to achieve this?

    Code:
    startDateTime = new DateTime(Time[0].Year, Time[0].Month, Time[0].Day, 9, 30, 0);
    
    if ((ToTime(Time[0]) == 94000)) 
    {
    	
      int ibarsAgo = Currentbar - Bars.GetBar(startDateTime);
    				
      openingRangeHigh =  MAX(High, ibarsAgo)[0];
      openingRangeLow = MIN(Low, ibarsAgo)[0];
    					
    // Draw
    
    }

    #2
    Hi ij001,

    Using Bars.GetBar() should work fine.

    Are you not getting the value you are looking for?
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_ChelseaB View Post
      Hi ij001,

      Using Bars.GetBar() should work fine.

      Are you not getting the value you are looking for?
      Yes. It appears that

      Code:
      if (ToTime(Time[0]) == 94000)
      wont work with tick charts?

      Comment


        #4
        Hi ij001,

        This will work as long as there is a bar that is stamped exactly 9:40 AM.

        If there is no bar at that time, the condition will not be true. (Meaning it can be skipped)

        Try using >= instead of == so you get the next bar after 9:40.
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Originally posted by NinjaTrader_ChelseaB View Post
          Hi ij001,

          This will work as long as there is a bar that is stamped exactly 9:40 AM.

          If there is no bar at that time, the condition will not be true. (Meaning it can be skipped)

          Try using >= instead of == so you get the next bar after 9:40.
          Hi Chelsea,

          Yes, I see what you mean, thanks.

          I also noticed in my code below, the use of GetBar was incorrect and I have fixed it.

          The only issue I am having is with the DrawFibRetracement and having it draw once during that time.

          Code:
          timeOfInterest = new DateTime(Time[0].Year, Time[0].Month, Time[0].Day, 9, 30, 0);
          			
          			if 	(
          				(ToTime(Time[0]) >= 93000) &&
          				(ToTime(Time[0]) <= 94000)
          				)
          			{
          				barsAgo = GetBar(timeOfInterest);
          				Print(barsAgo);
          				
          				openingRangeHigh = MAX(High, barsAgo)[0];
          				openingRangeLow = MIN(Low, barsAgo)[0];
          				
          				DrawFibonacciRetracements("Default", false, startDateTime, openingRangeHigh, endDateTime, openingRangeLow);
          			}
          The above code, will only draw once on the entire chart because the call to DrawFib tag is "Default" not "Default"+CurrentBar.

          If I call "Default"+CurrentBar it will draw over and over.

          How can I print once each day with CurrentBar?

          Comment


            #6
            Originally posted by ij001 View Post
            Hi Chelsea,

            Yes, I see what you mean, thanks.

            I also noticed in my code below, the use of GetBar was incorrect and I have fixed it.

            The only issue I am having is with the DrawFibRetracement and having it draw once during that time.

            Code:
            timeOfInterest = new DateTime(Time[0].Year, Time[0].Month, Time[0].Day, 9, 30, 0);
                        
                        if     (
                            (ToTime(Time[0]) >= 93000) &&
                            (ToTime(Time[0]) <= 94000)
                            )
                        {
                            barsAgo = GetBar(timeOfInterest);
                            Print(barsAgo);
                            
                            openingRangeHigh = MAX(High, barsAgo)[0];
                            openingRangeLow = MIN(Low, barsAgo)[0];
                            
                            DrawFibonacciRetracements("Default", false, startDateTime, openingRangeHigh, endDateTime, openingRangeLow);
                        }
            The above code, will only draw once on the entire chart because the call to DrawFib tag is "Default" not "Default"+CurrentBar.

            If I call "Default"+CurrentBar it will draw over and over.

            How can I print once each day with CurrentBar?
            Code:
            private IFibonacciRetracements fR;
            Code:
            if (fR.Tag != Time[0].Date.ToString("d")) fR = DrawFibonacciRetracements(Time[0].Date.ToString("d"), false, startDateTime, openingRangeHigh, endDateTime, openingRangeLow);

            Comment


              #7
              Hi ij001,

              If you want to control when there is a new drawing object appears, use your own custom variable that is incremented when you like.

              For example:
              In #region Variables:
              private int newFib = 0;
              private bool once = false;

              In OnBarUpdate():
              if (ToTime(Time[0]) < 93000 && ToTime(Time[0]) > 94000 && once == false)
              {
              newFib++;
              once = true;
              }

              if (ToTime(Time[0]) >= 93000 &&ToTime(Time[0]) <= 94000 )
              {
              barsAgo = GetBar(timeOfInterest);
              Print(barsAgo);

              openingRangeHigh = MAX(High, barsAgo)[0];
              openingRangeLow = MIN(Low, barsAgo)[0];

              DrawFibonacciRetracements("Default"+newFib, false, startDateTime, openingRangeHigh, endDateTime, openingRangeLow);
              once = false;
              }
              Chelsea B.NinjaTrader Customer Service

              Comment


                #8
                Originally posted by koganam View Post
                Code:
                private IFibonacciRetracements fR;
                Code:
                if (fR.Tag != Time[0].Date.ToString("d")) fR = DrawFibonacciRetracements(Time[0].Date.ToString("d"), false, startDateTime, openingRangeHigh, endDateTime, openingRangeLow);
                Was this line to be added after my DrawFibs line of code?

                I added it afterwards and no Fibs show up.

                Comment


                  #9
                  What is an example of this line of code?

                  if (ToTime(Time[0]) < 93000 && ToTime(Time[0]) > 94000 && once == false)

                  Comment


                    #10
                    Hi ij001,

                    This code:
                    Code:
                    if (ToTime(Time[0]) < 93000 && ToTime(Time[0]) > 94000 && once == false)
                    {
                    newFib++;
                    once = true;
                    }
                    Checks to see if the time is outside the hours you have specified in post #5 and increments the newFib variable so that the tag used for the fibonacci changes and allows for an additional fibonacci to be drawn.

                    The once variable, ensures that this variable is incremented once and not for every bar when outside of the specified hours.
                    Chelsea B.NinjaTrader Customer Service

                    Comment


                      #11
                      I see. I have used that code and it displays once on the current day but still does not display on previous days. I am not sure why since newfib is a unique id like CurrentBar?

                      Comment


                        #12
                        ij001,

                        Could you provide what you have written so far?
                        Cal H.NinjaTrader Customer Service

                        Comment


                          #13
                          Originally posted by NinjaTrader_Cal View Post
                          ij001,

                          Could you provide what you have written so far?
                          Yes please see below. I separated it into two snippets.
                          Code:
                          private double		openingRangeHigh		= 0;
                          private double		openingRangeLow		= 0;
                          private DateTime startDateTime; 
                          private DateTime endDateTime;
                          private DateTime timeOfInterest;
                          private int barsAgo2 = 0;
                          private int counter = 0;
                          private bool once = false;
                          Code:
                          if(CurrentBar < BarsRequired)
                          	return;
                          
                          startDateTime = new DateTime(Time[0].Year, Time[0].Month, Time[0].Day, 9, 30, 0);
                          endDateTime = new DateTime(Time[0].Year, Time[0].Month, Time[0].Day, 23, 00, 0);
                          timeOfInterest = new DateTime(Time[0].Year, Time[0].Month, Time[0].Day, 9, 30, 0);
                          
                          if (ToTime(Time[0]) < 93000 && ToTime(Time[0]) > 94000 && once == false)
                          {
                          	counter++;
                          	once = true;
                          }
                          			
                          if 	(
                          	BarsPeriod.Id == PeriodType.Tick &&
                          	(ToTime(Time[0]) >= 93000) &&
                          	(ToTime(Time[0]) <= 94000)
                          	)
                          {
                          	barsAgo2 = GetBar(timeOfInterest);
                          				
                          	openingRangeHigh = MAX(High, barsAgo2)[0];
                          	openingRangeLow = MIN(Low, barsAgo2)[0];
                          				
                          	DrawFibonacciRetracements("Default"+counter, false, startDateTime, openingRangeHigh, endDateTime, openingRangeLow);				
                          
                          	once = false;
                          }

                          Comment


                            #14
                            ij001,

                            Try using CurrentBar instead of Counter for the DrawObject tag name -
                            Code:
                            				DrawFibonacciRetracements("Default"+CurrentBar, false, startDateTime, openingRangeHigh, endDateTime, openingRangeLow);
                            Cal H.NinjaTrader Customer Service

                            Comment


                              #15
                              Originally posted by NinjaTrader_Cal View Post
                              ij001,

                              Try using CurrentBar instead of Counter for the DrawObject tag name -
                              Code:
                              				DrawFibonacciRetracements("Default"+CurrentBar, false, startDateTime, openingRangeHigh, endDateTime, openingRangeLow);
                              Hi Cal,

                              I usually use "Default"+CurrentBar but here I am using Tick Charts so I can not simply use "Default"+CurrentBar because it will draw multiple times from barsAgo2.

                              Cheslea was discussing using a counter but it only appears to work on the current day.

                              I have an extra couple of lines of code where the fib will draw on all days with "Default"+CurrentBar but it prints multiple times so the lines are quite dark.

                              Code:
                              if 	(
                              				BarsPeriod.Id == PeriodType.Tick &&
                              				(ToTime(Time[0]) >= 93000) &&
                              				(ToTime(Time[0]) <= 94000)
                              				)
                              			{
                              				barsAgo2 = GetBar(timeOfInterest);
                              				
                              				openingRangeHigh = MAX(High, barsAgo2)[0];
                              				openingRangeLow = MIN(Low, barsAgo2)[0];
                              			}
                              			
                              			if 	(
                              				BarsPeriod.Id == PeriodType.Tick &&
                              				ToTime(Time[0]) > 94000
                              				)
                              			{
                              				DrawFibonacciRetracements("Default"+CurrentBar, false, startDateTime, openingRangeHigh, endDateTime, openingRangeLow);				
                              			}
                              Perhaps you can suggest how I can get it to print only once instead of each time after 9:40. Ideally I would like to just draw the lines on the last call to barsAgo2...

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by WeyldFalcon, 08-07-2020, 06:13 AM
                              11 responses
                              1,422 views
                              0 likes
                              Last Post jculp
                              by jculp
                               
                              Started by RubenCazorla, Today, 09:07 AM
                              0 responses
                              4 views
                              0 likes
                              Last Post RubenCazorla  
                              Started by BarzTrading, Today, 07:25 AM
                              2 responses
                              29 views
                              1 like
                              Last Post BarzTrading  
                              Started by devatechnologies, 04-14-2024, 02:58 PM
                              3 responses
                              21 views
                              0 likes
                              Last Post NinjaTrader_BrandonH  
                              Started by tkaboris, Today, 08:01 AM
                              0 responses
                              6 views
                              0 likes
                              Last Post tkaboris  
                              Working...
                              X