Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Best way to reverse position on stop out?

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

    Best way to reverse position on stop out?

    I am trying to add some logic to an existing strategy that will reverse the position once a stop loss order has been filled. I've overridden onExecutionUpdate and added the following, but it doesn't seem to work:



    protected override void OnExecutionUpdate(Cbi.Execution execution, string executionId, double price, int quantity, Cbi.MarketPosition marketPosition, string orderId, DateTime time)

    {

    if (marketPosition == MarketPosition.Flat) return; // do nothing if position is flat



    if (marketPosition == MarketPosition.Long && execution.Order.OrderAction == OrderAction.Sell) // if long position was stopped out

    {

    EnterShortLimit(IQ, Close[0]); // enter short position

    SetStopLoss(CalculationMode.Price, Close[0] + StopLossATR * ATR(40)[0] );

    }

    else if (marketPosition == MarketPosition.Short && execution.Order.OrderAction == OrderAction.Buy) // if short position was stopped out

    {

    EnterLongLimit(IQ, Close[0]); // enter long position

    SetStopLoss(CalculationMode.Price, Close[0] - StopLossATR * ATR(40)[0] );

    }

    }

    I am wondering if it's because the market position cannot be NOT flat and a stop loss order is hit at the same time. Is there something I'm missing, or a different way to approach this?
    jaybedreamin
    NinjaTrader Ecosystem Vendor - Zion Trading Algos

    #2
    Hello jaybedreamin,

    Always call Set methods before calling an entry method.


    Further, ensure that the price you are supplying for the stop price is valid. A buy stop must be 1 tick above the ask, a sell stop must be 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


    If you are going to reverse the position using the managed approach, just call the entry in the opposite direction. Having an exit and an entry fill at the same time will double the position in the opposite direction.


    Use prints if you want to understand the behavior. Print all of the values in the condition.


    With the check for the position being flat, I would suggest instead checking for the execution.Name to be the order name you are looking for.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Thanks for the reply Chelsea, always appreciated. I didn't know about this

      Always call Set methods before calling an entry method.
      I clicked the link you shared but couldn't really understand the context there, can you explain why this is necessary in simple terms? I've refactored my code to call these methods first now, but I want to understand why this is required.
      jaybedreamin
      NinjaTrader Ecosystem Vendor - Zion Trading Algos

      Comment


        #4
        Hello jaybedreamin,

        Certainly. The ProfitChaseStopTrailSetMethodsExample_NT8 example (the approach you are using), demonstrates dynamically setting the price of a stop using SetStopLoss().
        On lines 94 to 103 the set methods are called before the entry to set these to a valid price before the entry is submitted.

        The idea of a set method, is these are set and ready for an entry once the entry fills.

        Use Exit methods when using the managed approach if you want to submit the exits after the entry is placed (ProfitChaseStopTrailExitOrdersExample_NT8).
        Chelsea B.NinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by NullPointStrategies, Yesterday, 05:17 AM
        0 responses
        59 views
        0 likes
        Last Post NullPointStrategies  
        Started by argusthome, 03-08-2026, 10:06 AM
        0 responses
        134 views
        0 likes
        Last Post argusthome  
        Started by NabilKhattabi, 03-06-2026, 11:18 AM
        0 responses
        74 views
        0 likes
        Last Post NabilKhattabi  
        Started by Deep42, 03-06-2026, 12:28 AM
        0 responses
        45 views
        0 likes
        Last Post Deep42
        by Deep42
         
        Started by TheRealMorford, 03-05-2026, 06:15 PM
        0 responses
        50 views
        0 likes
        Last Post TheRealMorford  
        Working...
        X