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

in an addon , identify position by order name

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

    in an addon , identify position by order name

    it seems that the positions taken while in addon, do not keep the name of the original order.
    so what should i do if instead of FLATTEN I want to EXIT the position that were only entered via addon with the name "Entry"
    there is no ....Name == "Entry" option in below code.
    Code:
    for (int i = 0; i < TotalNumberOfAccounts; i++)  
    {
     foreach (var position in accountSelectors[i].SelectedAccount.Positions)
      {
       // Check if the position has the desired name
       if (position.Name == "Entry")
        {
            position.Close();
        }
      }
    }
    ​

    #2
    Hello dadarara,

    Thanks for your post.

    position.Name is not a documented/supported NinjaScript property that could be called in a script. You could type "position." and use intellisense to see the properties available.

    Note that position.Name is not an option available. See the attached screenshot.

    See this help guide page for more information about <Account>.Positions: https://ninjatrader.com/support/help...ns_account.htm

    Note that Position.Close() is also not a documented/supported NinjaScript method in the help guide, however, it would be up to your discretion to use this method in your script.

    That said, you could use the <Account>.Orders collection to get orders by name using order.Name and then you could check if order.IsLong or order.IsShort and call an Exit order method to close the order.

    The 'Name' column on the Orders tab of the Control Center could be viewed to see what the name of an order you are submitting is.

    See this help guide page for more information about <Account>.Orders: https://ninjatrader.com/support/help...unt.htm​
    Attached Files
    Brandon H.NinjaTrader Customer Service

    Comment


      #3
      thank you for the post. but you have stated the obvious. Sorry for confusion. I KNOW that the position.name does not exist in ADDOn !

      So let me ask it again:
      HOW CAN I CLOSED POSITIONs based on the original order name being "Entry" ?

      are you saying that order.IsLong shows information linked to current position ? its sounds incorrect I think. even before testing it,

      Comment


        #4
        Hello dadarara,

        Thanks for your notes.

        You could use the <Account>.Orders collection to check if an order.Name is equal to "Entry" and you could check if order.IsLong == true to check if the order is a Long order or use order.IsShort == true to check if the order is a Short order.

        For example, in OnOrderUpdate() you could check if e.Order.Name == "Entry". You could also check if e.Order.IsLong == true or e.Order.IsShort == true to check if the order is a Long order or Short order.

        The sample code on this help guide page demonstrates the use of e.Order.IsLong and e.Order.IsShort in OnOrderUpdate(): https://forum.ninjatrader.com/forum/...01#post1175601

        Note that OnOrderUpdate() is an event driven method which is called each time an order managed by a strategy changes state. An order will change state when a change in order quantity, price or state (working to filled) occurs

        Account.OrderUpdate: https://ninjatrader.com/support/help...rderupdate.htm
        Orders: https://ninjatrader.com/support/help...rs_account.htm
        Order: https://ninjatrader.com/support/helpGuides/nt8/order.htm

        Another option would be to use <Account>.ExecutionUpdate to check if e.Execution.Order.Name is "Entry" in OnExecutionUpdate().

        Note that OnExecutionUpdate() is an event driven method that is called on an incoming execution of an order managed by a strategy. An execution is another name for a fill of an order.

        Account.ExecutionUpdate: https://ninjatrader.com/support/help...tionupdate.htm
        Executions: https://ninjatrader.com/support/help...executions.htm
        OnExecutionUpdate(): https://ninjatrader.com/support/help...tionupdate.htm
        Last edited by NinjaTrader_BrandonH; 10-15-2023, 07:46 PM.
        Brandon H.NinjaTrader Customer Service

        Comment


          #5
          We are talking about an addon. will check the info .
          I am not an expert so not exactly clear to me what the OnOrderUpdate() or OnExecutionUpdate() can help me in this case .
          Last edited by dadarara; 10-16-2023, 05:06 AM.

          Comment


            #6
            Hello dadarara,

            Thanks for your notes.

            <Account>.OrderUpdate and <Account>.Execution update could be used in an addon.

            Please review the help guide documentation linked in post # 4.

            When looking at the sample code in the <Account>.OrderUpdate help guide page, we can see that the account can subscribe to OrderUpdate events and then use OnOrderUpdate() to get information about orders, create orders, or submit orders in an addon.

            When looking at the sample code in the <Account>.ExecutionUpdate help guide page, we can see that the account can subscribe to ExecutionUpdate events and then use OnExecutionUpdate() to get information about an execution in an addon.
            Brandon H.NinjaTrader Customer Service

            Comment


              #7
              hoping for some help on this .
              my objective is to CLOSE only all positions in all accounts that originally were orders with "Entry" name. this should not touch anything that is running and open in parallel. like other strategies.

              Code:
              for (int i = 0; i < TotalNumberOfAccounts; i++)   //max 20
              
              {
                foreach (var order in accountSelectors[i].SelectedAccount.Orders)
                {
              
                 if(order.Name =="Entry" && (order.IsShort || order.IsLong))
                  {
                    accountSelectors[i].SelectedAccount.Positions. ......  ????
                  }
                }
              }

              Comment


                #8
                Hello dadarara,

                Thanks for your notes.

                An order name will either be 'Buy' for buy market orders submitted or 'Sell short' for sell market orders that are submitted manually.

                An order name submitted by a NinjaScript strategy will only have the Name 'Entry' if you sepcify 'Entry' as the SignalName property of an Entry order method in the strategy.

                To clarify, are you trying to close manually placed orders? Or, are you trying to close orders from a NinjaScript strategy where you are specifying 'Entry' as the SignalName of the Entry order method?

                If you are trying to detect manual trades and manage them programmatically, you can use the AddOn approach and create a NinjaScript that:
                1. Loops through the Account.Orders collection for the account to find active orders
                2. Subscribes to Account OrderUpdate events to look for any newly opened orders.
                3. Tracks these Orders in the script so they can be changed or cancelled with Account.Change or Account.Cancel. Or, you could use Account.CreateOrder() to create an order to close the position and submit the order with Account.Submit().

                If you are trying to close orders that are submitted by a NinjaScript strategy, you could loop through the Account.Orders collection to find active orders, subscribe to Account OrderUpdate events to look for any newly opened orders, check if the e.Order.Name == "Entry" in OnOrderUpdate(), check if the e.Order.IsLong == true or if the e.Order.IsShort == true, use Account.CreateOrder() to create an order to close the position, and use Account.Submit() to submit the order.

                I have included NinjaTrader 8 documentation on these items below if you wish to investigate further.

                Order object - https://ninjatrader.com/support/help...n-us/order.htm
                Account.Orders - https://ninjatrader.com/support/help...rs_account.htm
                Account.OrderUpdate - https://ninjatrader.com/support/help...rderupdate.htm
                Account.Change - https://ninjatrader.com/support/help...-us/change.htm
                Account.Cancel - https://ninjatrader.com/support/help...-us/cancel.htm
                Account.CreateOrder - https://ninjatrader.com/support/help...reateorder.htm
                Account.Submit - https://ninjatrader.com/support/help...mit.htm​
                Brandon H.NinjaTrader Customer Service

                Comment


                  #9
                  I am probably missing something ...

                  I have an addon for multi account trading that I have developed.
                  I have multiple accoountSelectors
                  I use the following commands to MANUALLY click a button and submit these orders :
                  Code:
                  for (int i = 0; i < TotalNumberOfAccounts; i++)
                  {
                  neworder = accountSelectors[i].SelectedAccount.CreateOrder(instrumentSelectors[i].Instrument, OrderAction.Sell, OrderType.Limit, TimeInForce.Day, quantityUpDowns[i].Value, double.Parse(TextBox2.Text), 0, string.Empty, "Entry", null);
                  accountSelectors[i].SelectedAccount.Submit(new[] { neworder});
                  ​
                  However, I am also running in parallel some strategies on the same accounts,

                  I have a button "CLOSE ALL" which I want to trigger a cancel of all OPEN orders that have the name "Entry" and also to close all positions that where originally executed following some of the above orders.

                  here is the loop where I cancel all orders :
                  Code:
                  for (int i = 0; i < TotalNumberOfAccounts; i++)  
                  {
                  foreach (var order in accountSelectors[i].SelectedAccount.Orders)
                  {
                      if (order.Name == "Entry" && order.OrderState == OrderState.Working)
                      {
                        accountSelectors[i].SelectedAccount.Cancel(new[] { order });
                      }
                  }
                  }


                  I don't seem to understand how to close open positions !!
                  for example , I might have an open position of total 2 contract of ES. Qty 1 was because of the strategy running in parallel , and Qty 1 is because a manual order of an addon.
                  I would like to push the "CLOSE ALL" and as a result, the total Qty will be 1 contract. 1 will be sold, because it was bought due to execution of an order that had a name "Entry".

                  in my previous post, I posted a partial loop that finds the orders , but I cant figure out how I close the position linked to these orders that are already executed.

                  Comment


                    #10
                    Hello dadarara,

                    Thanks for your notes.

                    The loop you shared should work for detecting active orders.

                    To clarify, are you wanting to close orders or positions?

                    After reading through the posts, it seems like you want to close positions. Positions do not have a name that could be detected.

                    Positions are just a quantity in a certain direction and has no specific information, such as a specific name for individual positions, and you would need to create your own custom tracking logic for this..

                    Something you could try is creating your own tracking for this. You would need to know which of the orders filled that had that name and make a count, then close X quantity off the position.

                    Instead of using the Orders collection, you could create your own custom logic that collects the orders you submit in a C# List, and then track those individual orders to know which ones are filled and which are active. Then, do a loop over the list to collect the count of the filled orders and add/remove orders from the List as needed.

                    Ultimately, it would be up to you to create your own custom logic to accomplish this.

                    If you want to close the total position then you could use <Account>.Flatten().
                    Brandon H.NinjaTrader Customer Service

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by rhyminkevin, Today, 04:58 PM
                    0 responses
                    13 views
                    0 likes
                    Last Post rhyminkevin  
                    Started by lightsun47, Today, 03:51 PM
                    0 responses
                    6 views
                    0 likes
                    Last Post lightsun47  
                    Started by 00nevest, Today, 02:27 PM
                    1 response
                    14 views
                    0 likes
                    Last Post 00nevest  
                    Started by futtrader, 04-21-2024, 01:50 AM
                    4 responses
                    48 views
                    0 likes
                    Last Post futtrader  
                    Started by Option Whisperer, Today, 09:55 AM
                    1 response
                    15 views
                    0 likes
                    Last Post bltdavid  
                    Working...
                    X