#region Using declarations using System; using System.Collections.Generic; using System.ComponentModel; using System.ComponentModel.DataAnnotations; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Input; using System.Windows.Media; using System.Xml.Serialization; using NinjaTrader.Cbi; using NinjaTrader.Gui; using NinjaTrader.Gui.Chart; using NinjaTrader.Gui.SuperDom; using NinjaTrader.Gui.Tools; using NinjaTrader.Data; using NinjaTrader.NinjaScript; using NinjaTrader.Core.FloatingPoint; using NinjaTrader.NinjaScript.DrawingTools; #endregion //This namespace holds Indicators in this folder and is required. Do not change it. namespace NinjaTrader.NinjaScript.Indicators { public class RefreshRate : Indicator { private const long TicksInMillisecond = 10000; private bool IsRefreshing; private long PreviousFrame; protected override void OnStateChange() { if (State == State.SetDefaults) { Description = @"Refreshes the chart on every incoming tick or market depth update according to the desired refresh rate"; Name = "Refresh Rate"; Calculate = Calculate.OnEachTick; IsChartOnly = true; IsOverlay = true; DisplayInDataBox = false; ScaleJustification = ScaleJustification.Right; IsSuspendedWhileInactive = true; RefreshRateMilliseconds = 0; } } protected override void OnMarketDepth(MarketDepthEventArgs marketDepthUpdate) { RefreshChart(); } protected override void OnBarUpdate() { RefreshChart(); } private void RefreshChart() { if (IsRefreshing || ChartControl == null || State == State.Historical) return; if (RefreshRateMilliseconds > 0) { long currentFrame = DateTime.Now.Ticks / (TicksInMillisecond * RefreshRateMilliseconds); if (currentFrame == PreviousFrame) return; PreviousFrame = currentFrame; } IsRefreshing = true; ChartControl.Dispatcher.InvokeAsync(() => { ChartControl.InvalidateVisual(); IsRefreshing = false; }); } public override string DisplayName { get { return Name; } } [NinjaScriptProperty] [Range(0, int.MaxValue)] [Display(Name = "Refresh Rate (Ms)", Description = "Number of milliseconds to wait between chart refreshes (0 for no waiting)", Order = 1, GroupName = "Refresh Rate")] public int RefreshRateMilliseconds { get; set; } } } #region NinjaScript generated code. Neither change nor remove. namespace NinjaTrader.NinjaScript.Indicators { public partial class Indicator : NinjaTrader.Gui.NinjaScript.IndicatorRenderBase { private RefreshRate[] cacheRefreshRate; public RefreshRate RefreshRate(int refreshRateMilliseconds) { return RefreshRate(Input, refreshRateMilliseconds); } public RefreshRate RefreshRate(ISeries input, int refreshRateMilliseconds) { if (cacheRefreshRate != null) for (int idx = 0; idx < cacheRefreshRate.Length; idx++) if (cacheRefreshRate[idx] != null && cacheRefreshRate[idx].RefreshRateMilliseconds == refreshRateMilliseconds && cacheRefreshRate[idx].EqualsInput(input)) return cacheRefreshRate[idx]; return CacheIndicator(new RefreshRate(){ RefreshRateMilliseconds = refreshRateMilliseconds }, input, ref cacheRefreshRate); } } } namespace NinjaTrader.NinjaScript.MarketAnalyzerColumns { public partial class MarketAnalyzerColumn : MarketAnalyzerColumnBase { public Indicators.RefreshRate RefreshRate(int refreshRateMilliseconds) { return indicator.RefreshRate(Input, refreshRateMilliseconds); } public Indicators.RefreshRate RefreshRate(ISeries input , int refreshRateMilliseconds) { return indicator.RefreshRate(input, refreshRateMilliseconds); } } } namespace NinjaTrader.NinjaScript.Strategies { public partial class Strategy : NinjaTrader.Gui.NinjaScript.StrategyRenderBase { public Indicators.RefreshRate RefreshRate(int refreshRateMilliseconds) { return indicator.RefreshRate(Input, refreshRateMilliseconds); } public Indicators.RefreshRate RefreshRate(ISeries input , int refreshRateMilliseconds) { return indicator.RefreshRate(input, refreshRateMilliseconds); } } } #endregion