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

Race Conditions

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

    Race Conditions

    I think I experienced my first Race Condition this morning on a Strategy (Sim Account thankfully).

    My SL was triggered on the last or one of the last ticks on a bar while the Bar Close signaled another entry in the same direction. SL and TP are set via Iorder. The OCO Exit has always been canceled when the posion has been closed. End result was that I was left with one open position and NO TP or SL.

    So what is the official Ninja recommended way to avoid Race Conditions? And maybe from other members what techniques are you using.

    Thanks.

    #2
    TAJTrades,

    Not sure what your exact scenario is. You would want to make sure your positions are truly closed or cancelled in the OnOrderUpdate() method and that you have received all OnExecution() events for your orders before moving on to the next step.
    Josh P.NinjaTrader Customer Service

    Comment


      #3
      Josh,

      I will dig through the logs tonight. My guess is that the sequence is as follows:

      1) Stop was Hit very very close to the close of the bar
      2) When the bar closed it issued a Buy Order because previous entryOrder == Null
      3) Previous SL and TP were != Null so no additional SL and TP executed
      4) Stop came back filled and Canceled the TP on #1 above.
      5) Open trade #2 above with no SL or TP

      The Gremlins got me!

      It was very fast, probably happened within a handful of ticks. Anyway 200 + trades on the strategy and never had this happen before. Good to find out now on the Sim.

      Comment


        #4
        You should never set your entryOrder to null until you have successfully confirmed you have closed out of the position. Not just because you submitted a stop loss or because the entryOrder has been filled.
        Josh P.NinjaTrader Customer Service

        Comment


          #5
          please explain further Josh

          I copied this code from SampleOnOrderUpdate.cs from OnExecution method and am using in my cs. Is this wrong?


          // Resets the entryOrder object to null after the order has been filled or partially filled
          if (execution.Order.OrderState != OrderState.PartFilled)
          {
          entryOrder = null;
          }

          Comment


            #6
            It really depends on how you want your strategy to ultimately behave. If your only entry condition was based off of a null status or not you will definitely not want to reset it there. If you don't care much for it because you will not re-enter the strategy any time soon it may be safe to reset it at that point in time. It comes down to how your strategy is designed.

            In the reference because EntriesPerDirection is 1 it also further adds an additional layer of protection from mishaps. Now, I have not fully investigated the case but in either situation, resetting to null only after you have closed the position is a better bet in all cases. It will give you that extra peace of mind.
            Josh P.NinjaTrader Customer Service

            Comment


              #7
              I have a similar problem exiting trades

              I enter and place a stop loss which I trail on conditions

              I also exit on other coded conditions (ie. that have nothing to do with the trailing stop)

              Often the trailing stop is hit at the same time as I do the coded exit...so I end up reversing uncovered

              My coded exit condition checks that the position is still long...however does not check if the original stop which I have been trailing has been hit.

              1. What can I check to make sure the trailing stop is not being processed at the same time as my coded exit on condition? ....

              2. or should I just try to tighten the trailing stop to make sure there is only one way to exit? This way I assume if the trailing stop is in process then the change will just be ignored without causing a problem

              3. If I try to tighten and the market is too close and the tighten order is refused, what will happen? i.e. will I be uncovered or will the stop remain in place?
              Last edited by ATI user; 12-12-2008, 11:46 AM.

              Comment


                #8
                1. There is no guarantee if you are running these so closely. Even with OCO orders managed by your brokerage there are no guarantees let alone non-OCO orders. The only safe bet you can try is instead of using a ExitLongStop() for your stop loss you submit market orders right when your strategy decides it is time to exit. Ultimately stop orders become market orders anyway so there should be no need for concern there. What you want to do is before you submit this ExitLong() order you want to go and check the OrderStatus of your trailing stop order. If it is Working then you want to cancel the order. Until you receive cancellation confirmation you should not submit ExitLong(). Once you receive a successful order cancellation then you can finally submit your ExitLong(). Risks involved = you may be naked and exposed for some time between trailing stop cancellation and market order submission. So I guess after writing this out even this approach is not 100%.

                2. That does not solve your overfill concerns. You can still get overfilled no matter how you tighten your stops, but it will work to decrease the likeliness of such conditions.

                3. If you submit an invalid order by default your strategy will stop. It will close all open positions and cancel all working orders and stop your strategy.
                Josh P.NinjaTrader Customer Service

                Comment


                  #9
                  looks like quick solution is to just stick with trailing stop and not do exits on conditions also

                  will see if I get a condition wherein the trail overfills

                  in my other code, that uses ATMStrategyCreate and either coded trailing stops or ATMs, I never had this problem...wonder why

                  thanks Josh
                  Last edited by ATI user; 12-12-2008, 12:48 PM.

                  Comment


                    #10
                    ATM managed orders are in their own world.
                    Josh P.NinjaTrader Customer Service

                    Comment


                      #11
                      Josh

                      I have yet to have an overfill using AtmStrategyCreate code. Typically I would enter a trade with an ATM trailing stops or skip the Stop Strategy and have the code trail the stops. On certain conditions I would close the strategy using AtmStrategyClose.

                      Was I just lucky or are ATM managed orders such that you can not get overfills? ...perhaps because on closing the strat everything is flattened?

                      I am thinking of going back to that code. I went away from it because I could not get it to run in StrategyAnalyzer. Now I am thinking maybe it is preferable.

                      Thanks.

                      Comment


                        #12
                        Overfills are a reality in any type of order handling you use. Especially on tight stops and fast moving markets. It is a fundamental concern for all electronic trading. It is possible to get overfills just from orders you directly place to the brokerage, let alone through a trading platform. You just need to be mindful of the possibility.
                        Josh P.NinjaTrader Customer Service

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by ETFVoyageur, 05-07-2024, 07:05 PM
                        17 responses
                        133 views
                        0 likes
                        Last Post ETFVoyageur  
                        Started by ETFVoyageur, Yesterday, 10:13 PM
                        1 response
                        7 views
                        0 likes
                        Last Post ETFVoyageur  
                        Started by somethingcomplex, Yesterday, 10:36 PM
                        0 responses
                        7 views
                        0 likes
                        Last Post somethingcomplex  
                        Started by sofortune, 05-10-2024, 10:28 AM
                        5 responses
                        22 views
                        0 likes
                        Last Post sofortune  
                        Started by guyonabuffalo, Yesterday, 10:01 PM
                        0 responses
                        4 views
                        0 likes
                        Last Post guyonabuffalo  
                        Working...
                        X