Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

How would I use the code below to make it an OCO order?​

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

    How would I use the code below to make it an OCO order?​

    Code:
    Order originalOrder = e.Order;
    
    Order order = _receiverAccount.CreateOrder(
    originalOrder.Instrument,
    orderAction,
    orderType,
    originalOrder.TimeInForce,
    originalOrder.Quantity,
    originalOrder.LimitPrice,
    originalOrder.StopPrice,
    OcoId,
    "Entry",
    originalOrder.CustomOrder);​​
    this snippet code in private void OnOrderUpdate(object sender, OrderEventArgs e)

    I'm creating an indicator that copies orders from another account to replicate, but to do this I need to send a copy order of the original. The original uses an ATM Strategy order and I wanted to replicate the same entry.


    Last edited by gabrielrodrigues; 01-27-2025, 02:50 PM.

    #2
    Hello gabrielrodrigues,

    You would need to fill in the OCOid string with a valid OCO that is associated with another order for that to be possible. Keep in mind some brokers require that both orders of the OCO pair are submitted at the same time to be linked, if you get a rejection that may be the reason and you would see that as the rejection reason.

    Comment


      #3
      Yes, I agree and in the code snippet above, there is an OCOid, but when that order is filled, it adds a stop order and a limit order, but when one of them is filled, it doesn't close the other.

      Code:
       string OcoId = Guid.NewGuid().ToString("N");
      
       Order order = null;
      
      
      
      
       float ratio = 1.0f;
      
       int quantity = (int)(originalOrder.Quantity * ratio);
      
       OrderAction orderAction = originalOrder.IsLong ? OrderAction.Buy : OrderAction.Sell;
      
       OrderType orderType = originalOrder.IsLimit ? OrderType.Limit : originalOrder.IsMarket ? OrderType.Market : OrderType.StopMarket;
      
      
      
      
       order = _receiverAccount.CreateOrder(
      
       originalOrder.Instrument,
      
          orderAction,
      
          orderType,
      
          originalOrder.TimeInForce,
      
          originalOrder.Quantity,
      
          originalOrder.LimitPrice,
      
          originalOrder.StopPrice,
      
          OcoId,
      
          "Entry",
      
          originalOrder.CustomOrder);
      
      
      
      
       _receiverAccount.Submit(new[] { order });
      ​

      Comment


        #4
        Hello gabrielrodrigues,

        OCO has nothing to do with adding more orders, that would be due to your logic being driven by the order events. OCO is simply a link between two orders that the broker knows about, if two orders have the same ID when one fills or is cancelled the associated or linked order is also cancelled.


        Comment


          #5
          Right, I understood that only the id for OCO works, but when the order is sent, the default is an ATM Strategy, which doesn't define this in them, it only defines the stop and limit price. How do I set these values? See the image.


          When I cancel the ATM it works as expected, but the other one doesn't, it doesn't close the other orders.

          When I cancel, they stay loose and don't cancel. How can I set the ocoid, if I only set the limit price and stop price there?​
          Last edited by gabrielrodrigues; 01-27-2025, 04:13 PM.

          Comment


            #6
            Hello gabrielrodrigues,

            ATM's have internal logic to control targets and orders but are still sent as OCO, you don't supply an OCO to atms.

            If you mean by it doesnt cancel other accounts orders, that's correct. OCO only works per account. You cant link orders between accounts, your logic would have to cancel each individual order.

            Comment


              #7
              Right. Where can I find sample codes that send orders with atm strategy?

              Comment


                #8
                Hello gabrielrodrigues,

                There is a sample called SampleATMStrategy that comes with NinjaTrader, its in the strategies folder of the NinjaScript editor.

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                0 responses
                558 views
                0 likes
                Last Post Geovanny Suaza  
                Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                0 responses
                324 views
                1 like
                Last Post Geovanny Suaza  
                Started by Mindset, 02-09-2026, 11:44 AM
                0 responses
                101 views
                0 likes
                Last Post Mindset
                by Mindset
                 
                Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                0 responses
                545 views
                1 like
                Last Post Geovanny Suaza  
                Started by RFrosty, 01-28-2026, 06:49 PM
                0 responses
                547 views
                1 like
                Last Post RFrosty
                by RFrosty
                 
                Working...
                X