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



    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
    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:



            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.




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

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by Mindset, 05-06-2023, 09:03 PM
                    9 responses
                    258 views
                    0 likes
                    Last Post ender_wiggum  
                    Started by Mizzouman1, Today, 07:35 AM
                    4 responses
                    18 views
                    0 likes
                    Last Post Mizzouman1  
                    Started by philmg, Today, 01:17 PM
                    1 response
                    6 views
                    0 likes
                    Last Post NinjaTrader_ChristopherJ  
                    Started by cre8able, Today, 01:01 PM
                    1 response
                    8 views
                    0 likes
                    Last Post NinjaTrader_ChelseaB  
                    Started by manitshah915, Today, 12:59 PM
                    1 response
                    5 views
                    0 likes
                    Last Post NinjaTrader_Erick  
                    Working...
                    X