Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Button not working to close al lpositions

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

    Button not working to close al lpositions

    Hi, I havea close al lbutton in my strategy when i click on it it closes and imideatly reopens position.
    Not sure where i am making a mistake

    Code:
    private void btnCloseAllClick(object sender, RoutedEventArgs e)
            {
                System.Windows.Controls.Button button = sender as System.Windows.Controls.Button;
    
                if (button != null)
                {
                    var unused2 = Dispatcher.InvokeAsync(() =>
                    {
                        if (Position.MarketPosition == MarketPosition.Long)
                        {
    //                        var unused1 = ExitLong();
                            ExitLong();
                        }
                        else if (Position.MarketPosition == MarketPosition.Short)
                        {
    //                        var unused = ExitShort();
                            ExitShort();
                        }
                    });
                }
            }
    
            private void DisposeCleanUp()
            {
                if (btnLongsOn != null)
                {
                    var unused2 = chartWindow.MainMenu.Remove(btnLongsOn);
                }
    
                btnLongsOn.Click -= btnLongsOnClick;
                if (btnShortsOn != null)
                {
                    var unused1 = chartWindow.MainMenu.Remove(btnShortsOn);
                }
    
                btnShortsOn.Click -= btnShortsOnClick;
                if (btnCloseAll != null)
                {
                    var unused = chartWindow.MainMenu.Remove(btnCloseAll);
                }
    
                btnCloseAll.Click -= btnCloseAllClick;
            }​

    #2
    Hello tkaboris,

    Thanks for your post.

    If the script is not behaving as expected, debugging prints need to be added to the script to understand exactly how it is behaving.

    Add prints to the script that print out the values being used for your logic to submit orders to understand how they are evaluating.

    Below is a link to a forum post that demonstrates how to use prints to understand behavior.
    https://ninjatrader.com/support/foru...121#post791121

    Here is a sample script from the Ecosystem User App Share you could view that demonstrates submitting orders using buttons: https://ninjatraderecosystem.com/use...bar-buttons-2/
    <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

    Comment


      #3
      Well i am not sure what to print in this case.
      if i simply put print it just prints ExitLong

      if (Position.MarketPosition == MarketPosition.Long)
      {
      // var unused1 = ExitLong();
      Print("ExitLong");
      ExitLong();
      }
      else if (Position.MarketPosition == MarketPosition.Short)
      {
      // var unused = ExitShort();
      Print("ExitShort");
      ExitShort();​

      Example doesnt have Close all function, only long and short

      Comment


        #4
        maybe its because setup is still being valid and reopens an order? If so how can i handle it
        My strategy is on eachtick. I have added bool to reset on tickofbar but its not helpful sits reset is on the next bar..
        Last edited by tkaboris; 05-30-2023, 02:39 PM.

        Comment


          #5
          After observing for a while, if i hit close button when position is in negative it reopens on next tick, if i hit close button when position is in profit it doesnt reopen the trade

          Comment


            #6
            Hello tkaboris,

            Thanks for your notes.

            A new order is likely being submitted when the previous order is closed because your conditions to place another Entry order are evaluating as true.

            You would have to further debug your script by reducing code and adding prints to understand why it is behaving unexpectedly. Prints would need to be added to the script that prints out the conditions being used to submit your Entry orders so that you can see how those conditions are evaluating.

            I suggest you also review the Ecosystem User App Share sample strategy linked in post # 2 to see how to submit entry and exit orders with the click of buttons.
            <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

            Comment


              #7
              I am looking into an example and I see that my script uses similar logic for sending close order function.. it closes as suppose to.
              I guess how to make sure order will not be re-triggered, after manually closing it with button, when entry conditions are still evaluating to true? Do we have an example of it?

              Comment


                #8
                Hello tkaboris,

                Thanks for your notes.

                No, we do not have an example available that demonstrates this specifically. If your entry order condition is evaluating to be true then an entry order will be placed.

                You could try using a bool in your script along with coming up with your own custom logic to control when trades are placed by the strategy.

                You could create a bool (initially set to false), create your entry order conditions, and set the bool to true within the condition. Then, in your condition to place an entry order add a check to see if the bool is true. Then, set the bool to false after the entry is placed. By doing this, the entry order will only submit when the bool is true. The bool will be true if your entry order conditions are true. Otherwise, the bool will be false.

                If you want the script to wait a certain number of bars since an exit order is placed, you could try using BarsSinceExitExecution().

                BarsSinceExitExecution: https://ninjatrader.com/support/help...texecution.htm
                <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by NullPointStrategies, Today, 05:17 AM
                0 responses
                52 views
                0 likes
                Last Post NullPointStrategies  
                Started by argusthome, 03-08-2026, 10:06 AM
                0 responses
                130 views
                0 likes
                Last Post argusthome  
                Started by NabilKhattabi, 03-06-2026, 11:18 AM
                0 responses
                70 views
                0 likes
                Last Post NabilKhattabi  
                Started by Deep42, 03-06-2026, 12:28 AM
                0 responses
                44 views
                0 likes
                Last Post Deep42
                by Deep42
                 
                Started by TheRealMorford, 03-05-2026, 06:15 PM
                0 responses
                49 views
                0 likes
                Last Post TheRealMorford  
                Working...
                X