Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Automated Strategy -> Order entry verification

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

    Automated Strategy -> Order entry verification

    I have a strategy that looks for an entry condition in the OnBarUpdate() event. When that condition is true it enters a trade (enter long or short). I also look for conditions to exit a trade here.

    While it would be nice to assume my order is always filled… the reality is.. I might get a partial fill or no fill at all and need to manage my strategy based on the reality of the trade.

    1. How can I properly find out if the order was filled, how many contracts and what price? Do I put this code in the OnPositionUpdate() event? or in the OnOrderUpdate() event?

    I need this information to change the state of my strategy so it does the right thing next.

    2. I also need to know how to cancel an order if it has been pending for too long. Then of course verify that it REALLY got cancelled.

    Are there any "generic" code samples to help do this
    kind of strategy?

    #2
    Hello GaryGuy,

    Thank you for your post.

    You can use OnExecution() to check the quantity filled on execution. For information on OnExecution() please visit the following link: http://www.ninjatrader.com/support/h...nexecution.htm

    You can use OnOrderUpdate() to check the time that has elapsed since the last update on the order and then use CancelOrder() to cancel the order.
    For information on CancelOrder() please visit the following link: http://www.ninjatrader.com/support/h...ancelorder.htm
    For information on OnOrderUpdate() please visit the following link: http://www.ninjatrader.com/support/h...rderupdate.htm

    For a reference sample on OnExecution() and OnBarUpdate() please visit the following link: http://www.ninjatrader.com/support/f...ead.php?t=7499

    Please let me know if I may be of further assistance

    Comment


      #3
      More Clarification - on structure of complex strategies

      Originally posted by NinjaTrader_PatrickH View Post
      Hello GaryGuy,

      Thank you for your post.

      You can use OnExecution() to check the quantity filled on execution. For information on OnExecution() please visit the following link: http://www.ninjatrader.com/support/h...nexecution.htm

      You can use OnOrderUpdate() to check the time that has elapsed since the last update on the order and then use CancelOrder() to cancel the order.
      For information on CancelOrder() please visit the following link: http://www.ninjatrader.com/support/h...ancelorder.htm
      For information on OnOrderUpdate() please visit the following link: http://www.ninjatrader.com/support/h...rderupdate.htm

      For a reference sample on OnExecution() and OnBarUpdate() please visit the following link: http://www.ninjatrader.com/support/f...ead.php?t=7499

      Please let me know if I may be of further assistance
      **********
      Patrick, thank you so much for your response to my questions! the links to the sample programs helped a lot. To clarify a bit further and clear up some of my confusion I will write how I think it all works and you can clarify.

      1. OnBarUpdate… the place where we look for entry and exit conditions for opening long or short orders. In complex strategies we also would use this to trigger change in behavior of the strategy (like a homemade ATM strategy).

      2. OnOrderUpdate… the place where we are in formed about any change in the status of an order (Pending Submit, Accepted, Working, Filled or Cancelled). Most strategies do not care about these individual stages except Filled or Cancelled. So we reset to a non trade state if we get cancelled so OnBarUpdate can start looking again.

      3. OnExecution… is the place where we know for sure we have a fill or a partial fill and this is the right place to put on our Stop loss, Target price or trail stop (can't have stop and trail stop active at the same time).

      This is also the place where we find out our Stop, Target or Trail stop have been Filled or part filled and if so we should change state to match the current condition.

      Q1. I can see how the OrderState can be filled or PartFilled… but I do not see how I can tell the quantity if PartFilled so I can make sure my strategy is synched with the stop, target, or trail stop quantity… help here please

      Q2. Is a part filled stop, target or trailing stop still active waiting for the rest of the contracts?

      Q3. If partial filled and not desiring to fill the rest… can we cancel the remaining contracts on that order?

      4. OnPositionUpdate… after this call all Position variables are set and ready to be used (but not before this happens). The structure actually represents the current state of the trade. No matter what orders are in progress the Position structure values are not set until this function is called.

      This is the place where we can fetch the information to keep state right on the current trade.

      Q1. We could wait until OnPositionUpdate to activate our stops, targets and trail stops… Any good reason to not do this? or major advantage to doing it at OnExecution?

      I would appreciate any clarification and some answers to my questions.. Thanks.

      Comment


        #4
        Hello GaryGuy,

        Thank you for your response.

        Your understanding of the methods that can be used is good. I would like to ask one question to assist me in answering your questions here: What Order Methods are you using? Unmanaged or Managed Approach?

        You can find information on the Order Methods at the following link: http://www.ninjatrader.com/support/h...er_methods.htm

        I look forward to your response.

        Comment


          #5
          Originally posted by NinjaTrader_PatrickH View Post
          Hello GaryGuy,

          Thank you for your response.

          Your understanding of the methods that can be used is good. I would like to ask one question to assist me in answering your questions here: What Order Methods are you using? Unmanaged or Managed Approach?

          You can find information on the Order Methods at the following link: http://www.ninjatrader.com/support/h...er_methods.htm

          I look forward to your response.
          While I have not set Unmanged = true; I may need to choose to do unmanaged.

          My intent for this program is to have it handle both bull and bear trades (entries determined by various strategies) open trades with a variable number of contracts (set by user paramater at startup). Optional stops, targets and trailing stops with number of contracts.. then when that stage (tier) is done take on new rule sets for the remaining contracts. This is a more advanced and flexible exit trade management program like the NT ATM but with more and varied exit types.

          I started this strategy with standard EnterLong-> exit long and Enter short-> exit short calls BUT i found slippage was terrible when I tried at close of candle to trigger manual stops and targets. I think the standard long stops / short stops, targets and trails will respond more quickly with less slippage. Hence my need to find the right way to do it using those calls.

          Simple enough.. basic structure of the program is sound.. BUT it becomes much more complex when you take into account partial fills, error conditions, cancelled orders (not by the strategy)..

          So perhaps the question back to you is.. knowing my intent.. which SHOULD I be using?

          Thanks again for your help.

          Gary

          Comment


            #6
            Use unmanaged,

            It's not as hard as it first appears.

            Comment


              #7
              Hello GaryGuy,

              Thank you for your response.

              If you have found Unmanaged to better suit you then I would stick with it and make sure you understand it. For information on Unmanaged Approach please visit the following link: http://www.ninjatrader.com/support/h...d_approach.htm

              I would use OnOrderUpdate() with OrderState.Filled to ensure the order is filled before submitting my protective stops and targets.
              Code:
              protected override void OnOrderUpdate(IOrder order)
              {
                  if (order.OrderState == OrderState.Filled)
              If you see the Partial Fills occur and you need to manage your orders based off of that then again use OnOrderUpdate() with OrderState.PartFilled and then ChangeOrder() to change the quantity.

              Please let me know if you have any questions.

              Comment


                #8
                Originally posted by NinjaTrader_PatrickH View Post
                Hello GaryGuy,

                Thank you for your response.

                If you have found Unmanaged to better suit you then I would stick with it and make sure you understand it. For information on Unmanaged Approach please visit the following link: http://www.ninjatrader.com/support/h...d_approach.htm

                I would use OnOrderUpdate() with OrderState.Filled to ensure the order is filled before submitting my protective stops and targets.
                Code:
                protected override void OnOrderUpdate(IOrder order)
                {
                    if (order.OrderState == OrderState.Filled)
                If you see the Partial Fills occur and you need to manage your orders based off of that then again use OnOrderUpdate() with OrderState.PartFilled and then ChangeOrder() to change the quantity.

                Please let me know if you have any questions.
                Hello Patrick,

                Thanks for your response... Still I am confused. My previous response I described the things I "intend" the strategy to do and was asking you if I NEEDED to go to Unmanaged to get them done. Error handling is a very complicated issue in any strategy but I imagine there are many big (expensive) holes to step in, in unmanaged strategies. Right?

                Quick question #2 I can use the IOrder form of the commands in both managed and unmanaged techniques?

                Please send me the link to one "unmanaged" full source example strategy. The tiny portions of code I have found are not enough to understand the differences in coding responsibilities between the two.

                Thanks for your help,

                Gary

                Comment


                  #9
                  Originally posted by BigWaveDave View Post
                  Use unmanaged,

                  It's not as hard as it first appears.
                  Hi BigWaveDave,

                  Thanks for the guidance but I have found no example code for how to properly manage orders and possible error conditions (partial fills, cancels, etc>) with the Unmanaged techniques. Remember back to when you were doing the "learning curve" thing. Where did you learn what you needed to know to do it right?

                  Gary

                  Comment


                    #10
                    Hello Gary,

                    Thank you for your response.
                    Still I am confused. My previous response I described the things I "intend" the strategy to do and was asking you if I NEEDED to go to Unmanaged to get them done. Error handling is a very complicated issue in any strategy but I imagine there are many big (expensive) holes to step in, in unmanaged strategies. Right?
                    Either Unmanaged or Managed will work for your items detailed earlier. However I must stress that you understand the difference between these Order Methods. Please review the information listed for both Unmanaged and Managed Order Methods at the following links:
                    I can use the IOrder form of the commands in both managed and unmanaged techniques?
                    Correct.
                    Please send me the link to one "unmanaged" full source example strategy. The tiny portions of code I have found are not enough to understand the differences in coding responsibilities between the two.
                    We do not have a reference sample detailing the use of the Unmanaged Approach. The Unmanaged approach is reserved for VERY EXPERIENCED programmers. In place of the convenience layer that the Managed approach offered, the Unmanaged approach instead offers ultimate flexibility in terms of order submission and management.

                    Please let me know if you have any questions.

                    Comment


                      #11
                      Originally posted by NinjaTrader_PatrickH View Post
                      Hello Gary,

                      Thank you for your response.

                      Either Unmanaged or Managed will work for your items detailed earlier. However I must stress that you understand the difference between these Order Methods. Please review the information listed for both Unmanaged and Managed Order Methods at the following links:

                      Correct.

                      We do not have a reference sample detailing the use of the Unmanaged Approach. The Unmanaged approach is reserved for VERY EXPERIENCED programmers. In place of the convenience layer that the Managed approach offered, the Unmanaged approach instead offers ultimate flexibility in terms of order submission and management.

                      Please let me know if you have any questions.
                      Thanks Patrick,

                      I think maybe I was not clear enough on my "intent" for this strategy.. I need to enter a "pool" of contracts at some entry criteria and exit different quantities for various reasons at any time until they are exhausted and I can look again for an entry point for a new "pool" of contracts.

                      I restructured my code to let the "managed" methods (EnterLong, EnterShort, and was just working on SetProfitTarget, SetStopLoss and SetTrailStop.. and can not find any way to let those functions know how many contracts I want to exit. Does this mean I MUST use the UNMANAGED approach (to get that level of control)?

                      Thanks again,

                      Gary

                      Comment


                        #12
                        You're basically just writing a state machine.

                        The sequence in which orders progress from state to state is well defined.

                        You can look at the Log window to see each state transition that the order goes through on its way to being filled.

                        Partial fills and cancellations do complicate things a little bit, because there is no easy way to test all possible scenarios. Overfills need to be addressed as well.

                        NT help and documentation does provide advice on where you should be doing certain things. For example, what to do in OnExecution vs. in OnOrderUpdate.

                        Comment


                          #13
                          Managing risk and trades....

                          Originally posted by BigWaveDave View Post
                          You're basically just writing a state machine.

                          The sequence in which orders progress from state to state is well defined.

                          You can look at the Log window to see each state transition that the order goes through on its way to being filled.

                          Partial fills and cancellations do complicate things a little bit, because there is no easy way to test all possible scenarios. Overfills need to be addressed as well.

                          NT help and documentation does provide advice on where you should be doing certain things. For example, what to do in OnExecution vs. in OnOrderUpdate.
                          Thanks BigWaveDave,

                          I am familiar with state machines, and actually find this a lot of fun.. but when I look in the NinjaTrader help files and it says "EXAMPLES" and it never has more than one small code sample... I find it lacking and frustrating. I have been a programmer for decades but only recently using C#.

                          I was asking if there is a way to SetStop or SetTarget with a number of contracts... that is all I need right now.. structure of the program is good.. overall state engine works.... after it works with normal orders I will figure out what to do about partial fills and cancellations.

                          If there is no way to set a real time stop and target.. with a specific number of contracts.. It looks like the only way to get the granularity I need is to go unmanaged.. (with so few code examples I think NT is trying to keep people from doing it).

                          Thanks.. again.

                          Comment


                            #14
                            Originally posted by GaryGuy View Post
                            Thanks Patrick,

                            I think maybe I was not clear enough on my "intent" for this strategy.. I need to enter a "pool" of contracts at some entry criteria and exit different quantities for various reasons at any time until they are exhausted and I can look again for an entry point for a new "pool" of contracts.

                            I restructured my code to let the "managed" methods (EnterLong, EnterShort, and was just working on SetProfitTarget, SetStopLoss and SetTrailStop.. and can not find any way to let those functions know how many contracts I want to exit. Does this mean I MUST use the UNMANAGED approach (to get that level of control)?

                            Thanks again,

                            Gary
                            The cost of convenience. Part of that cost is that the Set() methods are "all or nothing". Try using the Exit Methods: they do allow partials. Lookup ExitLong, ExitLongLimit, ExitLongStop et.c., in the NT Help.

                            ref: http://www.ninjatrader.com/support/h...er_methods.htm
                            Last edited by koganam; 02-22-2013, 10:36 AM. Reason: Corrected capitalization.

                            Comment

                            Latest Posts

                            Collapse

                            Topics Statistics Last Post
                            Started by DJ888, 04-16-2024, 06:09 PM
                            4 responses
                            12 views
                            0 likes
                            Last Post DJ888
                            by DJ888
                             
                            Started by terofs, Today, 04:18 PM
                            0 responses
                            8 views
                            0 likes
                            Last Post terofs
                            by terofs
                             
                            Started by nandhumca, Today, 03:41 PM
                            0 responses
                            6 views
                            0 likes
                            Last Post nandhumca  
                            Started by The_Sec, Today, 03:37 PM
                            0 responses
                            3 views
                            0 likes
                            Last Post The_Sec
                            by The_Sec
                             
                            Started by GwFutures1988, Today, 02:48 PM
                            1 response
                            9 views
                            0 likes
                            Last Post NinjaTrader_Clayton  
                            Working...
                            X