Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Strategy stop loss orders being left on after position is closed.

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

    Strategy stop loss orders being left on after position is closed.

    Hello, I have an issue in a strategy I have programmed where when the strategy exits a position it is leaving the stop loss order active. I tried coming up with something for canceling any open orders whenever the position is flat but today it didnt work and the stop loss order was left on after the strategy closed the position. This is what I had come up with:

    private void OnOrderUpdate()

    if (Position.MarketPosition == MarketPosition.Flat)

    {
    CancelOrders();
    }

    private void CancelOrders()
    {
    foreach (var order in Orders)
    {
    if (order.OrderState == OrderState.Working)
    {
    CancelOrder(order);
    }
    }
    }

    Is there a way to get this to work or is there another way I can get it to cancel all pending orders once position is flat?
    Thanks

    #2
    Hello RaygunWizzle,

    Thank you for your post.

    If your inquiry involves live orders, please always reach out to your broker's Orders Desk immediately to confirm and manage your live orders and positions.

    If your trade(s) are with NinjaTrader Brokerage, you can contact NinjaTrader Brokerage Order Desk by emailing [email protected].

    Please send me your log and trace files so that I may look into what occurred.

    You can do this by going to the Control Center-> Help-> Email Support

    Ensuring 'Log and Trace Files' is checked will include these files. This is checked by default.

    Please reference the following ticket number in the body of the email: Case Number: 04218219 ATTN Gaby

    Additionally, please include the following info in the email:
    • What Order ID was assigned to this order? You can check in the Executions or Orders tab of the Control Center.
    • Do you receive an error on screen? Are there errors on the Log tab of the Control Center? If so, what do these errors report?



    Please also note is a CancelOrder() NinjaScript method:



    However, set method orders cannot be canceled with CanceOrder(). So if the stop loss was submitted with SetStopLoss(), unfortunately you would not be able to cancel it this way.

    Thanks in advance; I look forward to your response and helping to resolve this item.

    Comment


      #3
      I have had the problem as well when position is closed programmatically (not by trailing stop), sometimes the trailer will then execute as well, reversing my position instead of closing it. I even added code in there to exit if that happens.. and most of the time it works... one time it did not (market replay). It's intermittent and hard to reproduce.

      Comment


        #4
        I fixed it by giving the stoploss orders a unique ID then when position is flat it cancels any orders with that ID. Looks like this:
        under OnBarUpdate if (Position.MarketPosition == MarketPosition.Flat)
        {
        currentStop = 0;
        foreach (var order in Orders)
        { if (order.Name == "PositionStop" && order.OrderState == OrderState.Working)
        {
        CancelOrder(order);
        }
        }
        }


        ​and under OnExecutionUpdate
        if (execution.Order.OrderState == OrderState.Filled && execution.MarketPosition == MarketPosition.Long)
        { if (currentStop == 0)
        {
        ExitLongStopMarket(0, true, Position.Quantity, (Position.AveragePrice - 140), "PositionStop", "");
        }
        else if (currentStop > 0)
        { ExitLongStopMarket(0, true, Position.Quantity, currentStop, "PositionStop", "");
        }
        }
        Last edited by RaygunWizzle; 01-28-2024, 03:56 AM.

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by NullPointStrategies, Today, 05:17 AM
        0 responses
        49 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
        67 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