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

OCO to Market Order

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

    OCO to Market Order

    Hi,
    I am new to ninja script and migrated all my code from different platforms to NT7.
    I am planning to code for OCO order using SET methods. Need some help in understanding more details on these methods, I have gone through HELP related to these.
    Below are the possible cases that I need to handle in my script.
    1) Use the set methods to handle exits ( profit or stop), with this once my order gets filled I will be having profit and stop orders on the exchange.
    2) I have couple of flags, based on these flags I need to convert Profit target order leg to market order or move the profit target little away from the current profit target price.
    3) I am able to move the profit target away from the current profit target price by resetting the value.
    4) How do I convert the profit target leg to market order?
    Can you please point me to an example if available? is it possible to do this with out over fill when price is trading on the initial Profit target leg?
    Thanks
    GSREDDY

    #2
    1) Use the set methods to handle exits ( profit or stop), with this once my order gets filled I will be having profit and stop orders on the exchange.
    Generally, stop losses and profit targets exist on the exchange as stop orders and limit orders respectively.

    2) I have couple of flags, based on these flags I need to convert Profit target order leg to market order or move the profit target little away from the current profit target price.
    You can update your profit target and stop loss orders using NinjaScript. Depending on the complexity of your order management, you may need to use the unmanaged approach here.



    3) I am able to move the profit target away from the current profit target price by resetting the value.
    Are you asking if you are able to do so here?

    4) How do I convert the profit target leg to market order?
    Can you please point me to an example if available? is it possible to do this with out over fill when price is trading on the initial Profit target leg?
    Thanks
    Profit targets by default are limit orders, however you can always monitor these orders yourself and then submit a market order if your conditions are satisfied.

    We have a wealth of reference samples on how to do certain things here : http://www.ninjatrader.com/support/f...splay.php?f=30
    Adam P.NinjaTrader Customer Service

    Comment


      #3
      Hi,
      Thanks for quick reply.
      I understand Profit targets by default are limit orders.
      Do I need to use cancelorder() method to cancel orders generated from profit and stop loss orders once I get the fill on market order to close the existing position?
      Thanks
      gsreddy

      Comment


        #4
        gsreddy,

        Generally, the managed approach handles this for you so cancelling orders isn't generally necessary.
        Adam P.NinjaTrader Customer Service

        Comment


          #5
          One more question related to orders.
          Sequence of events:
          1. Profit target is set ( limit order on the exchange is waiting to get filled)
          2. Modify the profit target order knowing it is not yet filled based on the current call in OnExecution.
          3. Pending profit target order got filled in the exchange before price change in 2 is applied in the exchange.

          What happens to the message passed to the exchange for price change in step2 ?
          Thanks
          gsreddy

          Comment


            #6
            gsreddy,

            In this case, the order change would be rejected.

            Please let me know if I may assist further.
            Adam P.NinjaTrader Customer Service

            Comment


              #7
              Thanks Adam for quick response. you are so quick.
              Need one more clarification related to order routing.
              I am planning to do the below connection setups on my NT7 running in my Home PC.
              Connections:
              1) Connect to IQfeed for data
              2) Connect to broker feed to route the orders
              If I my strategy submits a buy order how will it get routed to the exchange.
              Where can I get the more information related to order routing form my PC to exchange?
              Does my order need to go to Ninja server (not my PC) before it gets routed to broker or exchange?
              Thanks,
              gsreddy

              Comment


                #8
                Hello,

                There are no NinjaTrader order servers. All orders executed go directly to your broker and then to the exchange. We do not have any documentation on this area as it will depend on the technology you're using. Your broker's IT department may be able to give you the information you're requesting.
                MatthewNinjaTrader Product Management

                Comment


                  #9
                  hi,
                  Thanks for the help and suggestions. If I make the below changes to the existing OCO order resting in the exchange should not over fill theoretically. I haven’t coded for this yet, I am still in the design and learning phase. If anyone has used this method for filling please through some light on any issues faced.

                  Sequence of steps.
                  1. Use the SET methods to set profit target and stop loss
                  2. Enter the position in OnMarketData based on the flags set in on bar update
                  3. Once the position is filled exchange will have working initial profit target and stop orders
                  4. Based on few conditions verified in OnBarUpdate, need to set the flags to move profit target or close the position at market
                  5. Case1: once the conditions are met to increase PT without closing position
                  • In OnExecution check for any fill or partial fill @ initial PT
                  • If there is no fill call the setprofittarget with new price. This will change the PT to new price
                  • If the position gets filled before updating the price in exchange, order(new change) will get rejected
                  Case2: conditions are met to close the existing position @ market
                  • In OnExecution check for any fill or partial fill @ initial PT and collet order status and Profit target price into variables
                  • If the order is not completely filled use the set profit target method to move the PT way far from market (may be 20 ticks)
                  • In OnOrderUpdate check for change in the order status and price comparing the data collected in the above steps and issue the market order to close the position.
                  6. If the position is flat and there is no working orders in the exchange, rest the initial profit and stop loss to initial values
                  As per the information got from you, we don’t need to cancel the pending PT and stop loss orders in the exchange. Ninja should cancel these orders once the position goes to flat.
                  Can you please point me if I am missing anything in this design that needs to be taken care?
                  Are there any drawdowns in this approach? I really need second look in this design since this is the first time I am trying to do something in this area of scripting.
                  Thanks
                  gsreddy

                  Comment


                    #10
                    Hello,

                    Looks good.

                    I would change the following:

                    You had:

                    Case2: conditions are met to close the existing position @ market
                    • In OnExecution check for any fill or partial fill @ initial PT and collet order status and Profit target price into variables
                    • If the order is not completely filled use the set profit target method to move the PT way far from market (may be 20 ticks)
                    • In OnOrderUpdate check for change in the order status and price comparing the data collected in the above steps and issue the market order to close the position.

                    Change to:


                    In this scenario instead of moving the profit target away and submitting a second order (market close) to get out would be best to modify the profit target down to get filled instantly.

                    -Brett

                    Comment


                      #11
                      Hi,
                      Can you please conform below steps to get the market fill using setprofittarget?
                      Example with ES data to close Long open position:
                      1. enter long position @ 1395 with setprofittraget @ 1400 and setstoploss @ 1390
                      2. once the long entry is filled a limit order will be placed in the exchange @ 1400 for PT leg
                      3. market moved to 1398 and conditions are met to close the open long position @ market
                      4. change the setprofittarget using the below statement to close the position @ market
                      SetProfitTarget(“fromEntrySignal”,CalculationMode. Price, GetCurrentBid()-2*TickSize);
                      If I understand correctly SetProfitTarget is a Limit order, if we move the price below Current BID, will this order get filled @ best available price or will it get rejected as we are trying to place the SELL LIMIT order below the current bid. I am planning to use two ticks below the current market price to make sure the order will get filled in fast moving market conditions.
                      Thanks,
                      gsreddy.

                      Comment


                        #12
                        Hello,

                        It will not get rejected it will fill at best price. Its only stop orders you have to worry about that. I'm sure there is some limit on limit orders how far away from price you can go but technically you can go 10+ tickets under current market price and it will simply work as a market order filling you at best possible price.

                        Let me know if any further questions.

                        -Brett

                        Comment


                          #13
                          Hi,

                          Thanks for answering all the questions very quickly, this gives me the big boost to start coding my strategy to meet live trading conditions.

                          Thanks,
                          gsreddy

                          Comment


                            #14
                            Hi,
                            I have couple of questions related to managed order handling. Please correct me if my understand is wrong related sequence of actions that will happen in the below examples, also please provide the details if I miss any action in my understanding. This is very critical for my program design.

                            Example 1: Modify SetProfitTarget price or ticks
                            1. Initial profit target is set to 3 ticks in the initialize method
                            SetProfitTarget(CalculationMode.Ticks,3)
                            2. After entry order gets filled for quantity 10, Ninja will send the profit target limit order for 3 ticks
                            based on the initial SetProfitTarget.
                            3. Market condition has changed, need to convert profit target order to market order(close the open position).
                            4. Issued the SetProfitTarget(CalculationMode. Price, GetCurrentBid()-2*TickSize) for closing LONG open position and the SetProfitTarget(CalculationMode. Price, GetCurrentAsk()+2*TickSize) to close the SHORT position.

                            If the market moves 4 ticks in favor before the order from step 2(3 tick PT limit order) reaches exchange, will the profit target limit order gets executed with best price?

                            If I understand correctly, by modifying setprofittarget or setstoploss orders will not issue cancelorder, so I am NOT expecting any overfill but the order can get REJECTED if zero quantity is available in the exchange for modification. Is this correct?

                            In the event of step4, by issuing command to modify setprofittarget, expecting the below possible order state changes. Please correct me if I am wrong

                            Case1: all the 10 quantity is available for modification.
                            1. OrderState.PendingChange
                            2. OrderState.PendingSubmit
                            3. OrderState.Accepted or OrderState.Working
                            4. OrderState.PartFilled or OrderState.Filled

                            Case2: 10 quantity is available and issued the setprofirtarget to modify the price, quantity 2 got filled before the modification request reaches exchange. ( expecting price for quantity 8 or the unfilled quantity will get changed)
                            1. OrderState.PartFilled or OrderState.PendingChange (either once can come first)
                            3 OrderState.PendingSubmit
                            4 OrderState.Accepted or OrderState.Working
                            5 OrderState.PartFilled or OrderState.Filled

                            Case3: 10 quantity is available and issued the setprofirtarget to modify the price, quantity 10 got filled before the modification request reaches exchange. (all the open position quantity got executed in the exchange after issuing modification request and before it reaches the exchange )

                            1. OrderState.Filled or OrderState.PendingChange (either once can come first)
                            3 OrderState.PendingSubmit
                            4 OrderState.Rejected

                            please provide me the case where an overfill can occur. Also please include if I miss any possible order state changes.

                            thanks,
                            gsreddy,

                            Comment


                              #15
                              If the market moves 4 ticks in favor before the order from step 2(3 tick PT limit order) reaches exchange, will the profit target limit order gets executed with best price?
                              Yes up to the limit price of the order.

                              If I understand correctly, by modifying setprofittarget or setstoploss orders will not issue cancelorder, so I am NOT expecting any overfill but the order can get REJECTED if zero quantity is available in the exchange for modification. Is this correct?
                              Correct.



                              Not sure what you exactly are asking with the order state definitions but yes these are all order states you can expect when modifying orders.


                              please provide me the case where an overfill can occur. Also please include if I miss any possible order state changes.

                              Since you are moving the profit target down to fill vs submitting another market order to close you do not run into overfill issue on the profit target side. As far as the stop loss side the only scenario for overfill I see is if the stop loss and profit target get filled at the same time (due to the milliseconds it takes for OCO to cancel the other side) the chance of this happening depends on how volatile the market and how close your profit target and stop is.

                              -Brett

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by firefoxforum12, Yesterday, 08:53 PM
                              1 response
                              13 views
                              0 likes
                              Last Post NinjaTrader_BrandonH  
                              Started by kmunroe478, Yesterday, 05:39 PM
                              2 responses
                              14 views
                              0 likes
                              Last Post NinjaTrader_Jesse  
                              Started by kevinenergy, 02-17-2023, 12:42 PM
                              115 responses
                              2,699 views
                              1 like
                              Last Post kevinenergy  
                              Started by prdecast, Today, 06:07 AM
                              1 response
                              4 views
                              0 likes
                              Last Post NinjaTrader_LuisH  
                              Started by Christopher_R, Today, 12:29 AM
                              1 response
                              14 views
                              0 likes
                              Last Post NinjaTrader_LuisH  
                              Working...
                              X