Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

Strategy - Limit Order Not Cancelling

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

    Strategy - Limit Order Not Cancelling

    Hi there. I was using your sample CancelOrder code, slightly adjusted to a strategy I'm using. I have set the strategy to cancel the order if 5 bars pass without the order being filled - but when I generate Account Performance numbers from the control center, there are many trades on the TRADES tab that stayed active for well over 5 bars with the largest being 44 bars.

    The code is below. Am I misunderstanding how CancelOrder is supposed to work? There are some print statements I have which show that CancelOrder was called but it's not always being called apparently.

    ===

    #region Variables
    private IOrder MyEntryOrder = null; // This variable holds an object representing our entry order.
    private IOrder stopOrder = null; // This variable holds an object representing our stop loss order.
    private IOrder targetOrder = null; // This variable holds an object representing our profit target order.
    private int barNumberOfOrder = 0; // This variable is used to store the entry bar
    #endregion

    protected override void OnBarUpdate()
    {
    ...

    Print("BarOrderNumber = " +
    barNumberOfOrder);
    Print("CurrentBar = " + CurrentBar);

    // First, we need a simple entry. Then MyEntryOrder == null checks to make sure MyEntryOrder does not contain an order yet.
    if (Position.MarketPosition == MarketPosition.Flat)
    {
    // Check IOrder objects for null to ensure there are no working entry orders before submitting new entry order
    if (MyEntryOrder == null && OTHER_CONDITIONS)
    {
    if (MARKET_DIRECTION == "short")
    {
    /* Our IOrder object, MyEntryOrder, is assigned an entry order. */
    MyEntryOrder = EnterShortLimit(0, true, 1, GetCurrentBid(), "short limit entry");

    // Here, we assign barNumberOfOrder the CurrentBar, so we can check how many bars pass after our order is placed.
    barNumberOfOrder = CurrentBar;
    }
    else if (MARKET_DIRECTION == "long")
    {
    /* Our IOrder object, MyEntryOrder, is assigned an entry order. */
    MyEntryOrder = EnterLongLimit(0, true, 1, GetCurrentAsk(), "long limit entry");

    // Here, we assign barNumberOfOrder the CurrentBar, so we can check how many bars pass after our order is placed.
    barNumberOfOrder = CurrentBar;
    }
    }
    // If MyEntryOrder has not been filled within 5 bars, cancel the order.
    else if (MyEntryOrder != null && CurrentBar > barNumberOfOrder + 5)
    {
    // When MyEntryOrder gets cancelled below in OnOrderUpdate(), it gets replaced with a Market Order via EnterLong()
    CancelOrder(MyEntryOrder);
    }
    }
    }

    protected override void OnOrderUpdate(IOrder order)
    {
    // Checks for all updates to MyEntryOrder.
    if (MyEntryOrder != null && MyEntryOrder == order)
    {
    // Check if MyEntryOrder is cancelled.
    if (order.OrderState == OrderState.Cancelled)
    {
    // Reset MyEntryOrder back to null
    MyEntryOrder = null;
    currentOrderDirection = "";
    }
    }
    }

    protected override void OnExecution(IExecution execution)
    {
    if (MyEntryOrder != null && MyEntryOrder == execution.Order)
    {
    if (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled || (execution.Order.OrderState == OrderState.Cancelled && execution.Order.Filled > 0))
    {
    if (currentOrderDirection == "long")
    {
    // Simple stop and target
    stopOrder = ExitLongStop(0, true, 1, execution.Price - (AcceptableLoss * TickSize), "stop", "long limit entry");
    targetOrder = ExitLongLimit(0, true, 1, execution.Price + (DesiredProfit * TickSize), "target", "long limit entry");
    }
    else if (currentOrderDirection == "short")
    {
    // Simple stop and target
    stopOrder = ExitShortStop(0, true, 1, execution.Price + (AcceptableLoss * TickSize), "stop", "short limit entry");
    targetOrder = ExitShortLimit(0, true, 1, execution.Price - (DesiredProfit * TickSize), "target", "short limit entry");
    }

    // Resets the MyEntryOrder object to null after the order has been filled
    if (execution.Order.OrderState != OrderState.PartFilled)
    {
    MyEntryOrder = null;
    currentOrderDirection = "";
    }
    }
    }
    }
    Last edited by JayJoshi; 03-13-2011, 04:12 PM.

    #2
    JayJoshi, the CancelOrder() would just cancel a submitted and resting / working order, it would not close an open trade down after your x bars - so it can very well be that you see trades over 5 bars duration here.
    BertrandNinjaTrader Customer Service

    Comment


      #3
      Thanks Bertrand. To close an open trade that's been open longer than X minutes, what would you use? an ExitLongLimit()/ExitShortLimit() function?

      Comment


        #4
        Yes, you could combine those with BarsSinceEntry() for example to exit with your chosen order after x bars - http://www.ninjatrader.com/support/h...sinceentry.htm
        BertrandNinjaTrader Customer Service

        Comment


          #5
          Will the exit also close out existing profit/stop loss orders for the base order?

          Comment


            #6
            Correct, ExitOnClose would cancel all strategy generated orders too.
            BertrandNinjaTrader Customer Service

            Comment


              #7
              Ok - so to confirm... ExitLongLimit(), ExitShortLimit(), and ExitOnClose will cancel all strategy generated orders?

              Thanks Bertrand!

              Comment


                #8
                The ExitOnClose would do this - the other order methods would simply issue an exit order as specified.
                BertrandNinjaTrader Customer Service

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by Austiner87, Today, 03:42 PM
                1 response
                15 views
                0 likes
                Last Post NinjaTrader_Manfred  
                Started by cshox, Today, 11:11 AM
                2 responses
                15 views
                0 likes
                Last Post cshox
                by cshox
                 
                Started by algospoke, Today, 06:53 PM
                0 responses
                9 views
                0 likes
                Last Post algospoke  
                Started by mlprice12, 12-21-2021, 04:55 PM
                3 responses
                296 views
                0 likes
                Last Post paypachaysa  
                Started by lorem, 04-25-2024, 09:18 AM
                20 responses
                85 views
                0 likes
                Last Post lorem
                by lorem
                 
                Working...
                X