Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Why doesn't my code work?

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

    Why doesn't my code work?

    I need to move my chart by 1 bar forward.

    protected override void OnRender(ChartControl chartControl, ChartScale chartScale)
    {
    LastVisibleBar = ChartBars.ToIndex;

    if (LastVisibleBar < 200)
    {
    System.Threading.Thread.Sleep(1000);


    ChartControl.Dispatcher.InvokeAsync((Action)(() =>
    {
    ChartControl.OwnerChart.Focus();
    }));

    System.Windows.Forms.SendKeys.SendWait("{RIGHT}");

    }
    }

    #2
    Hello Leeroy_Jenkins,

    Thank you for the post.

    The first item I can note here is that you have Thread.Sleep in OnRender, that would be very bad and prevent the script from working correctly. That needs to be removed, you never want to delay any of the events your script produces. If you need to delay your code in OnRender you can use a DateTime and a condition to control the frequency at which your code is allowed to be called without delaying the thread.

    The second problem is using the Windows.Forms namespace code such as SendKeys. This is no longer applicable as we are not using WindowsForms, while this can technically work it does not account for WPF focus so it would not necessarily work unless the control in question is focused. To see how to send keys to a WPF control please see the Rollover Indications indicator in the public user app share: https://ninjatraderecosystem.com/use...indications-2/

    Another item is that this is from OnRender, have you considered using a Timer for this type of logic? You can either limit your logic by using a condition or use a timer with a fixed interval. https://ninjatrader.com/support/help...ghtsub=trigger

    A DateTime condition may look like this:


    Code:
    private DateTime lastCalled; 
    
    
    protected override void OnRender(ChartControl chartControl, ChartScale chartScale)
    {
        if(DateTime.Now > lastCalled)
        {
             // your code here
             lastCalled = DateTime.Now.AddSeconds(10); //call every 10 seconds
        }
    }


    I look forward to being of further assistance.

    Comment


      #3
      I tried to implement logic from that indicator. I can't call click event(object sender, RoutedEventArgs e) within OnRender. How should I call it?

      Then I tried more simple and less optimal way like this:

      protected override void OnRender(ChartControl chartControl, ChartScale chartScale)
      {
      LastVisibleBar = ChartBars.ToIndex;

      if((LastVisibleBar < 200) && (DateTime.Now > lastCalled));
      {
      ChartControl.OwnerChart.Focus();
      Keyboard.FocusedElement.RaiseEvent(new KeyEventArgs(Keyboard.PrimaryDevice,
      PresentationSource.FromVisual(ChartControl.OwnerCh art), 0, Key.Right) { RoutedEvent = Keyboard.PreviewKeyDownEvent } );
      lastCalled = DateTime.Now.AddSeconds(3);
      }
      Print(+LastVisibleBar);
      }
      But it ignores if((LastVisibleBar < 200) && (DateTime.Now > lastCalled)) condition.
      Last edited by Leeroy_Jenkins; 05-21-2020, 02:57 AM.

      Comment


        #4
        Hello Leeroy_Jenkins,

        You have a semi colon at the end of your if statement making it not valid, you need to remove that. An if statement will always be in the following format with no semi colons:

        Code:
        if(condition)
        {
        }
        Please let me know if I may be of further assistance.

        Comment


          #5
          Fixed. But it doesn't work. Chart doesn't move.
          Last edited by Leeroy_Jenkins; 05-25-2020, 02:23 PM.

          Comment


            #6
            Hello Leeroy_Jenkins,

            Are you certain your code is being called?

            Have you tried other events such as the existing working event from the rollover indicator as a test?

            What have you done to test at this point to find the problem?

            I can see a few problems in the sample that you provided, the semi colon is one major part. You are also not using dispatchers or checking if objects are null so this will likely see errors. I would suggest to take a look at the rollover indicator and try to more closely replicate how that uses the code you extracted. You will need to surround this code with a dispatcher, take a look starting at line 70 of the rollover indicator. All of that code with the dispatcher that is surrounding the CreateWPFControls is necessary to make sure you can execute that code.

            I would suggest to use Prints if you have not added any to make sure your logic is being called. Also please ensure to check the output window or log tab to see if you are having errors.


            Please let me know if I may be of further assistance.

            Comment


              #7
              Ok, get it, now it works.
              If this indicator sends some key, can another program catch this?
              I have 2 keys, one if right arrow to move the chart and another one is PrtScr to capture sreenshots via another program. But the last one doesn't work.

              Comment


                #8
                Hello Leeroy_Jenkins,

                Not by using this approach, this targets the control in question which is needed with how WPF windows/controls work with focus. Have you considered just using the charts screenshot feature?

                I look forward to being of further assistance.

                Comment


                  #9
                  I tried to implement some charts screenshot code but it causes an error: Object reference not set to an instance of an object.

                  Attached Files

                  Comment


                    #10
                    Hello Leeroy_Jenkins,

                    The error you are seeing means something is null, you would need to track down what line of code that is with what you have added.

                    I had commented about this in post 6, that you will run into some problems with the way you have this so far, it would be best to pause and review what you have made to see what needs fixed.

                    ChartControl can be null so you absolutely need a null check for that, you currently do not have any checks to make sure that object is not null. You need a condition like the following:
                    if(ChartControl != null)
                    {
                    }

                    If the error happened specifically after you added the screenshot code you can use prints to track down what line that is. Once you know what line is throwing the error you can see what is needed such as a condition checking if the object has a value.

                    I look forward to being of further assistance.

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                    0 responses
                    571 views
                    0 likes
                    Last Post Geovanny Suaza  
                    Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                    0 responses
                    331 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
                    549 views
                    1 like
                    Last Post Geovanny Suaza  
                    Started by RFrosty, 01-28-2026, 06:49 PM
                    0 responses
                    549 views
                    1 like
                    Last Post RFrosty
                    by RFrosty
                     
                    Working...
                    X