• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Close All Positions in a Strategy

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

    Close All Positions in a Strategy

    I need to close all open positions at a certain time of day. After testing for the current market position I am using ExitLong() or ExitShort(). I know my code gets to the right point because it displays a print message for me, but nothing happens. I am wondering if I need to give that signal a name or if I need to specify the position I am closing out.

    I am attaching a portion of the code for review:

    // Condition set 10 - get out of a Long position if still in at End Time.
    if ((ToTime(Time[0]) > TimeEnd)
    && Position.MarketPosition == MarketPosition.Long
    && Position.Quantity > 0)
    {
    Print ("9: End of Session, Close Long Position");
    ExitLong("End of Session");
    //Disable();
    }

    I have tested with and without a name in the ExitLong().

    Thanks for your help.

    #2
    Hello cpabiz20k,

    Thanks for your post.

    To assist, we will need more information:

    Are you using the strategy builder?

    Have you tried printing the values of TimeEnd and Time[0]? If so what do they show?

    What is the specific value of TimeEnd?

    What specific instrument are you testing?

    What specific bars type are you using?

    What is the calculation mode of the strategy? (OnBarClose, OnPriceChange, OnEachTick)?

    Are you testing with historical data on a chart? Live data on a chart? Market replay data on a chart? Historical data in the strategy analyzer?

    You actually do not need to check for market position or position quantity because if ExitLong() and ExitShort() are called and there is no position then nothing happens, here are the "Tips" notes from the helpguide:

    Tips:

    • This method is ignored if a long position does not exist
    • It is helpful to provide a signal name if your strategy has multiple exit points to help identify your exits on a chart
    • You can tie an exit to an entry by providing the entry signal name in the parameter "fromEntrySignal"
    • If you do not specify a quantity the entire position is exited rendering your strategy flat
    • If you do not specify a "fromEntrySignal" parameter the entire position is exited rendering your strategy flat
    https://ninjatrader.com/support/help.../?exitlong.htm


    Tips:

    • This method is ignored if a short position does not exist
    • It is helpful to provide a signal name if your strategy has multiple exit points to help identify your exits on a chart
    • You can tie an exit to an entry by providing the entry signal name in the parameter "fromEntrySignal"
    • If you do not specify a quantity the entire position is exited rendering your strategy flat
    • If you do not specify a "fromEntrySignal" parameter the entire position is exited rendering your strategy flat
    https://ninjatrader.com/support/help...?exitshort.htm
    Paul H.NinjaTrader Customer Service

    Comment


      #3
      i have a same Question; how i can set up so that when the clock sets a specific time, for example 4:00 PM, it automatically closes all open positions?

      Comment


        #4
        Hello andarbe,

        Thanks for your post.

        If you are referring to all positions of a particular strategy then you could create a time filter in the strategy that performs the actions to ExitLong and ExitShort at or near the specified time. Please see this reference for a time filter example: https://ninjatrader.com/support/foru...ead.php?t=3226

        If you want the platform to automatically close all open positions regardless of how they were created, then you can go to Tools>Options>Misc and set up the "Flatten Positions" section. Please see https://ninjatrader.com/support/help.../?misc_tab.htm for further information.
        Paul H.NinjaTrader Customer Service

        Comment


          #5
          is for Strategy, thanks Chelsea
          i will try it
          in NT8 is it configured the same?
          Last edited by andarbe; 10-03-2018, 01:58 PM.

          Comment


            #6
            Hello andarbe,

            Thanks for the reply.

            Here is the NinjaTrader 8 version of the time filter reference sample:

            https://ninjatrader.com/support/help...to_limit_t.htm

            The auto close position time can be found in Tools<Options>Trading>Auto Close Position.

            Please let us know if we can assist further.
            Chris L.NinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by chnh1, Today, 11:51 PM
            0 responses
            1 view
            0 likes
            Last Post chnh1
            by chnh1
             
            Started by Jasper99, Today, 10:29 PM
            0 responses
            7 views
            0 likes
            Last Post Jasper99  
            Started by Jasper99, Today, 09:50 PM
            0 responses
            2 views
            0 likes
            Last Post Jasper99  
            Started by StevenNelson, Today, 05:57 PM
            0 responses
            4 views
            0 likes
            Last Post StevenNelson  
            Started by srbdoc, Today, 03:59 PM
            1 response
            12 views
            0 likes
            Last Post NinjaTrader_EricB  
            Working...
            X