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

CancelOrder behavior in NT7 unmanaged approach

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

    CancelOrder behavior in NT7 unmanaged approach

    Hello again,

    Reading the documentation, "this method sends a cancel request to the broker and does not guarantee that an order is completely cancelled. Most of the time you can expect your order to come back 100% cancelled". However, in my case this doesn't seem to apply:

    -----------------------------------
    private static double LOT_SIZE = 100000.0;

    protected override void Initialize()
    {
    CalculateOnBarClose = true;
    }

    protected override void OnStartUp()
    {
    Unmanaged = true;
    }

    /// <summary>
    /// Called on each bar update event (incoming tick)
    /// </summary>
    protected override void OnBarUpdate()
    {
    if (Historical)
    {
    return;
    }
    start();
    }

    int cnt = 0;
    IOrder myOrder = null;

    void start()
    {
    cnt++;
    Print("Try #" + cnt);
    if (cnt == 1) {
    double volume = 0.05;
    myOrder = SubmitOrder(0, OrderAction.Buy, OrderType.Market, ((int)(LOT_SIZE * volume)), 0.0, 0.0, "", "Test cancel");
    } else if (cnt >= 2){
    CancelOrder(myOrder);
    Print("state -> " + myOrder.OrderState);
    }
    }

    protected override void OnOrderUpdate(IOrder order)
    {
    if (myOrder != null && myOrder == order)
    {
    Print("OnOrderUpdate => order = " + order.ToString());
    Print("OnOrderUpdate => state = " + order.OrderState);
    }
    }

    }
    -----------------------------------

    And the output is:

    -----------------------------------
    **NT** Enabling NinjaScript strategy 'ATest/cd6ee739e6d04ff8b91588ba085fad26' : On starting a real-time strategy - StrategySync=WaitUntilFlat SyncAccountPosition=False EntryHandling=AllEntries EntriesPerDirection=1 StopTargetHandling=PerEntryExecution ErrorHandling=StopStrategyCancelOrdersClosePositio ns ExitOnClose=True/ triggering 30 before close Set order quantity by=Strategy ConnectionLossHandling=KeepRunning DisconnectDelaySeconds=10 CancelEntryOrdersOnDisable=False CancelExitOrdersOnDisable=True CalculateOnBarClose=True MaxRestarts=4 in 5 minutes
    Try #1
    OnOrderUpdate => order = Order='04eaf61ec6ec4d29a0bddd0a47a298c0/Sim101' Name='Test cancel' State=PendingSubmit Instrument='$EURUSD' Action=Buy Limit price=0 Stop price=0 Quantity=5,000 Type=Market Tif=Gtc OverFill=False Oco='' Filled=0 Fill price=0 Token='04eaf61ec6ec4d29a0bddd0a47a298c0' Gtd='1/1/0001 12:00:00 AM'
    OnOrderUpdate => state = PendingSubmit
    OnOrderUpdate => order = Order='04eaf61ec6ec4d29a0bddd0a47a298c0/Sim101' Name='Test cancel' State=Accepted Instrument='$EURUSD' Action=Buy Limit price=0 Stop price=0 Quantity=5,000 Type=Market Tif=Gtc OverFill=False Oco='' Filled=0 Fill price=0 Token='04eaf61ec6ec4d29a0bddd0a47a298c0' Gtd='1/1/0001 12:00:00 AM'
    OnOrderUpdate => state = Accepted
    OnOrderUpdate => order = Order='04eaf61ec6ec4d29a0bddd0a47a298c0/Sim101' Name='Test cancel' State=Working Instrument='$EURUSD' Action=Buy Limit price=0 Stop price=0 Quantity=5,000 Type=Market Tif=Gtc OverFill=False Oco='' Filled=0 Fill price=0 Token='04eaf61ec6ec4d29a0bddd0a47a298c0' Gtd='1/1/0001 12:00:00 AM'
    OnOrderUpdate => state = Working
    OnOrderUpdate => order = Order='04eaf61ec6ec4d29a0bddd0a47a298c0/Sim101' Name='Test cancel' State=Filled Instrument='$EURUSD' Action=Buy Limit price=0 Stop price=0 Quantity=5,000 Type=Market Tif=Gtc OverFill=False Oco='' Filled=5000 Fill price=1.0906 Token='04eaf61ec6ec4d29a0bddd0a47a298c0' Gtd='1/1/0001 12:00:00 AM'
    OnOrderUpdate => state = Filled
    Try #2
    state -> Filled
    Try #3
    state -> Filled
    Try #4
    state -> Filled
    **NT** Disabling NinjaScript strategy 'ATest/cd6ee739e6d04ff8b91588ba085fad26'
    -----------------------------------

    ... looks like the order never gets cancelled, no matter how much I wait. This was run on Simulation account with EURUSD chart on 1 min frequency.

    Perhaps something is wrong with my account? ... Am I missing anything in the code?...

    I appreciate any suggestion or explanation regarding this behavior,

    Thanks,
    Sorin

    #2
    Hello smf2005,

    You cannot cancel a Market type order, you would need an order which remains working to cancel it. That would include orders like Limit orders.

    I look forward to being of further assistance.
    JesseNinjaTrader Customer Service

    Comment


      #3
      Thanks, Jesse.

      If I understand correctly, in the above example I will need to submit another order e.g.

      //CancelOrder(myOrder);
      tmpOrder = SubmitOrder(0, OrderAction.Sell, OrderType.Limit, ((int)(LOT_SIZE * volume)), 0.0, 0.0, "", "2nd try");

      Or this is still not correct?..

      Comment


        #4
        Hello smf2005,

        You wouldn't need a second order, you just need to not use a market order for the order you had shown. Market orders are intended to fill immediately, they are not working orders which you can cancel. You can use a limit order or an order which remains working/unfilled and that can be cancelled.

        I look forward to being of further assistance.


        JesseNinjaTrader Customer Service

        Comment


          #5
          OK, I see.. does the same limitation of Market type orders applies also to "ChangeOrder" method?

          Do I have to use also a limit or stop order instead, should I want to change it later?

          Comment


            #6
            Hello smf2005,

            Correct, market orders are intended to buy or sell as fast as allowable. The other types of orders can generally be modified or canceled.

            If you need clarification about the differences in these types of orders or other trading concepts there is a lot of information on this topic in public resources like investopedia. https://www.investopedia.com/terms/m/mar****rder.asp
            If you take a look on the left of that page the other order types are listed as well, that gives a little more of a trading description opposed to the NinjaScript manuals programmatic description.

            I look forward to being of further assistance.
            JesseNinjaTrader Customer Service

            Comment


              #7
              Thanks, I'll check it out.

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by TraderBCL, Today, 04:38 AM
              2 responses
              8 views
              0 likes
              Last Post TraderBCL  
              Started by martin70, 03-24-2023, 04:58 AM
              14 responses
              105 views
              0 likes
              Last Post martin70  
              Started by Radano, 06-10-2021, 01:40 AM
              19 responses
              606 views
              0 likes
              Last Post Radano
              by Radano
               
              Started by KenneGaray, Today, 03:48 AM
              0 responses
              4 views
              0 likes
              Last Post KenneGaray  
              Started by thanajo, 05-04-2021, 02:11 AM
              4 responses
              471 views
              0 likes
              Last Post tradingnasdaqprueba  
              Working...
              X