Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

ChangeOrder() example

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

    ChangeOrder() example

    Hello, I need to place "stop market entry orders" within a strategy that can follow (chase) the next bars, once submitted but not filled on the current bar, until they are filled if conditions are met, or cancel the entry order if conditions are no longer met.

    Can you provide an example of how this can be done?

    Thank you.

    #2
    Hello bobperez,

    Thank you for your note.

    In order to use ChangeOrder(), you will need to have an order object to amend. This means that once you submit your EnterLongStopMarket or EnterShortStopMarket order for entry, you should assign the order object to a private Order object in order to use that in the ChangeOrder() method. There is an example on this help guide page that shows how to assign entryOrder in OnOrderUpdate():


    private Order entryOrder = null;

    protected override void OnBarUpdate()
    {
    if (entryOrder == null && Close[0] > Open[0])
    EnterLong("myEntryOrder");
    }

    protected override void OnOrderUpdate(Order order, double limitPrice, double stopPrice, int quantity, int filled, double averageFillPrice, OrderState orderState, DateTime time, ErrorCode error, string nativeError)
    {
    // Assign entryOrder in OnOrderUpdate() to ensure the assignment occurs when expected.
    // This is more reliable than assigning Order objects in OnBarUpdate, as the assignment is not guaranteed to be complete if it is referenced immediately after submitting
    if (order.Name == "myEntryOrder")
    entryOrder = order;

    if (entryOrder != null && entryOrder == order)
    {
    Print(order.ToString());
    if (order.OrderState == OrderState.Filled)
    entryOrder = null;
    }
    }​


    Keeping with that example of assigning entryOrder, to use ChangeOrder() you would be able to set up your conditions and change the Stop price of the Stop Market order as desired, or use CancelOrder() if needed. An example of ChangeOrder() using the entryOrder object that has the order information assigned to it could look like this:

    protected override void OnBarUpdate()
    {
    if (entryOrder != null && // insert additional condition(s) here )
    ChangeOrder(stopOrder, stopOrder.Quantity, 0, // insert new stop price here);
    }​

    The reference sample strategy SampleOnOrderUpdate demonstrates how to use Order and Execution objects to submit and update orders. This sample may be found here:
    The OnOrderUpdate() and OnExecution() methods are reserved for experienced programmers. Instead of using Set() methods to submit stop-loss and profit target orders, you can submit and update them manually through the use of IOrder and IExecution objects in the OnOrderUpdate() and OnExecution() methods. The OnOrderUpdate()


    Please let us know if we may be of further assistance.
    Emily C.NinjaTrader Customer Service

    Comment


      #3
      Hi Emily, thank you for your support. Before even trying to insert the ChangeOrder code, I'm experiencing some issues that may be originated in the OnExecutionUpdate() event handler. Immediately after an order is issued it gets closed. Where in the code and how shall I define the initial Stoploss?

      This is the code that I have to consider Longs and Shorts in the OnExecutionUpdate() section. Is this correct?

      protected override void OnExecutionUpdate(Execution execution, string executionId, double price, int quantity, MarketPosition marketPosition, string orderId, DateTime time)
      {
      /* We advise monitoring OnExecution to trigger submission of stop/target orders instead of OnOrderUpdate() since OnExecution() is called after OnOrderUpdate()
      which ensures your strategy has received the execution which is used for internal signal tracking. */
      if (entryOrder != null && entryOrder == execution.Order)
      {
      if (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled || (execution.Order.OrderState == OrderState.Cancelled && execution.Order.Filled > 0))
      {
      // We sum the quantities of each execution making up the entry order
      sumFilled += execution.Quantity;

      // Submit exit orders for partial fills
      if (execution.Order.OrderState == OrderState.PartFilled)
      {
      if(Position.MarketPosition == MarketPosition.Short)
      stopOrder = ExitShortStopMarket(0, true, execution.Order.Filled, execution.Order.StopPrice, "MyStop", "MyEntry");
      else if (Position.MarketPosition == MarketPosition.Long)
      stopOrder = ExitLongStopMarket(0, true, execution.Order.Filled, CurrentStopPrice, "MyStop", "MyEntry");


      //targetOrder = ExitLongLimit(0, true, execution.Order.Filled, execution.Order.AverageFillPrice + 12 * TickSize, "MyTarget", "MyEntry");
      }
      // Update our exit order quantities once orderstate turns to filled and we have seen execution quantities match order quantities
      else if (execution.Order.OrderState == OrderState.Filled && sumFilled == execution.Order.Filled)
      {
      // Stop-Loss order for OrderState.Filled
      if(Position.MarketPosition == MarketPosition.Short)
      stopOrder = ExitShortStopMarket(0, true, execution.Order.Filled, execution.Order.StopPrice, "MyStop", "MyEntry");
      else if (Position.MarketPosition == MarketPosition.Long)
      stopOrder = ExitLongStopMarket(0, true, execution.Order.Filled, execution.Order.StopPrice, "MyStop", "MyEntry");


      //targetOrder = ExitLongLimit(0, true, execution.Order.Filled, execution.Order.AverageFillPrice + 12 * TickSize, "MyTarget", "MyEntry");
      }

      // Resets the entryOrder object and the sumFilled counter to null / 0 after the order has been filled
      if (execution.Order.OrderState != OrderState.PartFilled && sumFilled == execution.Order.Filled)
      {
      entryOrder = null;
      sumFilled = 0;
      }
      }
      }

      // Reset our stop order and target orders' Order objects after our position is closed. (1st Entry)
      if ((stopOrder != null && stopOrder == execution.Order) || (targetOrder != null && targetOrder == execution.Order))
      {
      if (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled)
      {
      stopOrder = null;
      targetOrder = null;
      }
      }
      Thanks again.
      Bobperez

      Comment


        #4
        Hello Bobperez,

        Thank you for your reply.

        In order to identify why a position is being closed immediately after the entry is submitted, I suggest adding debugging prints and/or enabling Trace Orders to see which order submission methods are being reached. Trace Orders may be enabled by setting it to true in State.SetDefaults. For more information:



        Enabling Trace Orders will give you useful order information in the NinjaScript Output window while testing your strategy to show you which orders are being placed and why your position is being closed unexpectedly.

        Additionally, you could add prints inside and outside of your conditions to understand what actions are being taken when testing your strategy. For more in-depth information about adding debugging prints, please see the page here:


        If you are unsure of the results in the NinjaScript Output window from Trace Orders or added prints, please provide my with the output so I may better assist you.
        Emily C.NinjaTrader Customer Service

        Comment


          #5
          Hi Emily,

          Thank you very much. Your suggestions were very useful. I have my strategy up and running.

          Regards,

          Bobperez

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by andrewtrades, Today, 04:57 PM
          1 response
          5 views
          0 likes
          Last Post NinjaTrader_Manfred  
          Started by chbruno, Today, 04:10 PM
          0 responses
          3 views
          0 likes
          Last Post chbruno
          by chbruno
           
          Started by josh18955, 03-25-2023, 11:16 AM
          6 responses
          436 views
          0 likes
          Last Post Delerium  
          Started by FAQtrader, Today, 03:35 PM
          0 responses
          7 views
          0 likes
          Last Post FAQtrader  
          Started by rocketman7, Today, 09:41 AM
          5 responses
          19 views
          0 likes
          Last Post NinjaTrader_Jesse  
          Working...
          X