• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

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;
                                                       myAccount.Cancel (myOrder);
                                                               Print ("the order had to be canceled");
                                                          }
                                                      }
                                             }
                                              done = true;
                                            }
                                      }
                            }

    in this case,(above) it doesn't even allow me to compile:
    http://prntscr.com/oip703



    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;
                                                        CancelOrder (myOrder);
                                                               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

    http://prntscr.com/oip8yj

    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.
    Kate W.NinjaTrader Customer Service

    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 hurleydood, Today, 10:07 AM
      0 responses
      0 views
      0 likes
      Last Post hurleydood  
      Started by Irukandji, Today, 09:43 AM
      1 response
      7 views
      0 likes
      Last Post NinjaTrader_ThomasC  
      Started by TazoTodua, Today, 09:06 AM
      0 responses
      4 views
      0 likes
      Last Post TazoTodua  
      Started by sambo81, Yesterday, 06:57 PM
      1 response
      20 views
      0 likes
      Last Post NinjaTrader_ThomasC  
      Started by tgn55, Today, 08:34 AM
      0 responses
      4 views
      0 likes
      Last Post tgn55
      by tgn55
       
      Working...
      X