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

Creating Time Based Breakout Range

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

    Creating Time Based Breakout Range

    Hello, I am trying to create a strategy that takes a two points in time and creates a range from them. This is the thread that I am using to help reference, but I am a little confused: http://www.ninjatrader.com/support/f...ad.php?t=63957. Possibly part of the confusion is that I am trying to take what this guy was trying to do and create a variable out of it rather than an if statement. So if you know of a better reference for me, that would be really great!

    Basically I want to say, "Find the high between 12:00 AM and 8:59 AM." Then I want to assign that to the variable rangeHigh and I would like to do the same thing for the Low. It will be trading FX on the 1 hour chart.

    Thanks for your help!

    #2
    Hello jg123,

    Thanks for your note.

    The link that you have given is to a thread where someone is attempting to enter trades based on time criteria. What you are trying to do is a bit different. You will still need the times for this but also you will need to find a bar number.

    I'm creating a sample for you right now that you can use.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Hi jg123,

      I've created a sample for your that prints a dot on the highest bar within two dates and times.

      Let me know if you have any questions with this sample.
      Attached Files
      Chelsea B.NinjaTrader Customer Service

      Comment


        #4
        Thank you for this...and, wow! that is a lot of information in that file just to get the high/low.

        I will have good look through it.

        Thank you for your help

        Comment


          #5
          Okay, I have finally had some time to look through this to see what you have done. Thank you very much, by the way.

          I am attaching a screenshot that is showing that it seems to not allow it to adjust the time - looks like there is no problem with adjusting the date.

          With that, I have gone in to look at the Properties to see if I could change that myself and I just want to get a bit of confirmation so that I don't screw things up.

          My thought is to simply separate the date and the time so there is a Start Date and a Start Time as well as an End Date and an End Time in 4 different cells. Does this seem like a "best practices" way of doing things, or is there a better way?

          Here is the current code
          Code:
                  //#region Properties
          		[XmlIgnore()]
          		[GridCategory("Parameters")]
          		[Description("Start date and time")]
          		public DateTime StartTime {
          			get { return startTime; }
          			set { startTime = value; }
          		}
          		[Browsable(false)]
          		public string StartTimeSerialize {
          			get { return startTime.ToString("yyyy-MM-ddTHH:mm:sszzz"); }
          			set { startTime = DateTime.Parse(value); }
          		}
          		
          		[XmlIgnore()]
          		[GridCategory("Parameters")]
          		[Description("End date and time")]
          		public DateTime EndTime {
          			get { return endTime; }
          			set { endTime = value; }
          		}
          		[Browsable(false)]
          		public string EndTimeSerialize {
          			get { return endTime.ToString("yyyy-MM-ddTHH:mm:sszzz"); }
          			set { endTime = DateTime.Parse(value); }
          		}
          But, before I go through the work of figuring out how to do that, am I just missing something in how to change the start end time?
          Attached Files
          Last edited by jg123; 06-02-2014, 07:34 AM. Reason: forgot screenshot

          Comment


            #6
            I have tried adding the Low to this as well but nothing has happened. Here is the code that I have put in there:

            Code:
                    protected override void OnBarUpdate()
            		{
            			if (CurrentBar != Bars.GetBar(endTime))
            				return;
            			
            			highBarAgo = HighestBar(High, CurrentBar-Bars.GetBar(startTime));
            			DrawDot("highestBar", true, highBarAgo, High[highBarAgo]+3*TickSize, Color.Green);
            			lowBarAgo = LowestBar(Low,CurrentBar-Bars.GetBar(startTime));
            			DrawDot("lowestBar", true, lowBarAgo, Low[lowBarAgo]-3*TickSize, Color.Red);
            		}
            I also added the variable:

            Code:
            		private int lowBarAgo = 0;
            could you please let me know why this is not giving me a dot at the low of the time range?

            Comment


              #7
              Hi jg123,

              You are correct, the date selector will change the date but there is not a selector for the time.

              However, you can simply type the time you want in HH:MM AM/PM form after the date.

              It would also be possible separate these, but if you did and the time was by itself, there still is not a time selector and you would still have to type the time in.

              But yes, it is possible to separate them if wanted.
              Chelsea B.NinjaTrader Customer Service

              Comment


                #8
                Great thank you.

                I am wondering if you have an answer for post #6. It may be possible that you missed it or just have not had the time to answer yet. Either way is fine, but if you could let me know what I am doing wrong there, that would be really wonderful.

                Comment


                  #9
                  Hello,

                  Try adding a print to find out what the period is that is used for LowestBar.
                  Just before where Lowestbar is used add:

                  Print(CurrentBar-Bars.GetBar(startTime));

                  Let me know what prints out.
                  Chelsea B.NinjaTrader Customer Service

                  Comment


                    #10
                    For whatever reason, it worked this time. I have no idea what I did differently. haha. Thanks for your help

                    Comment


                      #11
                      Something else that is interesting, is that on the screenshot in Post #5, you can see that the "End Time" is in the top cell and the "Start Time" in the second cell. That doesn't quite make sense because in the code, the start time comes first.

                      Can you let me know how to change those two around? The code is still exactly the same as it is in Post #5

                      Comment


                        #12
                        Hi jg123,

                        The parameters are listed alphabetically. If you want to change the order add a number to the beginning of the variable name by using the DisplayNameAttribute.

                        For example:
                        Code:
                        [GridCategory("Parameters")]
                        [Gui.Design.DisplayNameAttribute("1 Start Time")]
                        public DateTime StartTime
                        {
                        get { return startTime; }
                        set { startTime = value; }
                        }
                        
                        [GridCategory("Parameters")]
                        [Gui.Design.DisplayNameAttribute("2 End Time")]
                        public DateTime EndTime
                        {
                        get { return endTime; }
                        set { endTime = value; }
                        }
                        Chelsea B.NinjaTrader Customer Service

                        Comment


                          #13
                          Next Question on this

                          I am running into an error when turning this into a strategy. Currently it is requiring me to create to sets of variables for the start and end times of the range.

                          It is not allowing me to compare the current time of the bar with the DateTime so I have had to create an integer with which to compare. But I think that the way that I am going about it is not the most efficient method. Here is the relevant code that I am working with:

                          Variables:
                          Code:
                          private int startRange; // Hour that the range will start
                                 	private int endRange; // Hour that the range will end
                          		private int removeOrderHr = 18; // Hour that unfilled orders will be removed
                          		private double rangeHigh = 0; // High of overnight range
                          		private double rangeLow = 0; // Low of overnight range
                          		private DateTime startTime = DateTime.Now.AddHours(-5); // start of range for determining High
                          		private DateTime endTime = DateTime.Now; // start of range for determing Low
                          Here is the logic:

                          Code:
                                  protected override void OnBarUpdate()
                                  {
                          			rangeHigh = HighestBar(High, CurrentBar-Bars.GetBar(startTime));
                          			rangeLow = LowestBar(Low, CurrentBar-Bars.GetBar(startTime));
                          			
                          			switch (trendDirection)
                          			{
                          				case trendDirection = noTrend:
                          				{
                          					if (ToTime(Time[0]) > endRange
                          						&& ToTime(Time[0]) < removeOrderHr)
                          					{}
                          				}
                          (note: There is another error in this code that may or may not be pertinent information for this question. It refers to the "case trendDirection = noTrend:" I am still working on fixing this error - and have written about it in this forum and can be found at http://www.ninjatrader.com/support/f...ad.php?t=66658)

                          The goal of what I am trying to do is rather simple. Create a time based range in which the High and the Low of that time will be stored as a variable. Then, if the current time is after XX:XX and before XX:XX then place orders above the High and/or Below the Low of the High/Low of that range.

                          It seems as though I will have to put into the parameters the start and end time of the range twice in order to get this code to work. Could you please let me know if I am doing this the most efficiently or if there is a different way that I should be referencing the time range in order to create the breakout?
                          Last edited by jg123; 06-09-2014, 04:38 AM. Reason: attempt to make question more clear

                          Comment


                            #14
                            Hi jg123,

                            Time[0] is also a DateTime object that should compare with another DateTime object.

                            If you use ToTime(Time[0]) this returns the time in an integer. This also cuts off the date which is useful.

                            However, the following should work if you are including the date.

                            if (Time[0] > endTime)

                            OR

                            if (ToTime(Time[0]) > ToTime(endTime))
                            Chelsea B.NinjaTrader Customer Service

                            Comment


                              #15
                              The error that I am getting with this is

                              Operator '>' cannot be applied to operands of type 'System.DateTime' and 'int'

                              The endTime variable is currently an int which, clearly that is the where my issue is popping up - but what should I be changing that to then? I don't understand how make that variable

                              I have also tried, instead of typing "int" or "double" or another common variable type, I have also tried making it a DateTime object by typing

                              private DateTime endTime;

                              What are your thoughts?

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by cmtjoancolmenero, Yesterday, 03:58 PM
                              1 response
                              17 views
                              0 likes
                              Last Post NinjaTrader_Gaby  
                              Started by benmarkal, Yesterday, 12:52 PM
                              3 responses
                              23 views
                              0 likes
                              Last Post NinjaTrader_Gaby  
                              Started by helpwanted, Today, 03:06 AM
                              1 response
                              20 views
                              0 likes
                              Last Post sarafuenonly123  
                              Started by Brevo, Today, 01:45 AM
                              0 responses
                              12 views
                              0 likes
                              Last Post Brevo
                              by Brevo
                               
                              Started by pvincent, 06-23-2022, 12:53 PM
                              14 responses
                              244 views
                              0 likes
                              Last Post Nyman
                              by Nyman
                               
                              Working...
                              X