Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Need help with two-way strategy

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

    Need help with two-way strategy

    Hello.

    I'm trying to code a strategy which enters in either long or short at a certain point. When a condition is true it submits a buy limit order with EnterLongLimit and a short limit order with EnterShortLimit. Whichever fills, the other is cancelled (in the OnExecution method).

    I also use a stoploss, by using the ExitLongstop/ExitShortStop functions in the OnExecution method.

    The code in brief:
    Code:
    private IOrder stopOrder = null;
    private IOrder shortLimit = null;
    private IOrder longLimit = null;
    
    ...
    
    protected override void OnBarUpdate()
    {
       ..../* defining the limit prices */
       if(/*some condition here */ && shortLimit == null && longLimit == null)
       {
           longLimit = EnterLongLimit(0,true,1,longLimitPrice,Name+" EnterLong");
           shortLimit = EnterShortLimit(0,true,1,shortLimitPrice,Name+" EnterShort");
       }
    }
    
    protected override void OnExecution(IExecution exec)
    {
    	base.OnExecution(exec);
    	IOrder ord = exec.Order;
    	if(ord.OrderAction == OrderAction.Buy)
    	{
    		if(shortLimit != null)
    		{
    		             CancelOrder(shortLimit);
    			shortLimit = null;
    		}
    		stopOrder = ExitLongStop(0,true,ord.Quantity,exec.Price - stoptav*TickSize,"StopLoss",Name+ " EnterLong");
    								
    				
    	}
    	else if(ord.OrderAction == OrderAction.SellShort)
    	{
    		if(longLimit != null)
    		{
    			CancelOrder(longLimit);
    			longLimit = null;
    		}
    		stopOrder = ExitShortStop(0,true,ord.Quantity,exec.Price + stoptav*TickSize,"StopLoss",Name+ " EnterShort");
    				
    	}
    	else
    	{
    		if(stopOrder != null && stopOrder.OrderState != OrderState.Filled)
    		{
    			CancelOrder(stopOrder);	
    		}
    		stopOrder = null;
    				
    		if(shortLimit != null && shortLimit.OrderState != OrderState.Filled)
    		{
    			CancelOrder(shortLimit);	
    		}
    		shortLimit = null;
    				
    		if(longLimit != null && longLimit.OrderState != OrderState.Filled)
    		{
    			CancelOrder(longLimit);
    		}
    		longLimit = null;
    				
    				
    	}
    }
    The first problem:
    One of my EnterLimit orders is getting ignored due to the internal order handling rules., therefore it can only enter into one direction. The only way to get around this is using unmanaged orders? I also would like to use Trailing stops, and that takes a lot to code with unmanaged orders, so I'd be happy if you could provide a way to implement my strategy with managed orders.


    The second issue:
    The limit order (which was not ignored) gets filled always on the limit price, even if the last price wasn't near of that. See picture attached: The limit price is at the blue line, but as you can see the bars, it is impossible to get filled there.

    I'm open to solutions to these.

    Thanks!

    I'm using NT7b22
    Attached Files
    Last edited by vides2012; 10-17-2010, 04:49 AM. Reason: version

    #2
    Okay, I've found out that the execution prices are incorrect because I'm using limit orders instead of stop orders for entry. But the first problem still remains.

    Comment


      #3
      Hello vides2012,

      Correct, you can't submit EnterLongLimit and EnterShortLimit at the same time. You would have to move to an unmanaged strategy to do this.

      If you wanted to do this in an unmanaged strategy, you would have to ensure only one of these orders is active at a time.
      Ryan M.NinjaTrader Customer Service

      Comment


        #4
        Okay. Is there a proper way to have trailing stops with unmanaged orders?
        I can check Bar highs and lows at OnBarUpdate(), but that calculates only on bar close when backtesting.

        Comment


          #5
          There is no difference in the backtesting engine when using an unmanaged versus a managed approach - conditions are still only evaluated at bar close.

          If you want to use trailing stops in an unmanaged strategy, all price levels must be manually coded.
          Ryan M.NinjaTrader Customer Service

          Comment


            #6
            Yes, I'm aware of that. And that's the problem, as trailing stops should be evaluated at each ticks (as it is when using manmaged trailing stops via SetTrailStop() ). It can only be achieved with a multi-timeframe strategy, and with hard, and difficult coding.

            Comment


              #7
              Yes, this can be challenging to code. Please see here if you're looking for a sample on this:
              Backtesting NinjaScript Strategies with an intrabar granularity
              Ryan M.NinjaTrader Customer Service

              Comment


                #8
                Liquidity providing code

                Originally posted by NinjaTrader_RyanM View Post
                Hello vides2012,

                Correct, you can't submit EnterLongLimit and EnterShortLimit at the same time. You would have to move to an unmanaged strategy to do this.

                If you wanted to do this in an unmanaged strategy, you would have to ensure only one of these orders is active at a time.
                I have a related question: If I wanted to have both a buy limit and a sell limit orders working simultanuously on the exchange, how should I submit them? Sample code, pls? OrderAction?

                Comment


                  #9
                  Hello jp_kettunen,

                  This requires an unmanaged strategy.

                  Here is an example of these orders, tied together with the same OCO field. Once one is filled it will cancel the other.
                  Code:
                  SubmitOrder(0, OrderAction.Buy, OrderType.Limit, 1, Close[0] - TickSize * 10, 0, "entryOrder", "buyOrder");
                  SubmitOrder(0, OrderAction.Sell, OrderType.Limit, 1, Close[0] + TickSize * 10, 0, "entryOrder", "sellOrder");
                  Last edited by NinjaTrader_RyanM1; 12-13-2010, 04:31 PM.
                  Ryan M.NinjaTrader Customer Service

                  Comment


                    #10
                    And if I wanted to leave the other working instead of cancelling it - e.g. sell is filled, and I want to have the buy limit still working? How come they are OCO?

                    Comment


                      #11
                      OCO is just an example.

                      You can use OCO by matching the OCO id field for each order statement. If you don't want it as OCO then don't match these fields or use empty string for OCO id "".

                      You can review the syntax for SubmitOrder statements here:
                      Last edited by NinjaTrader_RyanM1; 12-14-2010, 09:49 AM.
                      Ryan M.NinjaTrader Customer Service

                      Comment


                        #12
                        OK, and if I don't want to send them as OCO, must use "" as OCO code in SubmitOrder, right?

                        Comment


                          #13
                          Thanks for the reply. Correct - You can also specify an empty string "" here if you don't want to use OCO.
                          Ryan M.NinjaTrader Customer Service

                          Comment


                            #14
                            Clear, many thanks

                            Comment

                            Latest Posts

                            Collapse

                            Topics Statistics Last Post
                            Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                            0 responses
                            651 views
                            0 likes
                            Last Post Geovanny Suaza  
                            Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                            0 responses
                            370 views
                            1 like
                            Last Post Geovanny Suaza  
                            Started by Mindset, 02-09-2026, 11:44 AM
                            0 responses
                            109 views
                            0 likes
                            Last Post Mindset
                            by Mindset
                             
                            Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                            0 responses
                            574 views
                            1 like
                            Last Post Geovanny Suaza  
                            Started by RFrosty, 01-28-2026, 06:49 PM
                            0 responses
                            577 views
                            1 like
                            Last Post RFrosty
                            by RFrosty
                             
                            Working...
                            X