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

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.
    JimNinjaTrader Customer Service

    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
          JimNinjaTrader Customer Service

          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.
              JimNinjaTrader Customer Service

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by mmenigma, Yesterday, 03:25 PM
              1 response
              11 views
              0 likes
              Last Post NinjaTrader_Gaby  
              Started by kujista, Today, 05:44 AM
              0 responses
              6 views
              0 likes
              Last Post kujista
              by kujista
               
              Started by ZenCortexCLICK, Today, 04:58 AM
              0 responses
              9 views
              0 likes
              Last Post ZenCortexCLICK  
              Started by sidlercom80, 10-28-2023, 08:49 AM
              172 responses
              2,281 views
              0 likes
              Last Post sidlercom80  
              Started by Irukandji, Yesterday, 02:53 AM
              2 responses
              18 views
              0 likes
              Last Post Irukandji  
              Working...
              X