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

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.
    Gaby V.NinjaTrader Customer Service

    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 ageeholdings, Today, 07:43 AM
        0 responses
        6 views
        0 likes
        Last Post ageeholdings  
        Started by pibrew, Today, 06:37 AM
        0 responses
        4 views
        0 likes
        Last Post pibrew
        by pibrew
         
        Started by rbeckmann05, Yesterday, 06:48 PM
        1 response
        14 views
        0 likes
        Last Post bltdavid  
        Started by llanqui, Today, 03:53 AM
        0 responses
        6 views
        0 likes
        Last Post llanqui
        by llanqui
         
        Started by burtoninlondon, Today, 12:38 AM
        0 responses
        12 views
        0 likes
        Last Post burtoninlondon  
        Working...
        X