Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

how close order ?? (pending orders)

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

    how close order ?? (pending orders)

    Hello greetings
    I'm new to ninjatrader and new to ninja script

    I am learning to automate a small strategy, which depends on pending orders

    The idea is that I can close the platform, and the next day or later on the same day, turn it on and have control over those orders that I put before.
    That is why I am checking the orders of the account and if they have the same "name", it is understood that they are of the same strategy and I want to work them.

    The problem arises when I want to close one of those open orders (of the strategy with the same name) and that have not yet been executed. (an example: a sell short limit in "working" state)

    I repeat, I am testing, and for this I have created an order with a particular name (which is useful for tracking in this example)

    I have tried 2 functions, without success.
    The first is as follows:

    Code:
    protected override void OnStateChange()
            {
                if (State == State.SetDefaults)
                {
                  myAccount = Account.All.FirstOrDefault(a => a.Name == "Sim101");
                }
           }
        protected override void OnBarUpdate ()
                 {
                       if (State == State.Historical) return;
                       if (this.CurrentBar> this.BarsRequiredToTrade)
                            {
                              if (done == false)
                                {
                                     if (myAccount != null)
                                           {
                                              // EnterShortLimit (0, true, 1,3000, "1522557SP");
                                             foreach (Order oo in myAccount.Orders)
                                                      {
                                                            int idx = oo.Name.IndexOf ("1522557SP");
                                                          if (idx >= 0 && oo.OrderState.ToString () == "Working")
                                                         {
                                                            myOrder = oo;
    [B]                                                   myAccount.Cancel (myOrder);[/B]
                                                               Print ("the order had to be canceled");
                                                          }
                                                      }
                                             }
                                              done = true;
                                            }
                                      }
                            }

    in this case,(above) it doesn't even allow me to compile:




    The second case I try to use is this:

    Code:
    private Account myAccount;        
            private bool done = false;
            private Order myOrder = null;
    
            protected override void OnBarUpdate ()
                 {
                       if (State == State.Historical) return;
                       if (this.CurrentBar> this.BarsRequiredToTrade)
                            {
                              if (done == false)
                                {
                                     if (myAccount != null)
                                           {
                                              // EnterShortLimit (0, true, 1,3000, "1522557SP");
                                             foreach (Order oo in myAccount.Orders)
                                                      {
                                                            int idx = oo.Name.IndexOf ("1522557SP");
                                                          if (idx >= 0 && oo.OrderState.ToString () == "Working")
                                                         {
                                                            myOrder = oo;
    [B]                                                    CancelOrder (myOrder);[/B]
                                                               Print ("the order had to be canceled");
                                                          }
                                                      }
                                             }
                                              done = true;
                                            }
                                      }
                            }
                // ////////////////
            protected override void OnOrderUpdate(Cbi.Order order, double limitPrice, double stopPrice,
                                        int quantity, int filled, double averageFillPrice,
                                        Cbi.OrderState orderState, DateTime time, Cbi.ErrorCode error, string comment)
            {
                Print("hhhhh");      
    
            }
    
    
            protected override void OnExecutionUpdate(Execution execution, string executionId, double price, int quantity, MarketPosition marketPosition, string orderId, DateTime time)
            {
                Print("Kjkjkjk");
            }

    Here, I have no problem compiling, but when I put the strategy to work on the symbol, it goes through the line where I should close or cancel the order, but it does not.
    It is evident in the print that it does.
    But no cancel



    also, do not call events
    OnOrderUpdate or OnExecutionUpdate

    How can I delete or cancel that order???
    I take the opportunity to ask if there is a combination of keys that allows a self-indented code

    Thanks for your help!!!

    Note: edit the topic title, since I have already updated the question
    Last edited by NinjaTrader_Jim; 07-24-2019, 07:45 AM.

    #2
    Hello Miguel_88,

    Thank you for your note.

    Try this for OnBarUpdate:

    protected override void OnBarUpdate()
    {
    if (State == State.Historical) return;

    if (this.CurrentBar> this.BarsRequiredToTrade)
    {
    if (done == false)
    {
    if (myAccount != null)
    {
    if(Position.MarketPosition == MarketPosition.Flat)
    EnterShortLimit (0, true, 1,8000, "1522557SP");
    else
    {
    foreach (Order oo in myAccount.Orders)
    {
    int idx = oo.Name.IndexOf ("1522557SP");

    if (idx >= 0 && oo.OrderState.ToString () == "Working")
    {
    myOrder = oo;
    myAccount.Cancel(new[] {myOrder});
    Print ("the order had to be canceled");
    }
    }
    }
    }
    done = true;
    }
    }
    }

    Please let us know if we may be of further assistance to you.

    Comment


      #3
      Originally posted by NinjaTrader_Kate View Post
      Hello Miguel_88,

      Thank you for your note.

      Try this for OnBarUpdate:

      protected override void OnBarUpdate()
      {
      if (State == State.Historical) return;

      if (this.CurrentBar> this.BarsRequiredToTrade)
      {
      if (done == false)
      {
      if (myAccount != null)
      {
      if(Position.MarketPosition == MarketPosition.Flat)
      EnterShortLimit (0, true, 1,8000, "1522557SP");
      else
      {
      foreach (Order oo in myAccount.Orders)
      {
      int idx = oo.Name.IndexOf ("1522557SP");

      if (idx >= 0 && oo.OrderState.ToString () == "Working")
      {
      myOrder = oo;
      myAccount.Cancel(new[] {myOrder});
      Print ("the order had to be canceled");
      }
      }
      }
      }
      done = true;
      }
      }
      }

      Please let us know if we may be of further assistance to you.
      Thank you very much, it works perfectly for me, the correction to apply is in this line

      Code:
      myOrder = oo;
      myAccount.Cancel(new[] {myOrder});
      thanks.

      please edit the tittle "how close order ?? (pending orders) "

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by NullPointStrategies, Yesterday, 05:17 AM
      0 responses
      59 views
      0 likes
      Last Post NullPointStrategies  
      Started by argusthome, 03-08-2026, 10:06 AM
      0 responses
      134 views
      0 likes
      Last Post argusthome  
      Started by NabilKhattabi, 03-06-2026, 11:18 AM
      0 responses
      75 views
      0 likes
      Last Post NabilKhattabi  
      Started by Deep42, 03-06-2026, 12:28 AM
      0 responses
      45 views
      0 likes
      Last Post Deep42
      by Deep42
       
      Started by TheRealMorford, 03-05-2026, 06:15 PM
      0 responses
      50 views
      0 likes
      Last Post TheRealMorford  
      Working...
      X