Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

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.
    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 algospoke, Yesterday, 06:40 PM
      2 responses
      21 views
      0 likes
      Last Post algospoke  
      Started by ghoul, Today, 06:02 PM
      3 responses
      14 views
      0 likes
      Last Post NinjaTrader_Manfred  
      Started by jeronymite, 04-12-2024, 04:26 PM
      3 responses
      45 views
      0 likes
      Last Post jeronymite  
      Started by Barry Milan, Yesterday, 10:35 PM
      7 responses
      21 views
      0 likes
      Last Post NinjaTrader_Manfred  
      Started by AttiM, 02-14-2024, 05:20 PM
      10 responses
      181 views
      0 likes
      Last Post jeronymite  
      Working...
      X