Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Cancel and submit order at the same bar

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

    Cancel and submit order at the same bar

    Hello,

    This is my code where I am testing canceling order and submitting the new one at the same bar.

    When such situation occurs the order is cancelled but the strategy is not placing new order. It works in backtest but does not work in real time.

    Please support.


    My code:

    Code:
    protected override void OnBarUpdate()
            {
    			Print("----------");
    			Print(Time[0]+ " START OrderLong1 = " +OrderLong1);
    			
    			if (Position.MarketPosition == MarketPosition.Long && CurrentBar == bar1 + 5)
    			{
    				ExitLong();
    				OrderLong1 = null;
    				
    				Print(Time[0]+ " | Zamkniecie otwartej pozycji LONG");
    				Print(Time[0]+ " OrderLong1 = " +OrderLong1);
    			}
    			
    			if (OrderLong1 != null && Position.MarketPosition == MarketPosition.Flat && CurrentBar == bar1 + 1)
    			{
    				CancelOrder(OrderLong1);
    				Print(Time[0]+ " | Anulowanie zlecenia LONG");
    				Print(Time[0]+ " OrderLong1 = " +OrderLong1);
    				
    				OrderLong1 = null;
    				Print(Time[0]+ " | OrderLong1 null");
    				Print(Time[0]+ " OrderLong1 = " +OrderLong1);
    			}
    			
    			if (OrderLong1 == null && Position.MarketPosition == MarketPosition.Flat && Close[0] > Close[1])
    			{
    				double 	entry = High[0] + ((3 * 10) + 10) * TickSize;
    						sl = DonchianChannel(5).Lower[0] - 10 * TickSize;
    				
    				OrderLong1 = EnterLongStop(0, true, 1000, entry, "Long 1");
    				SetStopLoss("Long 1", CalculationMode.Price, sl, false);
    					
    				bar1 = CurrentBar;
    												
    				DrawLine("Long" +CurrentBar, false, 0, entry, -1, entry, Color.Green, DashStyle.Solid, 2);
    				DrawLine("SL "+CurrentBar, false, 0, sl, -1, sl, Color.Red, DashStyle.Solid, 2);
    				
    				Print(Time[0]+ " | Ustawienie zlecenia LONG");
    				Print(Time[0]+ " OrderLong1 = " +OrderLong1);
    			}
    						
    			Print(Time[0]+ " KONIEC OrderLong1 = " +OrderLong1);
            }

    #2
    kucharek, as you have already debug prints added to your code - would the conditions for placement in real-time happen, so you would see the print getting hit in your output window?

    For working with the IOrder objects, I would suggest doing the check for fillstates and your resets of the object in OnOrderUpdate() and OnExecution(), as those would not be tied to any bar update timing, but the OrderUpdate or Execution event seen.

    The OnOrderUpdate() and OnExecution() methods are reserved for experienced programmers. Instead of using Set() methods to submit stop-loss and profit target orders, you can submit and update them manually through the use of IOrder and IExecution objects in the OnOrderUpdate() and OnExecution() methods. The OnOrderUpdate()
    BertrandNinjaTrader Customer Service

    Comment


      #3
      Yes, I see print getting hit. That is why it is very strange for me.

      Comment


        #4
        Thanks kucharek, can you please run this with TraceOrders = enabled as well?



        What output would you get then shown? Any order ignored?
        BertrandNinjaTrader Customer Service

        Comment


          #5
          Hello Bertrand,

          I have modified my code based on your suggestions and add OnOrderUpdate().

          First test trade went well. As you can see I have add placing an order in OnOrderUpdate(). Is this acceptable solution? Are there any disadvantages of such solution?

          Code:
          protected override void OnBarUpdate()
                  {
          			Print("----------");
          			Print(Time[0]+ " START OrderLong1 = " +OrderLong1);
          			
          			if (Position.MarketPosition == MarketPosition.Long && CurrentBar == bar1 + 5)
          			{
          				ExitLong();
          								
          				Print(Time[0]+ " | Zamkniecie otwartej pozycji LONG");
          				Print(Time[0]+ " OrderLong1 = " +OrderLong1);
          			}
          			
          			if (OrderLong1 != null && Position.MarketPosition == MarketPosition.Flat && CurrentBar == bar1 + 1)
          			{
          				CancelOrder(OrderLong1);
          				Print(Time[0]+ " | Anulowanie zlecenia LONG");
          				Print(Time[0]+ " OrderLong1 = " +OrderLong1);
          			}
          			
          			if (OrderLong1 == null && Position.MarketPosition == MarketPosition.Flat && Close[0] > EMA(5)[0])
          			{
          				double 	entry = High[0] + 10 * TickSize;
          						sl = DonchianChannel(5).Lower[0] - 10 * TickSize;
          				
          				OrderLong1 = EnterLongStop(0, true, 1000, entry, "Long 1");
          				SetStopLoss("Long 1", CalculationMode.Price, sl, false);
          					
          				bar1 = CurrentBar;
          												
          				DrawLine("Long" +CurrentBar, false, 0, entry, -1, entry, Color.Green, DashStyle.Solid, 2);
          				DrawLine("SL "+CurrentBar, false, 0, sl, -1, sl, Color.Red, DashStyle.Solid, 2);
          				
          				Print(Time[0]+ " | Ustawienie zlecenia LONG");
          				Print(Time[0]+ " OrderLong1 = " +OrderLong1);
          			}
          						
          			Print(Time[0]+ " KONIEC OrderLong1 = " +OrderLong1);
                  }
          		
          		protected override void OnOrderUpdate(IOrder order)
                  {
          			if (OrderLong1 != null && OrderLong1 == order)
          			{	
          				if (order.OrderState == OrderState.Filled || order.OrderState == OrderState.PartFilled)
          				{
          					OrderLong1 = null;
          				}
          				
          				if (order.OrderState == OrderState.Cancelled && order.Filled == 0)
          				{
          					OrderLong1 = null;
          				}
          				
          				if (OrderLong1 == null && Position.MarketPosition == MarketPosition.Flat && Close[0] > EMA(5)[0])
          				{
          					double 	entry = High[0] + 10 * TickSize;
          						sl = DonchianChannel(5).Lower[0] - 10 * TickSize;
          				
          					OrderLong1 = EnterLongStop(0, true, 1000, entry, "Long 1");
          					SetStopLoss("Long 1", CalculationMode.Price, sl, false);
          					
          					bar1 = CurrentBar;
          												
          					DrawLine("Long" +CurrentBar, false, 0, entry, -1, entry, Color.Green, DashStyle.Solid, 2);
          					DrawLine("SL "+CurrentBar, false, 0, sl, -1, sl, Color.Red, DashStyle.Solid, 2);
          				
          					Print(Time[0]+ " | Ustawienie zlecenia LONG");
          					Print(Time[0]+ " OrderLong1 = " +OrderLong1);
          				}
          			}
                  }

          Comment


            #6
            kucharek, great - I see rather an advantage than disadvantage with this amendment done.
            BertrandNinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by timmbbo, Today, 08:59 AM
            1 response
            2 views
            0 likes
            Last Post NinjaTrader_ChelseaB  
            Started by KennyK, 05-29-2017, 02:02 AM
            2 responses
            1,281 views
            0 likes
            Last Post marcus2300  
            Started by fernandobr, Today, 09:11 AM
            0 responses
            3 views
            0 likes
            Last Post fernandobr  
            Started by itrader46, Today, 09:04 AM
            1 response
            6 views
            0 likes
            Last Post NinjaTrader_Clayton  
            Started by bmartz, 03-12-2024, 06:12 AM
            5 responses
            33 views
            0 likes
            Last Post NinjaTrader_Zachary  
            Working...
            X