Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Variables with ToTime

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

    Variables with ToTime

    Hi,

    I want to have the command ToTime in conjunction with a time (for example 9:00). I use it as a variable.

    First I set variables:

    #region Variables
    private int target = 10; // Default setting for Target
    private int stop = 10; // Default setting for Stop
    private int timeStart = 90000; // Default setting for TimeStart (trading)
    private int timeEnd = 100000; // Default setting for TimeEnd
    #endregion

    protected override void Initialize()
    {
    SetProfitTarget("", CalculationMode.Ticks, Target);
    SetStopLoss("", CalculationMode.Ticks, Stop, false);
    CalculateOnBarClose = true;
    }

    protected override void OnBarUpdate()
    {
    if (ToTime(Time[0]) > timeStart && ToTime(Time[0]) < timeEnd)
    ......
    }



    If I now use the backtest or use the strategy in a chart, I do not get the variables for timeStart and timeEnd in the little window bevor. All I can change, is ProfitTarget and StopLoss.

    What mistake I make?

    Thanks

    #2
    Hello Torso,

    In addition to delcaring in variables region, you must also add public property. The wizard can set this up for you, but can also see the following post for adding these through code:

    Ryan M.NinjaTrader Customer Service

    Comment


      #3
      It works, but...

      Thanks for the reply. I have considered it and now it works correctly.

      I declared the variables region and also the public property:

      private int timeStart = 90000; // Default setting for TimeStart
      private int timeEnd = 150000; // Default setting for TimeEnd

      .
      .
      .
      [Description("")]
      [GridCategory("Parameters")]
      public int TimeStart
      {
      get { return timeStart; }
      set { timeStart = Math.Max(1, value); }
      }

      [Description("")]
      [GridCategory("Parameters")]
      public int TimeEnd
      {
      get { return timeEnd; }
      set { timeEnd = Math.Max(1, value); }
      }


      ...

      it works fine, but if I use the strategy analyzer with the Optimization, then the sequence of the variable (paramters) is
      first: TimeEnd
      second: TimeStart

      this is confusing. How can I change the presentation of the parameters? (first TimeStart, then TimeEnd)

      Thanks Torso

      Comment


        #4
        The sequence here is alphabetical order. Maybe change TimeStart to TimeBegin?
        Ryan M.NinjaTrader Customer Service

        Comment


          #5
          Thanks,

          easy way!

          Now it's fine.

          Comment


            #6
            I'm trying to add time options to the parameter box in my strategy to start and stop at specific time. I've been able to add the options into the parameter box, but can't link the times to the strategy. Here's the code. Any suggestions?
            #region Variables
            private TimeSpan startTime1;
            private TimeSpan endTime1;
            private TimeSpan startTime2;
            private TimeSpan endTime2;
            private TimeSpan startTime3;
            private TimeSpan endTime3;
            #endregion

            #region Properties
            [Description(
            "1st Start Time")]
            [Category(
            "Parameters")]
            [Gui.Design.DisplayName(
            "\t\t\t\t\t\t1stStart Time")]
            public TimeSpan StartTime1
            {
            get { return startTime1; }
            set { startTime1 = value; }
            }

            [Description(
            "1st End Time")]
            [Category(
            "Parameters")]
            [Gui.Design.DisplayName(
            "\t\t\t\t\t1stEnd Time")]
            public TimeSpan EndTime1
            {
            get { return endTime1; }
            set { endTime1 = value; }
            }
            etc...

            Comment


              #7
              Hello RickStevenson,

              That looks good for capturing a TimeObject as a public property. How are you using this in your strategy code and what are you seeing happen?
              Ryan M.NinjaTrader Customer Service

              Comment


                #8
                Well, that's the part I'm having a problem with. I didn't actually add anything into the conditions. I realized after I posted that I kind of missed that part.
                I've been trying to add something to the conditions. So far I've come up with:
                ToTime(Time[0] > startTime1)), but won't compile. Error says "Operator '>' cannot be applied to aperands of type 'System.DateTime''
                Unfortunately I don't have a clue as to what that means. Help from a brain bigger than mine would be greatly appreciated.

                Comment


                  #9
                  You should be able to use the same ToTime function on your start time variable. This returns an integer that you can compare with <

                  if (ToTime(Time[0]) > ToTime(startTime1))
                  Ryan M.NinjaTrader Customer Service

                  Comment


                    #10
                    Still having issues. Here is the code:

                    if (RValueCharts(5).VClose[0] >= 3.8
                    && Position.MarketPosition == MarketPosition.Flat
                    && ToTime(Time[
                    0]) > ToTime(startTime1)
                    && ToTine(Time[
                    0]) < ToTime(endTime1))
                    The CS1502 error is suggesting that I'm passing an incorrect parameter object type into the method. The ToTime() is not recognizing 'startTime1'


                    Now it's telling me that I'm missing 2 ' ) ' after the last 2 on that line.

                    Comment


                      #11
                      Yes, you're right. ToTime() can only accept true DateTime objects - not TimeSpan.

                      Please see the following indicator from our file sharing section, which shows one way to work around this:


                      Basically you create a date time object. Part of this objects properties are taken from bar objects, and the hours, minutes, seconds is taken from your public input.
                      Ryan M.NinjaTrader Customer Service

                      Comment


                        #12
                        Same Problem - can't get &quot;ToTime&quot; to work

                        Please take a look at this application of ToTime - i'd like to be able to assign a public int.

                        #region Variables
                        privateint timeBegin = 80500;
                        privateint timeEnd = 142800;
                        if((ToTime(Time[0])>timeBegin)||(ToTime(Time[0])<timeEnd)) {
                        ((do my thing))
                        ; }
                        THE ABOVE DOES NOT WORK.
                        HOWEVER, THIS DOES WORK VERY WELL:
                        if((ToTime(Time[0])>080000)||(ToTime(Time[0])< 143000)) {
                        ((do my thing))
                        ; }

                        The variable is declared as public property like other variables i use successfully. Suggestions ?

                        Comment


                          #13
                          Hi JulieC,

                          What issue are you seeing with this? Can you please share all the relevant pieces, including the properties region for this file?
                          Ryan M.NinjaTrader Customer Service

                          Comment


                            #14
                            Good Morning Ryan,
                            In the properties region the set-up is (both int variables same format):
                            [Description("Time Begin")]
                            [GridCategory(
                            "Parameters")]
                            publicint TimeBegin
                            {
                            get { return timeBegin; }
                            set { timeBegin = Math.Max(1, value); }
                            }

                            This ToTime application is functional as long as i hard code the time as 6 digits. But i can't get it to work by utilizing a named public variable int. When using the variable the strategy simply does not activate as though it is not within the allowable time frame.

                            Thanks for your help.

                            Comment


                              #15
                              Thanks, Julie. Most likely this is due to using the lower case private variable in your code rather than the upper case public property. Try Changing to:

                              if((ToTime(Time[0])>TimeBegin)||(ToTime(Time[0])<TimeEnd))
                              Ryan M.NinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                              0 responses
                              637 views
                              0 likes
                              Last Post Geovanny Suaza  
                              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                              0 responses
                              366 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by Mindset, 02-09-2026, 11:44 AM
                              0 responses
                              107 views
                              0 likes
                              Last Post Mindset
                              by Mindset
                               
                              Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                              0 responses
                              569 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by RFrosty, 01-28-2026, 06:49 PM
                              0 responses
                              571 views
                              1 like
                              Last Post RFrosty
                              by RFrosty
                               
                              Working...
                              X