Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

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 algospoke, Yesterday, 06:40 PM
              2 responses
              19 views
              0 likes
              Last Post algospoke  
              Started by ghoul, Today, 06:02 PM
              3 responses
              14 views
              0 likes
              Last Post NinjaTrader_Manfred  
              Started by jeronymite, 04-12-2024, 04:26 PM
              3 responses
              45 views
              0 likes
              Last Post jeronymite  
              Started by Barry Milan, Yesterday, 10:35 PM
              7 responses
              20 views
              0 likes
              Last Post NinjaTrader_Manfred  
              Started by AttiM, 02-14-2024, 05:20 PM
              10 responses
              180 views
              0 likes
              Last Post jeronymite  
              Working...
              X