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

Out of order processing in Market Replay?

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

    Out of order processing in Market Replay?

    OnExecutionUpdateHello NT Support,

    I'm seeing a bizarre sequence of events in Market Replay. Here's my entry code:

    EntrySignalName​, TargetPrice​, and StopLossPrice​ are my own variables.

    SetProfitTarget(EntrySignalName, CalculationMode.Price, TargetPrice, false);
    SetStopLoss(EntrySignalName, CalculationMode.Price, StopLossPrice, false);
    EnterLong(Quantity, EntrySignalName);


    ​Here's the scenario of the price on the chart, along with relative positions of the orders.

    Click image for larger version

Name:	image.png
Views:	164
Size:	9.3 KB
ID:	1249423

    I understand that using a market order means that the order may be filled at any price. In the diagram above, the yellow arrow is simply the expected price (give or take a few ticks) for getting filled, that is, near the close of the green bar / open of the new bar. This happens 99% of the time. However, when the next bar is news event, the order could get filled out of the expected range.

    In this situation, the order is filled below the range. This results in the Stop Loss and Profit Target orders getting rejected. The Stop Loss is at an invalid price, so it is rejected. Once the Stop Loss order is rejected, the Profit Target order is rejected due to the OCO connection between the two.

    Understanding this scenario, I set Error Handling to RealtimeErrorHandling = RealtimeErrorHandling.StopCancelCloseIgnoreRejects and have code to handle the rejected orders.

    Here is the sequence I'm seeing in my OnOrderUpdate and OnExecutionUpdate:
    1. OnOrderUpdate
      1. Order State is Rejected
      2. Stop Loss order is identified and associated with the Entry Signal order
      3. Exit Long is immediately called with an Exit Signal Order associated with the Entry Signal order
    2. OnOrderExecution
      1. Exit Long is identified and filled with the Exit Signal Order set in Step 1.3 above.
    3. OnOrderUpdate
      1. Order State is Rejected
      2. Profit Target order is identified and associated with the same Entry Signal order in 1.1 above.
      3. Since the original order was exited this order rejection is ignored.
    4. OnOrderExecution
      1. Order State is Filled
      2. Order is identified at the same Entry Signal order from 1.2 above
    So, the Stop Loss order is being rejected, processed and the Exit Order is filled before the actual entry order is filled. My understanding is that the Stop Loss and Profit Target are submitted once the entry order is filled.

    Here's the trace from the sequence:
    2023-03-31 8:30:01 AM Strategy 'RyanPivotor01/291669901': Entered internal SetStopTarget() method: Type=Target FromEntrySignal='eL-1t1-0048' Mode=Price Value=13089.5 IsSimulatedStop=False IsMarketIfTouched=False
    2023-03-31 8:30:01 AM Strategy 'RyanPivotor01/291669901': Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='eL-1t1-0048' Mode=Price Value=13059.5 IsSimulatedStop=False IsMarketIfTouched=False
    2023-03-31 8:30:01 AM Strategy 'RyanPivotor01/291669901': Entered internal SubmitOrderManaged() method at 2023-03-31 8:30:01 AM: BarsInProgress=0 Action=Buy OrderType=Market Quantity=1 LimitPrice=0 StopPrice=0 SignalName='eL-1t1-0048' FromEntrySignal=''
    2023-03-31 8:30:01 AM Strategy 'RyanPivotor01/291669901': Entered internal SubmitOrderManaged() method at 2023-03-31 8:30:01 AM: BarsInProgress=0 Action=Sell OrderType=Market Quantity=0 LimitPrice=0 StopPrice=0 SignalName='xL-1t1-0048' FromEntrySignal='eL-1t1-0048'
    2023-03-31 8:30:01 AM Strategy 'RyanPivotor01/291669901': Entered internal SetStopTarget() method: Type=Target FromEntrySignal='eL-1t1-0048' Mode=Price Value=13085.75 IsSimulatedStop=False IsMarketIfTouched=False
    2023-03-31 8:30:01 AM Strategy 'RyanPivotor01/291669901': Entered internal SetStopTarget() method: Type=Target FromEntrySignal='eL-1t1-0048' Mode=Price Value=13085.75 IsSimulatedStop=False IsMarketIfTouched=False


    Questions:
    ​Is that to be expected?
    Should I have the SetStopLoss and SetProfitTarget be in the OnOrderUpdate or OnExecutionUpdate and only be called when the entry order is filled?

    Thanks!
    Matt

    #2
    Hello Matt,

    Set methods are set (like a trigger) before the entry is submitted and will not be submitted until the entry fills. However, if you are supplying specific prices, you will need logic to ensure that the profit target for a long position is at least 1 tick above the bid and the stop loss for a long position is 1 tick below the bid.
    Hi I have a sellshort StopLimit order that i change if unfilled. I tick below the last bar's low. I keep getting the stop price can't be changed above the market but the price value it quotes ( the one I want) is below the market. Currently I am on the simulated feed. I appreciate in real market situations this could happen


    Below is a link to a working example that uses set methods named 'ProfitChaseStopTrailSetMethodsExample_NT8.zip'.

    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Thanks, NinjaTrader_ChelseaB,

      Yes, I am aware of what you shared and looked at that specific example before posting. My code is consistent with the sample. Also, I realize the SetStopLoss value was invalid and that I would get a rejection.

      The issue is the order of rejection. It doesn't make any sense, or am I missing something?

      Thanks,
      Matt

      Comment


        #4
        Quick question... OnExecutionUpdate is effectively the same as OnOrderUpdate where OrderState == OrderState.Filled or OrderState == OrderState.PartFilled. Is that a correct assessment?

        Comment


          #5
          Hello StealthM93,

          It would depend on if this is historical or real-time, as all order processing occurs simultaneously when the bar closes and could be a historical overfill.

          In real-time the entry would fill first, before the set method is submitted.

          Both OnOrderUpdate() and OnExecutionUpdate() will update when an order fills or part fills. OnExecutionUpdate() updates for the execution, which is a specific filled amount. The order updates for when there is an execution and the Filled amount changes or the orderState changes.
          Chelsea B.NinjaTrader Customer Service

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by lightsun47, Today, 03:51 PM
          0 responses
          4 views
          0 likes
          Last Post lightsun47  
          Started by 00nevest, Today, 02:27 PM
          1 response
          8 views
          0 likes
          Last Post 00nevest  
          Started by futtrader, 04-21-2024, 01:50 AM
          4 responses
          44 views
          0 likes
          Last Post futtrader  
          Started by Option Whisperer, Today, 09:55 AM
          1 response
          13 views
          0 likes
          Last Post bltdavid  
          Started by port119, Today, 02:43 PM
          0 responses
          8 views
          0 likes
          Last Post port119
          by port119
           
          Working...
          X