Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Problem with Print() to Output Window

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

    Problem with Print() to Output Window

    Hey guys.
    If I load more than 4 days of data and then load my indicator with a command to execute a Print() in the output window, the printed data repeats itself and loses order.​

    An example with the same type of code as my indicator.​

    Code:
            private List<string> dateBarPositionEntry = new List<string> { "0" };
    
            protected override void OnBarUpdate()
            {
                if (CurrentBar < 3)
                    return;
    
                if (Low[0] < Low[1])
                {
                    string date = Time[0].ToShortDateString() + "," + Time[0].DayOfYear.ToString() + "," +  Time[0].DayOfWeek.ToString() + "," +  Time[0].ToShortTimeString();
    
                    dateBarPositionEntry.Insert(0, date);
                }
    
                if (Bars.IsLastBarOfSession)
                {
                    for(int i = 0; i <= dateBarPositionEntry.Count - 1; i++)
                    {
                        Print(dateBarPositionEntry[i]);
                    }
                }
            }​
    the output:

    Click image for larger version  Name:	15_02_10-Window.png Views:	0 Size:	9.7 KB ID:	1223733

    What is wrong?​ My list is incorrect?
    By the way, my goal with this condition "if (Bars.IsLastBarOfSession)" is to make only one print after all bars are loaded.​
    Last edited by rafaelcoisa; 11-15-2022, 02:02 PM.

    #2
    Hello rafaelcoisa,

    Thank you for your patience.

    What are your Data Series settings for less than 4 days compared to the settings with more than 4 days where you seem to be observing unexpected results? You may view the Data Series by right-clicking a chart and then selecting Data Series. Please provide a screenshot of both charts so I may test on my end with the same instrument, timeframe, etc. and report back.

    I look forward to your reply.

    Comment


      #3
      I'm using free data from CQG.

      this one is wrong:
      Click image for larger version

Name:	1.png
Views:	363
Size:	78.0 KB
ID:	1223765

      this one work fine:

      Click image for larger version

Name:	2.png
Views:	312
Size:	71.8 KB
ID:	1223766

      this one is wrong:
      Click image for larger version

Name:	3.png
Views:	333
Size:	76.4 KB
ID:	1223767

      Comment


        #4
        Hello rafaelcoisa,

        Thank you for your response.

        I believe I understand what you have explained - you only want the list printed one time for all of the data once it has loaded. I suspect that was is happening is that the condition "if (Bars.IsLastBarOfSession)" is triggered for the last bar of each session on the chart. Essentially it is printing the list for the first session loaded on the chart, then when it gets to the last bar of the next session, it prints the list with all of the new values added for the second session, etc. So you are getting the list printed multiple times with values from a new session added each time the list prints, and the more days to load on your chart the more times this list will be printed.
        If you only want the list to print once, you will likely need to modify the logic in your condition to ensure it is only running the code block once. For example, I created a private bool listPrinted that is set to false in State.DataLoaded. Inside of OnBarUpdate(), I kept your original logic and just changed the condition for when to loop through and print the list to "if (State == State.Realtime && !listPrinted)" which will check if the historical data has loaded and the chart has reached realtime. That would mean that the historical data completed loading on the chart. Once the list is printed, listPrinted is set to true so this will only occur once. If you ever needed to print again for any reason, you could reload NinjaScript.

        Code:
        protected override void OnBarUpdate()
        {
        if (CurrentBar < 3)
        return;
        
        if (Low[0] < Low[1])
        {
        string date = Time[0].ToShortDateString() + "," + Time[0].DayOfYear.ToString() + "," + Time[0].DayOfWeek.ToString() + "," + Time[0].ToShortTimeString();
        
        dateBarPositionEntry.Insert(0, date);
        }
        
        if (State == State.Realtime && !listPrinted)
        {
        for(int i = 0; i <= dateBarPositionEntry.Count - 1; i++)
        {
        Print(dateBarPositionEntry[i]);
        }
        listPrinted = true;
        }
        }
        Please let us know if we may be of further assistance.​​

        Comment


          #5
          I tried as you indicated:

          Code:
                       private bool listPrinted;
          
                      else if (State == State.DataLoaded)
                      {
                            listPrinted = false;
                      }
                  
          
                  protected override void OnBarUpdate()
                  {
                      if (CurrentBar < 3)
                          return;
          
                      if (Low[0] < Low[1])
                      {
                          string date = Time[0].ToShortDateString() + "," + Time[0].DayOfYear.ToString() + "," +  Time[0].DayOfWeek.ToString() + "," +  Time[0].ToShortTimeString();
          
                          dateBarPositionEntry.Insert(0, date);
                      }
          
                      if (State == State.Realtime && !listPrinted)//(Bars.IsLastBarOfSession)
                      {
                          for(int i = 0; i <= dateBarPositionEntry.Count - 1; i++)
                          {
                              Print(dateBarPositionEntry[i]);
                          }
          
                          listPrinted = true;
                      }
                  }​
          Now, print does not execute.​

          Comment


            #6
            @NinjaTrader_Emily.

            oh i got it.
            the code is run only when a new bar is plotted.
            Is there a way to run it once but offline, that is, without receiving data from the data provider?
            I want to load, for example, 1 year of data and after everything is loaded and calculated, I want to check my lists in the output window in the order of the occurrences of the conditions, etc.

            Comment


              #7
              Hello rafaelcoisa,

              Thanks for your reply.

              If you would like to run OnBarUpdate() for only the last historical bar, you could use the following condition:
              Code:
              if (State == State.Historical && CurrentBar == Count - 2)
              Please let us know if we may be of further assistance.

              Comment


                #8
                Hey, @NinjaTrader_Emily

                When I use
                Code:
                if (State == State.Historical && CurrentBar == Count - 2)
                with TickReplay enabled, the Print() doesn't work.

                Is there any code I can add to make it work?​

                Comment


                  #9
                  Hello rafaelcoisa,

                  Thank you for your note.

                  Do you receive any error messages on the Log tab of the Control Center or in the NinjaScript Output window? What are you trying to print? Are you able to get other print statements to show, like a "test" print for when State == State.RealTime?

                  I look forward to your reply.

                  Comment


                    #10
                    I'm trying to print just once in the output window like you showed me, but now with TickReplay enabled.

                    Code:
                            //private bool listPrinted = false;
                    
                            protected override void OnBarUpdate()
                            {
                                //if (State == State.Realtime && !listPrinted)
                                if (State == State.Historical && CurrentBar == Count - 2)
                                {
                                    Print("A");
                    
                                    //listPrinted = true;
                                }
                            }​

                    Comment


                      #11
                      Hello rafaelcoisa,

                      Thank you for your reply.

                      I tested out your snippet and I see it is printing for each historical bar. This is because Count is updated as the bars are built with Tick Replay. An alternative would be to save the count to a variable in State.DataLoaded and use that variable in your condition such as the following:

                      Code:
                      private int myCount;
                      protected override void OnStateChange()
                      {
                      if (State == State.DataLoaded)
                      {
                      Print("Count: " + Count);
                      myCount = Count;
                      }
                      }
                      
                      protected override void OnBarUpdate()
                      {
                      if (State == State.Historical && CurrentBar == myCount - 2)
                      {
                      Print(String.Format("CurrentBar: {0} Count: {1} myCount: {2} myCount - 2: {3}", CurrentBar, Count, myCount, myCount - 2));
                      }
                      }
                      Please don't hesitate to reach out with any additional questions or concerns.

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                      0 responses
                      647 views
                      0 likes
                      Last Post Geovanny Suaza  
                      Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                      0 responses
                      369 views
                      1 like
                      Last Post Geovanny Suaza  
                      Started by Mindset, 02-09-2026, 11:44 AM
                      0 responses
                      108 views
                      0 likes
                      Last Post Mindset
                      by Mindset
                       
                      Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                      0 responses
                      572 views
                      1 like
                      Last Post Geovanny Suaza  
                      Started by RFrosty, 01-28-2026, 06:49 PM
                      0 responses
                      573 views
                      1 like
                      Last Post RFrosty
                      by RFrosty
                       
                      Working...
                      X