Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Double Entry

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

    Double Entry

    During live trading, I entered into a short trade of 600 shares. But then the strategy got a long signal and it is suppose to sell the 600 shares and buy another 600 shares. However, it bought 1200 shares instead.

    On the chart, it showed that 600 shares was "Buy to cover" but the weird thing is there was another 600 shares which showed "Close position". Which I think could be why the strategies bought the extra 600 shares.

    Why would there be 2 order to exit the trade? And what should I add in my code to prevent this from happening again?

    #2
    tjendra, please ensure your account and strategy positions are in sync with eachother - http://www.ninjatrader-support.com/H...tPosition.html

    Comment


      #3
      Originally posted by NinjaTrader_Bertrand View Post
      tjendra, please ensure your account and strategy positions are in sync with eachother - http://www.ninjatrader-support.com/H...tPosition.html
      Of coz they are in sync with each other. I started with both account and strategy position flat. That's why I dont understand why this happened.

      Also, this is an intraday strategy which exit at close, so there shouldn't be any carry over positions in the strategy.

      What is the difference between a "Close position" order and a "Sell" or "Buy to cover" order? How are these 2 different orders evoke in the strategy? e.g. When I have an ExitLong() in my strategy, would a "Sell" order be used or a "Close position" order be used? In what circumstances would both occurs at the same time?
      Last edited by tjendra; 05-06-2009, 01:40 PM.

      Comment


        #4
        Sounds like you may have overfills. Possible if you have multiple exit orders in place working at the same time for the same position.

        ExitLong() will generate Sell orders. "Close position" orders are attached to reversal orders. If you were long and you went EnterShort() you will see a "Close position" order. The vice versa is true too.

        I suggest you use TraceOrders = true to follow your trades. You may also want to add Print() to follow the position sizing and direction throughout your code.
        Josh P.NinjaTrader Customer Service

        Comment


          #5
          Originally posted by NinjaTrader_Josh View Post
          Sounds like you may have overfills. Possible if you have multiple exit orders in place working at the same time for the same position.

          ExitLong() will generate Sell orders. "Close position" orders are attached to reversal orders. If you were long and you went EnterShort() you will see a "Close position" order. The vice versa is true too.

          I suggest you use TraceOrders = true to follow your trades. You may also want to add Print() to follow the position sizing and direction throughout your code.
          How would I be able to resolve this if my ExitLong() and my reversal falls in the same time which would generate the overfills problem you mentioned. What can I change to the code to catch such issues? Shouldn't the strategy cancel either one of the orders automatically? In backtesting, this never happens.

          The weird thing is when I reload the historical data and the strategy, it doesn't show or execute the overfills. It will just show the "Sell" order and doesn't have the "Close position" order. So it shouldn't be something wrong with the strategy code right? But with the way Ninjatrader handle this concurrent orders.

          Comment


            #6
            For sure overfills don't happen in backtesting because there is no possibility of inflight executions for both orders occurring at the same time. When you reload your chart it is the same thing as doing a backtest instead of real-time.

            You need to catch this in your code yourself by not submitting two orders simultaneously. If you have submitted one, do not submit the other. Reason NinjaTrader cannot catch this automatically is due to inflight executions. Normally it will cancel the extra order when one gets filled, but both can get filled at the same time and in those cases the extra order does not know the first was filled and thinks it can fill too so you end up with an overfill.
            Josh P.NinjaTrader Customer Service

            Comment


              #7
              I have to same double exit problem with my script. Never shows in backtesting.

              Comment


                #8
                Hi luckynat, welcome to our support forums - unfortunately this is expected as Josh stated below, inflight executions can occur in realtime only and are a risk of electronic trading in general, hopefully this helps explaining - http://www.ninjatrader-support.com/H...njaTrader.html

                Comment


                  #9
                  Originally posted by NinjaTrader_Josh View Post
                  For sure overfills don't happen in backtesting because there is no possibility of inflight executions for both orders occurring at the same time. When you reload your chart it is the same thing as doing a backtest instead of real-time.

                  You need to catch this in your code yourself by not submitting two orders simultaneously. If you have submitted one, do not submit the other. Reason NinjaTrader cannot catch this automatically is due to inflight executions. Normally it will cancel the extra order when one gets filled, but both can get filled at the same time and in those cases the extra order does not know the first was filled and thinks it can fill too so you end up with an overfill.
                  Any suggestion what I can change to the code to catch such issues? They fall on the same bar, so how do you catch one and ignore the other? I won't be able to test using the market position statement right since they are on the same bar?

                  Comment


                    #10
                    tjendra,

                    There is no way to catch it on a fast moving market. Your best bet is to see when one of your order changes either in OnOrderUpdate() or when your position changes in OnPositionUpdate(), but again, if the other order is already inflight there is nothing that can be done.

                    The time it takes for your brokerage to report back and let you know order A was filled is plenty of time for order B to be filled. If order B is already filled when you just received order A's state of being filled there is no opportunity for you to cancel order B. This is a risk of electronic trading and there is nothing that can really be done except not submitting orders bracketed so closely together.
                    Josh P.NinjaTrader Customer Service

                    Comment


                      #11
                      Originally posted by NinjaTrader_Josh View Post
                      tjendra,

                      There is no way to catch it on a fast moving market. Your best bet is to see when one of your order changes either in OnOrderUpdate() or when your position changes in OnPositionUpdate(), but again, if the other order is already inflight there is nothing that can be done.

                      The time it takes for your brokerage to report back and let you know order A was filled is plenty of time for order B to be filled. If order B is already filled when you just received order A's state of being filled there is no opportunity for you to cancel order B. This is a risk of electronic trading and there is nothing that can really be done except not submitting orders bracketed so closely together.
                      But isn't the processing sequential? Are there any functions to test whether a working order was send out before I enter into the logic of the actual strategy in the same bar?

                      Comment


                        #12
                        tjendra,

                        This is a critical point you need to understand. There is no sequentialness about orders already placed to the market. We do not govern when they get filled. Whatever happens on the exchange is out of our control. If one gets filled and the second gets filled a microsecond later, while your brokerage's report for that first fill is coming through the internet to your computer there was zero possibility for NinjaTrader to submit a cancel on the second order. There is nothing that can be done to prevent inflight executions except to minimize the chances of them by not submitting orders bracketed so closely.

                        Any order you submit is sent out once it passes the pending submit state. You can check order states through the use of IOrder objects. Even with states, there is nothing you can do against inflight executions. The time it takes you to get info on an order being filled is long enough for your second order to fill without a chance to cancel.
                        Josh P.NinjaTrader Customer Service

                        Comment


                          #13
                          Originally posted by NinjaTrader_Josh View Post
                          tjendra,

                          This is a critical point you need to understand. There is no sequentialness about orders already placed to the market. We do not govern when they get filled. Whatever happens on the exchange is out of our control. If one gets filled and the second gets filled a microsecond later, while your brokerage's report for that first fill is coming through the internet to your computer there was zero possibility for NinjaTrader to submit a cancel on the second order. There is nothing that can be done to prevent inflight executions except to minimize the chances of them by not submitting orders bracketed so closely.

                          Any order you submit is sent out once it passes the pending submit state. You can check order states through the use of IOrder objects. Even with states, there is nothing you can do against inflight executions. The time it takes you to get info on an order being filled is long enough for your second order to fill without a chance to cancel.
                          Josh, I am not talking about the state of filled orders. I am just wondering if I can check when the orders are send out?

                          E.g. I am currently long with 500 shares. My ExitLong condition is triggered, so a "Sell" order is being send to my broker right? At this point in time, other codes within my strategy are not evaluated yet isn't it (that what I mean by sequential processing).

                          I am asking whether I can test for this "Sell" order. If I can test for this order being placed, I can then ask strategy to exit without evaluating the strategy further if it is true.

                          Comment


                            #14
                            As stated, you can see order status with IOrder objects.


                            The OrderState property is what is important to you.
                            Josh P.NinjaTrader Customer Service

                            Comment


                              #15
                              Originally posted by NinjaTrader_Josh View Post
                              As stated, you can see order status with IOrder objects.


                              The OrderState property is what is important to you.
                              Do you think testing OrderState.PendingSubmit will work as given in the example of my earlier post? Just trying to get your view since there is no way I will know whether this solution will work unless I encounter the same situation again in live trade. Thanks.

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by NullPointStrategies, Today, 05:17 AM
                              0 responses
                              50 views
                              0 likes
                              Last Post NullPointStrategies  
                              Started by argusthome, 03-08-2026, 10:06 AM
                              0 responses
                              126 views
                              0 likes
                              Last Post argusthome  
                              Started by NabilKhattabi, 03-06-2026, 11:18 AM
                              0 responses
                              69 views
                              0 likes
                              Last Post NabilKhattabi  
                              Started by Deep42, 03-06-2026, 12:28 AM
                              0 responses
                              42 views
                              0 likes
                              Last Post Deep42
                              by Deep42
                               
                              Started by TheRealMorford, 03-05-2026, 06:15 PM
                              0 responses
                              46 views
                              0 likes
                              Last Post TheRealMorford  
                              Working...
                              X