Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Close All Open Positions NT Script

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

    Close All Open Positions NT Script

    At the end of the day I would like to close all positions. Is this possible? If so is there a code sniplet?


    If I do the following


    if(abortOrders == true && Positions[0] != null){
    Print(
    "Exit Positoin");
    if(Positions[0].Quantity > 0){
    Print(
    "Position Close Requested");
    Positions[
    0].Close();
    Print(
    "Position Close");
    }
    }

    I get an " Object reference not set to an instance of an object." error.. Even though it's not null and the quantity is greater then 0.
    Thanks!
    Chris
    Last edited by cjwallac; 01-19-2011, 04:31 PM.

    #2
    Hi Chris,

    You may be able to get the results you want without code. There is an option you can set under tools > options > Misc tab. Check the Flatten All option and then specify a time. It closes all positions across all accounts.



    To set on a per strategy basis, you can set ExitOnClose property to true and specify ExitOnClose seconds.
    Ryan M.NinjaTrader Customer Service

    Comment


      #3
      Yes, but that will only work if I am not in Replay Mode....

      Currently we are doing simulation and wanted to do it with code... When we go Live we may decide to use the option you specified.

      So I still require to code this; do you have any examples? or have any thoughts to what is wrong with the code I provided?

      Thanks So Much!
      Chris

      Comment


        #4
        You can code for exits based on time or time range.

        if (ToTime(Time[0]) == 154500)
        {
        ExitLong();
        ExitShort();
        }

        A sample on working with time filters is available here.

        To track down what's happening with your code, you will need try catch blocks. Help for using this is available here:


        Positions[0].Close() is not supported NinjaScript so that would be the first place I'd look. Please see this link for help with Positions usage.
        Last edited by NinjaTrader_RyanM1; 01-20-2011, 09:11 AM.
        Ryan M.NinjaTrader Customer Service

        Comment


          #5
          I know for sure that the problem is with:

          Positions[0].Close();

          Since I can't use that to close positions... What method is supported in NT script?

          Thanks!

          Comment


            #6
            This command is totally unsupported, so do not expect any help with it, but this command flattens you unconditionally, and near instantly. At any rate, very fast.

            Code:
            NinjaTrader.Gui.SuperDom.SuperDom.FlattenEverything();

            Comment


              #7
              Thanks @koganam

              Flatten everything also closes the strategy?

              Is there anyway to keep the strategy running?

              Thanks!!!
              Chris

              Comment


                #8
                Originally posted by cjwallac View Post
                Thanks @koganam

                Flatten everything also closes the strategy?

                Is there anyway to keep the strategy running?

                Thanks!!!
                Chris
                Not that I have found. As I said, it takes you OUT!!

                However, in terms of just exiting the market, have you thought of just using :

                Code:
                if  (Position.MarketPosition != MarketPosition.Flat)
                {
                //find and close all orders using [I]IPosition.Quantity[/I] to provide the correct number of shares/contracts to cancel the positions
                }

                Comment


                  #9
                  Thanks @koganam!

                  Code:
                  Print("Position Quantity" + Position.Quantity.ToString());
                  Returns Position Quantity as 1

                  When I view the Orders Tab in NT all Orders are of the status Cancelled or Filled. Because I have already used:

                  Code:
                  if (g_OrderObject != null){
                      CancelOrder(g_OrderObject);
                      g_OrderObject = null;
                  }
                  to Cancel Each Order .

                  How Can I Close the Position Quantity if all Orders are in the Cancelled or Filled Status?

                  I am more of a programer then a trader so maybe I am miss understanding something...

                  Thanks!

                  Comment


                    #10
                    I believe that the real question there is whether there is still really a position being held in the market. Either there is or there is not. If there is, then you simply have to issue an order for the current position quantity, to close the entire position, at market: a buy order if short; a sell order if long,

                    Comment


                      #11
                      Cancelling is only needed if you want to cancel open orders. You can submit ExitLong(), and ExitShort() to close your position. Once your positions is closed, it will automatically cancel any working orders.

                      If you want to send CancelOrder() before you close the position, this sample can help with its usage:
                      When using NinjaTrader's Enter() and Exit() methods, the default behavior is to automatically expire them at the end of a bar unless they are resubmitted to keep them alive. Sometimes you may want more flexibility in this behavior and wish to submit orders as live-until-cancelled. When orders are submitted as live-until
                      Ryan M.NinjaTrader Customer Service

                      Comment


                        #12
                        @RyanM
                        In this Strategy

                        Code:
                        Unmanaged = true;
                        So therefore I can't use ExitLong nor ExitShort... Correct?

                        This would be easier, but don't think with Unmanaged=true; this is possible; please correct me because would like to use that method if possible.

                        Thanks!

                        Comment


                          #13
                          Yes, that's correct. In unmanaged strategy you can't issue those order commands. You can only submit order so I can see why you have to be careful about the management of your orders and positions.

                          Since you don't have any of the internal rules keeping track of your position, this all must custom coded. You can check for filled or cancelled states in OnExecution().

                          Possible order state values are listed on this page.
                          Ryan M.NinjaTrader Customer Service

                          Comment


                            #14
                            @koganam Thanks Buddy! That works perfectly

                            Comment


                              #15
                              Hello,

                              This is discussed in this post and others... But Is there a way documented or undocumented to our own risk to "flatten everything", all kind of orders, stops open using an automated strategy like NT7 in NT8 when some conditions are meet?
                              In NT7 it can be used:

                              Code:
                                   NinjaTrader.Gui.SuperDom.SuperDom.FlattenEverything()
                              Thanks for your support as usual

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by jaybedreamin, Today, 05:56 PM
                              0 responses
                              7 views
                              0 likes
                              Last Post jaybedreamin  
                              Started by DJ888, 04-16-2024, 06:09 PM
                              6 responses
                              18 views
                              0 likes
                              Last Post DJ888
                              by DJ888
                               
                              Started by Jon17, Today, 04:33 PM
                              0 responses
                              4 views
                              0 likes
                              Last Post Jon17
                              by Jon17
                               
                              Started by Javierw.ok, Today, 04:12 PM
                              0 responses
                              12 views
                              0 likes
                              Last Post Javierw.ok  
                              Started by timmbbo, Today, 08:59 AM
                              2 responses
                              13 views
                              0 likes
                              Last Post bltdavid  
                              Working...
                              X