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

Data changes, Plot does not

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

    Data changes, Plot does not

    My cursor is over the green line of my indicator... the data changed... i can even see it in a Print()... but the Plot of the indicator doesn't update
    Attached Files

    #2
    Hello Fernand0,

    Thank you for your note.

    If you right click on your indicator >properties and set Calculate to OnEachTick or OnPriceChange are you seeing updates on the current bar?

    I look forward to your reply.
    Alan P.NinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_AlanP View Post
      Hello Fernand0,

      Thank you for your note.

      If you right click on your indicator >properties and set Calculate to OnEachTick or OnPriceChange are you seeing updates on the current bar?

      I look forward to your reply.
      OnEachTick.
      But.. paying more attention..
      For some reason at the beginning of each session it doesn't work properly (i can't assure this because, well, the session is closed right now.. i'm just trying to remember).
      I think it's because of the CQG demo account that doesn't work like a normal account. Has delay, lost of packages.. etc.
      Sometimes, at the first hour, the Level 2 data works as if the session has not started yet(price moves, and there are no movement in the DOM ladder).
      I have no idea if this is normal, I am kinda new at futures.

      Comment


        #4
        Alan, I will appreciate and if it's not too much to ask: if you can check the code, I added a timer, because of the lack of event's triggered when there is not much data moving around.

        Code:
        [SIZE="2"]namespace NinjaTrader.NinjaScript.Indicators
        {
        	
        	public class DOMSpeed : Indicator
        	{
        		
        		private List<Price> Lasts;
        		private double CurrentAsk;
        		private double CurrentBid;
        		
        		private System.Windows.Forms.Timer MyTimer;
        		
        		protected override void OnStateChange()
        		{
        			if (State == State.SetDefaults)
        			{
        				
        				// UI Variables
        				Description									= @"Speed of Ask and Bid (hits/second) from Depth Of Market.";
        				Name										= "DOM Speed";
        				
        				Calculate									= Calculate.OnEachTick;
        				IsOverlay									= false;
        				DisplayInDataBox							= true;
        				DrawOnPricePanel							= true;
        				DrawHorizontalGridLines						= true;
        				DrawVerticalGridLines						= true;
        				PaintPriceMarkers							= true;
        				ScaleJustification							= NinjaTrader.Gui.Chart.ScaleJustification.Right;
        				
        				//Disable this property if your indicator requires custom values that cumulate with each new market data event. 
        				//See Help Guide for additional information.
        				IsSuspendedWhileInactive					= false;
        				
        				AddPlot(new Stroke(Brushes.Green, 2), PlotStyle.TriangleUp, "Ask");
        				AddPlot(new Stroke(Brushes.Red, 2), PlotStyle.TriangleDown, "Bid");
        				
        				AddLine(Brushes.Black, 0, NinjaTrader.Custom.Resource.NinjaScriptIndicatorZeroLine);
        				
        				// Indicator Variables
        				Lasts = new List<Price>();
        				
        			}
        			else if (State == State.Configure)
        			{
        				MyTimer = new System.Windows.Forms.Timer();
        				MyTimer.Tick += new EventHandler(EventProcessor);
        				MyTimer.Interval = 10;
        				MyTimer.Start();
        			}
        			else if (State == State.Terminated)
        			{
        				MyTimer.Dispose();
        			}
        			
        		}
        
        		protected override void OnMarketData(MarketDataEventArgs e)
        		{
        			
        			// here.. should i execute "TriggerCustomEvent(...);"
        			// or "OnEveryInterval();" it's fine?
        
        			if (e.MarketDataType == MarketDataType.Last)
        			{
        
        				// here i stop the timer because sometimes OnMarketData and the function of the timer(OnEveryInterval) are being executed at the same time and this changes the Lasts's data and generates an error on OnEveryInterval(); that stops the Indicator
        				MyTimer.Stop();
        				
        				Lasts.Add(new Price(e));
        				OnEveryInterval();
        				
        				// once i end the calculations.. i resume the timer.. just in case
        				// [B]but I don't know if this is the best way to do it, maybe there is a better way to accomplish the same[/B]
        				MyTimer.Start();
        				
        			}
        			else
        				OnEveryInterval(); 
        			
        		}
        
        		protected override void OnBarUpdate()
        		{
        			//Add your custom indicator logic here.
        		}
        		
        		protected void EventProcessor(Object obj, EventArgs e)
        		{
        			TriggerCustomEvent(OnEveryInterval, 0, MyTimer.Interval);
        		}
        		
        		protected void OnEveryInterval(object e = null)
        		{
        			
        			/*PrintTo = PrintTo.OutputTab1;
        			Print("0: " + MyTimer.Enabled);
        			MyTimer.Stop();
        			Print("1: " + MyTimer.Enabled);
        			MyTimer.Start();
        			Print("2: " + MyTimer.Enabled);*/
        			
        			CurrentAsk = 0;
        			CurrentBid = 0;
        			
        			if (Lasts.Count == 0)
        				return;
        			
        			
        			Lasts.RemoveAll(p =>
        				p.Time < Lasts.Max(t => t.Time).AddSeconds(-1)
        			);
        		
        			CurrentAsk = Lasts.Where(p =>
        				p.Type == MarketDataType.Ask
        			).
        			Sum(p => p.Volume);
        				
        			CurrentBid = Lasts.Where(p =>
        				p.Type == MarketDataType.Bid
        			).
        			Sum(p => p.Volume);
        			
        			
        			Ask[0] = CurrentAsk;
        			Bid[0] = CurrentBid;
        			
        			
        			if (PrintData)
        			{
        				PrintTo = PrintTo.OutputTab1;
        				ClearOutputWindow();
        				Print("AskSpeed: " + CurrentAsk + "H/s");
        				
        				PrintTo = PrintTo.OutputTab2;
        				ClearOutputWindow();
        				Print("BidSpeed: " + CurrentBid + "H/s");
        			}
        			
        		}
        		
        		
        		public class Price
        		{
        			
        			public MarketDataType Type;
        			
        			public DateTime Time;
        			public double Value;
        			public long Volume;
        			
        			public Price(MarketDataEventArgs e)
        			{
        				
        				if (e.Price >= e.Ask)
        					Type = MarketDataType.Ask;
        				else if (e.Price <= e.Bid)
        					Type = MarketDataType.Bid;
        				
        				Time = e.Time;
        				Value = e.Price;
        				Volume = e.Volume;
        				
        			}
        			
        		}
        		
        
        		#region Properties
        		....
        		#endregion
        
        	}
        }[/SIZE]
        Thanks for your time.
        Last edited by Fernand0; 05-11-2018, 04:24 PM.

        Comment


          #5
          Hello Fernand0,

          I would suggest connecting to the simulated data feed (Control Center>Connections>Simulated Data Feed) and then on the chart you apply the strategy to, you could right click>data series, then set the Trading Hours to Default 24x7. This will allow simulated data to be pushed to the chart regardless of hours or day.

          With the approach above you would remove the possibility the issue is a data feed issue. Upon testing under the approach, does the issue resolve itself?

          I look forward to your reply.
          Alan P.NinjaTrader Customer Service

          Comment


            #6
            Originally posted by NinjaTrader_AlanP View Post
            Hello Fernand0,

            I would suggest connecting to the simulated data feed (Control Center>Connections>Simulated Data Feed) and then on the chart you apply the strategy to, you could right click>data series, then set the Trading Hours to Default 24x7. This will allow simulated data to be pushed to the chart regardless of hours or day.

            With the approach above you would remove the possibility the issue is a data feed issue. Upon testing under the approach, does the issue resolve itself?

            I look forward to your reply.
            I'm so sorry Alan, I've been taking care of personal matters.

            I tested with Simulated Data Feed as you suggested. It works perfectly, the thing is that it keeps sending data, all the time, there is a tick every second in the simulation.

            I downloaded some historical data, I tested in Playback Connection, and at the first hours of the session, when there is no much traffic, I was able to see that it does not work.
            Then i remember your previous question:
            Originally posted by NinjaTrader_AlanP View Post
            If you right click on your indicator >properties and set Calculate to OnEachTick or OnPriceChange are you seeing updates on the current bar?
            So, my question is this.. considering that the timer works, and you asked me that...
            There is a chance that the data from the Indicator can't be updated UNLESS there is a tick? So the timer is useless?



            BTW the last code had some irregularities, please excuse me, I'll put the new code
            System.Windows.Forms.Timer does not work properly, System.Windows.Threading.DispatcherTimer does..
            Code:
            [SIZE="2"]namespace NinjaTrader.NinjaScript.Indicators
            {
            	
            	
            	public class DOMSpeed : Indicator
            	{
            		
            		private double CurrentAsk;
            		private double CurrentBid;
            		
            		private List<Price> Lasts;
            		
            		private System.Windows.Threading.DispatcherTimer MyTimer;
            		
            		protected override void OnStateChange()
            		{
            			
            			if (State == State.SetDefaults)
            			{
            				
            				Description									= @"Speed of Ask and Bid (hits/second) from Depth Of Market.";
            				Name										= "DOM Speed";
            				
            				Calculate									= Calculate.OnEachTick;
            				IsOverlay									= false;
            				DisplayInDataBox							= true;
            				DrawOnPricePanel							= true;
            				DrawHorizontalGridLines						= true;
            				DrawVerticalGridLines						= true;
            				PaintPriceMarkers							= true;
            				ScaleJustification							= NinjaTrader.Gui.Chart.ScaleJustification.Right;
            				IsSuspendedWhileInactive					= false;
            				BarsRequiredToPlot							= 1;
            				
            				PrintData									= false;
            				Lasts										= new List<Price>();
            				CurrentAsk									= 0;
            				CurrentBid									= 0;
            				
            				AddPlot(new Stroke(Brushes.Green, 2), PlotStyle.TriangleUp, "AskSpeed");
            				AddPlot(new Stroke(Brushes.Red, 2), PlotStyle.TriangleDown, "BidSpeed");
            				
            				AddLine(Brushes.Black, 0, NinjaTrader.Custom.Resource.NinjaScriptIndicatorZeroLine);
            				
            			}
            			else if (State == State.Configure)
            			{
            				
            				MyTimer				= new System.Windows.Threading.DispatcherTimer();
            				MyTimer.Tick		+= new EventHandler(OnTimerTick);
            				MyTimer.Interval	= new TimeSpan(0, 0, 0, 0, 10);
            				MyTimer.Start();
            				
            			}
            			else if (State == State.Realtime)
            			{
            				
            				PrintTo = PrintTo.OutputTab1;
            				ClearOutputWindow();
            				PrintTo = PrintTo.OutputTab2;
            				ClearOutputWindow();
            				PrintTo = PrintTo.OutputTab1;
            				
            			}
            			else if (State == State.Terminated)
            			{
            				
            				if (MyTimer == null)
            					return;
            				
            				MyTimer.Stop();
            				MyTimer.Tick		-= new EventHandler(OnTimerTick);
            				MyTimer = null;
            				
            			}
            			
            		}
            
            		protected override void OnMarketData(MarketDataEventArgs e)
            		{
            			
            			if (e.MarketDataType != MarketDataType.Last)
            				return;
            			
            			MyTimer.Stop();
            			Lasts.Add(new Price(e));
            			MeasureSpeed();
            			MyTimer.Start();
            			
            		}
            		
            		protected override void OnBarUpdate()
            		{
            			
            			if (CurrentBar <= BarsRequiredToPlot)
            				return;
            			
            			AskSpeed[0] = CurrentAsk;
            			BidSpeed[0] = CurrentBid;
            			
            		}
            		
            		private void OnTimerTick(object sender, EventArgs e)
            		{
            			
            			// For testing, uncomment the Block below
            			/*PrintTo = PrintTo.OutputTab1;
            			ClearOutputWindow();
            			Print(DateTime.Now.ToString("HH:mm:ss.fff"));
            			
            			PrintTo = PrintTo.OutputTab2;
            			ClearOutputWindow();
            			Print(DateTime.Now.ToString("HH:mm:ss.fff"));
            			
            			PrintTo = PrintTo.OutputTab1;*/
            			
            			MeasureSpeed();
            			
            		}
            		
            		private void MeasureSpeed()
            		{
            			
            			CurrentAsk = 0;
            			CurrentBid = 0;
            			
            			if (Lasts.Count != 0)
            			{
            				
            				try {
            					
            					Lasts.RemoveAll(p =>
            						p.Time < Lasts.Max(t => t.Time).AddSeconds(-1)
            					);
            					
            					CurrentAsk = Lasts.Where(p =>
            							p.Type == MarketDataType.Ask
            					).
            					Sum(p => p.Volume);
            
            					CurrentBid = Lasts.Where(p =>
            						p.Type == MarketDataType.Bid
            					).
            					Sum(p => p.Volume);
            					
            				} catch(Exception e) {}
            				
            			}
            			
            			OnBarUpdate();
            			Output();
            			
            		}
            		
            		private void Output()
            		{
            			
            			if (PrintData)
            			{
            				
            				PrintTo = PrintTo.OutputTab1;
            				ClearOutputWindow();
            	    		Print("AskSpeed: " + CurrentAsk + "H/s");
            				
            				PrintTo = PrintTo.OutputTab2;
            				ClearOutputWindow();
            	    		Print("BidSpeed: " + CurrentBid + "H/s");
            				
            				PrintTo = PrintTo.OutputTab1;
            				
            			}
            			
            		}
            		
            		
            		public class Price
            		{
            			
            			public MarketDataType Type;
            			
            			public DateTime Time;
            			public double Value;
            			public long Volume;
            			
            			public Price(MarketDataEventArgs e)
            			{
            				
            				if (e.Price >= e.Ask)
            					Type = MarketDataType.Ask;
            				else if (e.Price <= e.Bid)
            					Type = MarketDataType.Bid;
            				
            				Time = e.Time;
            				Value = e.Price;
            				Volume = e.Volume;
            				
            			}
            			
            		}
            		
            
            		#region Properties
            		
            		[NinjaScriptProperty]
            		[Display(Name="Print Data", Order=1, GroupName="Parameters")]
            		public bool PrintData
            		{ get; set; }
            
            		[Browsable(false)]
            		[XmlIgnore]
            		public Series<double> AskSpeed
            		{
            			get { return Values[0]; }
            		}
            
            		[Browsable(false)]
            		[XmlIgnore]
            		public Series<double> BidSpeed
            		{
            			get { return Values[1]; }
            		}
            		
            		#endregion
            
            		
            	}
            	
            	
            }[/SIZE]
            Attached Files

            Comment


              #7
              Hello,

              Thank you for writing in.

              Could you please send an email to platformsupport[at]ninjatrader[dot]com with Attn: Alan P in the Subject line. Also within the email please include a link to this thread, and attach the log and trace files for the day in subject which you can find in My Documents>NinjaTrader8>Log and My Documents>NinjaTrader8/Trace folders.

              I look forward to your email.
              Alan P.NinjaTrader Customer Service

              Comment


                #8
                Ok, I tested the 3 timers... testing it with Draw.Text() and Print()

                The Print() shows that the timer works.
                Now.. Draw.Text()... does not work, changing Plots, etc.

                So, NT refuses to change some data unless a Tick is triggered.


                There is a way to "bypass" this..?

                Comment


                  #9
                  Hello Fernand0,

                  I have followed up with an email and will work with you on that thread.
                  Alan P.NinjaTrader Customer Service

                  Comment


                    #10
                    Originally posted by Fernand0
                    I've already did, you can't change a Plot if OnBarUpdate() is not triggered.

                    Requesting advice to change Plot data from Timer Event method.
                    Originally posted by NinjaTrader_AlanP
                    Using a timer in a script is going to be unsupported an beyond the scope of which our support desk can assist with. In the support department at NinjaTrader we do not create, debug, or modify code for our clients. This is so that we can maintain a high level of service for all of our clients.

                    You can also contact a professional NinjaScript Consultants who would be eager to create or modify a script at your request or assist you with your script. Please let me know if you would like our business development follow up with you with a list of professional NinjaScript Consultants who would be happy to create this script or any others at your request.
                    End of the thread. Thanks Alan
                    Last edited by Fernand0; 06-04-2018, 02:47 PM.

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by funk10101, Today, 09:43 PM
                    0 responses
                    6 views
                    0 likes
                    Last Post funk10101  
                    Started by pkefal, 04-11-2024, 07:39 AM
                    11 responses
                    37 views
                    0 likes
                    Last Post jeronymite  
                    Started by bill2023, Yesterday, 08:51 AM
                    8 responses
                    44 views
                    0 likes
                    Last Post bill2023  
                    Started by yertle, Today, 08:38 AM
                    6 responses
                    26 views
                    0 likes
                    Last Post ryjoga
                    by ryjoga
                     
                    Started by algospoke, Yesterday, 06:40 PM
                    2 responses
                    24 views
                    0 likes
                    Last Post algospoke  
                    Working...
                    X