Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Strategy Order entry and exit issues

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

    Strategy Order entry and exit issues

    Hello fellow traders and programmers

    I created a robot/strategy which, when I leave it alone, works fine, but.....when I place an order manually using the chart trader, I can only close this out forcefully using CloseStrategy(), but this also stops the whole strategy, going against the logic that I like to program a robot (so I can play tennis ~

    Also, when the strategy orders 1 contract, and I add manually another contract, this also cannot be seen by the strategy unless I force it to close. (It sees I have x number of contracts, but cannot close out)

    Am I missing something here? I can see positions, even when I open one manually using the chart trader, but cannot do anything with it?

    (I have the same strategy running with a 'competitor' using easylanguage, and there it does not matter how the order was filled.

    #2
    Hello tullips,

    The items you described are normal for a strategy, strategies are not intended to be used with manual trading. There are a couple of ways that you could accomplish what you described.

    If you use a strategy and want to also manually trade the strategy should add custom buttons to the chart so that you can do custom actions by clicking the buttons. That allows the strategy to still submit or close orders so it can keep track of them. The other way would be to use an indicator and the addon framework. In that use case you would subscribe to the account you want to use and then the indicator can observe manual orders or it can also submit orders to that account.

    There is a sample of making custom buttons here: https://ninjatrader.com/support/help...sub=usercontro

    You can also read about the addon framework here: https://ninjatrader.com/support/help...ount_class.htm

    Comment


      #3
      Yes, and I do have buttons already on my strategy (for example I am 'starting the whole thing using a button). But it does seem weird I cannot handle the positions, for example you change a chart, come back, or changed an input value from your strategy, and 'enable' it again. The positions placed by the strategy are now 'forgotten' and cannot be addressed unless you give it a close command and it closes all positions and stops the strategy.

      The reason I am asking is that I do not put in the target/stop loss orders in advance, so the 'market' knows about this, it is placed on a certain condition in the strategy.

      You can imagine that if you must reboot your computer because Windows is doing an update, the 'strategy' is losing all its info and is just seeing 'You got positions, but I cannot do anything about it' situation.

      I was hoping for a solution for this....I am guessing I can send order ticket, check for the positions, and when the strategy is back on, it should just wait until it is flat again to restart (I was giving myself a solution here lol)

      But if you have a better, cleaner solution let me know, thanks -- Ronald


      Comment


        #4
        Hello tullips,

        Yes that would be normal when the strategy is being disabled, it only knows about what it has done while enabled. Its the same as if you disable and re enable it the performance is reset for that strategy and it starts over.

        You likely would have a better experience by using an indicator instead of a strategy, that would be able to see the overall account and any existing positions or orders. You could still continue using the buttons you made in an indicator. That could be reloaded and still keep track of what's on the account as well. A downside to doing it this way would be that the indicator can't be backtested.

        There is a sample on this page which shows finding and subscribing to an accounts events: https://ninjatrader.com/support/help...ount_class.htm

        Once you have a account variable you can use any of the other items like the orders or positions collections:



        You can also submit orders to the account:


        While you technically could do the same concept in a strategy we don't recommend using the addon account in strategies. The reason for that is the strategy still wont see any of the addon framework orders or changes to the position so its information would be out of sync. By using an indicator you force yourself to avoid any problems by excluding any of the strategies normal properties or methods.

        Comment


          #5
          Thank you, Jesse. I now know seeing this that I must rebuild the strategy into an indicator, but I do not mind. I am also using an indicator on TS to place orders, I just never thought that NT was also able to do this.

          And not using a strategy does not bother me, as back testing is not needed, this strategy was already proved and works fine.
          I was also seeing you have a flatten() function as well, this could be my backup in case I still have 'leftovers'

          Comment


            #6
            Hello Jesse

            I was able to port my strategy into an indicator and it is working well, but......I am running into the same issue here as well. When I refresh the chart with CTRL + R I lose my positions tally.

            The positions themselves are still there, just the indicator does not know it anymore. And using this NinjaTrader 8trick to show that you have positions does not show the AMMOUNT of what I have in this instrument.

            I tried but it throws errors when using myPositions.Quantity:

            lock (myAccount.Positions)
            {
            Print("Positions in State.DataLoaded:");

            foreach (Position myPosition in myAccount.Positions)
            {

            Print(String.Format("Position: {3} - {0} at {1}", myPosition.MarketPosition, myPosition.AveragePrice, myPosition.Quantity.ToString()));

            }
            }

            Any idea how to get the number of positions of my instrument (symbol)? (this would also be the same when attaching the indicator to a chart which already has positions)

            Thanks -- Ronald​

            Comment


              #7
              Hello tullips,

              When you reload the script you have to essentially do everything over again like finding the account and then re loop through the positions to find any open positions which may be relevant.

              What error are you seeing when you try to loop over the positions?

              Comment


                #8
                Hello Jesse. I have found a solution which is working fine, the programming style might not be the best, but I do not believe this is slow. Here under is my code

                // This method is fired on any change of an account value
                private void OnAccountItemUpdate(object sender, AccountItemEventArgs e)
                {
                // Output the account item
                var myArray = e.Account.Positions.ToArray(); // put all positions from this account into an array
                var myString = ""; // in here we put in the content from the array
                char[] delimiterChars = { ' ' };

                if (myArray.Length > 0) // now we know there are some positions in this account
                {
                foreach (var item in myArray)
                {
                myString = item.ToString();
                if (myString.Contains(Instrument.FullName)) // here we look for the symbol to match
                {
                string[] words = myString.Split(delimiterChars);
                Print("We have " + words[5] + " quantity= " + words[4].Substring(9) + " from symbol " + Instrument.FullName + " with an average price of " + words[3].Substring(9));
                }

                }
                }
                }
                ​

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                0 responses
                647 views
                0 likes
                Last Post Geovanny Suaza  
                Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                0 responses
                368 views
                1 like
                Last Post Geovanny Suaza  
                Started by Mindset, 02-09-2026, 11:44 AM
                0 responses
                108 views
                0 likes
                Last Post Mindset
                by Mindset
                 
                Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                0 responses
                571 views
                1 like
                Last Post Geovanny Suaza  
                Started by RFrosty, 01-28-2026, 06:49 PM
                0 responses
                573 views
                1 like
                Last Post RFrosty
                by RFrosty
                 
                Working...
                X