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

Unable to submit closing order.

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

    Unable to submit closing order.

    I have a strategy with a stop loss and profit target:

    Code:
                    SetProfitTarget(CalculationMode.Percent, 0.025);
                    SetStopLoss(CalculationMode.Percent, 0.01);​
    And a condition to close the order:

    Code:
                if (startTimeForLongExits < Times[0][0] && Times[0][0] <= endTimeForLongExits)
                    ExitLong();​
    I am running the strategy live againts TD Ameritrade.

    Everytime ExitLong gets called I get this error:

    2023-07-25 12:33:28:616 (TD AMERITRADE) Cbi.Account.OrderUpdateCallback: realOrderState=Rejected orderId='8765432' account='erikreiter 098765' name='Sell' orderState=Rejected instrument='XXXX' orderAction=Sell limitPrice=0 stopPrice=0 quantity=60 orderType='Market' filled=0 averageFillPrice=0 time='2023-07-25 12:33:28' statementDate='2023-07-25' error=NoError comment='This order may result in an oversold/overbought position in your account. Please check your position quantity and/or open orders.' nr=-1

    This is because the other the stop loss and profit target are real orders.

    How do I fix this?

    Thanks,
    Erik
    Attached Files

    #2
    Hello Erik,

    Thank you for your post.

    The issue is definitely because you have a working stop loss and profit target while also trying to exit with a market order. My colleague has suggested a solution to a similar inquiry in the following thread:
    Hi, I have multiple open positions and at the end of the day if they don't hit their respective Target/Stop, I would like cancel the linked OCO's and close the positions. I know TD Ameritrade makes things difficult, what would be the best way to do this? I've tried various ways but cant seem to get it to work. Any help would be


    It seems that you can use Account.Cancel to cancel the stop loss and profit target orders first, then once the cancellation is seen in OnOrderUpdate() you may submit the ExitLong() order.

    Please let us know if we may be of further assistance.
    Emily C.NinjaTrader Customer Service

    Comment


      #3
      Hi Emily,

      Thanks I will look at the post.

      From the documentation:
      • If a stop loss or trail stop order is generated in addition to a profit target order, they are submitted as OCO (one cancels other)
      • A profit target order is automatically cancelled if the managing position is closed by another strategy generated exit order

      It seems like I can

      Change the SetStopLoss to:

      Code:
      //Pass true as last argument.
      SetStopLoss("", CalculationMode.Percent, this.StopLoss / 100.00, true);
      This should solve the problem too, since the Stop Loss will not be a real order, and the profit targetr will get handled???

      Thanks,
      Erik

      Comment


        #4
        Hello Erik,

        Thank you for your reply.

        You could test out the overload for isSimulatedStop set to true and see if this suits your needs. You may also need to consider the internal order handling rules that reduce unwanted positions; these rules are not able to apply to market orders. The rules may be found here:


        Please let us know if we may be of further assistance.
        Last edited by NinjaTrader_Emily; 07-26-2023, 07:21 AM.
        Emily C.NinjaTrader Customer Service

        Comment


          #5
          Hi Emily, The link doesn't work for me.

          Comment


            #6
            Originally posted by ErikHR1969 View Post
            Hi Emily, The link doesn't work for me.
            Thank you for letting me know. I have edited the link to lead to the "Managed Approach" help guide page. Please expand the header "Internal Order Handling Rules that Reduce Unwanted Positions" in order to view the internal rules I was referring to.

            Please feel free to reach out with any additional questions or concerns.
            Emily C.NinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by aligator, 01-06-2022, 12:14 PM
            4 responses
            233 views
            0 likes
            Last Post john_44573  
            Started by reynoldsn, Today, 05:56 PM
            0 responses
            4 views
            0 likes
            Last Post reynoldsn  
            Started by bortz, 11-06-2023, 08:04 AM
            51 responses
            1,989 views
            0 likes
            Last Post aligator  
            Started by dmking, 11-12-2019, 12:31 PM
            4 responses
            4,150 views
            0 likes
            Last Post jasonw
            by jasonw
             
            Started by roblogic, Today, 04:31 PM
            0 responses
            10 views
            0 likes
            Last Post roblogic  
            Working...
            X