Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Strange Print behavior

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

    Strange Print behavior

    I have an indicator that detects a mouse click to run a method.

    this is the mouse click code
    HTML Code:
            protected void MouseClicked(object sender, MouseButtonEventArgs e)
            {
                clickPoint.X = ChartingExtensions.ConvertToHorizontalPixels(e.GetPosition(ChartControl as IInputElement).X, ChartControl.PresentationSource);
                clickPoint.Y = ChartingExtensions.ConvertToVerticalPixels(e.GetPosition(ChartControl as IInputElement).Y, ChartControl.PresentationSource);
                
                if(clickPoint.Y > 50 && clickPoint.Y < ChartPanel.H - 150 && clickPoint.X < ChartPanel.W - 200)        
                {
                    CheckForMTF(IFVGopen);
                    CheckForMTF(IFVGclosed);                
                }
    ​

    A mouse click runs this code

    HTML Code:
            private void CheckForMTF(List<FVG> myList)
            {
                showMTF = 0;
                {
                    double anchorPrice    = Instrument.MasterInstrument.RoundToTickSize(chartScale.GetValueByY((float)clickPoint.Y));            
                    DateTime clickTime    = ChartControl.GetTimeBySlotIndex((int)ChartControl.GetSlotIndexByX((int)clickPoint.X));
                    
                    for (int index = myList.Count - 1; index >= 0 ; index--)    
                    {
                        currentTopMTF         = myList[index].top;
                        currentBottomMTF     = myList[index].bottom;
                        currentLeftMTF         = myList[index].gapStartTime;
                        currentRightMTF     = myList[index].gapEndTime;
                        
                        if (currentTopMTF >= anchorPrice && currentBottomMTF <= anchorPrice)
                            if (currentLeftMTF <= clickTime && currentRightMTF >= clickTime)
                        {                            
                            if (myList[index].type == BULL)
                                showMTF = -1;
                            if (myList[index].type == BEAR)
                                showMTF = 1;
                                    
    Print(anchorPrice + "   " + clickTime + "   " + myList[index].heightT + "    " + showMTF);                    
                            
                        }
                        
                        
                    }
                }
            }
    ​

    It runs fine. But when I first load the indicator, there is a single Print line. If I press F5 on the chart to reload the indicator, I get 2 Print lines when I click. If I press F5 again I get 3 Print lines. then 4, then 5. If I remove the indicator and reload it, it goes back to 1 Print line.

    here is the output window

    Click image for larger version

Name:	image.png
Views:	73
Size:	38.7 KB
ID:	1316813


    This seems like odd behavior so I want to make sure I'm not screwing up something else.

    . What is causing it?

    #2
    I got the same multiplying Print effect when I used reload historical data. Every time I reloaded Historical data, it printed one more time than before.

    That seems wrong

    Comment


      #3
      Correct me if I'm wrong, but the Print statement is inside the loop... so yeah... it's going to print it "index" times based on the decrement of that variable index
      Maybe only print it when index = 1 by putting if(index=1) in front of the Print statement?
      Last edited by MojoFilter; 09-04-2024, 02:19 PM.

      Comment


        #4
        hey MojoFilter

        the challenge is that the number of times it prints doesn't have anything to do with the loop. The loop just finds the one valid case.

        t depends entirely on how many times i either reload historical data or press F5 to refresh the indicator. And there is a one to one correlation to the number of times I refresh to the number of times it prints.

        very strange

        Comment


          #5
          Just for fun I would put the index value itself into the print statement and see what it does

          Comment


            #6
            this keeps getting "funnier"

            here is my updated code. First I changed the print statement to just print out the Count and the index. Then I added clickTime to the print statement

            You can see that it is STILL printing the line with just Count and index even though there is no longer a Print statement for that. There is some residual printing phenomena going on.

            And each time I refresh the indicator it adds another print line. Same as before. But the count and index haven't changed.

            But it DOESN'T change the number of times the phantom Print statement occurs. Probably because it's not longer in the source code. But it still prints



            Click image for larger version  Name:	image.png Views:	0 Size:	34.8 KB ID:	1316828


            current code

            HTML Code:
                    private void CheckForMTF(List<FVG> myList)
                    {
                        showMTF = 0;
                        double anchorPrice    = Instrument.MasterInstrument.RoundToTickSize(chartScale.GetValueByY((float)clickPoint.Y));            
                        DateTime clickTime    = ChartControl.GetTimeBySlotIndex((int)ChartControl.GetSlotIndexByX((int)clickPoint.X));
                        
                        for (int index = myList.Count - 1; index >= 0 ; index--)    
                        {
                            currentTopMTF         = myList[index].top;
                            currentBottomMTF     = myList[index].bottom;
                            currentLeftMTF         = myList[index].gapStartTime;
                            currentRightMTF     = myList[index].gapEndTime;
                            
                            if (currentTopMTF >= anchorPrice && currentBottomMTF <= anchorPrice &&
                                currentLeftMTF <= clickTime && currentRightMTF >= clickTime)
                            {        
            //---    set foundGap to true so no other gaps get checks                    
                                foundGap = true;
                                if (myList[index].type == BULL)
                                    showMTF = -1;
                                if (myList[index].type == BEAR)
                                    showMTF = 1;
                                        
            Print(clickTime + "   " + myList.Count + "   " + index);                    
            ​

            It appears to be some kind of phantom print statement. When I remove the indicator and then re-add it, I get the same multi print line behavior. When I close the workspace and then re-open it, I get the right result

            Click image for larger version  Name:	image.png Views:	0 Size:	18.4 KB ID:	1316829

            the problem is that I use Print statements to debug code so this kind of behavior is a pain since I have to close and re-open the workspace for the Print statement to work correctly if I change what I print out.

            I have put print statements in other parts of my code to debug. Even if I remove the Print statement and re-compile, it continues to Print the phantom line until I close and re-open the workspace.

            I don't think its a programming problem ​
            Last edited by cre8able; 09-04-2024, 03:30 PM.

            Comment


              #7
              Commented out Print statement. Re-compiled 3 times (you can tell by the number of times the 2nd Print statement is there.

              Commented out print statement still outputs.

              Is there something I'm missing on how the print statement is supposed to work? It works correctly if I close and re-open the workspace. But that can't be the way it is intended

              Click image for larger version

Name:	image.png
Views:	48
Size:	59.6 KB
ID:	1316831

              Comment


                #8
                I found this thread because I am having strange print behavior also... something is printing the word "else" on every tick apparently, so the data I want to see goes by in a tenth of a second. Long live the print window... the window is junk! lol

                Comment


                  #9
                  It's a great tool... when it works. And I don't remember having this problem in the past.

                  Comment


                    #10
                    Hello cre8able,

                    May I confirm that the button .Click event handler method is being subscribed in State.DataLoaded and unsubscribed in State.Terminated?
                    Chelsea B.NinjaTrader Customer Service

                    Comment


                      #11
                      hey Chelsea

                      I have this in State.DataLoaded

                      HTML Code:
                                  else if(State == State.DataLoaded)
                                  {
                      //---    General            
                                      if (ChartControl != null)
                                          ChartControl.MouseLeftButtonDown += MouseClicked;
                                      
                      ​
                      This in State.Terminated

                      HTML Code:
                                  else if (State == State.Terminated)
                                  {
                                      if (ChartControl != null)
                                          ChartControl.MouseLeftButtonDown -= MouseClicked;
                                  }
                      ​
                      I got the code from MouseXYtoBarTimerPriceExample

                      Comment


                        #12
                        Hello cre8able,

                        Using the MouseXYtoBarTimerPriceExample I am not seeing this behavior.


                        Using this test script are you using the behavior on your end?

                        Chelsea B.NinjaTrader Customer Service

                        Comment


                          #13
                          hey Chelsea

                          I also don't see the behavior using the MouseXYtoBarTimerPriceExample.

                          But there must be something weird going on since I get the phantom print statements even if I have already commented them out. I wouldn't think that had anything to do with mouse clicks.

                          I have a 3526 line program so it's hard to find the source of the problem if I don't have a general direction to look in.

                          Any suggestions?

                          Comment


                            #14
                            Hello cre8able,

                            That would suggest it's something in the code of your specific script that is different than the example.

                            I would suggest commenting out all of the logic and starting simple with the logic from the example. Be sure to restart NinjaTrader first incase there are left over handlers assigned during changes.
                            Chelsea B.NinjaTrader Customer Service

                            Comment

                            Latest Posts

                            Collapse

                            Topics Statistics Last Post
                            Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                            0 responses
                            579 views
                            0 likes
                            Last Post Geovanny Suaza  
                            Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                            0 responses
                            334 views
                            1 like
                            Last Post Geovanny Suaza  
                            Started by Mindset, 02-09-2026, 11:44 AM
                            0 responses
                            101 views
                            0 likes
                            Last Post Mindset
                            by Mindset
                             
                            Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                            0 responses
                            554 views
                            1 like
                            Last Post Geovanny Suaza  
                            Started by RFrosty, 01-28-2026, 06:49 PM
                            0 responses
                            551 views
                            1 like
                            Last Post RFrosty
                            by RFrosty
                             
                            Working...
                            X