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

How to sum 2 time variables

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

    How to sum 2 time variables

    How can I add X minutes to a time variable ;

    int Mar****pen = 830 // 8:30 AM
    int TradingTime = 130 // 1 Hour and 30 Minutes

    This way dont works because i cant add 830 with 130 ;
    if (ToTime(Time[0]) <= Mar****pen + TradingTime )

    I should convert Mar****pen and TradingTime to time, do the sum and then convert again to integer, but dont know how to do it...

    Any idea ?
    pmaglio
    NinjaTrader Ecosystem Vendor - The Indicator Store

    #2
    Create yourself TiimeSpan objects.

    TimeSpan mar****pen = new TimeSpan(hh, mm, ss)

    Then you can use C# and do things like mar****pen.Add(new TimeSpan(hh, mm, ss)).
    Josh P.NinjaTrader Customer Service

    Comment


      #3
      Ok, now my variable are TimeSpan :
      private TimeSpan StartTime = new TimeSpan(12,30,00);
      so I can use StartTime.Add(new TimeSpan(1,30,00)....
      But now, I cant compare them ; cant use > or < with timespan variables...How can I do the check
      Time[0] <= StartTime.Add(new TimeSpan(1,30,00)) ?
      pmaglio
      NinjaTrader Ecosystem Vendor - The Indicator Store

      Comment


        #4
        TimeSpan and DateTime are different. If you wanted to compare to Time[0] you will want to create a DateTime instead. Please see MSDN or google for more information on how to use these.
        Josh P.NinjaTrader Customer Service

        Comment


          #5
          If anyone interested, here is the solution :
          The most appropiate variable type for time is TimeSpan, so :
          private TimeSpan StartTime = new TimeSpan(8,30,00); // 8:30 AM

          But to compare a TimeSpan variable with Time[0] it has to be converted with Time[0].TimeOfDay

          And then, to compare with StartTime + ( 1 Hour + 30 Minutes ) ;
          (Time[0].TimeOfDay.CompareTo(StartTime.Add(new TimeSpan(0,30,00) ))<=0)

          Thanks Josh
          pmaglio
          NinjaTrader Ecosystem Vendor - The Indicator Store

          Comment


            #6
            Indicator Settings not saved on Workspace nor Layout

            Im having this problem since i changed 2 variables from int to TimSpan.
            The definition on the variables section is this :

            private TimeSpan StartTime = new TimeSpan(8,30,00);

            ..and when the indicator is added to a new chart, its default value is ok ( 8:30 AM )
            But when the chart layout is saved and reloaded, its value changes to 0,0,0 ( Same happens when saving and reloading workspace)

            Here is the Properties Section :

            [Description("Time For Start of Session")]
            [Category(
            "Parameters")]
            public TimeSpan Time_Start
            {
            get { return StartTime; }
            set { StartTime = value; }
            }

            Any idea ?
            pmaglio
            NinjaTrader Ecosystem Vendor - The Indicator Store

            Comment


              #7
              pmaglio,

              Unfortunately I cannot provide much more support on this. Use of TimeSpan is more advanced C# and you may want to dig around MSDN or google to see if you can find anything.
              Josh P.NinjaTrader Customer Service

              Comment


                #8
                C# itself doesn't serialise Timespan, pmaglio. You have to do it by your own. Here is an example.

                Regards
                Ralph
                Code:
                [FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2][XmlIgnore()]
                [/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]public[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2] TimeSpan PrimaryProfileEnd
                {
                [/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]  get[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2] { [/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]return[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2] primaryProfileEnd; }
                [/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]  set[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2] { primaryProfileEnd = value; }
                }
                [Browsable([/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]false[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2])]
                [/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]public[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]string[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2] PrimaryProfileEndSerialize
                {
                [/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]  get[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2] { [/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]return[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2] primaryProfileEnd.ToString(); }
                [/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]  set[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2] { primaryProfileEnd = TimeSpan.Parse(value); }
                }
                [/SIZE][/FONT][/SIZE][/FONT]

                Comment


                  #9
                  Just needed to add [XmlIgnore()]

                  I resolved the problem by just adding this line to the Properties Section

                  [XmlIgnore()]
                  [Description("Time For Start of Session")]
                  [Category(
                  "Parameters")]
                  public TimeSpan Time_Start
                  {
                  get { return StartTime; }
                  set { StartTime = value; }
                  }


                  Thanks !
                  pmaglio
                  NinjaTrader Ecosystem Vendor - The Indicator Store

                  Comment


                    #10
                    Hey Ralph, i didn saw your post....and now I see that you are right.
                    If not doing that serialize process, the indicator values are not saved on workspaces or layouts...

                    Thanks !
                    pmaglio
                    NinjaTrader Ecosystem Vendor - The Indicator Store

                    Comment


                      #11
                      Yes pmaglio, and even more worse the TimeSpan is saved in the workspace, but not initialised. So, next time the workspace is loaded, your pre-defined variable is overwritten with the null-value.

                      Regards
                      Ralph

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by ghoul, Today, 06:02 PM
                      3 responses
                      14 views
                      0 likes
                      Last Post NinjaTrader_Manfred  
                      Started by jeronymite, 04-12-2024, 04:26 PM
                      3 responses
                      44 views
                      0 likes
                      Last Post jeronymite  
                      Started by Barry Milan, Yesterday, 10:35 PM
                      7 responses
                      20 views
                      0 likes
                      Last Post NinjaTrader_Manfred  
                      Started by AttiM, 02-14-2024, 05:20 PM
                      10 responses
                      180 views
                      0 likes
                      Last Post jeronymite  
                      Started by DanielSanMartin, Yesterday, 02:37 PM
                      2 responses
                      13 views
                      0 likes
                      Last Post DanielSanMartin  
                      Working...
                      X