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

Cancelling opposite orders, OCO style, in NinjaScript?

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

    Cancelling opposite orders, OCO style, in NinjaScript?

    Hi, I am trying to get it so that if either the target exit is filled or the stop is filled, it will cancel the opposite side pending order. This was how I attempted it at first:

    protected override void OnExecutionUpdate(Execution execution, string executionId, double price, int quantity, MarketPosition marketPosition, string orderId, DateTime time)
    {
    if (execution.Order.OrderState == OrderState.Filled && execution.Order.Name == "longEntry")
    {
    ExitLongLimit(0, true, 1, (Close[0] + 20) , @"exitLong1", @"longEntry");
    ExitLongStopMarket(0, true, 1, (Close[0] - 20) , @"longStop", @"longEntry");
    }
    else if (execution.Order.OrderState == OrderState.Filled && execution.Order.Name == "shortEntry")
    {
    ExitShortLimit(0, true, 1, (Close[0] - 20) , @"exitShort1", @"shortEntry");
    ExitShortStopMarket(0, true, 1, (Close[0] + 20) , shortstop, @"shortEntry");
    }

    // if (execution.Order.OrderState == OrderState.Filled && execution.Order.Name == "exitLong1")
    // {
    // CancelOrder("longStop");
    // }

    // else if (execution.Order.OrderState == OrderState.Filled && execution.Order.Name == "longStop")
    // {
    // CancelOrder("exitLong1");
    // }

    // if (execution.Order.OrderState == OrderState.Filled && execution.Order.Name == "exitShort1")
    // {
    // CancelOrder("shortStop");
    // }

    // else if (execution.Order.OrderState == OrderState.Filled && execution.Order.Name == "shortStop")
    // {
    // CancelOrder("exitShort1");
    // }

    }​

    But I get the error "​Argument 1: cannot convert from 'string' to 'NinjaTrader.Cbi.Order'" when trying to compile. I commented it out for now just so I could get everything to compile.
    How can I accomplish this?
    Attached Files

    #2
    Hello RaygunWizzle,

    Thanks for your post.

    To clarify, what is the exact line of code throwing the error message in the code you shared?

    If the error is coming from the lines of code where you are calling CancelOrder() this is because you are passing in a string value into CancelOrder() when an Order object is required to be passed in.

    CancelOrder(Order order)

    ​See the help guide documentation and reference sample below for more information about CancelOrder().

    CancelOrder(): https://ninjatrader.com/support/help...ancelorder.htm
    SampleCancelOrder: https://ninjatrader.com/support/help...thod_to_ca.htm
    Order: https://ninjatrader.com/support/help.../nt8/order.htm
    Brandon H.NinjaTrader Customer Service

    Comment


      #3
      Yes, it was that. I was able to get it working with the following modifications.

      private Dictionary<string, Order> orders = new Dictionary<string, Order>();


      protected override void OnExecutionUpdate(Execution execution, string executionId, double price, int quantity, MarketPosition marketPosition, string orderId, DateTime time)
      {
      if (execution.Order.OrderState == OrderState.Filled)
      {
      string orderName = execution.Order.Name;

      if (orderName == "longEntry")
      {
      ExitLongLimit(0, true, 1, (Close[0] + 20) , @"exitLong1", @"longEntry");
      ExitLongStopMarket(0, true, 1, (Close[0] - 20) , @"longStop", @"longEntry");
      }
      else if (orderName == "shortEntry")
      {
      ExitShortLimit(0, true, 1, (Close[0] - 20) , @"exitShort1", @"shortEntry");
      ExitShortStopMarket(0, true, 1, (Close[0] + 20) , shortstop, @"shortEntry");
      }
      else if (orderName == "exitLong1")
      {
      CancelOrder("longStop");
      }
      else if (orderName == "longStop")
      {
      CancelOrder("exitLong1");
      }

      else if (orderName == "exitShort1")
      {
      CancelOrder("shortStop");
      }
      else if (orderName == "shortStop")
      {
      CancelOrder("exitShort1");
      }

      orders.Remove(orderName);
      }
      }​

      Just for anyone else who researches this topic.

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by futtrader, 04-21-2024, 01:50 AM
      4 responses
      41 views
      0 likes
      Last Post futtrader  
      Started by Option Whisperer, Today, 09:55 AM
      1 response
      11 views
      0 likes
      Last Post bltdavid  
      Started by port119, Today, 02:43 PM
      0 responses
      1 view
      0 likes
      Last Post port119
      by port119
       
      Started by Philippe56140, Today, 02:35 PM
      0 responses
      3 views
      0 likes
      Last Post Philippe56140  
      Started by 00nevest, Today, 02:27 PM
      0 responses
      2 views
      0 likes
      Last Post 00nevest  
      Working...
      X