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()

    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?

        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.

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by argusthome, 03-08-2026, 10:06 AM
            0 responses
            65 views
            0 likes
            Last Post argusthome  
            Started by NabilKhattabi, 03-06-2026, 11:18 AM
            0 responses
            41 views
            0 likes
            Last Post NabilKhattabi  
            Started by Deep42, 03-06-2026, 12:28 AM
            0 responses
            23 views
            0 likes
            Last Post Deep42
            by Deep42
             
            Started by TheRealMorford, 03-05-2026, 06:15 PM
            0 responses
            26 views
            0 likes
            Last Post TheRealMorford  
            Started by Mindset, 02-28-2026, 06:16 AM
            0 responses
            52 views
            0 likes
            Last Post Mindset
            by Mindset
             
            Working...
            X