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

Advanced Order Handling with Market Orders

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

    Advanced Order Handling with Market Orders

    begin edit
    3.17.21 2:00 pm EST

    I discovered the problem. It was my lack of understanding of how an order object is processed during high-volume environments.
    The error was mine in that the logic was wrong due to my ignorance.

    My script was working in backtesting and pretty much any other time than 9:30 am at the open. Due to increased volume at the open, the logic used, and the script being set to calculate OnPriceChange, the increased volume was causing my logic to process faster than NT could acknowledge that a variable has been assigned a new value that was needed to set my exit levels.

    The script has 3 separate steps that need to be completed in order, and each of them must be complete before the logic progresses to the next step.
    At the open, the price is changing so quickly that based on how the script was written the 2nd and 3rd steps were occurring almost simultaneously before a value could be assigned and then referenced to set the exits.

    I separated the 2nd and 3rd step with an additional 1 second and the script is working beautifully.

    end edit




    Hello,

    I am using Advanced Order Handling and I have a functioning ES strategy that places a long market order, then successfully submits the stop and target limit orders. I do not have any problems with this script.


    I attempted to create a separate duplicate strategy that places a short market order instead of a long market order that then submits the exit orders.
    I have reversed the logic so that the stop order from the long strategy is no longer below the entry and so the target is not above the short entry.


    On the short script I am getting an immediate buy to cover market order that closes the position.

    The following is the trace I am getting for the exit.

    Entered internal SubmitOrderManaged() method...: BarsInProgress=0 Action=BuyToCover OrderType=Market Quantity=0 LimitPrice=0 StopPrice=0 SignalName=" FromEntrySignal"


    All of the values are zeros, and my SignalName from the market order entry is not referenced. I cannot figure out where the Buy To Cover order is coming from.
    I have added to prints to the script to attempt to find the issue, but the logic is flowing properly through the Advanced Order Handling sequence.
    This is the only trace appearing - no errors are printed in the output or being logged.


    Does this sound familiar to anyone? I am hoping that this sounds familiar to someone and they might have some thoughts on it.



    Thank you for any help!

    Last edited by 9817_dynamic; 03-17-2021, 12:03 PM. Reason: I discovered the problem. It was my lack of understanding of how an order object is processed.

    #2
    If your Long version of the script works perfectly ...

    Well, then, your duplicate Short version has some logic that
    wasn't reversed correctly -- or completely.

    Captain Obvious says that is obviously the issue here.

    Try this: start a new 3rd duplicate script, comment out each
    line of 'Long' code, then duplicate each line, un-comment it
    and reverse the logic for a 'Short' entry. The point is, having
    both Long code (commented out) and Short code in the same
    file right next to each other can be helpful during these fine
    tooth comb verification of code changes.

    Good luck.

    PS:
    Keep adding Print statements to verify assumptions, calculations,
    and code paths. Keep doing this until you narrow down and find
    the problem. You've written to this forum in the middle of your
    debugging -- but obviously your debugging is not finished. Keep
    going -- you're on the right road to discovery.

    Hang in there!

    Comment


      #3
      Obviously, without providing any code to this forum, no one will be able to
      really help you all that much.

      The only other suggestion I have is this:
      For every Print you add to the Short version that verifies something important,
      go back to the working Long script and add the same Print with the same
      information. The goal of this exercise is to eliminate any assumptions
      you may be inadvertently making in the Long script.

      Print, print, print -- verify, verify, verify.

      I like this method for my prints,

      Code:
      // print formatted string to NinjaTrader output window
      private void PrintString(string format, params object[] args)
      {
          Print(string.Format(format, args));
      }
      which allows easy formatting using string.Format.

      Comment


        #4
        Hello 9817_dynamic,

        A BuyToCover could be submitted to close a short position when reversing the position.

        Is the position being reversed?

        Is the Trace you have provided the full exact message and copied and pasted?

        Is ExitShort() anywhere in the code?

        Is EnterLong() anywhere in the code?

        When you say you are using the Advanced Order Handling, you are meaning you are using the Managed approach and not the Unmanaged approach correct?

        Below is a link to a forum post that demonstrates how to use prints to find which line of code is being triggered.
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Thank you for your help.


          Is the position being reversed?
          The position is not being reversed.

          Is the Trace you have provided the full exact message and copied and pasted?
          It is not a copy and paste of the trace. I submitted the forum question from a different computer. This is the exact trace excluding the dates, time, and strategy name though.

          Is ExitShort() anywhere in the code?
          Yes, but it is inside a block of code with that block's bool set to false so it is not active. I have a print inside that block and it is not being triggered.

          Is EnterLong() anywhere in the code?
          Yes, but it is inside a block of code with that block's bool set to false so it is not active. I have a print inside that block and it is not being triggered.


          When you say you are using the Advanced Order Handling, you are meaning you are using the Managed approach and not the Unmanaged approach correct?
          Yes. I am using object orders opposed to simply using EnterLong()/EnterShort() to place market orders and then SetProfitTarget() and SetStopLoss() to place my OCO exits.
          So I am using the Managed Approach and using the overrides for OnOrderUpdate(), OnExecutionUpdate(), and OnPositionUpdate() to place my exits after my entries are triggered in OnBarUpdate().



          The script that I used as a basis for this script was placing EnterLongLimit and EnterShortLimit entry orders and it is working perfectly. I duplicated the script, changed the entry orders to market orders from limit orders, and I am running into trouble on keeping the market orders open.

          My original script followed this logic sequence:
          1. Set an opening value to reference at a specific time
          like if (ToTime (Time[0]) == 93000) {openingValue = Open[0] ; }
          2. Place limit orders x number of ticks from openingValue

          My new script is attempting to do this:
          1. Enter market entry orders at 93000
          2. Set an opening value like above at 93001
          3. then place the exits referencing the openingValue.

          When I attempted to adapt the logic to enter market orders, I began running into the problem of my position closing immediately with a "short short" and "buy to cover" orders without the signal name attached to them. And the trace values are all zero for the quantity and zeros for the limit and stop prices for the entered orders that NinjaTrader submits immediately to close the positions.

          The new strategy uses objects for the market entry orders too. I have played around with using an EnterLong() market order that is not an object (or does not process through OnOrderUpdate, OnExecutionUpdate, and OnPositionUpdate) to enter the position with a SignalName, and then have the stop and target limit orders placed as object orders using the same SignalName that are controlled by the overrides just mentioned in the parenthesis. But this seemed clunky to be mixing the two order processing methods.

          I need the exit orders to be able to be checked for null so I can amend or cancel them under certain conditions.

          When using the managed approach will NinjaTrader create immediate exit orders for entry market orders and close the position if a script does not place the exit orders as soon as the entry order and executions update?


          The entire strategy is a fairly long script (over 8,000 lines at this point) but it contains many blocks of code that are turned on and off with bools by the user.
          I can pull out exactly what I am attempting to debug and possibly email it to you. What is the process to do that?

          I think I am looking for more information on how the OnOrderUpdate and OnExecution overrides operate. I have read what I can in the help guide, I have looked at examples, and I have looked through the forum submissions but I cannot seem to find anything directly related to what I am attempting to do. I might need to learn to use the unmanaged approach.



          Thank you again for any help.

          Comment


            #6
            Hello 9817_dynamic,

            Comment out code or add prints throughout the code to determine what lines are being executed.

            Extract the lines of code causing the behavior and put these into a new blank test script. Ensure only the absolute bare minimum necessary to reproduce the behavior is included.

            Export this and then attach this to your next post so that I may test.

            To export a NinjaTrader 8 NinjaScript so this can be shared and imported by the recipient do the following:
            1. Click Tools -> Export -> NinjaScript...
            2. Click the 'add' link -> check the box(es) for the script(s) and reference(s) you want to include
            3. Click the 'Export' button
            4. Enter a unique name for the file in the value for 'File name:'
            5. Choose a save location -> click Save
            6. Click OK to clear the export location message
            By default your exported file will be in the following location:
            • (My) Documents/NinjaTrader 8/bin/Custom/ExportNinjaScript/<export_file_name.zip>
            Below is a link to the help guide on Exporting NinjaScripts.
            http://ninjatrader.com/support/helpG...-us/export.htm
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              Got it. I will work on this in a little while.

              Thank you again!

              Comment


                #8
                Hello.

                I worked on the script yesterday. I successfully isolated the logic I am attempting to debug.

                I am now attempting to export the script, and I keep running into an error when I try to save the source file to ExportNinjaScript.

                I attached a screenshot of the error.
                NT8 appears to be looking for a strategy named "@strategy.cs"
                I have no such strategy on my computer. I created a basic script with that name, complied it, and I am still getting the same error.
                I have restarted NT and my computer.

                Any ideas on what to do?
                Should I take this up with Platform Technical Support?

                Thank you again for your help


                . Click image for larger version

Name:	NT8 export error 3.16.21.JPG
Views:	296
Size:	12.6 KB
ID:	1146830

                Comment


                  #9
                  My compiler inside of NinjaScript Editor is working just fine. This error only occurs when I attempt to export the script using the instructions in the NT help guide from link above.

                  Comment


                    #10
                    Hello 9817_dynamic,

                    The @Strategy.cs file is part of the installation.

                    If that file is missing, run the repair from the 8.0.24.1 installer.
                    Chelsea B.NinjaTrader Customer Service

                    Comment


                      #11
                      I copied the script and pasted it into a notepad file and saved it as a C# file. I will attach that and hopefully you can run that.
                      I will run the installer to repair the missing file too.

                      I have also attached the log, NinjaScript Output from this morning, and a screenshot of the configuration of an example test that is running properly for me.

                      I run this on 1 second candlestick or OHLC charts.
                      Any tests not entered live (after the OpeningValue Set Time) need to have the "Backtest" box checked to properly set the intended value.

                      Only Long OR Short entries can be chosen as true in the parameters or a position will open then immediately close or an error will occur.

                      The exit logic is structured as absolute for both long and short positions.
                      So for a short position the stop is a positive number (ex. 500) and the target is a negative number (ex. -10) indicating the ticks from the opening value.
                      For a long position the stop is negative (ex. -500) and the target is positive (ex. 10).

                      And the default fib extensions that are drawn have no impact on the functioning of the script. These are for my own visual reference to verify the exits are settings accurately. I plan to remove this logic eventually.

                      I now have the strategy working most of the time. It seems to work 100% of the time in back tests added to charts with sufficient bars, in Strategy Analyzer for back testing multiple days, and live tests during slow market periods.

                      Perhaps I am just going about this all wrong though. I have only been programming for about 6 months.

                      I just tested it at the 9:30 am EST open this morning and ran into trouble again.
                      I tested it in 6 charts in 6 different sim accounts hoping to test it with the following parameters:

                      1.) ES Long. Market Orders 9:30:01 Opening Value Set: 9:30:02
                      2.) ES Short. Market Orders 9:30:01 Opening Value Set: 9:30:02

                      3.) ES Long. Market Orders 9:30:00 Opening Value Set: 9:30:02
                      4.) ES Short. Market Orders 9:30:00 Opening Value Set: 9:30:02

                      5.) MES Long. Market Orders 9:30:01 Opening Value Set: 9:30:02
                      2.) MES Short. Market Orders 9:30:01 Opening Value Set: 9:30:02

                      All of these parameters can be set as user-defined inputs.


                      None of the tests worked completely at 9:30 am.
                      The Market Orders were all placed and executed in all 6 tests.
                      Some immediately set their own exits with zero or values of 2.75, resulting in errors of stops or limit orders being placed above or below the market and closing the positions and strategies per the settings.
                      3 of them successfully drew the fib extensions.
                      2 of them left the positions open but did not draw the fib extensions.

                      At 9:35 am I setup another test with only two charts (1 and 2 above) and they both worked properly.
                      So it seems the issue may have something to do with high-volume trading spikes?

                      I have other strategies that do not use market orders at the open. I can run 8 instances of these without issue on my system, so I do not think it is I am trying to run too much.



                      Thank you for any insights into how I am going about this...

                      Attached Files
                      Last edited by 9817_dynamic; 03-16-2021, 10:07 AM. Reason: I noticed a few typos so I edited the post to avoid confusion.

                      Comment


                        #12
                        Hello 9817_dynamic,

                        To confirm, all of this code is necessary to reproduce the behavior?
                        This can cannot be reduced to a single order to produce the behavior without any custom logic?

                        Where you able to determine which line of code is being run?

                        The issue is that there is a sell order to close a long position that is unexpected, is this correct?
                        Chelsea B.NinjaTrader Customer Service

                        Comment


                          #13
                          Oh sorry, a single order will reproduce the behavior. I can edit it and send it again. That might not be necessary though depending on the following question.

                          I think I just realized what is happening. The values that the targets are being set at are from 0. So the stops and targets are being set before my reference value variable is set.

                          The issue is that the target limit prices of 1.75, 2.75, 2.25, 3.75 and a stop values of 125 are being submitted. I cannot determine where this is occurring.


                          It is like NT is placing these exit orders early (on the wrong side of the trade, so the stop is below the entry level on a short position and breaking the trade rules) before the logic to submit my exit levels is reached. And this creates an error and the strategy closes. I am guessing it is from an error in my programming where I do not understand some aspect of using objects to process the orders.

                          Will NinjaTrader work with market entry orders being placed with Signal Names without creating order objects for them and without flowing through OnOrderUpdate(), OnExecutionUpdate(), and OnPositionUpdate() as objects but then referencing those same Signal Names to use order objects to place and cancel the stop and target?

                          Or another way of asking, does the Signal Name tracing change whether or not the order was placed by creating an order object for that order?

                          I have tested this in the past but it seemed like I was fitting a square peg in a round hole by combining two different methods of processing a trade from start to finish.

                          Because if I can combine them without creating any additional issues I think this would solve my issue. I believe the problem is arising as the market orders are being processed as objects. Because the only thing I changed was entering market orders and not limit orders when this problem began.
                          Last edited by 9817_dynamic; 03-16-2021, 01:01 PM.

                          Comment


                            #14
                            I edited my post above. I think I realized what is happening to cause the unintended behavior. I think I have a solution to fix it too.

                            However, my solution would be simpler and more concise if you could still please answer this question from above:

                            Will NinjaTrader work with market entry orders being placed with Signal Names without creating order objects for them and without flowing through OnOrderUpdate(), OnExecutionUpdate(), and OnPositionUpdate() as objects but then referencing those same Signal Names to use order objects to place and cancel the stop and target?


                            So place the order with something like:

                            EnterShort(0, 1, "short1"); without creating an order object for it


                            and then creating exit objects like:

                            private Order short1StopOrder = null;
                            private Order short1TargetOrder = null;

                            and still place the exits in OnExecutionUpdate()?

                            Or another way of asking, does the Signal Name tracing change whether or not the order was placed by creating an order object for that order?
                            Last edited by 9817_dynamic; 03-16-2021, 01:12 PM.

                            Comment


                              #15
                              Hello 9817_dynamic,

                              When you mention:
                              " The values that the targets are being set at are from 0. So the stops and targets are being set before my reference value variable is set.
                              The issue is that the target limit prices of 1.75, 2.75, 2.25, 3.75 and a stop values of 125 are being submitted. I cannot determine where this is occurring.
                              It is like NT is placing these exit orders early (on the wrong side of the trade, so the stop is below the entry level on a short position and breaking the trade rules) before the logic to submit my exit levels is reached. And this creates an error and the strategy closes. I am guessing it is from an error in my programming where I do not understand some aspect of using objects to process the orders."

                              Are you referring to set methods like SetProfitTarget or SetStopLoss which cannot be unset?

                              From the help guide:
                              "Should you call this method to dynamically change the target price in the strategy OnBarUpdate() method, you should always reset the target price / offset value when your strategy is flat otherwise, the last price/offset value set will be used to generate your profit target order on your next open position"



                              Where you mention:
                              "Will NinjaTrader work with market entry orders being placed with Signal Names without creating order objects for them and without flowing through OnOrderUpdate(), OnExecutionUpdate(), and OnPositionUpdate() as objects but then referencing those same Signal Names to use order objects to place and cancel the stop and target?"

                              If no signal names are used, the exits will apply to all entries and not specific ones. The order object from OnOrderUpdate will always exist for the order updating OnOrderUpdate(). You do not have to assign the order from OnOrderUpdate() to a variable if you do not want to. You can assign the order object to a variable if you want to get the state from it or cancel it at a later time.

                              If you are referring to market orders such as EnterLong(), these will not be submitted unless the logic calls the method.

                              Where you mention:
                              "Or another way of asking, does the Signal Name tracing change whether or not the order was placed by creating an order object for that order?"

                              A signal name does not change when assigning an order object from OnOrderUpdate() to a variable. Assigning any object to variable does not change the object.


                              Below is a link to an example script that assigns orders objects to variables from OnOrderUpdate().
                              Chelsea B.NinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Waxavi, Today, 02:10 AM
                              0 responses
                              3 views
                              0 likes
                              Last Post Waxavi
                              by Waxavi
                               
                              Started by TradeForge, Today, 02:09 AM
                              0 responses
                              9 views
                              0 likes
                              Last Post TradeForge  
                              Started by Waxavi, Today, 02:00 AM
                              0 responses
                              2 views
                              0 likes
                              Last Post Waxavi
                              by Waxavi
                               
                              Started by elirion, Today, 01:36 AM
                              0 responses
                              4 views
                              0 likes
                              Last Post elirion
                              by elirion
                               
                              Started by gentlebenthebear, Today, 01:30 AM
                              0 responses
                              4 views
                              0 likes
                              Last Post gentlebenthebear  
                              Working...
                              X