Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

OCO orders

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

    OCO orders

    Hello,

    I would like to implement a strategy and I have some troubles with orders.
    Related to this I have some questions to more experienced guys.

    1) How to fill SellStopLimit order , when sygnal bar appears?
    2) How to implement the logic to fill OCO orders in the same manner as described in attached screen by Jessica


    Thank you in advance!

    --
    Andriy
    Attached Files

    #2
    Hello akushyn,

    Thank you for writing in.

    If you wish for a EnterShortStopLimit() order to be called only if a specific condition is true, you'll need to place the order within a condition. The help guide section for EnterShortStopLimit() provides an example of a condition that needs to be met in order for the order to be submitted: https://ninjatrader.com/support/help...tstoplimit.htm

    If you would like to have a stop loss and profit target automatically submit once your order has filled, you can utilize SetProfitTarget() and SetStopLoss():



    Please, let us know if we may be of further assistance.
    Last edited by NinjaTrader_ZacharyG; 03-29-2016, 02:54 PM.
    Zachary G.NinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_ZacharyG View Post
      Hello akushyn,

      Thank you for writing in.

      If you wish for a EnterShortStopLimit() order to be called only if a specific condition is true, you'll need to place the order within a condition. The help guide section for EnterShortStopLimit() provides an example of a condition that needs to be met in order for the condition to be true: https://ninjatrader.com/support/help...tstoplimit.htm

      If you would like to have a stop loss and profit target automatically submit once your order has filled, you can utilize SetProfitTarget() and SetStopLoss():



      Please, let us know if we may be of further assistance.
      Thank you for the quick response!

      Yes, I'm not new in programming. Thank you for your advise.
      I'm using condition to place the order in OnBarUpdate method.


      Code:
      if (sellEntryOrder == null && AkFractalChecker.Down(Low, 2))
      {
        BarColorSeries[2] = Color.Lime; // visualize
        sellEntryOrder =  EnterShortStopLimit(0, true, 1, Low[2] - 2 * TickSize, Low[2] - TickSize, SELL_ENTRY_ORDER_NAME);
      }
      Where should I manage these orders (SetStopLoss, SetProfitTarget) ?
      I'm trying to use ExitShortLimit & ExitShotStop like this:


      Code:
              protected override void OnOrderUpdate(IOrder order)
              {
      			
      			// Handle entry orders here. The entryOrder object allows us to identify that the order that is calling the OnOrderUpdate() method is the entry order.
      			if (buyEntryOrder != null && buyEntryOrder == order)
      			{	
      				Print(order.ToString());
      		        if (order.OrderState == OrderState.Filled)
      				{
      					ExitLongStop(0, true, 1, this.sygnal.Points.CorrectionPeak - TickSize, "Exit by Stop", BUY_ENTRY_ORDER_NAME);
      					ExitLongLimit(0, true, 1, this.sygnal.Points.ProfitTarget, "Exit by Profit", BUY_ENTRY_ORDER_NAME);
      				}
      
      				// Reset the buyEntryOrder object to null if order was cancelled without any fill
      				if (order.OrderState == OrderState.Cancelled && order.Filled == 0)
      					buyEntryOrder = null;
      			}
      
      			if (sellEntryOrder != null && sellEntryOrder == order)
      			{	
      				Print(order.ToString());
      		        if (order.OrderState == OrderState.Filled)
      				{
      					ExitShortStop(0, true, 1, this.sygnal.Points.CorrectionPeak + TickSize, "Exit by Stop", SELL_ENTRY_ORDER_NAME);
      					ExitShortLimit(0, true, 1, this.sygnal.Points.ProfitTarget, "Exit by Profit", SELL_ENTRY_ORDER_NAME);
      
      					//SetStopLoss(SELL_ENTRY_ORDER_NAME, CalculationMode.Price, this.sygnal.Points.CorrectionPeak + TickSize, false);
      					//SetProfitTarget(BUY_ENTRY_ORDER_NAME, CalculationMode.Price, );
      				}
      
      				// Reset the sellEntryOrder object to null if order was cancelled without any fill
      				if (order.OrderState == OrderState.Cancelled && order.Filled == 0)
      					sellEntryOrder = null;
      			}
      		}
      What is the difference between SetProfitTarget and ExitLongLimit approach?
      Last edited by akushyn; 03-29-2016, 02:56 PM.

      Comment


        #4
        Also I found one issue with this, that when the order placed , after next bar closed the order automatically canceled. And I can't get what is the problem?

        Any ideas?

        Comment


          #5
          Hello akushyn,

          You wouldn't be able to use Set() methods with those Exit() methods per the Internal Order Handling Rules for the Managed Approach: https://ninjatrader.com/support/help...d_approach.htm

          You would need to replace those Exit() methods with SetStopLoss() and SetProfitTarget().
          Zachary G.NinjaTrader Customer Service

          Comment


            #6
            Originally posted by NinjaTrader_ZacharyG View Post
            Hello akushyn,

            You wouldn't be able to use Set() methods with those Exit() methods per the Internal Order Handling Rules for the Managed Approach: https://ninjatrader.com/support/help...d_approach.htm

            You would need to replace those Exit() methods with SetStopLoss() and SetProfitTarget().
            Thank you for your answer and advise!
            I've replaced all as you recommended.



            Code:
                    protected override void OnOrderUpdate(IOrder order)
                    {
            			
            			// Handle entry orders here. The entryOrder object allows us to identify that the order that is calling the OnOrderUpdate() method is the entry order.
            			if (buyEntryOrder != null && buyEntryOrder == order)
            			{	
            				Print(order.ToString());
            		        if (order.OrderState == OrderState.Filled)
            				{
            					SetStopLoss(BUY_ENTRY_ORDER_NAME, CalculationMode.Price, this.sygnal.Points.CorrectionPeak - TickSize, false);
            					SetProfitTarget(BUY_ENTRY_ORDER_NAME, CalculationMode.Price, this.sygnal.Points.ProfitTarget);
            				}
            
            				// Reset the buyEntryOrder object to null if order was cancelled without any fill
            				if (order.OrderState == OrderState.Cancelled && order.Filled == 0)
            					buyEntryOrder = null;
            			}
            
            			if (sellEntryOrder != null && sellEntryOrder == order)
            			{	
            				Print(order.ToString());
            		        if (order.OrderState == OrderState.Filled)
            				{
            					SetStopLoss(SELL_ENTRY_ORDER_NAME, CalculationMode.Price, this.sygnal.Points.CorrectionPeak + TickSize, false);
            					SetProfitTarget(SELL_ENTRY_ORDER_NAME, CalculationMode.Price, this.sygnal.Points.ProfitTarget);
            				}
            
            				// Reset the sellEntryOrder object to null if order was cancelled without any fill
            				if (order.OrderState == OrderState.Cancelled && order.Filled == 0)
            					sellEntryOrder = null;
            			}
            		}
            The strategy place SellStopLimit order. After next one bar it automatically cancels by strategy. The question is why and how to eliminate the cancellation?


            --
            Best wishes, Andriy

            Comment


              #7
              Hello akushyn,

              Please note with the Managed Approach that EnterShortStopLimit() will need to be called on every bar or it will be cancelled.

              To get around this, you can use the following method variation. You'll want to make sure that bool liveUntilCancelled is set to true:

              Code:
              EnterShortStopLimit(int barsInProgressIndex, bool liveUntilCancelled, int quantity, double limitPrice, double stopPrice, string signalName)
              The only way the order will be cancelled is if CancelOrder() is called or its time in force has been reached.

              Please take a look at this help guide link for more information about this method variation: https://ninjatrader.com/support/help...tstoplimit.htm

              Please take a look at this help guide link for more information about the Managed Approach: https://ninjatrader.com/support/help...d_approach.htm

              By default, orders submitted via Entry() and Exit() methods automatically cancel at the end of a bar if not re-submitted
              Zachary G.NinjaTrader Customer Service

              Comment


                #8
                Originally posted by NinjaTrader_ZacharyG View Post
                Hello akushyn,

                Please note with the Managed Approach that EnterShortStopLimit() will need to be called on every bar or it will be cancelled.

                To get around this, you can use the following method variation. You'll want to make sure that bool liveUntilCancelled is set to true:

                Code:
                EnterShortStopLimit(int barsInProgressIndex, bool liveUntilCancelled, int quantity, double limitPrice, double stopPrice, string signalName)
                The only way the order will be cancelled is if CancelOrder() is called or its time in force has been reached.

                Please take a look at this help guide link for more information about this method variation: https://ninjatrader.com/support/help...tstoplimit.htm

                Please take a look at this help guide link for more information about the Managed Approach: https://ninjatrader.com/support/help...d_approach.htm
                Hello, Zachary
                Thank you for the answer! Very useful.

                1. What approach would you recommend me to use?
                How to recall placing order on each bar close to avoid cancellation?

                2. If I'll decide to use Managed Approach, what is the best place to manage CancelOrder() for IOrder object: in OnBarUpdate method or in OnOrderUpdate method ?

                3. I need only one way to cancel placed order EnterShortStopLimit: when order placed, then price higher high ZigZag high point as I showed in attached screen.

                --
                Best regards, Andriy
                Attached Files

                Comment


                  #9
                  Hello akushyn,

                  I cannot recommend what approach for you to use.

                  I would suggest taking a look at the Managed and Unmanaged Approach sections of the help guide for further information about how to program in each.

                  Managed Approach
                  Unmanaged Approach

                  With the Managed Approach, the order would need to be called again on the next OnBarUpdate() call. When using EnterShortStopLimit() for example, if this is called within a specific condition that is true on one OnBarUpdate() call, but not the next, the order will be cancelled as it wasn't called again since the condition wasn't true. This is assuming that you are not using the method overload with bool liveUntilCancelled as a parameter.

                  Where you use CancelOrder() would be up to you. Please note that you will only need to use this method if you wish to manually cancel an order.

                  You'll need a condition to check if the price has passed above the most recent price of the ZigZag high.

                  The ZigZag indicator section of the help guide provides a sampleof obtaining the most recent ZigZag high: https://ninjatrader.com/support/help...t7/?zigzag.htm
                  Zachary G.NinjaTrader Customer Service

                  Comment


                    #10
                    ...
                    2. If I'll decide to use Managed Approach, what is the best place to manage CancelOrder() for IOrder object: in OnBarUpdate method or in OnOrderUpdate method ?

                    ...
                    Thank you for answers 1 and 3.

                    I want you to clarify which method is best approach to use to Cancel Order?
                    * OnBarUpdate
                    * OnOrderUpdate
                    * smth else


                    --
                    Best regards, Andriy[/QUOTE]

                    Comment


                      #11
                      Hello akushyn,

                      Where to put your CancelOrder() would be up to you.

                      Think about these two questions:
                      • Do you want the CancelOrder() to be called on each tick/on the close of a bar (depending on your CalculateOnBarClose property)?
                      • Or, do you want the CancelOrder() to be called when an order managed by the strategy changes state?
                      Zachary G.NinjaTrader Customer Service

                      Comment


                        #12
                        Order rejection

                        I'm getting this error message: order is in a state that prohibits using it as a compound leg
                        My order filled and terminated itself.

                        Does anyone know what it means? Or can help solve the problem?

                        Also I'm using a VPS
                        Last edited by Gym_junki21; 04-12-2016, 08:51 AM.

                        Comment


                          #13
                          Hello Gym_junki21,

                          Thank you for writing in and welcome to the NinjaTrader Support Forum!

                          Please send me your log and trace files for today so that I may look into what occurred.

                          You can do this by going to the Control Center-> Help-> Mail to Platform Support.

                          Please reference a link to this thread and my name in the body of the email.
                          Zachary G.NinjaTrader Customer Service

                          Comment


                            #14
                            Originally posted by NinjaTrader_ZacharyG View Post
                            Hello akushyn,

                            Where to put your CancelOrder() would be up to you.

                            Think about these two questions:
                            • Do you want the CancelOrder() to be called on each tick/on the close of a bar (depending on your CalculateOnBarClose property)?
                            • Or, do you want the CancelOrder() to be called when an order managed by the strategy changes state?
                            Hello , Zachary

                            I've played a little with orders and decided to use the approach to manage orders state in OnExecution method and cancellation of orders in OnOrderUpdate.

                            Next step , what I would implement in my strategy is
                            * to find potential entry sygnal with CalculateOnBarClose=true property
                            * and to manage order changes on every tick.

                            What should I change in OnBarUpdate() & Initialize() method to do this?

                            Code now looks like this:
                            Code:
                                    protected override void OnBarUpdate()
                            		{
                            			if(CurrentBar < 5 || CurrentBar < BarsRequired)
                            					return;
                            			
                            			 	sygnalFound = calculate();
                            			
                            			
                            			if (Position.MarketPosition == MarketPosition.Flat)
                            			{
                            				// manage entryBuyOrder
                            				if (entryBuyOrder == null && sygnalFound && this.sygnal.PriorityDirrection == AkPriorityDirrection.Long)
                            				{
                            					BarColorSeries[2] = Color.Lime;
                            
                            					// place buy order : fractal bar high + ticksize
                            					entryBuyOrder = EnterLongStopLimit(0,true, 1, High[2] + 2 * TickSize, High[2] + TickSize, BUY_ENTRY_ORDER_NAME);
                            				}
                                                 }
                                      }

                            Comment


                              #15
                              Hello akushyn,

                              I cannot recommend how to create your strategy. You will need to determine what you would like to accomplish and create your code based on what you wish to accomplish.

                              Remember that with CalculateOnBarClose set to true, OnBarUpdate() will run at the close of every bar.

                              If you wish to find your entry signal on the close of a bar, you can do so in OnBarUpdate().

                              Despite CalculateOnBarClose set to true, you can utilize OnMarketData() if you wish to do real-time calculations. This method is called for every change in level one market data for the underlying instrument: http://ninjatrader.com/support/helpG...marketdata.htm

                              I would suggest the following references below for additional information on how to create your own strategies:

                              There are a few Sample Automated Strategies which come pre-configured in NinjaTrader that you can use as a starting point. These are found under Tools--> Edit NinjaScript--> Strategy. You will see locked strategies where you can see the details of the code, but you will not be able to edit (you can though always create copies you can later edit via right click > Save as)

                              We also have some Reference samples online as well as ‘Tips and Tricks’ for both indicators and strategies:
                              Click here to see our NinjaScript Reference Samples
                              Click here to see our NinjaScript Tips

                              These samples can be downloaded, installed and modified from NinjaTrader and hopefully serve as a good base for your custom works.

                              Further, the following link is to our help guide with an alphabetical reference list to all supported methods, properties, and objects that are used in NinjaScript.
                              Alphabetical Reference

                              We also have a few tutorials in our help guide for both Indicators and Strategies.
                              Indicator tutorials
                              Strategy tutorials
                              Zachary G.NinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by argusthome, 03-08-2026, 10:06 AM
                              0 responses
                              77 views
                              0 likes
                              Last Post argusthome  
                              Started by NabilKhattabi, 03-06-2026, 11:18 AM
                              0 responses
                              45 views
                              0 likes
                              Last Post NabilKhattabi  
                              Started by Deep42, 03-06-2026, 12:28 AM
                              0 responses
                              27 views
                              0 likes
                              Last Post Deep42
                              by Deep42
                               
                              Started by TheRealMorford, 03-05-2026, 06:15 PM
                              0 responses
                              32 views
                              0 likes
                              Last Post TheRealMorford  
                              Started by Mindset, 02-28-2026, 06:16 AM
                              0 responses
                              63 views
                              0 likes
                              Last Post Mindset
                              by Mindset
                               
                              Working...
                              X