• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.


No announcement yet.

Partner 728x90


Time [] & ToTime()

  • Filter
  • Time
  • Show
Clear All
new posts

    Time [] & ToTime()

    Do Time and/or ToTime ever look at only the computer time or do they always look at the chart time?

    Reason being, the computer time may be 9:30 AM but the time stamp on the 1 hour candle would be 10:00 AM on the chart.

    Hello jg123,

    When using Time[0] this is simply referring to the datetime object stored in the bar it is currently accessing. So to answer your question, Time[] is looking at the bar time and not your computer time.

    ToTime and also ToDay convert the datetime object into a integer value to make comparing times or dates easy.

    If you are looking to get the system time you do have access to the C# datetime methods.
    Here is a MSDN page about Datetime in C#

    Please let me know if I may be of additional assisstance.
    JesseNinjaTrader Customer Service


      Great thank you

      This does lead me to 2 questions.

      In my code I use both Time & ToTime.

      In my user defined Parameters, I have a start time designed to be the beginning of a range. The Parameters code that I use is here:

      		[Gui.Design.DisplayName("1 Start Range")]
              public DateTime StartRange
                  get { return startRange; }
                  set { startRange = value; }
      Here is how startRange is calculated in the Variables:

              private DateTime startRange = DateTime.Today.AddHours(2); // Hour that the range will start
      but, whenever I bring up the properties window in order to start the strategy and set the user defined inputs, the wrong date comes up. For example, in the screenshot attached, you will see the properties window. This is a brand new instance of this window immediately after restarting NinjaTrader and connecting it to a data feed. It says 8 July rather than today which is 14 July. Why is that, do you think, and how can I set it so that it will automatically start up with today's date?

      2nd Question:
      This is a debugging question.

      I calculate an overnight range and then, at or after 9:00 AM Germany time, if the price breaks out of that range, I enter the market. Today, it did not enter the market at the correct time (It should have been at 9:08 AM). It entered the market on a break of the 10:00 candle when the 15:00 candle broke it.

      Here is the code that is used to in order to enter on the breakout.

      				if (LongEntryOCO == null
      					&& ShortEntryOCO == null
      					&& ToTime(Time[0]) >= ToTime(endRangeHr, endRangeMin, 00)
      					&& ToTime(Time[0]) < ToTime(removeOrderHr, removeOrderMin,00)
      					&& High[0] >= LongEntryPrice
      					&& restrictTrade == false)
      					LongEntryOCO = EnterLong(positionSize, "BreakoutLongOCO");
      Here are the calculations for the variables:

             	private int endRangeHr = 10; // Hour that the range will end
      	private int endRangeMin = 00; // Minute that the range will end
              private int removeOrderHr = 19; // Hour that unfilled entry orders will be removed
      	private int removeOrderMin = 00; // Minute that that unfilled entry orders will be removed
      Can you please help me figure out why this did not trigger at 9:08?

      for endRangeHour, I have it set to 10. The reason is because I am trading this on hourly charts and I want it to trigger at 9:00 AM or later and the time Stamp for a tick at 9:00 AM on an hourly chart is 10:00 AM. So it seems to me that this should have triggered earlier than it did.

      I am attaching a screenshot of the chart also.
      Attached Files



        For your first question, I believe that this is caused by the variable being set where you are declaring it; I don't think the script can get a date before the script is initialized, so I just added
        protected override void Initialize()
        startRange = DateTime.Today.AddHours(2);
        That gets the date and sets it before the user can see the properties appear and displays the correct date.

        For your second question I would like to get a few more details on how the strategy is run before I try to answer you.

        From what you described this is how I interpreted it. -> You are running a strategy that checks if the current bars time is after 9 and before 10, if so and the price breaks, then submit an order, am I correct in thinking this is the logic you are going for?.

        I would like to ask, are you running this on CalculateOnBarClose = false ?

        If you have calculate on bar close set to true and you are using an hourly chart I would expect it to not place trades in between 9 and 10 because it is only processing as each bar closes so if the condition permitted as the 9 bar closed and moved to the 10 bar, it would place the order.

        Another way you could possibly check what is happening is after trading is done today, if you have the market replay data available either by recording or downloading, to playback the day up until the point where it makes the error trade.
        This would probably be the best way to check this as you can pause, rewind and retry with Print() statements or whatever else you may need to add in order to debug the logic.

        I look forward to being of further assistance.
        JesseNinjaTrader Customer Service


          Thanks for your reply Jesse.

          will look into the addition that you suggested for the Initialize method.

          For further clarification on Question #2.

          I am looking for a breakout that happens between 9:00 AM and 18:00. If an order is not triggered and filled within that time, then it will call the CancelOrder() method.

          CalculateOnBarclose is set to False.


          Latest Posts


          Topics Statistics Last Post
          Started by memonolog, Today, 02:39 AM
          0 responses
          Last Post memonolog  
          Started by Z-Starter, Today, 01:21 AM
          2 responses
          Last Post Z-Starter  
          Started by SamuelO, 05-22-2020, 11:58 AM
          8 responses
          Last Post guidoisot  
          Started by atman, Today, 12:37 AM
          0 responses
          Last Post atman
          by atman
          Started by random9570, Yesterday, 09:01 PM
          1 response
          Last Post Fulcanelli