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

What is the best way to intercept a market trade

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

    What is the best way to intercept a market trade

    Hello

    I am implementing an addons that does not allow placing orders of any kind if a condition is met. I know that Account.CancellAllOrders (e.execution.instrument) does not work to cancel filled orders and I also know that using Account.Cancel (new [] {e.order}) is not recommended because the orders remain in Cancel Pending status and that It is also not advisable to disable the Charttrader buttons.

    For example: If a defined daily loss limit has been reached, no order of any kind can be entered or canceled before being submitted. I have the logic of the condition but I would like to know what is the best practice to intercept the trades properly.

    Thank you

    #2
    Hello jleira, thanks for your post.

    I was able to block all orders by doing this in OnOrderUpdate:

    Code:
    private Account myAccount;
    ...
    myAccount.OrderUpdate += OnOrderUpdate;
    ...
    private void OnOrderUpdate(object sender, OrderEventArgs e)
    {
        myAccount.Cancel(new[] { e.Order });
    }
    That will put the orders into a CancelPending state before they get to the Submitted state, preventing any orders from leaving the platform.


    Please let me know if I can assist any further.
    Chris L.NinjaTrader Customer Service

    Comment


      #3
      Hi NinjaTrader_ChrisL

      I was using this option that you share with me but I had read in another post that it was not convenient for orders to be in Cancel Pending status.

      Would those orders remain forever in that Cancel Pending state? Would they never become totally Canceled?

      See how the limit or stop orders are. They cannot be canceled. Of course, if I reset the SIM if they are going to be canceled but in a real one it cannot be canceled

      Click image for larger version

Name:	image cancel pending.PNG
Views:	374
Size:	90.7 KB
ID:	1146138

      Thanks for your help.
      Attached Files
      Last edited by jleira; 03-11-2021, 02:09 PM.

      Comment


        #4
        Hello jleira, thanks for your reply.

        When I tested I could not submit any orders at all when canceling them immediately in OnOrderUpdate. You should not be able to get the order to the submitted state if you are canceling it when it becomes initialized, which is what looks like what happened in your screenshot. Make sure the order does not get past the Initialized state to keep the order from submitting.

        Please let me know if I can assist any further.
        Chris L.NinjaTrader Customer Service

        Comment


          #5
          I am testing with this directly in OnOrderUpdate and if I place any limit or stop order it will remain on the chart ...

          I know what the problem is. If you place orders without Atm Strategy selected, it cancels any order you place but if you select an ATM strategy, everything you put is in Cancel Pending and the limits / stops are left on the chart.

          You can do the test. Do you know what I could do to prevent this from happening when launching them with ATM Strategy?

          This is the code that I am using
          Code:
          private void OnOrderUpdate(object sender, OrderEventArgs e)
          {
          if (e.OrderState == OrderState.Initialized)
          {
          accountSelectorMaster.SelectedAccount.Cancel(new[] { e.Order });
          return;
          }
          }
          Last edited by jleira; 03-11-2021, 03:19 PM.

          Comment


            #6
            Helloi jleira, thanks for your reply.

            I'm thinking there is something else in the code causing the orders to stick. My test script is not doing what yours is. I have attached my test script here if you would like to test it on your end (it is an indicator, place in the Indicators folder then apply to any chart). Does it work if you use a singular account object as I do? Please note the differences between my test script and your current addon.

            Best regards.
            Attached Files
            Chris L.NinjaTrader Customer Service

            Comment


              #7
              Hello

              I have done the tests with the script you sent me but before I reinstalled clean ninjatrader completely to avoid any error. I loaded just your indicator and ran the test and this is what happens:

              Without ATM Strategy selected: All the operations that you place through the Chart Trader are canceled before entering the market as I want them to happen and none are left in a "Cancel pending" status. It's perfect! but...

              With ATM Strategy selected: All the operations you place are in "Cancel pending" status and the ATM Strategy remains active as if there were a placed trade. The limit and stop orders are placed on the chart in the initialized state and the only way to eliminate them from the chart is by resetting the SIM account (this is where I am doing the tests). This only happens when I have placed an order with the ATM Strategy selected.

              I also did the test with the Addon Framework and it happens exactly the same as with your indicator. When seeing the flow of orders that appear in the outbox I noticed that when the ATM Strategy is selected, all the orders that are launched through the Chart Trader are initialized 2 times but when there is no ATM Strategy selected, all the orders that are launched are they initialize only 1 time.

              If you perform the tests with the selected ATM Strategy, either with your indicator or with the Addon Framework, the same should happen.

              My question at this point is: Why is it that with ATM Strategy selected not all orders are canceled as it happens when an order is placed without ATM Strategy selected? and Why does the ATM Strategy remain active without any market order and how can I inactivate it?

              Code:
              private void OnOrderUpdate(object sender, OrderEventArgs e)
              {
                if (e.OrderState == OrderState.Initialized)
                {
                  Dispatcher.InvokeAsync(() =>
                  {
                    accountSelector.SelectedAccount.Cancel(new[] { e.Order });
                  });
                }
              }

              Click image for larger version  Name:	image_65342.png Views:	5 Size:	59.7 KB ID:	1146678Click image for larger version  Name:	NoOrderPlacedButATMisActive.PNG Views:	0 Size:	95.3 KB ID:	1146679
              Last edited by jleira; 03-15-2021, 11:04 AM.

              Comment


                #8
                Hello jleira, thanks for letting me know.

                The ATM order is being submitted through the NinjaTrader core, so this is going to be out of our control, unfortunately. I will only be able to submit a feature request, requesting the ability to turn on Global Simulation Mode programmatically.

                Best regards.
                Chris L.NinjaTrader Customer Service

                Comment


                  #9
                  Hi ChrisL

                  I've seen other people cut off orders and they don't have the same problem I have when trying to cancel incoming orders. Please give me a hint how I can cancel the orders without them being initialized in the Chart ...

                  There must be a solution for this ... What methods are those programmers using to correctly cancel any order that is placed?

                  I'm trying to use some of these statements to cancel them by ATM but I can't get results:

                  atmStrategySelector.SelectedAtmStrategy.CloseStrat egy("Entry"); or
                  atmStrategySelector.SelectedAtmStrategy.AtmStrateg yCancelEntryOrder(OrderId); or
                  atmStrategySelector.SelectedAtmStrategy.CloseStrat egy("Entry"); or
                  atmStrategySelector.SelectedAtmStrategy.AtmStrateg yCancelEntryOrder(OrderId);
                  atmStrategySelector.SelectedAtmStrategy.AtmStrateg yClose(Convert.ToString(atmStrategySelector.Select edAtmStrategy.Id));

                  Thank you,

                  Comment


                    #10
                    Hi jleira, thanks for your reply.

                    I apologize but there is nothing documented or supported for doing this so I will not be able to provide any more information than I have already.
                    Chris L.NinjaTrader Customer Service

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by ftsc2022, 10-25-2022, 12:03 PM
                    5 responses
                    255 views
                    0 likes
                    Last Post KeyonMatthews  
                    Started by ScottW, Today, 06:09 PM
                    0 responses
                    3 views
                    0 likes
                    Last Post ScottW
                    by ScottW
                     
                    Started by Board game geek, 10-29-2023, 12:00 PM
                    14 responses
                    244 views
                    0 likes
                    Last Post DJ888
                    by DJ888
                     
                    Started by Waxavi, 04-19-2024, 02:10 AM
                    4 responses
                    56 views
                    0 likes
                    Last Post sonia0101  
                    Started by cmtjoancolmenero, Today, 03:58 PM
                    0 responses
                    9 views
                    0 likes
                    Last Post cmtjoancolmenero  
                    Working...
                    X