Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

chartScale GetValueByY

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

    chartScale GetValueByY

    Hi

    Is it only possible to access chartScale within OnRender()?

    I have:
    Code:
    valueByY = Instrument.MasterInstrument.RoundToTickSize(chartScale.GetValueByY((float)mousePosition.Y));
    within OnMouseMove, but am getting Object reference not set to an instance of an object.

    #2
    Hello FatCanary,

    Thanks for your question.

    You could create your own private ChartScale property and update this in OnRender, and then use this elsewhere in your script.

    Code:
    namespace NinjaTrader.NinjaScript.Indicators
    {
        public class ChartScaleTest : Indicator
        {
            private ChartScale myChartScale;
    
            protected override void OnStateChange()
            {
                if (State == State.SetDefaults)
                {
                    Name                                        = "ChartScaleTest";
                    IsOverlay                                   = true;
                }
            }
    
            protected override void OnBarUpdate()
            {
                if (myChartScale != null)
                    Print(myChartScale.GetYByValue(Close[0]));
            }
    
            protected override void OnRender(ChartControl chartControl, ChartScale chartScale)
            {
                myChartScale = chartScale;
            }
        }
    }
    Please let me know if I can be of further assistance.

    Comment


      #3
      Hi Jim

      That's got it, many thanks.

      I've spend all flipping day on that, and you sort it in 10 minutes!

      Comment


        #4
        In my indicator I need to access chartScale BEFORE OnRender is called... is that possible?
        To be specific...I'd like to access this in OnStateChange - State.Historical...
        Last edited by tgn55; 08-28-2019, 10:08 PM.

        Comment


          #5
          Hello tgn55,

          We will have to wait for the first render pass for this to be available, so this would not be ready until after processing historical data completes. Chart Scale will also update as the chart is scrolled, so it should be updated on new render passes.

          Since OnRender is used for drawing, it should calculate how everything needs to be drawn by looping through visible bar indexes and focus on that alone. You may want to consider using various Series objects that are calculated in OnBarUpdate, and then use those Series objects to control your drawings. I have attached an example that takes such an approach.

          Let us know if you have any additional questions.
          Attached Files

          Comment


            #6
            Thanks Jim. I have implemented a workaround already.. using ChartBars.FromIndex/ToIndex, but it still has some issues if I change the data series that is a bit puzzling... resetting a variable to zero in State.Configure, recalculating other things in DataLoaded... does not seem to be happening. Anyway - thanks for your input. I'll play around a bit with this..

            Comment


              #7
              Hello tgn55,

              I may suggest focusing on resetting in State.DataLoaded and then to use prints to monitor what is being re-calculated more closely to move forward from there. ChartBars.FromIndex and ChartBars.ToIndex will be related to the primary data series, so if you switch instruments on the chart, ChartBars will then change to that data series.

              If there is anything else I can do to help, please let me know.

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
              0 responses
              650 views
              0 likes
              Last Post Geovanny Suaza  
              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
              0 responses
              370 views
              1 like
              Last Post Geovanny Suaza  
              Started by Mindset, 02-09-2026, 11:44 AM
              0 responses
              109 views
              0 likes
              Last Post Mindset
              by Mindset
               
              Started by Geovanny Suaza, 02-02-2026, 12:30 PM
              0 responses
              574 views
              1 like
              Last Post Geovanny Suaza  
              Started by RFrosty, 01-28-2026, 06:49 PM
              0 responses
              577 views
              1 like
              Last Post RFrosty
              by RFrosty
               
              Working...
              X