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

Close and cancel all positions first

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

    Close and cancel all positions first

    Hi there folks,

    What code and where would one place that code to instruct a strategy to:

    If in a position long, for example, and a signal to go short appears...be sure to FIRST close all positions and all associated orders BEFORE entering the short position. After all positions and orders are closed, wait X seconds to enter the short position (this would give NT enough time to be SURE all positions AND associated orders are FIRST closed BEFORE entering the short position.

    This is to avoid having strategies going rogue.

    A side benefit is that it will free up account margin with many brokers too before entering the opposite side (gives NT and broker enough time to "catchup" and reconcile with what the strategy wants to do).

    Thanks all . . .
    Last edited by birdog; 10-10-2014, 08:56 AM.

    #2
    Hello,

    Thank you for the question.

    for closing all of the positions before doing anything you will need to loop through the accounts and then close each position.

    Normally you would submit either an exit order or an order in the opposite direction to close out your position, but if you are looking to just close out everything you could do something like the following:

    Code:
    foreach (Account acct in Cbi.Globals.Accounts)
    {		
            if (acct.Positions != null)
    	{
    		PositionCollection positions = acct.Positions;
    		foreach (Position pos in positions)
    	       {
    			pos.Close();
    		}
                    foreach (Order order in acct.Orders)
    		{
    			if(order.OrderState != OrderState.Cancelled || order.OrderState != OrderState.Filled)
    		               order.Cancel();
    	         }
    	 }
    }

    These would need to be called when your logic decides to close out everything.

    For the timing part, you could set a switch for each of the above statements or use the above to check if there are no longer any orders or positions.

    If so then you can set a date time variable to the current time + X seconds, now in your OnbarUpdate or OnMarketData you can check if the current time is >= the variable date time then go ahead or do not do logic.

    Here is an example of what i mean

    Code:
    private DateTime varibleTime = DateTime.MinValue;
    protected override void OnBarUpdate()
    {
    	varibleTime = DateTime.Now.AddSeconds(10); //set this when you are ready for the timer and all the above is correct. 
    	if(DateTime.Now >= varibleTime)
    		//do some logic
    	else
    		//do something else
    }
    Please let me know if I may be of additional assistance.
    JesseNinjaTrader Customer Service

    Comment


      #3
      Thanks for the info...

      It looks good and will try...BUT, one thing, I did not mean to close ALL account positions and orders in the entire account. ONLY all positions and orders for that particular instrument. That being said, what would I adjust in the code you provided to do that vs. all instruments in the account?

      Thanks a lot...once I hear back from you, then I will work on this over the weekend...

      Thanks again!!!

      Comment


        #4
        Hello,

        Thank you for the question.

        You should be able to access any Position object information such as the Instrument.Full name or:

        Code:
        if(pos.Instrument.FullName == Instrument.FullName)
                 pos.Close();
        This would be saying that if the current chart where the strategy is running, its instrument is equal to the positions instrument, go ahead and cancel.

        The order is very similar:

        Code:
        if(order.Instrument.FullName == Instrument.FullName)
        	order.Cancel();

        Please let me know if I may be of additional assistance.
        Last edited by NinjaTrader_Jesse; 10-10-2014, 11:16 AM.
        JesseNinjaTrader Customer Service

        Comment


          #5
          @Jesse,

          Ok, this would work also if the strategies are running/enabled WITHOUT a actual chart up for the strategies...having them just enabled (no chart)?

          Originally posted by NinjaTrader_Jesse View Post
          Hello,

          Thank you for the question.

          You should be able to access any Position object information such as the Instrument.Full name or:

          Code:
          if(pos.Instrument == Instrument)
                   pos.Close();
          This would be saying that if the current chart where the strategy is running, its instrument is equal to the positions instrument, go ahead and cancel.

          The order is very similar:

          Code:
          if(order.Instrument == Instrument)
          	order.Cancel();
          If you want you can even filter it by the full name (for futures you may need to) that would simply be:

          Code:
          if(order.Instrument.FullName == Instrument.FullName)
          	order.Cancel();
          Please let me know if I may be of additional assistance.

          Comment


            #6
            Hello,

            I amended my prior answer you will want to use .FullName after testing further.

            Also yes this works if you just create the strategy in the control center because you are still assigning a instrument to it when you initially set up the strategy this way.

            Please let me know if I may be of additional assistance.
            JesseNinjaTrader Customer Service

            Comment


              #7
              @Jesse,

              Ok. Thanks. Amendment noted.

              The instrument full name info is auto-populated by calling that command upon each strategy being enabled right? I don't have to manually put in all the different actual names of each instrument I want it applied to?

              Comment


                #8
                Hello,

                Yes the order of the object would be:

                for the chart:
                Code:
                Instrument - this is the object that contains information regarding the charts instrument.
                .FullName - a string value that returns the Instruments full name for checking.
                for the order:
                Code:
                order - this is the order object containing the information regarding 1 specific order
                order.Instrument - this is the specific instrument that submitted the order
                Here is some additional information on the items that the IOrder object contains:

                And instrument:

                and IPosition:


                Please let me know if I may be of additional assistance.
                JesseNinjaTrader Customer Service

                Comment


                  #9
                  Hello Jesse,

                  Would the following code work as well?

                  Code:
                  public void ClosePosition(string Reason)
                  
                  {
                  
                  if (LogLevel > 1)
                  
                  PrintLog(Reason);
                  
                  // Cancel Orders first in case any are entry orders
                  
                  Account.CancelAllOrders(Bars.Instrument);
                  
                  // Close Positions
                  
                  if (PositionAccount.MarketPosition == MarketPosition.Long)
                  
                  ExitLong();
                  
                  else if (PositionAccount.MarketPosition == MarketPosition.Short)
                  
                  ExitShort();
                  
                  }
                  ​
                  Many Thanks, Caesar.

                  Comment


                    #10
                    Hello Skechers,

                    Yes that could be used as well to cancel all orders for a given instrument.
                    JesseNinjaTrader Customer Service

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by Barry Milan, Yesterday, 10:35 PM
                    3 responses
                    10 views
                    0 likes
                    Last Post NinjaTrader_Manfred  
                    Started by WeyldFalcon, 12-10-2020, 06:48 PM
                    14 responses
                    1,428 views
                    0 likes
                    Last Post Handclap0241  
                    Started by DJ888, 04-16-2024, 06:09 PM
                    2 responses
                    9 views
                    0 likes
                    Last Post DJ888
                    by DJ888
                     
                    Started by jeronymite, 04-12-2024, 04:26 PM
                    3 responses
                    41 views
                    0 likes
                    Last Post jeronymite  
                    Started by bill2023, Yesterday, 08:51 AM
                    2 responses
                    16 views
                    0 likes
                    Last Post bill2023  
                    Working...
                    X