Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Position handling issue

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

    #16
    Originally posted by NinjaTrader_PatrickH View Post
    Hello sburtt,

    Thank you for your response.

    You are correct, it will use a limit order to exit the position.

    So you will need to check that the exit order was filled and you are flat. With CalculateOnBarClose = True and the conditions to check for the entries in OnBarUpdate() we would not see the entry after the close until the next bar (which as you detailed is not what you want).

    In this case there is a need to not only check that the exit order went through but also be able to then enter a new order on the same bar that recieved the condition to exit.

    You could incorporate FirstTickOfBar and CalculateOnBarClose = false to achieve this to calculate the conditions to enter on bar close but be able to change positions (exit and enter) on the same bar. For a reference sample detailing this concept please visit the following link: http://www.ninjatrader.com/support/f...ad.php?t=19387

    Please let me know if you have any questions.
    Patrick, i was reasoning on the methodology you proposed above (FirstTickOfBar) and I suspect this woudn't work for my strategy, let me expain why and please correct me if I am wrong:

    Assume I am running a long position and assume a CrossBelow occurs right on the FirstTickOfBar. Now on this first tick my entry condition would be triggered, but considering I have a short position, NT7 would be entering 2 separate limit orders (first to flatten my position, second to go short), and on the following tick I would be flat thanks to the market order that would get triggered. It is a remote scenario, but again I think I would end out with a double position. Moreover, I could hardly back test this properly with COBC=false; whilst intrabar granularity gives me major control and allows me to properly backtest. De facto this way if I ideally want to run my limit order for 120 minutes I would be effectively running the order for 119 min instead. Let me post a snap of my code so you can tell me if this makes sense, I,'ve tested it last night and it is been running live without issues so far

    #region Variables
    private IOrder entryOrder = null;
    private int barNumberOfOrder = 0;
    private int secondaryTimeFrame = 60;
    #endregion

    protected override void Initialize()
    {
    Add(PeriodType.Minute,secondaryTimeFrame);
    EntryHandling = EntryHandling.UniqueEntries;
    CalculateOnBarClose = true;
    ExitOnClose = false;
    TraceOrders = true;
    }

    protected override void OnBarUpdate()
    {
    if(BarsInProgress != 0)
    return;

    //Entry condition
    if(Position.MarketPosition == MarketPosition.Flat && entryOrder == null)
    {
    //Long
    if (CrossAbove())
    {
    entryOrder = EnterLongLimit(0, true, DefaultQuantity, Closes[1][0] - 0.2* (Highs[1][0] - Lows[1][0]), "Long");
    barNumberOfOrder = CurrentBar;
    }

    //Short
    if (CrossBelow())
    {
    entryOrder = EnterShortLimit(0, true, DefaultQuantity, Closes[1][0] + 0.2* (Highs[1][0] - Lows[1][0]),"Short");
    barNumberOfOrder = CurrentBar;
    }
    }

    //Cancel limit order if not done within lookback period
    if(CurrentBar > barNumberOfOrder +(secondaryTimeFrame*2-lookback) && entryOrder != null)
    {
    CancelOrder(entryOrder);
    }

    //Exit Condition - Long
    if (Position.MarketPosition == MarketPosition.Long)
    {
    if (CrossBelow())
    {
    ExitLong(0,DefaultQuantity, "Exit Long On Cross", "Long");
    }
    }

    //Exit condition - Short
    if (Position.MarketPosition == MarketPosition.Short)
    {
    if (CrossAbove())
    {
    ExitShort(0,DefaultQuantity, "Exit Short On Cross", "Short");
    }
    }
    Last edited by sburtt; 05-16-2013, 01:23 PM.

    Comment


      #17
      Hello sburtt,

      Thank you for your update on this matter.

      It is true that this would not be an option for backtesting and your idea for a smaller interval would be ideal.

      In addition, have you implemented entering the new position in the same condition as the exit condition?
      Code:
      //Exit Condition - Long 
      if (Position.MarketPosition == MarketPosition.Long)
      {
      if (CrossBelow())
      {
      ExitLong(0,DefaultQuantity, "Exit Long On Cross", "Long");
      entryOrder = EnterShortLimit(0, true, DefaultQuantity, Closes[1][0] + 0.2* (Highs[1][0] - Lows[1][0]),"Short");
      barNumberOfOrder = CurrentBar;
      }
      }
      Please let me know if you have any questions.

      Comment


        #18
        Originally posted by NinjaTrader_PatrickH View Post
        Hello sburtt,

        Thank you for your update on this matter.

        It is true that this would not be an option for backtesting and your idea for a smaller interval would be ideal.

        In addition, have you implemented entering the new position in the same condition as the exit condition?
        Code:
        //Exit Condition - Long 
        if (Position.MarketPosition == MarketPosition.Long)
        {
        if (CrossBelow())
        {
        ExitLong(0,DefaultQuantity, "Exit Long On Cross", "Long");
        entryOrder = EnterShortLimit(0, true, DefaultQuantity, Closes[1][0] + 0.2* (Highs[1][0] - Lows[1][0]),"Short");
        barNumberOfOrder = CurrentBar;
        }
        }
        Please let me know if you have any questions.
        Patrick, from my experience I see 2 issues:
        1. Not always I will be long or short, as i have no guarantee the limit order will be filled, and if I am flat and a cross occurs, the MarketPosition check will keep me out of the market;
        2. Unless COBC=false, we are back to base 1, this will duplicate my position as thereis no link between exitlong and the new limit order;

        Again please correct me if wrong

        Comment


          #19
          Hello sburtt,

          Thank you for your response.

          That would be correct, is the point of your strategy not to go long on CrossAbove(), exit on CrossBelow() and enter short, exit on CrossAbove() and enter long, etc.?

          Also, you are looking to exit and go long on the same bar?

          Have you tested out your idea for adding a smaller time frame yet?

          Comment


            #20
            Originally posted by NinjaTrader_PatrickH View Post
            Hello sburtt,

            Thank you for your response.

            That would be correct, is the point of your strategy not to go long on CrossAbove(), exit on CrossBelow() and enter short, exit on CrossAbove() and enter long, etc.?

            Also, you are looking to exit and go long on the same bar?

            Have you tested out your idea for adding a smaller time frame yet?
            Yes I would be looking to exit short and go long (via limit order) on the same (60 min) bar.

            I'm now using the MTF code posted earlier, running the strategy on 1 min bars and it works ok

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by Geovanny Suaza, 02-11-2026, 06:32 PM
            0 responses
            683 views
            0 likes
            Last Post Geovanny Suaza  
            Started by Geovanny Suaza, 02-11-2026, 05:51 PM
            0 responses
            386 views
            1 like
            Last Post Geovanny Suaza  
            Started by Mindset, 02-09-2026, 11:44 AM
            0 responses
            111 views
            0 likes
            Last Post Mindset
            by Mindset
             
            Started by Geovanny Suaza, 02-02-2026, 12:30 PM
            0 responses
            584 views
            1 like
            Last Post Geovanny Suaza  
            Started by RFrosty, 01-28-2026, 06:49 PM
            0 responses
            585 views
            1 like
            Last Post RFrosty
            by RFrosty
             
            Working...
            X