Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Canceling an Order

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

    Canceling an Order

    Hello

    I'm running an automated strategy that's working fine but I'm having an issue with canceling an order if it hasn't been executed. The order shall be cancelled if the next candle hasn't made a higher high (for longs) or a lower low (for shorts).

    Here's the code for entering the trade (works well):

    if(orderId.Length == 0 && atmStrategyId.Length == 0 && my criteria here)

    { atmStrategyId = GetAtmStrategyUniqueId();
    orderId = GetAtmStrategyUniqueId();
    AtmStrategyCreate(Cbi.OrderAction.Buy, OrderType.StopLimit,High[0]+1*TickSize,High[0]+1*TickSize, TimeInForce.Day, orderId, "MyStrat", atmStrategyId);
    }

    If I add the part that's in charge of canceling the order, it doesn't work anymore: (The last part of the code is critical)

    if (orderId.Length > 0)
    {
    string[] status = GetAtmStrategyEntryOrderStatus(orderId);

    // If the status call can't find the order specified, the return array length will be zero otherwise it will hold elements
    if (status.GetLength(0) > 0)
    {


    // If the order state is terminal, reset the order id value
    if (status[2] == "Filled" || status[2] == "Cancelled" || status[2] == "Rejected")
    orderId = string.Empty;
    }
    } // If the strategy has terminated reset the strategy id
    else if (atmStrategyId.Length > 0 && GetAtmStrategyMarketPosition(atmStrategyId) == Cbi.MarketPosition.Flat)
    atmStrategyId = string.Empty;




    if (High[0] <= High[1])
    {
    AtmStrategyCancelEntryOrder("orderId");
    }

    Thanks for highlighting where I made a mistake.

    #2
    Hi Laocoon,

    Thanks for posting.

    I have highlighted where you are having the potential problem with the code.
    Code:
    if (High[0] <= High[1])
    {
    AtmStrategyCancelEntryOrder("orderId");
    }
    The orderId is in quotations will which try to pass it as a string instead of a variable. Removing the quotations will eliminate the passing as a string and instead pass it as a variable.

    Let me know how this worked out for you.
    Cal H.NinjaTrader Customer Service

    Comment


      #3
      Hi Cal

      Thanks a lot for your reply, I should have spotted this myself!
      I really appreciate your help.

      Have a great day.

      Comment


        #4
        I'm afraid I spoke too soon. The code compiles OK now but it ignores the "Cancel" part.
        It looks like it's not a syntax error anymore but rather a logic error.

        As you can see from my code snippet, I placed the "Cancel" bit at the very end of the code, but by doing so I even get an error message that says " AtmStrategyCancleEntryOrder()method error: Missing orderId parameter".

        The funny thing is that I get this error message every ten minutes (I'm using 10 min bars) even if no trade has been placed and thus there's no trade to cancel.

        Thanks.

        Comment


          #5
          Hi Laocoon,

          From what you are describing it sounds like the code is not checking to make sure you are in a ATM strategy before calling the AtmStrategyCancelEntryOrder() method. I suggest that for your Cancel() method that you put that in a condition that tests for if you are in an ATM Strategy.

          There is a built in sample code of the ATMStrategy that you can use a great example of the reference. From your Control Center>Tools>Edit NinjaScript>Strategy. Select SampleATMStrategy.

          I would also look into the GetAtmStrategyMarketPosition(id) from the link below
          http://www.ninjatrader.com/support/h...etposition.htm

          The link below should help you get started on using the MarketPosition.
          http://www.ninjatrader.com/support/h...etposition.htm

          Let me know if I can be of further assistance.
          Cal H.NinjaTrader Customer Service

          Comment


            #6
            Thanks a lot for your reply Cal.
            I'm familiar with the SampleATMStrategy, in fact it served as a template for the implementation of my automated strategy. The thing is, it doesn't include an example of the AtmStrategyCancelEntryOrder method.

            I played with the MarketPosition command, but it didn't really help in this case, as for an order to be cancelled in my strategy I obviously have to be Flat. As a reminder: an order shall only be cancelled if the current candle hasn't made a higher high than the high of the previous candle (for longs). I'm using a 10 min chart, which means that if after 10 minutes no higher high has been made (we're still flat here), the order shall be cancelled.

            That's why the following snippet doesn't produce the desired outcome:

            if (Position.MarketPosition == MarketPosition.Flat && High[0] <= High[1])
            {
            AtmStrategyCancelEntryOrder(orderId);
            }

            If there's a valid entry signal, the ATM Strategy shall be activated, the order will be active for 10 minutes and it will execute if that tick higher is made. If not, it shall be cancelled.

            It would be really helpful to have a generic example of the AtmStrategyCancelEntryOrder method in order to study how it works when embedded into a strategy.

            Thanks.
            Last edited by laocoon; 04-08-2013, 08:28 AM.

            Comment


              #7
              Hi Laocoon,

              Try using the GetAtmStrategyMarketPosition(id) from the link below
              http://www.ninjatrader.com/support/h...etposition.htm

              This will check your ATM Strategy and what the market position is at.

              Let me know if you need further assistance.
              Cal H.NinjaTrader Customer Service

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by argusthome, 03-08-2026, 10:06 AM
              0 responses
              85 views
              0 likes
              Last Post argusthome  
              Started by NabilKhattabi, 03-06-2026, 11:18 AM
              0 responses
              47 views
              0 likes
              Last Post NabilKhattabi  
              Started by Deep42, 03-06-2026, 12:28 AM
              0 responses
              29 views
              0 likes
              Last Post Deep42
              by Deep42
               
              Started by TheRealMorford, 03-05-2026, 06:15 PM
              0 responses
              32 views
              0 likes
              Last Post TheRealMorford  
              Started by Mindset, 02-28-2026, 06:16 AM
              0 responses
              67 views
              0 likes
              Last Post Mindset
              by Mindset
               
              Working...
              X