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

Bracket OCO Management

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

    Bracket OCO Management

    Hello,

    I would like some clarification on the topic of order management. I would like update my order variables (for the entry order, profit target, and stoploss) when my orders are filled, not when I first submit the orders.
    Please have a look at my general logic to let me know if I am headed in the right direction.

    Note: I did not define all variables here for simplicity, but I can provide executable code if necessary.

    Code:
    protected override void OnBarUpdate()
    {
    	if (condition == true)
    	{
    		// Submit three orders to form a bracket OCO. These will trigger OnBarUpdate to be called.
                    // I do not want to assign my class variables (_entryOrder, profitTarget, _stopLoss) here partly because they may not be internally assigned / filled yet.
    		string name = "Long Oco";
    		EnterLong(quantity, name);								// Long entry					
    		ExitLongLimit(0, true, quantity, profitTarget, name, name);		// Profit target order
    		ExitLongStopMarket(0, true, quantity, stopLoss, name, name);	// Stoploss order
    	}
    }
    
    
    protected override void OnOrderUpdate()
    {
    	// Three orders were submitted as OCO (entry, profit target, stop loss).
    	// I want to assign my order variables to the proper order now that they are filled/accepted/working
    	// Question: Is OnOrderUpdate() called for each of these orders? Only for the EnterLong() order? Please explain.
    	if (order.OrderState == OrderState.Filled)
    	{
    		// The goal here is to know which of the orders above is handled in this OrderUpdate. 
    		if (order.OrderType == OrderType.Limit || order.OrderType == OrderType.Market)	
    			_profitTarget = order;	// OrderUpdate is for the profit target
    		else if (order.OrderType == OrderType.StopLimit || order.OrderType == OrderType.StopMarket)
    			_stopLoss = order;		// OrderUpdate is for the stoploss
    		else
    			_entryOrder = order; 	// OrderUpdate is for the entry order.
    	}	
    }

    #2
    Hello jflaggs,

    It would be suggested you assign order objects outside of OnBarUpdate. I have attached a sample strategy which assigns an order object and submits a profit target and stop loss from within OnExecutionUpdate.

    Please see the following sections of our helpguide for additional reference,

    http://ninjatrader.com/support/helpG...tionupdate.htm .


    Please let us know if you need further assistance.
    Attached Files
    Alan P.NinjaTrader Customer Service

    Comment


      #3
      Hi Alan, Thanks for your reply.

      Is it not possible to submit all three orders at the same time, and assign the order objects at the same time in OnOrderUpdate() or OnExecutionUpdate(). I would prefer to do it this way so that I can see that all three of my orders are submitted together, and all three order objects are assigned in one place--rather than order objects and order commands being sprinkled throughout OnBarUpdate(), OnOrderUpdate(), and OnExecutionUpdate().

      When another developer looks at my code, they should see where all of my orders are placed and not have to search around to find where the stray order objects are being assigned. I hope this makes sense.

      As another rough example,

      Code:
      OnBarUpdate()
      {
      
      	if (condition == true)
      	{
      		// Submit orders *Together*. 
      		EnterLong(quantity, "Enter-Long");										// Long entry					
      		ExitLongLimit(0, true, quantity, profitTarget, "Profit-Target", "Enter-Long");		// Profit target order
      		ExitLongStopMarket(0, true, quantity, stopLoss, "Stop-Loss", "Enter-Long");	// Stoploss order
      	}	
      
              // Wait for confirmation from OnOrderUpdate() / OnExecutionUpdate() that order objects are all Filled or accepted/working
      	if (_entryOrder.State == Filled && _stopLoss.State == Working/Accepted && _profitTarget.State == Working/Accepted)
      	{
      		// I now know that objects are ready to reference. Do other things here.
      	}
      
      }
      
      OnOrderUpdate/OnExecutionUpdate()
      {
      	// Order objects should be assigned *together* in *one* location. 
              // Once the orders are ready to go, they can be assigned to order objects. Never sooner.
      	if (order.State == Filled && order.Name == "Enter-Long")
      		_entryOrder = order;
      	else if (order.State == Working/Accepted && order.Name == "Profit-Target")
      		_profitTarget = order;
      	else if (order.State == Working/Accepted && order.Name == "Stop-Loss")
      		_stopLoss = order;
      }

      This is pseudo code, but I hope my goal is clear about keeping order submissions and order object assignment in separate groups of code. This makes more sense to us than tracking down all the places where an order is placed or an order object is assigned.

      Thanks for your help!

      Comment


        #4
        Hello jflaggs,

        It is possible to submit three orders at one time however in your code example, you are submitting exit calls right after your entry call. It’s likely that no position exists yet immediately after your EnterLong call has been made as the order is sent to the exchange and comes back with a fill. I would suggest submitting your exit calls inn OnExecutionUpdate or OnOrderUpdate.

        ExitLongLimit and ExitLongStopMarket will be ignored if no position exits.

        Please let us know if you need further assistance.
        Alan P.NinjaTrader Customer Service

        Comment


          #5
          Got it. That's what I was missing. I wasn't aware that the oco would be ignored if the entry order is not working yet. Thanks for the information.

          I will move my oco orders to OnExecutionUpdate() and assign the order objects there as well. If I do that, I can assign the entry order object just fine since the order is filled at that time. Will submitting the oco orders (ExitLimit/ExitMarket) in OnExecutionUpdate() trigger a call of OnOrderUpdate()? How do you recommend that I ensure my oco orders are working/accepted so that I can assign my oco order objects?

          Again, my goal is to have all order objects null until the orders are accepted/working/filled.
          Last edited by jflaggs; 07-06-2017, 10:39 AM. Reason: clarification

          Comment


            #6
            Hello jflaggs,

            Yes, submitting orders within OnExecutionUpdate would trigger an OnOrderUpdate.
            If you were to submit the ExitLimit/Exit orders within OnOrderUpdate upon the entry order being filled, you could check if those cover orders have a status of accepted or working, at which point you could assign order objects to those cover orders.

            I put together a sample of what I’m suggesting above, except I submit the PT and SL in OnExecutionUpdate. If you apply the strategy to a 5 second chart with the output window open, you’ll see prints when the PT and SL are accepted/working.

            Please let us know if you need further assistance.
            Attached Files
            Alan P.NinjaTrader Customer Service

            Comment


              #7
              Beautiful. That implementation will work just fine even though order object assignment is somewhat scattered (we'll have to live with it). Thank you for the awesome support, Alan!

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by DJ888, 04-16-2024, 06:09 PM
              4 responses
              12 views
              0 likes
              Last Post DJ888
              by DJ888
               
              Started by terofs, Today, 04:18 PM
              0 responses
              7 views
              0 likes
              Last Post terofs
              by terofs
               
              Started by nandhumca, Today, 03:41 PM
              0 responses
              6 views
              0 likes
              Last Post nandhumca  
              Started by The_Sec, Today, 03:37 PM
              0 responses
              3 views
              0 likes
              Last Post The_Sec
              by The_Sec
               
              Started by GwFutures1988, Today, 02:48 PM
              1 response
              9 views
              0 likes
              Last Post NinjaTrader_Clayton  
              Working...
              X