Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

How to trigger a button click in my code

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

    How to trigger a button click in my code

    Hi,
    I have two buttons in my code, a Buy and a Sell that I like to be triggered either by a click or through the code.
    • myBuyButton.Click += OnMyButtonClick;
    • mySellButton.Click += OnMyButtonClick;
    I do believe I need to call the method OnMyButtonClick so I tried the following:
    • OnMyButtonClick(MyBuyButton) to simulate the buy button click
    • OnMyButtonClick(MySellButton) to simulate the sell button click

    None of them works.
    Attached Files

    #2
    Hello GoodBro,

    Welcome to the NinjaTrader forums!

    The OnButtonClick() event handler method will be run when the button is clicked.

    Currently this code sets a variable, like longButtonClicked. In OnBarUpdate() if this variable is set, and a quite a few other conditions are also true at at the same time (&& Close[1] >= MAX(High, 2)[2] + 0 * TickSize && High[1] < CurrentDayOHL().CurrentHigh[1] && High[0] > High[1]), an order is submitted with an order method such as EnterLong().

    Below I am also providing a link to a forum post with helpful information about getting started with programming in C#.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      This is how I find the chart trader buttons in my addon:

      Code:
      Button buyMktButton = parentWindow.FindFirst("ChartTraderControlQuickBuyMarketButton") as Button;
      Button sellMktButton = parentWindow.FindFirst("ChartTraderControlQuickSellMarketButton") as Button;​
      Then I use WPF automation Ids to invoke them like this:
      Code:
      using System.Windows.Automation;
      using System.Windows.Automation.Peers;
      using System.Windows.Automation.Provider;​
      
      ...
      ...
      ButtonAutomationPeer buyMktButtonPeer = new ButtonAutomationPeer(buyMktButton);
      IInvokeProvider buyMktButtonInvokeP = buyMktButtonPeer.GetPattern(PatternInterface.Invoke) as IInvokeProvider;
      buyMktButtonInvokeP.Invoke();​
      ...


      See here for reference: https://stackoverflow.com/a/728444/2877168

      Comment


        #4
        I was looking for this solution for some time. ​​Thanks a lot to skipper-flatiron-18 ​​for this post.

        Comment

        Latest Posts

        Collapse

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