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

OCO order is accepted by NinjaTrader, but is cancelled right away.

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

    OCO order is accepted by NinjaTrader, but is cancelled right away.

    I am trying to send an order with an additional two as a Target and Stop order. The original purchase executed perfectly. The others execute without an error but are canceled by NinjaTrader:

    Client myClient = new Client();
    int connect = myClient.Connected(1);

    Decimal StockPrice = (Decimal)getPrice;
    Decimal TakeProfit = StockPrice + (1.40M * ADR); ;
    Decimal StopLoss = StockPrice - ADR;

    DateTime foo = DateTime.Now;
    long unixTime = ((DateTimeOffset)foo).ToUnixTimeSeconds(); //Ensures that an original OCOID is always generated

    //Original Price
    int mainPosition = myClient.Command("PLACE", "Sim101", "MNQ 03-22", "BUY", 1, "MARKET", 0, 0, "GTC", unixTime.ToString(), "", "", "");

    //Take Profit Order
    int takeProfitPosition = myClient.Command("PLACE", "Sim101", "MNQ 03-22", "SELL", 1, "LIMIT", (double)TakeProfit, 0, "GTC", unixTime.ToString(), "", "", "");

    //Stop Loss Order
    int stopLossPosition = myClient.Command("PLACE", "Sim101", "MNQ 03-22", "SELL", 1, "STOPMARKET", 0, (double)StopLoss, "GTC", unixTime.ToString(), "", "", "");

    #2
    Hello techsigma,

    Thanks for your post.

    We are unable to reproduce the behavior you are reporting when testing submitting two orders tied by an OCO ID.

    Please see the video linked below demonstrating sending a Stop Market order and Limit order tied by an OCO ID.

    https://brandonh-ninjatrader.tinytak...M18xODk3NTUwOA

    Do you see that a cancellation request is being sent in the Log tab of the Control Center?

    Is there an order being rejected?

    Could you tell us more about what you are doing to place the orders?

    I look forward to further assisting.
    Brandon H.NinjaTrader Customer Service

    Comment


      #3
      Hello, a follow up on this topic - is it possible to submit 2 OCO orders using same approach you have shown in video - but both of these orders would have different order quantity? For example, I want to have "Stop" and "Target" orders linked through OCO. Then I want to scale in into position, and as my position size increases - I want to update existing two "OCO" orders with the new quantity. Is it possible to do? Could you demonstrate using your tool to submit and then modify these two orders with different quantities while retaining original OCO ID?

      Also Brandon, wondering if you could share your small utility / VS solution so that I could also perform some tests there...

      Thanks a lot!
      Last edited by music_p13; 02-08-2022, 04:12 PM.

      Comment


        #4
        Hello music_p13,

        Thanks for your note.

        It is likely that it would be possible to submit 2 OCO orders and have each order have a different order quantity.

        Support for the API is limited. We would be able to submit bug reports for any incorrect behavior. We are not able to assist with any code in an application external to NinjaTrader.

        Below I am providing a publicly available link to the help guide that lists the functions available to the API.
        Help Guide NT8 API functions - http://ninjatrader.com/support/helpG..._interface.htm

        Please also refer to this forum post link for more information about the NinjaTrader API — https://ninjatrader.com/support/foru...art#post793626

        Note that the script used in my demonstration video is an internal script used for testing purposes and this script cannot be shared publicly.

        This thread will be open for other developers in the community to share their insight if anyone has tips for external application code.

        Let us know if we may assist further.
        Brandon H.NinjaTrader Customer Service

        Comment


          #5
          Thanks for the prompt reply. I placed 3 orders. The original purchase at market price. Then, the take profit and subsequent stop loss orders. I do this without using ATM Strategy because my trading rules use ATR, and I don't see a quick way of doing that through ATM Strategy. So, I use a C# program that has it internal algorithm to calculate all that, and then throw 3 orders to Ninja trader as seen below. The orders are not being rejected. They are received without a hitch, except for the cancellation of the SL and TP orders.


          This one below is the market order.
          int mainPosition = myClient.Command("PLACE", "Sim101", "MNQ 03-22", "BUY", 1, "MARKET", 0, 0, "GTC", unixTime.ToString(), unixTime.ToString() + "-1", "", "");

          I set the a limit order for the take profit
          //Take Profit Order
          int takeProfitPosition = myClient.Command("PLACE", "Sim101", "MNQ 03-22", "SELL", 1, "LIMIT", (double)TakeProfit, 0, "GTC", unixTime.ToString(), unixTime.ToString() + "-1", "", "");

          Stopmarket order for the stop loss
          //Stop Loss Order
          int stopLossPosition = myClient.Command("PLACE", "Sim101", "MNQ 03-22", "SELL", 1, "STOPMARKET", 0, (double)StopLoss, "GTC", unixTime.ToString(), unixTime.ToString() + "-1", "", "");

          Maybe would you mind sending me your code? I think maybe what I am doing wrong is sending the stop loss and take profit orders under the same order as the original. In your video, I only saw two.

          As per the log, here is the information that it has:

          Order='23b85bbd8c2d4a5583990da8b9d22da5/Sim101' Name='' New state='Cancelled' Instrument='MNQ 03-22' Action='Sell' Limit price=0 Stop price=-4.5 Quantity=1 Type='Stop Market' Time in force=GTC Oco='1644362079' Filled=0 Fill price=0 Error='No error' Native error=''

          Order='0d31ff13da904972b2667b48ba55d047/Sim101' Name='' New state='Cancelled' Instrument='MNQ 03-22' Action='Buy' Limit price=0 Stop price=0 Quantity=1 Type='Market' Time in force=GTC Oco='1644362079' Filled=0 Fill price=0 Error='No error' Native error=''

          Instrument='MNQ 03-22' Account='Sim101' Average price=14760.5 Quantity=1 Market position=Short Operation=Operation_Add

          Execution='3689743e038449a1a29337840b3fc7c6' Instrument='MNQ 03-22' Account='Sim101' Exchange=Default Price=14760.5 Quantity=1 Market position=Short Operation=Operation_Add Order='67e0af8b808e42d9b5c6bf4c97e03f2d' Time='2/8/2022 6:14 PM'

          Best Regards,

          Jaime

          Comment


            #6
            Hello techsigma,

            Thanks for your note.

            "Maybe would you mind sending me your code?"

            As stated in my previous post: the script used in my demonstration video is an internal script used for testing purposes and this script cannot be shared publicly.

            Support for the API is limited. We are NOT able to assist with any code in an application external to NinjaTrader.

            This thread will be open for other community members to share their insight on external application code.

            Let us know if we may assist further.
            Brandon H.NinjaTrader Customer Service

            Comment


              #7
              OK...can you at least tell me why the OCO orders are cancelled by Ninja Trader?

              Comment


                #8
                Hello techsigma,

                Thanks for your note.

                I am unable to determine why the orders are being canceled without testing and debugging the script.

                That said, to understand how orders are being placed and how the script is behaving, you would need to take debugging steps.

                Below is a link to a forum post that demonstrates debugging to understand the behavior of a script.
                https://ninjatrader.com/support/foru...121#post791121

                Note that in the support department at NinjaTrader it is against our policy to create, debug, or modify, code or logic for our clients. Further, we do not provide C# programming education services in our support. This is so that we can maintain a high level of service for all of our clients as well as our partners.

                Let us know if we may assist further.
                Brandon H.NinjaTrader Customer Service

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by funk10101, Yesterday, 09:43 PM
                1 response
                13 views
                0 likes
                Last Post NinjaTrader_Gaby  
                Started by TheWhiteDragon, 01-21-2019, 12:44 PM
                5 responses
                551 views
                0 likes
                Last Post NinjaTrader_ChelseaB  
                Started by rtwave, 04-12-2024, 09:30 AM
                5 responses
                37 views
                0 likes
                Last Post NinjaTrader_ChelseaB  
                Started by funk10101, Today, 12:02 AM
                1 response
                11 views
                0 likes
                Last Post NinjaTrader_LuisH  
                Started by GLFX005, Today, 03:23 AM
                1 response
                6 views
                0 likes
                Last Post NinjaTrader_Erick  
                Working...
                X