Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

DateTime as argument in textLayout of OnRender?

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

    DateTime as argument in textLayout of OnRender?

    Hello all!

    I have rendered a text successfully in the function OnRender. The text was a string.
    I want to replicate the same thing with the DateTime instead of a string, but I am receiving an error message that the second argument is not allowed. Even after converting the Datetime to string as followed: DateTime time.toString. the same error message remains.
    Here is the code:
    HTML Code:
    protected override void OnRender(ChartControl chartControl, ChartScale chartScale)
            {
                SharpDX.DirectWrite.TextFormat textFormat1 = simpleFont.ToDirectWriteTextFormat();
                string textToRender = "I am rendering my text here. What about DateTime?";
    
                SharpDX.DirectWrite.TextLayout textLayout1 = new SharpDX.DirectWrite.TextLayout(NinjaTrader.Core.Globals.DirectWriteFactory,
                        ToTime(Time[0]).toString, textFormat1, ChartPanel.X + ChartPanel.W, textFormat1.FontSize);
            }​
    Could someone please advise how to use the Datetime as argument in textLayout of OnRender?

    Many thanks!

    #2
    Hello stanfillirenfro,

    Thanks for your post.

    Time[0] cannot be used in OnRender() since OnRender() only uses a bar index and not a barsAgo value. Further, the code you shared is invalid. You are calling .toString ​when you would instead need to call .ToString().

    That said, GetTime() could be used in OnRender() to get the time stamp of a bar based on the bar index value you supply.

    GetTime(): https://ninjatrader.com/support/help...8//gettime.htm

    For more information about working with DateTime, you could consider doing a Google search for something like 'DateTime Format In C#'.

    Let me know if I may assist further.
    <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

    Comment


      #3
      Many thanks NinjaTrader_BrandonH for your reply!
      The idea is is to create a clock related to different time zones (like New York, London, Tokyo...). I was thinking to use a DateTime. My problem is not the DateTime format. I have already the texts for the names of the cities correctly rendered, but I need to have the times corresponding to each city. But I am failing to render the times. The DateTime is the correct Series to be used right?
      Any advice?

      Many thanks

      Comment


        #4
        Hello stanfillirenfro,

        Thanks for your note.

        Yes, using DateTime would be correct. For example, GetTime() can be used in OnRender() and returns a DateTime structure that represents the time stamp at the desired bar index.

        Bars.GetTime() could be used to get the DateTime from an index. You could then use .ToString() on that DateTime and if you want a specific format you would use .ToString("<date format you want here>"). For example, .ToString("dddd, dd MMMM yyyy") which would return a date format of somehting like Friday, 29 February 2023.

        To clarify, are you placing text on certain bars or just rendering the, on the chart? Note that OnRender is not the same as OnBarUpdate. OnRender has no context of BarsAgo like OnBarUpdate does so you will need to use the FromIndex and ToIndex to know what bars are currently visible.

        OnRender: https://ninjatrader.com/support/help...8/onrender.htm
        FromIndex: https://ninjatrader.com/support/help..._fromindex.htm
        ToIndex: https://ninjatrader.com/support/help...​​

        To get a solid understanding of how DateTime could be used to accomplish this I suggest researching the C# DateTime class and C# .ToString() if you have not yet done so.
        <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

        Comment


          #5
          Many thanks NinjaTrader_BrandonH for your help.

          What I want to achieve is not to have a time for a specific bar, but simply a clock on my desktop. I just want to have the times of choosing cities on my desktop (I have attached an example as picture (Clock)).

          I am not placing the text on a specific bar, but simply on the right corner of my desktop.

          By using Bars.GetTime().ToString(), I have the current day plus the time of the Bar[0] ( I MUST give a barindex here) (See picture: Clock1). I just want the time.
          I do not have problem with text, but I am failing to have a right <Series> for the time.

          Any help?

          Many thanks​
          Attached Files

          Comment


            #6
            You want to use chartControl.CanvasRight.
            Bruce DeVault
            QuantKey Trading Vendor Services
            NinjaTrader Ecosystem Vendor - QuantKey

            Comment


              #7
              Manx thanks Bruce for your reply.
              My probloem is not to locate the text or the clock on the desktop. I want to have a clock runing on the desktop. So that I am failing to get the appropirate <Series> to implement it.

              Bars.GetTime().ToString() as suggested by NinjaTrader_BrandonH gives me the date and the time, but on a specific bar. For example, on each computer, there is a local time at the bottom right. I want exactly such time, and the idea is to implement, in addition of the local time, the times of London or Tokyo... for example.

              The following code renders the text on the chart:
              HTML Code:
              SharpDX.DirectWrite.TextLayout textLayout1 = new SharpDX.DirectWrite.TextLayout(NinjaTrader.Core.Globals.DirectWriteFactory,
              textToRender, textFormat1, ChartPanel.X + ChartPanel.W, textFormat1.FontSize);​
              Which argument could replace "textToRender" so that I can have the time instead, "textToRender" being a string?

              Any idea?

              Many thanks!
              Last edited by Stanfillirenfro; 04-14-2023, 06:18 AM.

              Comment


                #8
                Are you looking for something like DateTime.Now.ToString("MM/dd/yyyy HH:mm:ss.fffffff")?

                Here's one way to find the current time in a specific time zone:

                DateTime EasternNow = TimeZoneInfo.ConvertTimeBySystemTimeZoneId( DateTime.UtcNow, "Eastern Standard Time");
                string EasternString = EasternNow.ToString("MM/dd/yyyy HH:mm:ss.fffffff");

                You also could optimize this by saving the TimeZoneInfo for re-use, by adjusting the time or format, etc.
                Last edited by QuantKey_Bruce; 04-14-2023, 06:50 AM.
                Bruce DeVault
                QuantKey Trading Vendor Services
                NinjaTrader Ecosystem Vendor - QuantKey

                Comment


                  #9
                  Hello Bruce!

                  Awsome!!!

                  Many many and many thanks! It is exactly what I was looking for.

                  Many thanks again!

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                  0 responses
                  648 views
                  0 likes
                  Last Post Geovanny Suaza  
                  Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                  0 responses
                  369 views
                  1 like
                  Last Post Geovanny Suaza  
                  Started by Mindset, 02-09-2026, 11:44 AM
                  0 responses
                  108 views
                  0 likes
                  Last Post Mindset
                  by Mindset
                   
                  Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                  0 responses
                  572 views
                  1 like
                  Last Post Geovanny Suaza  
                  Started by RFrosty, 01-28-2026, 06:49 PM
                  0 responses
                  573 views
                  1 like
                  Last Post RFrosty
                  by RFrosty
                   
                  Working...
                  X