Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

NT8 current positions printout

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

    NT8 current positions printout

    Hello everyone,

    I'm new to coding in NinjaTrader 8 (NT8) and need some assistance. I want to create an indicator that sends current position information to another app (which could be a text file). I've already developed a simple trade timer that I want to automate.

    I discovered a Positions object that seems perfect for this task, but it appears to be used in strategies. I'm unclear about the differences between an indicator and a strategy. Is it possible to have a strategy running in the background that doesn't actually trade but instead just watches positions and sends that information to my Timer app? Or would I use an Indicator in this case?

    Additionally, how can I get a printout of all positions that occurred on a given day?

    Thank you in advance for your help!
    Last edited by Matt Skinner; 06-12-2024, 12:37 AM.

    #2
    Hello Matt Skinner,

    Welcome to the NinjaTrader forums!

    From an indicator the addon approach can be used to get positions from an <Account>.

    Below are links to the help guide.



    "Additionally, how can I get a printout of all positions that occurred on a given day?"

    Unfortunately, there is not a collection of historical positions. The Positions collection contains only currently open real-time positions.
    You could view the <Account>.Executions collection to see what order fills have happened today.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Amazing, thank you very much. This is exactly what I needed. As for the second part, that is shame. Hopefully executions will be enough for my purposes. Take care

      Comment


        #4
        So, I don't know why, but the code works really well on the Sim101 account. Everything gets handled well. Once I switch to my normal account though, the script no longer prints out 0 when I exit a position, it just gets stuck on whatever amount I was in before the flatten of the last order. Example: I enter 2 short, the program prints that out well. Then I exit on 1, the program catches that too. Then I flatten the last one, the program should now print out that the position is 0, but instead it prints out the position size when exiting. When I generate the execution history, the positions are shown well there (picture 1). The problem can be seen with printing out from the script (picture 2, first 2 lines are on normal account, next 4 lines are on SIM). Do normal accounts work in a different way than SIM? Looks like I need to work with the MarketPosition property instead of Quantity.

        Code:
        //This namespace holds Add ons in this folder and is required. Do not change it.
        namespace NinjaTrader.NinjaScript.AddOns
        {
            public class TimerAddon : NinjaTrader.NinjaScript.AddOnBase
            {
                protected override void OnStateChange()
                {
                    if (State == State.SetDefaults)
                    {
                        Description                                    = @"Sends position info into a text file for processing";
                        Name                                        = "TimerAddon";
                    }
                    else if (State == State.Configure)
                    {
                    }
                }
                
                private Account account;
                public TimerAddon()
                {
                    // Find account
                    lock (Account.All)
                          account = Account.All.FirstOrDefault(a => a.Name == "APEX-91531-10");
         
                    // Subscribe to position updates
                    if (account != null)
                          account.PositionUpdate += OnPositionUpdate;
                }
                
                // This method is fired as a position changes
                private void OnPositionUpdate(object sender, PositionEventArgs e)
                {
                    // Output the new position
                        string PositionSize = e.Quantity.ToString();
                    string path = "C:\\Users\\matej\\Desktop\\Trading_Code\\TradeTimer\\PositionSize.txt";
                    File.WriteAllText(path, PositionSize);
                }
            }
        }
        ​
        Picture 1

        Click image for larger version  Name:	image.png Views:	0 Size:	6.0 KB ID:	1307069
        Picture 2
        Click image for larger version  Name:	image.png Views:	0 Size:	4.4 KB ID:	1307070​​
        Last edited by Matt Skinner; 06-13-2024, 05:24 AM.

        Comment


          #5
          Hello Matt Skinner,

          You might be looking at the 'remove' operation.

          Try using the e.Position.MarketPosition and e.Position.Quantity instead.
          Chelsea B.NinjaTrader Customer Service

          Comment

          Latest Posts

          Collapse

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