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

Using OnExecution() to send market order directly after stop order fills

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

    Using OnExecution() to send market order directly after stop order fills

    Need some help/advice here. The main goal is to hide the lot size I'm trading and keep it off the book. I use buy stop orders for breakouts. Even though the order turns into a market order, the # of contracts I believe is still visible on the order book. So my idea was to default all my contract sizes to 1 contract, and then add some code using OnExecution() to immediately send a market order after the buy stop order is executed. For example, lets say I want to trade 20 contracts on ES. My code would eventually dictate placement of a buy stop order at 4700 for 1 contract. When that order executes, I would have some code in OnExecution() that would immediately submit a market order for 19 more contracts. I was thinking to just use something like:

    if (Position.MarketPosition == MarketPosition.Long &&EntryAllowed)
    {
    EnterLongMarket();
    EntryAllowed = False;
    }

    Any issues with this architecture?

    #2
    Hello nmk85roll,

    Thanks for your post.

    You could consider hiding your intentions by triggering separate order submissions after your initial order triggers.

    Checking for a long position and if additional entries are allowed in OnExecution could do that like you describe.

    Keep in mind that EnterLong woul be used for submitting a buy market order, and EntryHandling/EnteriesPerDirection would have to allow the additional entry to take place.

    EntryHandling - https://ninjatrader.com/support/help...ryhandling.htm

    EntriesPerDirection - https://ninjatrader.com/support/help...rdirection.htm

    We look forward to assisting.
    JimNinjaTrader Customer Service

    Comment


      #3
      So I coded this idea and seemed to got it to work with one slight error. It's clear the market order following the Buy Stop Entry is NOT entering right away, but waiting for the start of the next bar. ?I tried putting the code in both the OnOrderUpdatea() Method and the OnExecution() which seems to be the preferred method. Is this a limitation of backtesting or is the Market.Position not change internally to NinjaTrader until the start of the next bar. Here is the two snippets of code in question:

      Main Strategy

      protected override void OnExecution(IExecution execution)
      {
      if (Position.MarketPosition == MarketPosition.Long && HiddenOrderNotPlaced)
      {
      Print ("MarketTrigger" + Time[0].ToString() + Time[0].ToString("D"));
      NK_EnterLongHidden(NShares);
      HiddenOrderNotPlaced = false;
      }
      }

      Order Management Strategy
      public void NK_EnterLongHidden(int NShares)
      {
      if (LongEntryOrder == null || LongEntryOrder.OrderState == OrderState.PendingCancel)
      {
      if (Position.MarketPosition == MarketPosition.Short)
      {
      NK_ExitShortMarket(); // Close short position before going long
      }
      LongEntryOrderHidden = SubmitOrder(0, OrderAction.Buy, OrderType.Market, NShares-1, 0, 0,"", "EnHidden-L");
      }
      }

      Comment


        #4
        Hello nmk85roll,

        When I test on my end with the attached script and the Strategy Analyzer, I see both entries executed on the same bar.

        If something is unclear, could you modify my example so it can illustrate what you are seeing?
        Attached Files
        JimNinjaTrader Customer Service

        Comment


          #5
          It looks like your first order is a market order, it should be a buy stop order, so maybe change the order type to "OrderType.Stop" with an arbitrary entry price in the middle of the bar and see if your Entry2 still triggers in the middle of the bar or waits till the bar close/open.

          Comment


            #6
            Hello nmk85roll,

            I have attempted with OrderType.Stop and I do not see these symptoms. Could you modify the strategy and attach your changes to demonstrate?
            Last edited by NinjaTrader_Jim; 12-22-2021, 01:34 PM.
            JimNinjaTrader Customer Service

            Comment


              #7
              Jim,

              I modified your script to use a buy stop, and it does indeed show the issue. Script and screenshot attached.
              Attached Files

              Comment


                #8
                Hello nmk85roll,

                I was using 10 for the stop price and was testing minute bars, and I still see the same results when I use the same stop prices you are and test against range bars.

                Could you test in a clean environment and can you note your data provider, PC time zone, and the data series settings used in the Strategy Analyzer?

                Please also see if we can reproduce this with minute data, or with a very small set of tick data so I can test more rapidly to see the same results.

                When I am able to reproduce the same results, I can comment further.

                Clean Environment Test:

                Creating a clean environment can be done by following the steps below:
                1. Close NinjaTrader 7, and rename the "NinjaTrader 7" folder in My Documents to something like: "NinjaTrader 7 OLD"
                2. Uninstall NinjaTrader from the Windows Control Panel and reinstall using the installer from http://ninjatrader.com/PlatformDirect
                3. Import the test script and test to reproduce the split entry. Please let me know specifics to reproduce. (data series settings, time zone, data provider, version of the script used. I.E. Market orders vs. Stop Market orders)
                If you ever need to switch back to your original platform, you may do so by closing NinjaTrader and swapping the platform folder names.

                For example, Close NinjaTrader and rename the new "NinjaTrader 7" folder to "NinjaTrader 7 NEW" and the "NinjaTrader 7 OLD" folder to "NinjaTrader 7." Then restart the platform. Simply put: NinjaTrader 7 will always load the "NinjaTrader 7" folder in My Documents.
                JimNinjaTrader Customer Service

                Comment


                  #9
                  I can reproduce on both minute and tick data. PC time zone is set to central.

                  I have another PC i can test a clean environment on, will do that shortly. Can you be more specific on what you are asking for with "data series settings used in strategy analyzer?

                  Thanks!

                  Comment


                    #10
                    Ran this again on a separate PC with a fresh clean install of NT7. This graph looks even worse, somehow even with limiting the directions per entry to 3 it takes 80!!! And still puts the market order on the next bar. Something seems off with the way NT7 is processing this code. Thoughts?
                    Attached Files

                    Comment


                      #11
                      Also my datafeed is Ninjatrader Continuum

                      Comment


                        #12
                        Hello nmk85roll,

                        If I am given a full screenshot of the Strategy Analyzer, showing the settings you used specifically, I can create the same test results where the entries take place on different bars and I would then be able to explain what we are seeing. (I also need to know your PC clock's timezone)

                        Could you provide this detail, and could you provide a screenshot of the Strategy Analyzer (showing the settings) with minute data as opposed to Range bars? This will make testing faster as I will not have to acquire large amounts of tick data.

                        EntriesPerDirection was left in my test script because I was testing with the Managed Approach before I tested Unmanaged and posted the script. EntriesPerDirection does not affect the Unmanaged Approach.
                        JimNinjaTrader Customer Service

                        Comment


                          #13
                          5 minute data chart and analyzer settings attached from today's today. PC clock is central time zone. Data feed is Continuum. Please let me know if you need anything else.
                          Attached Files

                          Comment


                            #14
                            Hello nmk85roll,

                            We were not having aligned tests so we connected to make sure our test were identical.

                            We noticed the difference between our tests was due to our difference of using the second order.

                            We would not see the second entry order execute on the same bar of the chart because the orders are filled bar by bar with the bar's OHLC values, and the second order is triggered by the execution of the first order (which would fill with one bar of data) and then the other order would fill with the next bar's data.

                            This is simply a nuance with backtesting since the OHLC of each bar are used to simulate order fills.

                            If having the orders executed in the same bar is important to anyone trying the same, it an be considered to submit orders to a single tick data series.

                            https://ninjatrader.com/support/help...ipt_strate.htm

                            Last edited by NinjaTrader_Jim; 12-27-2021, 01:03 PM.
                            JimNinjaTrader Customer Service

                            Comment


                              #15
                              Jim,

                              thanks for your time on the phone. Interesting discover I made based off our trials... Can you please confirm that if we use TWO buy stop orders that they BOTH trigger on the same bar? I can't explain why the buy stop order would trigger on the same bar, but the market order wouldn't. My thought was, we could set the 2nd buy stop to Position.AvgPrice - 1, so basically it would always trigger as a market order. Two things I politely ask of you:

                              1. If you confirm the same behavior on your end with the attached script.
                              2. Can you help shed some light on why the buy stop order would trigger on the same bar but the market order would NOT? This seems like a much more efficient solution than having to add a 1 tick data series for intrabar granularity in this specific scenario.

                              Thanks Jim!
                              Attached Files

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by fx.practic, 10-15-2013, 12:53 AM
                              5 responses
                              5,403 views
                              0 likes
                              Last Post Bidder
                              by Bidder
                               
                              Started by Shai Samuel, 07-02-2022, 02:46 PM
                              4 responses
                              94 views
                              0 likes
                              Last Post Bidder
                              by Bidder
                               
                              Started by DJ888, Yesterday, 10:57 PM
                              0 responses
                              6 views
                              0 likes
                              Last Post DJ888
                              by DJ888
                               
                              Started by MacDad, 02-25-2024, 11:48 PM
                              7 responses
                              158 views
                              0 likes
                              Last Post loganjarosz123  
                              Started by Belfortbucks, Yesterday, 09:29 PM
                              0 responses
                              8 views
                              0 likes
                              Last Post Belfortbucks  
                              Working...
                              X