Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

The Fuction GetXByTime() returns weird values

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

    The Fuction GetXByTime() returns weird values

    Hello!

    I am trying to have the xCoordinate in OnRender().
    I have the following code:
    HTML Code:
    protected override void OnRender(ChartControl chartControl, ChartScale chartScale)
    {
        base.OnRender(chartControl, chartScale);
        DateTime timeToCheck = new DateTime(2023, 5, 24, 11, 0, 0);
        int xCoordinate = chartControl.GetXByTime(timeToCheck);
        Print(xCoordinate);
    }​

    This code is returning negative high numbers in absolute values, what is surprising for me. I have -14055.

    I have also tried with
    HTML Code:
    DateTime timeToCheck = new DateTime(Time[0]);
    and also here, I have the same behaviour.

    How can I have the x coordinate in OnRender() using the function GetXByTime()? What is wrong in my code?

    I would appreciate any help.

    Best regards
    Last edited by Stanfillirenfro; 05-29-2023, 09:36 AM.

    #2
    The negative numbers mean that the pixel coordinate is off your screen because the chart doesn't go back that far. Or, with multiple monitors, the pixel coordinates could be visible on another monitor left of the primary one, could they not?

    It should be stated that absolute pixel coordinate (0,0) is the upper left of your PRIMARY monitor. If you have a monitor positioned in Windows to the left of that, every single pixel on that one will have an ultimate x coordinate that is negative.

    That having been said, the coordinates should not be negative relative to the client window of the chart component unless the first thing is true - that datetime is off the chart's visible scrolled/zoomed area.

    Also, I would be very cautious about using something like "Time[0]" in OnRender - basically, don't do that. You should be using ChartBars or ChartBars.Bars and .GetTime(index) etc. because you don't have the right context there to be using Time. If you have more than one data series, you can't even be sure which one that would refer to, or whether CurrentBar is >= 0.

    If you're just doing a quick test, you might try DateTime.Now.
    Last edited by QuantKey_Bruce; 05-29-2023, 01:36 PM.
    Bruce DeVault
    QuantKey Trading Vendor Services
    NinjaTrader Ecosystem Vendor - QuantKey

    Comment


      #3
      Many thanks QuantKey_Bruce for your valuable help.

      This problem is fixed. Many thanks again!

      I would be gratefull to you if you could tell me if ChartControl and ChartScale can be used outside of OnRender(). Is it possible to get the x Coordinate for example with ChartControl outside of OnRender()? What would be the function to be added or removed to achieve the goal?

      Many thanks in advance

      Comment


        #4
        Well, ChartControl is accessible outside of OnRender, but you would have to always check for nulls. Also, bear in mind your indicator or strategy may not be directly on a chart e.g. your indicator could be nested in a strategy or on market analyzer, or your strategy could be in strategy analyzer, and in these cases ChartControl will be null so you have to always have a valid code path when it is null. I don't think it's wise to be doing any actual drawing outside of OnRender - that defeats the whole purpose. In particular, you defintiely do not want to do any actual drawing on each tick or faster than every 250 ms or you will greatly decrease the platform's performance and stability. Sometimes you need access to ChartControl to do something like grab the color of the axis brush so you can coordinate some other drawing with that same color, but I would really encourage you to stick with NinjaTrader's modular design and do your custom rendering in OnRender and your calculations in OnBarUpdate (or OnMarketData if appropriate). One has to ask why one needs an x coordinate outside of OnRender, and the answer is probably going to lead to being told you shouldn't do it that way - you should do your drawing in OnRender. Most of the time when someone is trying to force it to render faster or to custom draw outside of OnRender it turns out they're doing something regrettable that really shouldn't be done that way and that will lead to instability or crashes in other environments e.g. they're testing and think it's okay on their desktop, but when run on a VPS or on an old laptop it crashes the platform or makes it run so slowly no one would be able to stand it. It's designed the way it is for a reason, and it's generally well thought out so it's good to try to work with the design, and the design says you do your custom drawing in OnRender.
        Last edited by QuantKey_Bruce; 05-29-2023, 03:53 PM.
        Bruce DeVault
        QuantKey Trading Vendor Services
        NinjaTrader Ecosystem Vendor - QuantKey

        Comment


          #5
          Many thanks QuantKey_Bruce for yor explanation.

          I have gone one step further with my above code and another problem has arose.
          I have used the following loop to find the x Coordinate and everything was FINE:
          HTML Code:
          for(int barIndex = ChartBars.FromIndex; barIndex <= ChartBars.ToIndex; barIndex++)
          {
              DateTime timeValue = Bars.GetTime(barIndex);
              int xCoordinate = chartControl.GetXByTime(timeValue);​
          }
          But when I used a loop with predefined number such as 50 for example, I continue to have negative values.
          HTML Code:
          for(int barIndex = 0; barIndex <= 50; barIndex++)
          {
              DateTime timeValue = Bars.GetTime(barIndex);
              int xCoordinate = chartControl.GetXByTime(timeValue);​
          }​
          I make it sure that the bars are also visible on the display.
          What could be the problem please and how to fix it?

          Many thanks

          Comment


            #6
            I don't understand in this case why you're not getting the X by the index itself: https://ninjatrader.com/support/help...bybarindex.htm.

            Also, you need to be checking that 0 and 50 are within the range ChartBars.FromIndex and ChartBars.ToIndex.

            Perhaps, if you have to, you could do something like:

            for (int barIndex = Math.Max(0, ChartBars.FromIndex); barIndex <= Math.Min(50, ChartBars.ToIndex); barIndex++)
            {
            int xCoordinate = chartControl.GetXByBarIndex(ChartBars, barIndex);
            }

            In this way you can be sure that the 0..50 is indeed on the display. Most likely, it isn't, if as you say, it fails. In all likelihood, bar 0 is off the screen to the left, which perfectly explains why X would be negative.
            Last edited by QuantKey_Bruce; 05-29-2023, 06:29 PM.
            Bruce DeVault
            QuantKey Trading Vendor Services
            NinjaTrader Ecosystem Vendor - QuantKey

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by Geovanny Suaza, 02-11-2026, 06:32 PM
            0 responses
            666 views
            0 likes
            Last Post Geovanny Suaza  
            Started by Geovanny Suaza, 02-11-2026, 05:51 PM
            0 responses
            377 views
            1 like
            Last Post Geovanny Suaza  
            Started by Mindset, 02-09-2026, 11:44 AM
            0 responses
            110 views
            0 likes
            Last Post Mindset
            by Mindset
             
            Started by Geovanny Suaza, 02-02-2026, 12:30 PM
            0 responses
            575 views
            1 like
            Last Post Geovanny Suaza  
            Started by RFrosty, 01-28-2026, 06:49 PM
            0 responses
            580 views
            1 like
            Last Post RFrosty
            by RFrosty
             
            Working...
            X