Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

basic question for exiting positions

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

    basic question for exiting positions

    Is it possible to have 2 open positions for the same security close on two separate exit criteria? For example, suppose I open a position for MSFT on 1/1/2010 and then open another position for MSFT on 1/2/2010. I want each position to exit after 5 days. When I code the following I get all simulatenously open positions exiting on the same day instead of each position exiting >5 bars since entry. I'd like each simultaneously open position to be treated uniquely instead of a single exit criterion appy to all open positions.

    Code:
     
    [FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]protected [/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]override [/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]void[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2] OnBarUpdate()[/SIZE][/FONT]
    [SIZE=2][FONT=Courier New]{[/FONT][/SIZE]
    [/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#008000][FONT=Courier New][SIZE=2][COLOR=#008000][FONT=Courier New][SIZE=2][COLOR=#008000]// Condition set 1[/COLOR][/SIZE][/FONT]
    [/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]if[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2] (Close[[/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080]0[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2]] < Close[[/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080]1[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2]])[/SIZE][/FONT]
    [SIZE=2][FONT=Courier New]{[/FONT][/SIZE]
    [SIZE=2][FONT=Courier New]EnterLongLimit(DefaultQuantity, Low[[/FONT][/SIZE][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080]1[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2]], [/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800000][FONT=Courier New][SIZE=2][COLOR=#800000][FONT=Courier New][SIZE=2][COLOR=#800000]"A"[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2]);[/SIZE][/FONT]
    [SIZE=2][FONT=Courier New]}[/FONT][/SIZE]
    [/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#008000][FONT=Courier New][SIZE=2][COLOR=#008000][FONT=Courier New][SIZE=2][COLOR=#008000]// Condition set 2[/COLOR][/SIZE][/FONT]
    [/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]if[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2] (BarsSinceEntry() > [/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080]5[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2])[/SIZE][/FONT]
    [SIZE=2][FONT=Courier New]{[/FONT][/SIZE]
    [SIZE=2][FONT=Courier New]ExitLong([/FONT][/SIZE][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800000][FONT=Courier New][SIZE=2][COLOR=#800000][FONT=Courier New][SIZE=2][COLOR=#800000]"ExitA"[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2], [/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800000][FONT=Courier New][SIZE=2][COLOR=#800000][FONT=Courier New][SIZE=2][COLOR=#800000]"A"[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2]);[/SIZE][/FONT]
    [SIZE=2][FONT=Courier New]}[/FONT][/SIZE]
    [/SIZE][/FONT]

    #2
    Hello mkalayog,

    Yes, this is possible. See the reference sample below for help with this:


    Each entry must have a unique name associated with it if you would like this behavior.
    Ryan M.NinjaTrader Customer Service

    Comment


      #3
      RyanM, after reviewing the script I'm not sure how it applies to the script I posted. So suppose you backtest my posted script for MSFT in 2010 and look at the chart. Sometimes I would get 5 simultaneously open positions before all exit the same bar, and sometimes I get 3, other times I get 1. In other words I dont know beforehand how many positions I'm going to get and therefore cant name the positions beforehand like demonstrated in your example script.

      Is there a way to reference the built-in unique entry/position ID for exit logic?

      Comment


        #4
        Using the managed order system requires that exits are paired with entries.

        Knowing how many times you are set to enter in any one direction is part of developing a strategy, and doesn't negate that exits need to be paired with entries.

        You may be interested in an unmanaged approach, which allows you to submit orders without this requirement. This is available in version 7. Can read more about this at the following link:
        Ryan M.NinjaTrader Customer Service

        Comment


          #5
          Thanks for your reply. I'm still confused. Is it possible to modify the code I posted in a way that allows each of the open positions to be closed after 5 bars instead of all simultaneously open positions to be closed on a single bar? If so, could you or someone else help me out by editing the code? It would help me out greatly. I would rather not used unmanaged order entries at this time if possible. Thanks in advance-

          Comment


            #6
            You will have to follow the guidelines in the reference sample on scaling out. Each entry is named uniquely and then the exit is tied to this entry with the fromEntrySignal paramater.


            Below is your code with 5 entries tied to 5 exits.

            if (Close[0] < Close[1])
            {
            EnterLongLimit(DefaultQuantity, Low[
            1], "A"
            );
            EnterLongLimit(DefaultQuantity, Low[1], "B");
            EnterLongLimit(DefaultQuantity, Low[1], "C");
            EnterLongLimit(DefaultQuantity, Low[1], "D");
            EnterLongLimit(DefaultQuantity, Low[1], "E");
            }
            // Condition set 2
            if (BarsSinceEntry() > 5
            )
            {
            ExitLong(
            "ExitA", "A"
            );
            ExitLong("ExitB", "B");
            ExitLong("ExitC", "C");
            ExitLong("ExitD", "D");
            ExitLong("ExitE, "E");
            }

            Ryan M.NinjaTrader Customer Service

            Comment


              #7
              Ok, but what this does is open multiple positions on the same bar as opposed to opening a single position on each bar that meets the entry criteria. I'd like to open a single position for each bar, then close each open position after 5 bars that that position was kept open. There will be instances when some positions are open simultaneously; when this happens, I'd like to make sure that all open positions are kept open for the full 5+ bars as opposed to what's happening now, which is that all simultaneously open positions exit on the bar that the 1st open position is due to exit at.

              If this can't be accomplished without converting to unmanaged = true, that's fine - is that the case?

              Comment


                #8
                It's possible with a managed order system but you have to write code according to the sequence you're looking for.

                You name each entry uniquely and set entry handling to unique entries.

                If you don't want to accept all 5 entries on the same bar, you have to code for this. You can work with bool flags or user variables to code a sequence.

                This reference sample can help with setting up a sequence using user variables.

                Example sequence with three entries:

                Code:
                 
                if (Close[0] < Close[1] )
                {
                EnterLongLimit(DefaultQuantity, Low[1], "A");
                Variable0 = 1;
                }
                 
                if (Close[0] < Close[1] && Variable0 = 1)
                {
                EnterLongLimit(DefaultQuantity, Low[1], "B");
                Variable1 = 1;
                }
                 
                
                if (Close[0] < Close[1] && Variable1 = 1)
                {
                EnterLongLimit(DefaultQuantity, Low[1], "C");
                }

                You'd also have to reset these values at some point, typically when flat. You can use Position.MarketPosition to check for this.
                Ryan M.NinjaTrader Customer Service

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by ESHunter, Today, 08:06 PM
                2 responses
                9 views
                0 likes
                Last Post ESHunter  
                Started by ETFVoyageur, 05-07-2024, 07:05 PM
                19 responses
                149 views
                0 likes
                Last Post ETFVoyageur  
                Started by ETFVoyageur, Yesterday, 10:13 PM
                3 responses
                26 views
                0 likes
                Last Post ETFVoyageur  
                Started by ETFVoyageur, Yesterday, 12:52 AM
                3 responses
                33 views
                0 likes
                Last Post ETFVoyageur  
                Started by ETFVoyageur, Today, 02:10 AM
                2 responses
                27 views
                0 likes
                Last Post ETFVoyageur  
                Working...
                X