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

Stale Values in OnRender method and correct use of GetYByValue?

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

    Stale Values in OnRender method and correct use of GetYByValue?

    Hello,

    I am learning Ninjascript + C# and have 2 questions.. One is regarding accessing updated values in both the OnBarUpdate and OnRender methods. The other is regarding the use of chartScale.GetYbyValue.

    My expectation is that the OnBarUpdate method gets called as NT sees fit and then the OnRender method is scheduled to run sometime later at max of 250ms. That seems basic, however, I am would expect any of the core PriceSeries values to be updated and accessible in the OnRender method.

    Code:
    Code:
    private double ticksRemaining;
    		private double currentPriceValue;
    		
    		protected override void OnStateChange()
    		{
    			if (State == State.SetDefaults)
    			{
    				Description			= NinjaTrader.Custom.Resource.NinjaScriptIndicatorDescriptionTickCounter;
    				Name				= " MyTickCounter";
    				Calculate			= Calculate.OnEachTick;
    				DisplayInDataBox	= false;
    				DrawOnPricePanel	= false;
    				IsChartOnly			= true;
    				IsOverlay			= true;
    			}
    			else if (State == State.Configure)
    			{	
    				ticksRemaining			= 0;
    				currentPriceValue       = 0;
    			}
    		}
                                    
    		protected override void OnBarUpdate()
    		{
    			ticksRemaining = BarsPeriod.Value - Bars.TickCount;
    			Print("Called OnBarUpdate @ " + Time[0]);
    			Print("OnBarUpdate Close value = " + Close[0]);
    			currentPriceValue = Close[0];
    		}
    		
    		protected override void OnRender(ChartControl chartControl, ChartScale chartScale)
    		{
    			Print("Called OnRender @ " + Time[0]);
    			Print("OnRender Close value = " + Close[0]);
    			Print("OnRender persisted Close value = " + currentPriceValue); // value is correct
    		    
    			// gets the pixel coordinate of the price value passed to the method
    			int     yByValue = chartScale.GetYByValue(currentPriceValue);
    
    			Print(Time[0] + " the value of yByValue is: " + yByValue + "ticksRemaing =" + ticksRemaining);
    
    			NinjaTrader.Gui.Tools.SimpleFont myFont = new NinjaTrader.Gui.Tools.SimpleFont("Courier New", 12) { Size = 12, Bold = true };
    			Draw.Text(this, "NinjaScriptInfo", true, ticksRemaining.ToString(), 0, yByValue, 0, Brushes.Orange, myFont, System.Windows.TextAlignment.Center, null, null, 1);
    		}
    Output:
    Called OnBarUpdate @ 12/26/2016 10:01:27 PM
    OnBarUpdate Close value = 53.07
    Called OnRender @ 12/15/2016 5:00:50 PM // Note that this is the timestamp of the first bar on the chart.
    OnRender Close value = 52.24 // Note that this is the close of the first bar on the chart.
    OnRender persisted Close value = 53.07
    12/15/2016 5:00:50 PM the value of yByValue is: 933ticksRemaing =839
    1. Is this expected output? If so, is there a place in the documentation that can help me more clearly understand the why?
    2. What I am trying to do is customize the TickCounter method to display the counter to the right of the price bar, but because the Close value is stale I am not even close to printing it at the correct Y pixel value. Am I misusing the method? See screenshot for where it is printing. This does not align with the example here:http://ninjatrader.com/support/helpG...chartscale.htm
    Attached Files
    Last edited by ryanswood; 12-27-2016, 08:17 AM.

    #2
    This might help: http://ninjatrader.com/support/helpG...s/getclose.htm

    Comment


      #3
      Hello,

      Thank you for the post.

      What you are seeing would be expected, items which are called out of sync with the Event driven logic such as OnRender would need to be reviewed to use different syntax.

      As tradesmart noted you would instead need to use the Get methods to get values rather than using the Series and a BarsAgo. This would also entail you may need to create variables for items in which OnRender should show.

      For example if you are wanting to display the Close price, you can use the GetClose method if you know the Index of the bar in question, or have OnBarUpdate set the current close to a variable, and render the variable instead.

      For an example of OnRender where a value is retrieved you could see the Pivots indicator. The Pivots indicator uses the existing Plots for storage and then draws the lines bases on the values in the plots.

      Regarding a help guide mention of this, there was a note coming from one of the prior beta versions here:


      The Implementation changes specifically.

      The links provided in that description give other areas of the help guide that explain this occurrence further.


      I look forward to being of further assistance.
      JesseNinjaTrader Customer Service

      Comment


        #4
        Thank you NinjaTrader_Jesse and tradesmart..

        That confirmed some of my assumptions. You confirmed the behavior I was noticing. In my example I set values in the OnBarUpdate method and was able to refer to them accurately in OnRender.

        Now my 2nd question is still outstanding. You can see the question in the original post and the resulting screenshot. The goal is to print the TicksRemaining floated to the right of the most current price bar. But as you can see in the screen shot, the result of chartScale.GetYByValue(currentPriceValue) is that it prints far away from the pixel value at which price is at. Any thoughts on that?

        I am trying to modernize an old TickCounter indicator that enabled this functionality in NT 7's Plot method. There has to be a cleaner and less involved way to do it.

        Code:
        switch (DispLocation)
        				{
        					case DisplayLocation.Float_with_price:
        						x = bounds.X + bounds.Width - ChartControl.BarMarginRight + 5;
        						y = (int)(((bounds.Y + bounds.Height) - ((Close[0] - min) / ChartControl.MaxMinusMin(max, min)) * bounds.Height) - textHeight*0.4);
        						if(ChartControl.BarMarginRight < textWidth-12)
        						graphics.DrawString("Increase chart Properties - Right side margin", TextFont, textBrush, bounds.X + bounds.Width - noTickTextWidth, bounds.Y + bounds.Height - noTickTextHeight, stringFormat);
        						break;
        Last edited by ryanswood; 12-27-2016, 05:15 PM.

        Comment


          #5
          Not sure why you're using OnRender. If you take the built-in @TickCounter and save as a new name and then replace Draw.TextFixed(...) on line 62 with the following code, it should be close to what you're requesting.

          Code:
          Draw.Text(this, 
                          "anewtickcounter", 
                          IsAutoScale, 
                          "   " + tick1, 
                          Core.Globals.Now, 
                          Close[0], 
                          5, 
                          ChartControl.Properties.ChartText,
                          ChartControl.Properties.LabelFont,
                          System.Windows.TextAlignment.Left, 
                          null,
                          null,
                          0        
                      ) ;
          Attached Files
          Last edited by tradesmart; 12-27-2016, 06:04 PM. Reason: Added screenshot

          Comment


            #6
            Tradesmart you were exactly right once again. That got me close enough. Thank you for teaching me a few things in this thread. I was using OnRender because of the limited examples I have seen and the unawareness of how to use the other method signatures of the Draw.Text method.

            Consider this thread solved.

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by helpwanted, Today, 03:06 AM
            1 response
            16 views
            0 likes
            Last Post sarafuenonly123  
            Started by Brevo, Today, 01:45 AM
            0 responses
            11 views
            0 likes
            Last Post Brevo
            by Brevo
             
            Started by aussugardefender, Today, 01:07 AM
            0 responses
            6 views
            0 likes
            Last Post aussugardefender  
            Started by pvincent, 06-23-2022, 12:53 PM
            14 responses
            244 views
            0 likes
            Last Post Nyman
            by Nyman
             
            Started by TraderG23, 12-08-2023, 07:56 AM
            9 responses
            387 views
            1 like
            Last Post Gavini
            by Gavini
             
            Working...
            X