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

Can't get chart cancel button to do what it's supposed to do

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

    Can't get chart cancel button to do what it's supposed to do

    I've implemented chart buttons and they're working great at turning my various sub-strategies on/off, modifying stop loss and initiating arbitrary trades that connect with order and execution management in my strategy. The one thing I can't get to fulfill it's purpose is a Cancel button. I've run a print on it and the strategy definitely recognizes when it's clicked. The code for the buttons is fine, I assure you. The problem is getting the click implemented through the rest of my code. It all compiles but doesn't do it's job.

    I've used the Ninjascript order cancel example from 2015 to build the code. Maybe it's dated and doesn't work with NinjaTrader 8. Anyway, here's my code which doesn't actually cancel pending orders:

    if (Position.MarketPosition == MarketPosition.Flat)
    {

    if (entryOrderLONG == null && LONG1buttonClicked == true && Close1UP == true)
    { EnterLongLimit(Convert.ToInt32(DefaultQuantity), Close[1], @"entryLONG");
    LastTradeCount = CurrentBar; }
    else if (entryOrderLONG != null && CancelbuttonClicked == true)
    { CancelOrder(entryOrderLONG);}
    }

    Any suggestions to end my banging my head against the wall experience?
    Thanks in advance.

    #2
    Hello Doctor JR,

    Thanks for your post.

    Please clarify that using prints, you are hitting the CancelOrder method after the button is clicked. If you are not hitting the CancelOrder method, please check the condition controlling that method to ensure the condition is getting processed, and also check how the condition evaluates (I.E. is entryOrderLONG null when this condition evaluates?)

    Best practice is to assign order objects, and also set them to null in OnOrderUpdate. (If the order object needs to be held later, it is also permissible to set it to null when you see the order has filled in OnExecutonUpdate.)

    SampleOnOrderUpdate demonstrates how you may work with order objects in your strategy. I suggest making sure that you are properly assigning the order to an order object, and your logic is properly reaching the CancelOrder method to troubleshoot the issue you are facing. You will also want to make sure the order is properly being set to null when you are done with it (when the order is filled, cancelled or rejected.)

    SampleOnOrderUpdate - https://ninjatrader.com/support/help...and_onexec.htm

    We look forward to assisting.
    JimNinjaTrader Customer Service

    Comment


      #3
      I've verified with prints that the button has been clicked and

      Comment


        #4
        I've verified with prints that the button has been clicked, that the order is no longer null and that the market position is flat. The trade to be cancelled is no problem handled by OnOrderUpdate in all other contexts. So I think I'm complying with the best practices.

        Since you didn't point out any problems with the code I submitted I'll assume there's a goofy issue somewhere else in my code and I need to do some detective work.

        Comment


          #5
          Hello Doctor JR,

          The matter would have to come down to if CancelOrder was called with a valid order object that was not null.

          I have attached an example that you can compare against something that is working. Please note that TriggerCustomEvent is used with the order submission so the BarsAgo reference on the Low price series is valid.

          TriggerCustomEvent - https://ninjatrader.com/support/help...ustomevent.htm
          Attached Files
          JimNinjaTrader Customer Service

          Comment


            #6
            Your suspicion was right Jimbo my pal. The problem was in OnOrderUpdate which I did not question since my paste and tinker job has handled everything else correctly for quite some time.

            Like many of your customers, I'm sure, I'm not inclined to think through all the Ninjascript as long as it does what I want.'

            Thanks for your input.

            Comment


              #7
              I guess I got ahead of myself on this issue. The applicable instructions in onBarUpdate are verified as indicating what they're supposed to indicate. Best as I can tell OnOrderUpdate is simply not recognizing that CancelOrder(entryOrderLONG) or CancelOrder(entryOrderSHORT) mean that the applicable order is cancelled. Would you take a look at my OnOrderUpdate code and tell me if I need to make some adjustments to fix this problem?

              --------------------------------------------------------------------------------------------------------------------------------

              protected override void OnOrderUpdate(Order order, double limitPrice, double stopPrice, int quantity, int filled, double averageFillPrice, OrderState orderState, DateTime time, ErrorCode error, string nativeError)
              {

              if (order.Name == "entryLONG")
              entryOrderLONG = order;

              if (entryOrderLONG != null && entryOrderLONG == order)
              {

              if (entryOrderLONG.OrderState == OrderState.Cancelled && order.Filled == 0)
              {
              entryOrderLONG = null;
              }

              }

              if (order.Name == "entrySHORT")
              entryOrderSHORT = order;

              if (entryOrderSHORT != null && entryOrderSHORT == order)
              {
              if (entryOrderSHORT.OrderState == OrderState.Cancelled && order.Filled == 0)
              {
              entryOrderSHORT = null;
              }
              }
              }

              Comment


                #8
                I found the problem which was a silly oversight.
                In placing an original order I was using:
                EnterLongLimit(int quantity, double limitPrice, string signalName)
                instead of:
                EnterLongLimit(int barsInProgressIndex, bool isLiveUntilCancelled, int quantity, double limitPrice, string signalName)

                So the system apparently assumed "isLiveUntilCancelled" was false. I used the longer version with the "true" declaration and my Cancel button works just fine.
                Thanks for your feedback.

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                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
                6 views
                0 likes
                Last Post FAQtrader  
                Started by rocketman7, Today, 09:41 AM
                5 responses
                19 views
                0 likes
                Last Post NinjaTrader_Jesse  
                Started by frslvr, 04-11-2024, 07:26 AM
                9 responses
                127 views
                1 like
                Last Post caryc123  
                Working...
                X