• 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, 02: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


              #7
              hi,
              what if I want to close all manually entered positions for NT8 when a certain loss limit in currency is reached?

              Comment


                #8
                Hello asmmbillah,

                You would need to use the addon account for that, you could collect the Positions and submit appropriate exit orders to close them. This type of logic should not be combined with strategies specifically but you could use an Indicator and do this from the indicator.

                https://ninjatrader.com/support/help...ount_class.htm
                https://ninjatrader.com/support/helpGuides/nt8/flatten.htm


                I look forward to being of further assistance.
                Last edited by NinjaTrader_Jesse; 06-18-2020, 02:45 PM.
                JesseNinjaTrader Customer Service

                Comment


                  #9
                  thanks, is there anyway I can get current pnl of a particular instrument where I have place ordered manually? what should be the syntax for that please?

                  Comment


                    #10
                    Hello

                    You could either use the Position object to get the PnL from the position or you could access the account and access its values. I am not aware of a way to filter per instrument in this context.

                    https://ninjatrader.com/support/help...ns_account.htm
                    https://ninjatrader.com/support/helpGuides/nt8/get.htm

                    I look forward to being of further assistance.
                    JesseNinjaTrader Customer Service

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by PageNotFound404, Today, 01:53 PM
                    1 response
                    11 views
                    0 likes
                    Last Post NinjaTrader_BrandonH  
                    Started by sciman, Today, 08:42 AM
                    1 response
                    13 views
                    0 likes
                    Last Post NinjaTrader_Kate  
                    Started by brunoviveiros, Today, 08:18 AM
                    1 response
                    11 views
                    0 likes
                    Last Post NinjaTrader_Kate  
                    Started by connorgrant5, Today, 02:02 PM
                    1 response
                    8 views
                    0 likes
                    Last Post NinjaTrader_ChrisL  
                    Started by jeremy83846, Today, 01:44 PM
                    1 response
                    5 views
                    0 likes
                    Last Post NinjaTrader_Jim  
                    Working...
                    X