Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Prevent orders from closing

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

    Prevent orders from closing

    Hi,

    when I use EnterLong() and EnterShort() methods, I'd like to prevent those orders from closing until ExitShort(), ExitLong(), SL or PT closes them. Nothing else.

    So when EnterLong() or EnterShort() is called and position is already opened, do nothing.

    Is that possible?
    Last edited by TJohn; 07-06-2015, 06:05 AM.

    #2
    Yes, add in a check for market position is flat for your if statements on the EnterShort and EnterLong orders. This means that the statement will only be true if you are in a flat state and ready to place another entry order.

    if(MarketPosition == Position.Flat)

    http://ninjatrader.com/support/helpG...etposition.htm

    Comment


      #3
      Ok, so when I use that, there's no other possible way for the order to be closed, right?
      Because I am still experiencing some odd closing for no reason. (No closing method called)

      Comment


        #4
        You might be hitting the default functionality with the Managed approach for strategy order entry. Your orders are going to be active for one bar unless the IF statement is true on the next bar update, it will then resubmit the order.
        "By default, orders submitted via Entry() and Exit() methods automatically cancel at the end of a bar if not re-submitted"

        There is an overload available to set a LiveUntilCancelled property for the order for Limit and Stop orders -
        EnterLongLimit(int barsInProgressIndex, bool liveUntilCancelled, int quantity, double limitPrice, string signalName)

        If you are submitting Market Orders that are being closed out early then you have another order being submitted that is cancelling out that initial entry order in the code. Would need to see what the strategy is doing for better detailed answer

        Comment


          #5
          protected override void OnBarUpdate()
          {
          if(CheckLong1() && MarketPosition == Position.Flat)
          EnterLong("1")
          if(CheckShort1() && MarketPosition == Position.Flat)
          EnterShort("1")
          if(CheckLong2() && MarketPosition == Position.Flat)
          EnterLong("2")
          if(CheckShort2() && MarketPosition == Position.Flat)
          EnterShort("2")
          }

          bool CheckLong1()
          {
          if(Something)
          {
          ExitLong("1");
          ExitShort("1");
          return true;
          }
          return false;
          }

          bool CheckShort1()
          {
          if(Something)
          {
          ExitLong("1");
          ExitShort("1");
          return true;
          }
          return false;
          }

          etc.

          What I am trying to do is that orders marked as "1" should be opened only when orders marked as "2" closes and VV.

          I have set properties like this:
          EntryHandling = EntryHandling.UniqueEntries;
          EntriesPerDirection = 1;
          ExitOnClose = false;

          Basically, it shouldn't happen, that nothing is openened. But it does.
          Last edited by TJohn; 07-06-2015, 09:24 AM.

          Comment


            #6
            Hello TJohn,

            First, Calonious is correct. If you want to ensure the trade is exited before allowing for a new entry, use if (Position.MarketPosition == MarketPosition.Flat) to check that the position is flat.

            If you want to ensure that the last trade was a particular trade (such as having a signal name of "1") then use a bool that is initially set to false, and set this to true after that particular trade is exited. After the new order is placed, set this back to false so that it does not continue triggering the new order.

            Using a set of bools, you would be able to tell which trade was the last trade.


            You could also check the performance and ensure that the last trade has a signal name of what you are looking for.

            For example:
            if (Performance.AllTrades[Performance.AllTrades.Count-1].Entry.Name == "2")
            {
            // execute code
            }

            This would evaluate as true if the last exited trade had an entry with the name 2.
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              Thank you for your kind answers, it really helped me a lot.

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
              0 responses
              627 views
              0 likes
              Last Post Geovanny Suaza  
              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
              0 responses
              359 views
              1 like
              Last Post Geovanny Suaza  
              Started by Mindset, 02-09-2026, 11:44 AM
              0 responses
              105 views
              0 likes
              Last Post Mindset
              by Mindset
               
              Started by Geovanny Suaza, 02-02-2026, 12:30 PM
              0 responses
              562 views
              1 like
              Last Post Geovanny Suaza  
              Started by RFrosty, 01-28-2026, 06:49 PM
              0 responses
              568 views
              1 like
              Last Post RFrosty
              by RFrosty
               
              Working...
              X