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:
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); }
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
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
Comment