Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Using Print() to debug

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

    Using Print() to debug

    I am using Print() to debug and was wondering if there was a way to print the time as well as the message? So I can see when it happened when looking in the output window.

    Thanks

    Mark

    #2
    Hello,

    Thanks for the forum post.

    Sure you can use the command:

    PrintWithTimeStamp("Hello World");

    Instead of Print();

    Let me know if I can be of further assistance.
    BrettNinjaTrader Product Management

    Comment


      #3
      Thanks Brett

      Comment


        #4
        More Flexible Way to Use Print Statements for Debugging

        Originally posted by MJUK10 View Post
        I am using Print() to debug and was wondering if there was a way to print the time as well as the message? So I can see when it happened when looking in the output window.
        Mark, my preferred way to add debugging statements is to use String.Format() to combine output into a single string for the Print() function.

        Along with the time, I like to always output the name of the Indicator/Strategy, too, so if you're debugging more than one thing at a time (or have older code that still has Print statements in it) you can tell where all the messages are coming from. That's what Base.Name does in the following example.

        Lastly I usually put in the originating method name (e.g., "OnBarUpdate") because I frequently have Print's in more than one place in my Indicator/Strategy.

        Here's an example,

        Print( String.Format( "{0} - {1} / OnBarUpdate: CurrentBar={2} Close[0]={3}",

        Time[0], base.Name, CurrentBar, Close[0] ));

        The {0}, {1}, {2}, {3} values get replaced by the parameters that follow the format string in the same sequence starting with {0}. You can google "String.Format" for more info & examples, but mostly it figures everything out for you. (Note: There are no ".ToString()"s or "+"s required - which I think is much nicer and more readable.)

        When you look at the Output Window, the results of using the above statement will look something like this:

        4/28/2011 1:05:06 PM - Test_Strategy / OnBarUpdate: CurrentBar=20 Value[0]=805.2
        4/28/2011 1:05:08 PM - Test_Strategy / OnBarUpdate: CurrentBar=21 Value[0]=805.5
        4/28/2011 1:05:12 PM - Test_Strategy / OnBarUpdate: CurrentBar=22 Value[0]=805.8


        Good luck with your debugging.
        Last edited by KBJ; 04-29-2011, 10:39 AM. Reason: fixed formatting of example so it looks better

        Comment


          #5
          Thanks for the info KBJ.

          Comment


            #6
            Brett

            Does PrintWithTimeStamp no longer work in NT7 in 7.0.1000.5? or is there a special syntax?

            Comment


              #7
              What issues are you seeing MindSet? It should work, gave it a quick test and see no issue.

              Thanks.

              Comment


                #8
                Print

                Here is my sample line in OnBarUPdate()
                if(FirstTickOfBar)
                PrintWithTimeStamp("Hello");

                does not exist in the current context??

                Comment


                  #9
                  Are you trying this in an indicator or strategy?

                  Comment


                    #10
                    Sample

                    In this instance I added it to samplegetbar


                    Code:
                           protected override void OnBarUpdate()
                            {
                    			// Plots the SMA indicator.
                    			SMAPlot.Set(SMA(sMAPeriod)[0]);
                    		
                    				
                    				
                    			
                    			/* This sample assumes instrument sessions that go from Monday-Friday and not the weekends.
                    			   If you are using an instrument that has weekend trading sessions like the ES futures, please be aware that the logic presented will need to be
                    			   modified to reflect the trading sessions for your particular instrument.
                    			   Here, timeOfInterest is reset once per day at session break. */
                    			if (Bars.FirstBarOfSession && FirstTickOfBar)
                    			{
                    				PrintWithTimeStamp("Hello");
                    				/* If the day is Monday, and we want the value from Friday, we must subtract 3 days from the current date.
                    				   If the day is Tuesday-Friday, just subtract one day. */
                    				if (Time[0].DayOfWeek == DayOfWeek.Monday)
                    					timeOfInterest = new DateTime(Time[0].Year, Time[0].Month, Time[0].Day - 3, 9, 30, 0);
                    				else
                    					timeOfInterest = new DateTime(Time[0].Year, Time[0].Month, Time[0].Day - 1, 9, 30, 0);
                    			}
                    			
                    			/* Determine the number of bars ago it would take to access the 9:30AM bar for the previous trading day.
                    			   GetBar() returns either the bars ago number for the 9:30AM bar if it exists or the number for the first bar after 9:30AM 
                    			   if there isn’t a specific bar with the exact 9:30AM timestamp (e.g. tick-based bars). With tick bars, it is possible 
                    			   for many bars to have the same exact time stamp. In this case, the first bar of all the bars with the same timestamp is returned.*/
                    			barsAgo = GetBar(timeOfInterest);
                    			
                    			/* If GetBar returns 0, the time you are looking for would either be the current bar or at some time after the current bar.
                    			   Also, make sure there are enough bars to draw the line 10 bars back. */
                    			if (barsAgo != 0 && CurrentBar > 10)
                    			{
                    				// Draw a line with the value of the SMA the correct number of bars ago.
                    				DrawLine("yesterday SMA", false, 10, SMAPlot[barsAgo], 0, SMAPlot[barsAgo], Color.Black, DashStyle.Solid, 2);
                                }
                            }

                    Comment


                      #11
                      It would only be available in strategy context, so from an indicator it would not work unfortunately.

                      Comment


                        #12
                        Really?

                        Please put that forward as a suggestion then thanks.

                        Comment


                          #13
                          Hello,

                          Looks like PrintWithTimeStamp is depreciated as I can no longer find it in the help guide. In this case instead of using this method to print with time stamps.

                          Please use the following:

                          Print(Time[0] + " Hello World");

                          Instead, this will then work in a strategy or indicator.

                          Let me know if I can be of further assistance.
                          BrettNinjaTrader Product Management

                          Comment


                            #14
                            Hi

                            What I was trying to do was make a method so that when debugging and such I could just do something like
                            PrintMyWay("Hello");

                            but my skill level isn't up to this just yet!!

                            Code:
                            		public string PrintMyWay(string s)
                            		{
                            			if (s.Length>1)
                            			Print(Time[0] + " " + s);
                            			else
                            			Print("No string defined");
                            		}

                            Comment


                              #15
                              Yea thats one way to do it if you dont want to use Print(Time[0]);
                              BrettNinjaTrader Product Management

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by CarlTrading, 03-31-2026, 09:41 PM
                              1 response
                              67 views
                              0 likes
                              Last Post NinjaTrader_ChelseaB  
                              Started by CarlTrading, 04-01-2026, 02:41 AM
                              0 responses
                              36 views
                              0 likes
                              Last Post CarlTrading  
                              Started by CaptainJack, 03-31-2026, 11:44 PM
                              0 responses
                              59 views
                              1 like
                              Last Post CaptainJack  
                              Started by CarlTrading, 03-30-2026, 11:51 AM
                              0 responses
                              62 views
                              0 likes
                              Last Post CarlTrading  
                              Started by CarlTrading, 03-30-2026, 11:48 AM
                              0 responses
                              53 views
                              0 likes
                              Last Post CarlTrading  
                              Working...
                              X