Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

cancel order

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

    cancel order

    hi,

    I have something like :

    if (xxxxx)
    enterlonglimit(1,close[0],"LongA1")

    with a profit target of 1 tick and stop loss of 10

    on xxx min bars. (if that actually makes a difference)

    now my problem is that when I get a signal to buy, it is only good for (i) bbb min and (ii) 1 one tick increase in price. I have part (i) done but part (ii) is causing me some trouble.

    I have looked at IOrders with Cancel() but I was wondering if there was an "easier" way to cancel my order if it was left open but the price had gone up by 2 ticks already for what ever reason?


    JP
    Last edited by chancehero; 04-04-2013, 09:48 AM.

    #2
    JP, you would need to monitor current price in realtime with CalculateOnBarClose = false so you can CancelOrder if it moves too far away from your desired entry point.

    Comment


      #3
      is this better?

      would this do the trick? or is there a easier way?
      PHP Code:
       protected override void Initialize()
              {
                  SetProfitTarget(CalculationMode.Ticks, 1);
                  SetStopLoss(CalculationMode.Ticks, 10);
      
                  CalculateOnBarClose = false;
              }
      
              /// <summary>
              /// Called on each bar update event (incoming tick)
              /// </summary>
              protected override void OnBarUpdate()
              {
                  // Condition set 1
                  if (Open[0] && myEntryOrder == null)
                  {
                     DrawArrowUp(CurrentBar.ToString(), true, 0, Low[0] + TickSize, Color.Red);
                      myEntryOrder = EnterLongLimit(1, Close[0],"A1");
                      barNumberOfOrder = CurrentBar;
                  }
                  // condition set 2 :  if position is filled but nothing happening for more than 1 bar, cancel order.
                  if(myEntryOrder != null && CurrentBar > barNumberOfOrder +1)
                  {    
                      CancelOrder(myEntryOrder);
                  }
              }
              protected override void OnOrderUpdate(IOrder order)
              {
                  // cancel order setting 2 : when placing order at last bars close, if price gaps or that order just not filled , by more than 2 cancel the working order.
                  {
                  if(myEntryOrder != null && OrderState.Filled == null && GetCurrentAsk() >= Close[0]+2)    
                      CancelOrder(myEntryOrder); 
      
      Last edited by chancehero; 04-04-2013, 09:51 AM.

      Comment


        #4
        JP, that would work but for one bar for example you could just let it expire at the end of the bar it was submitted on (you're not working with liveUntilCancelled so default NT behavior is to expire the order then on the next bar).

        Comment


          #5
          yea !

          ok, finally, I am starting to getthis IOrder thing ! F ! allright will keap working on it.

          question:in the strategy tab, is it possible to add a collum with the session manager in use?

          Comment


            #6
            Great - unfortunately this is not possible - you would need to disable it and check your settings.

            Comment


              #7
              loop?

              ok, so now I tested it and , problem, it trades the first signal, sell, makes money , and then stops, even if more signals are popping up,

              I imagine there is a loop of some sort that I need to place. pointing me in the right directions would be great.

              jP

              Comment


                #8
                I would suggest you add first of all TraceOrders to further debug your strategy's order placement -



                Next : are you you're synched up properly in your account so it mirror the strategy position and qty?

                Comment


                  #9
                  I am getting this loop of the same errors:
                  28/08/2010 1:04:11 AM Entered internal PlaceOrder() method at 28/08/2010 1:04:11 AM: Action=Sell OrderType=Market Quantity=0 LimitPrice=0 StopPrice=0 SignalName='' FromEntrySignal='A1'
                  28/08/2010 1:04:11 AM Ignored PlaceOrder() method: Action=Sell OrderType=Market Quantity=0 LimitPrice=0 StopPrice=0 SignalName='' FromEntrySignal='A1' Reason='This was an exit order but no position exists to exit'
                  exitlong

                  28/08/2010 1:04:11 AM CancelAllOrders: BarsInProgress=0
                  **NT** Disabling NinjaScript strategy 'jp1/49bf9bf9bb784eb1a4079b87697443b5'

                  PHP Code:
                   #region Variables
                          // Wizard generated variables
                          private int myInput0 = 50; // Default setting for MyInput0
                          // User defined variables (add any user defined variables below)
                          private IOrder myEntryOrder = null;
                          private int barNumberOfOrder = 0;
                          #endregion
                  
                          /// <summary>
                          /// This method is used to configure the strategy and is called once before any strategy method is called.
                          /// </summary>
                          protected override void Initialize()
                          {
                              SetProfitTarget(CalculationMode.Ticks, 1);
                              SetStopLoss(CalculationMode.Ticks, myInput0);
                              TraceOrders = true;
                              CalculateOnBarClose = false;
                          }
                  
                          /// <summary>
                          /// Called on each bar update event (incoming tick)
                          /// </summary>
                          protected override void OnBarUpdate()
                          {
                              // Condition set 1
                              if (Close[0] > Open[0] /*&& myEntryOrder == null*/)
                              {
                                 // DrawArrowUp(CurrentBar.ToString(), true, 0, Low[0] + TickSize, Color.Red);
                                  myEntryOrder = EnterLongLimit(1, Close[0],"A1");
                                  barNumberOfOrder = CurrentBar;
                              }
                              // condition set 2 :  if position is filled but nothing happening for more than 1 bar, cancel order.
                              /* if(myEntryOrder != null && CurrentBar > barNumberOfOrder + 1);
                              {    
                                  CancelOrder(myEntryOrder);
                              } */
                              // condition set 3 : if position filled, close at end of bar. 
                              if (/*myEntryOrder != null*/ Position.MarketPosition == MarketPosition.Long && CurrentBar > barNumberOfOrder + 1);
                              {
                                  ExitLong("A1");
                                  Print("exitlong");
                              }
                          }
                          protected override void OnOrderUpdate(IOrder order)
                          {
                              // cancel order setting 2 : when placing order at last bars close, if price gaps or that order just not filled , by more than 2 cancel the working order.
                              {
                              if(myEntryOrder != null && myEntryOrder == order && GetCurrentAsk() >= Close[0] + 2 * TickSize);
                              {
                                  if (order.OrderState == OrderState.Working);
                                  CancelOrder(myEntryOrder);
                                  Print("ordercancelled+2");
                              }
                              }    
                          } 
                  
                  Last edited by chancehero; 04-04-2013, 09:50 AM.

                  Comment


                    #10
                    chancehero, you will have to check and wait for the actual fill to occur in OnExecution() before submitting an exit order. I have a feeling your script is running the Enter() method and then the Exit() method is being called before the position actually exists.
                    AustinNinjaTrader Customer Service

                    Comment


                      #11
                      next issue

                      Hi Austin,

                      Alright, so took care of some problems, but I am still unsure for one part.

                      how do I save the last sold price so that I can compare it ? this needs to be in real time. not on onbarupdate or onorderupdate.

                      I will show you what I have until now.

                      I want to obtain the price data right now, am I calling the right thing? so my first is that if my order is running then I save the price data ask into price1. then if price1 is greater than 2 ticks off the last bar close . I would cancel my order, and reset it.

                      PHP Code:
                      public class test4 : Strategy
                          {
                              #region Variables
                              private int var1 = 50; // Default setting for Var1
                              private int var2 = 1; // Default setting for Var2
                              private int var3 = 1; // Default setting for Var3
                              // User defined variables (add any user defined variables below)
                              private IOrder myEntryOrder = null;
                              private int barNumberOfOrder = 0;
                              private int var4 = 50; // Stop loss for short
                              private int var5 = 1; // profit target for short
                              private double price1 = 0;
                              #endregion
                              /// <summary>
                              /// This method is used to configure the strategy and is called once before any strategy method is called.
                              /// </summary>
                              protected override void Initialize()
                              {
                                  SetProfitTarget("LongA1",CalculationMode.Ticks, Var2);
                                  SetStopLoss("LongA1",CalculationMode.Ticks, Var1,false);
                                  TraceOrders = true;
                                  CalculateOnBarClose = false;
                              }
                      
                              /// <summary>
                              /// Called on each bar update event (incoming tick)
                              /// </summary>
                              protected override void OnBarUpdate()
                               {
                                  //if (ToTime(Time[0]) >= 224500 && ToTime(Time[0]) <= 164500)
                                      
                                  if (CurrentBar < 1)
                                      return;
                                  
                                  if (Position.MarketPosition == MarketPosition.Flat)
                                  {    
                              // entry long logic :
                                  if( myEntryOrder == null && Close[0] > Open[0])
                                  {
                                      myEntryOrder = EnterLongLimit(1,Close[0],"LongA1");
                                      barNumberOfOrder = CurrentBar; 
                                  }
                                  
                              // exit set 1 : close  the long limit order position after one bar.
                                  else if (myEntryOrder != null && CurrentBar > barNumberOfOrder )
                                  {
                                      CancelOrder(myEntryOrder);
                                      myEntryOrder = null;
                                      Print("canceled order after 1 bar");
                                  }
                                  }
                              // exit set 2 : close pocition after 1 bar.
                                  else if (CurrentBar > barNumberOfOrder && Position.MarketPosition == MarketPosition.Long)
                                  {
                                      ExitLong(1,"closeA1 +1bar", "LongA1");
                                      myEntryOrder = null;
                                      Print("closeA1 +1bar");
                                  }    
                              }
                              //here I want my order to cancel if it is not filled and the price is over my entry by 2 ticks
                                  
                              protected override void OnOrderUpdate(IOrder order)
                              {
                                   if (myEntryOrder != null && myEntryOrder.Token == order.Token)
                                   {
                                        if (order.OrderState == OrderState.Filled)
                                      {
                                         myEntryOrder = null;
                                      Print("order status reset");
                                      }
                                    }
                              }
                      /*this is were I have a problem, I want to obtain the price data right now,
                       am I calling the right thing? so my first is that if my order is running then
                       I save the price data ask into price1. then if price1 is greater than 2 ticks
                       off the last bar close . I would cancel my order, and reset it. */
                              protected override void OnMarketData(MarketDataEventArgs e)
                              {
                                  if (myEntryOrder != null)
                                      price1 = GetCurrentAsk();          
                                  {
                                  if (price1 >= Close[0] + 2 * TickSize)
                                  {
                                      CancelOrder(myEntryOrder);
                                      myEntryOrder = null;
                                      Print("cancel +2 ticks");
                                  }
                                  }
                              } 
                      

                      Comment


                        #12
                        This is the error I get, when it does not get filled, it gets the values but just craps out on me.


                        11/09/2010 5:17:00 PM Entered internal PlaceOrder() method at 11/09/2010 5:17:00 PM: Action=Buy OrderType=Limit Quantity=1 LimitPrice=1096.50 StopPrice=0 SignalName='LongA1' FromEntrySignal=''
                        1096.50is price1 greater?
                        1096.50is price1 greater?
                        1096.50is price1 greater?
                        1096.50is price1 greater?
                        cancel +2 ticks
                        **NT** Error on calling 'OnMarketData' method for strategy 'test4/a327216c83bf4f4db13c1f3b018e24c2': Object reference not set to an instance of an object.

                        Comment


                          #13
                          Sorry the last post makes no sence if you dont have this part, I was just changing it

                          PHP Code:
                          // here cancel working order if ask price is greater than 2 ticks of close[0]
                                  protected override void OnMarketData(MarketDataEventArgs e)
                                  {
                                      if (myEntryOrder != null && e.MarketDataType == MarketDataType.Last)
                                      {
                                      price1 = e.Price;         
                                      Print(Close[0].ToString("0.00")+ "is price1 greater?");
                                      }
                                          if (price1 >= Close[0] + 2 * TickSize)
                                          {
                                          CancelOrder(myEntryOrder);
                                          myEntryOrder = null;
                                          Print("cancel +2 ticks");
                                          }
                                  } 
                          

                          Comment


                            #14
                            chancehero, can you please post your full code so we can test this out on our end?
                            AustinNinjaTrader Customer Service

                            Comment


                              #15
                              sorry

                              here you go.
                              it is very strange, I open a simulated data feed (the generic from NT) and when it gets a signal to place an order limit, and a gap forms, the onmarketdata starts to work and prints the prices and then prints the cancel and ERROR. Which I though is weird.


                              PHP Code:
                              public class test4 : Strategy 
                                  { 
                                      #region Variables 
                                      private int var1 = 50; // Default setting for Var1 
                                      private int var2 = 1; // Default setting for Var2 
                                      private int var3 = 1; // Default setting for Var3 
                                      // User defined variables (add any user defined variables below) 
                                      private IOrder myEntryOrder = null; 
                                      private int barNumberOfOrder = 0; 
                                      private int var4 = 50; // Stop loss for short 
                                      private int var5 = 1; // profit target for short 
                                      private double price1 = 0; 
                                      #endregion 
                                      /// <summary> 
                                      /// This method is used to configure the strategy and is called once before any strategy method is called. 
                                      /// </summary> 
                                      protected override void Initialize() 
                                      { 
                                          SetProfitTarget("LongA1",CalculationMode.Ticks, Var2); 
                                          SetStopLoss("LongA1",CalculationMode.Ticks, Var1,false); 
                                          TraceOrders = true; 
                                          CalculateOnBarClose = false; 
                                      } 
                              
                                      /// <summary> 
                                      /// Called on each bar update event (incoming tick) 
                                      /// </summary> 
                                      protected override void OnBarUpdate() 
                                       { 
                                          //if (ToTime(Time[0]) >= 224500 && ToTime(Time[0]) <= 164500) 
                                               
                                          if (CurrentBar < 1) 
                                              return; 
                                           
                                          if (Position.MarketPosition == MarketPosition.Flat) 
                                          {     
                                      // entry long logic : 
                                          if( myEntryOrder == null && Close[0] > Open[0]) 
                                          { 
                                              myEntryOrder = EnterLongLimit(1,Close[0],"LongA1"); 
                                              barNumberOfOrder = CurrentBar;  
                                          } 
                                           
                                      // exit set 1 : close  the long limit order position after one bar. 
                                          else if (myEntryOrder != null && CurrentBar > barNumberOfOrder ) 
                                          { 
                                              CancelOrder(myEntryOrder); 
                                              myEntryOrder = null; 
                                              Print("canceled order after 1 bar"); 
                                          } 
                                          } 
                                      // exit set 2 : close pocition after 1 bar. 
                                          else if (CurrentBar > barNumberOfOrder && Position.MarketPosition == MarketPosition.Long) 
                                          { 
                                              ExitLong(1,"closeA1 +1bar", "LongA1"); 
                                              myEntryOrder = null; 
                                              Print("closeA1 +1bar"); 
                                          }     
                                      } 
                                      //here I want my order to cancel if it is not filled and the price is over my entry by 2 ticks 
                                           
                                      protected override void OnOrderUpdate(IOrder order) 
                                      { 
                                           if (myEntryOrder != null && myEntryOrder.Token == order.Token) 
                                           { 
                                                if (order.OrderState == OrderState.Filled) 
                                              { 
                                                 myEntryOrder = null; 
                                              Print("order status reset"); 
                                              } 
                                            } 
                                      } 
                              // here cancel working order if ask price is greater than 2 ticks of close[0] 
                                      protected override void OnMarketData(MarketDataEventArgs e) 
                                      { 
                                          if (myEntryOrder != null && e.MarketDataType == MarketDataType.Last) 
                                          { 
                                          price1 = e.Price;          
                                          Print(Close[0].ToString("0.00")+ "is price1 greater?"); 
                                          } 
                                              if (price1 >= Close[0] + 2 * TickSize) 
                                              { 
                                              CancelOrder(myEntryOrder); 
                                              myEntryOrder = null; 
                                              Print("cancel +2 ticks"); 
                                              } 
                                      } 
                              

                              the error I get is : (which is weird since I was pretty sure that all my variables were explicitly stated.)m could it be that onmarketdata is the wrong thing to call upon?

                              11/09/2010 5:17:00 PM Entered internal PlaceOrder() method at 11/09/2010 5:17:00 PM: Action=Buy OrderType=Limit Quantity=1 LimitPrice=1096.50 StopPrice=0 SignalName='LongA1' FromEntrySignal=''
                              1096.50is price1 greater?
                              1096.50is price1 greater?
                              1096.50is price1 greater?
                              1096.50is price1 greater?
                              cancel +2 ticks
                              **NT** Error on calling 'OnMarketData' method for strategy 'test4/a327216c83bf4f4db13c1f3b018e24c2': Object reference not set to an instance of an object.

                              Comment

                              Latest Posts

                              Collapse

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