Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

To get Trades of real account

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

    To get Trades of real account

    Hi Support--

    You do great jpb! I've got good help on all of my inquiries and I have another one.

    NT7 has a function to display trades and its results http://www.ninjatrader.com/support/h..._data_tabs.htm

    I'd like my script can get it either. I mean not only script's trades but manualy too.

    Earler you gave me the following code to get access to real account data

    foreach (Account acct in Cbi.Globals.Accounts)
    {
    if (acct.Positions != null)
    {
    PositionCollection positions = acct.Positions;
    foreach (Position pos in positions)
    {
    Print(pos.Account.Name + " " + pos.Instrument + " " + pos.MarketPosition + " " + pos.Quantity + " " + pos.AvgPrice);
    }
    }

    I tried to use it together with Trades/ TradeCollection/ Performance methods but it doesn't work.

    I'm beginner in coding and may not new or understand something.

    Please, Help!

    Thank you in advance.

    #2
    Hi Alex, thanks for the kind words. Accessing those performance data tabs would unfortunately not be supported by us and I would not have a further snippet here other than the one you posted as well to start 'playing' in this area. Rebuilding those tabs in your script would not be trivial and I'm not entirely sure how far one could get. What info you want to have available in your script?

    Comment


      #3
      Hi Bernart! Thank you for your quick reply

      The idea is that the script prints information about a trade on a chart.

      I need
      enter price,
      exit price,
      type of trade (long or short),
      result of trade (profit or loss).

      I’m trying to do these OnExecution() method call where the script should understands whether it closeing of a position or entering in a new one. The only variable useful for me I can get here is a price of an execution (execution.Price), which I interpret as exit price. Playing with execution.MarketPosition and execution.OrderAction I’v achieved to determine closeing and opening of a trade, but the code is not stable. And the only what I need here is the entering price. I have some ideas of keeping it in a verible and use in my calculations, but the problems are that I use the same script in different instrument and the veriable is nuuled each time I reload the chart.

      I’ve read about Trade http://www.ninjatrader.com/support/h...nt7/index.html

      and I’m thinking it ideal suits me. But it uses “Performance” which holds script’s trdes data.

      I want to use “Trades” in working with my acoount trades, but my codeing experience is poor and may be I’m just need the right code. Using “Trade” I avoid the lots issues, I’m trying to go different way to solve the problem.

      From the other hand NT7 displays the information about trades, so there should be ways to get it programmeticaly.

      Bernart, any thoughts wellcome!

      Regards,
      Alex.

      Comment


        #4
        Hi Alex, you are correct the Trade class is really for NinjaScript strategies only - there's no supported way to access the overall full account performance stats, so those would need to be rebuild by custom coding - which of the executions are only a single piece unfortunately. You still would need to tie that together to arrive at the final tracking. What you can review as ideas in addition are the MarketAnalyzer type columns, they often uses undocumented NinjaScript internally for their calcs.

        An indicator emailing the executions per account could be as well found here, however I think you're aware of the methods used by now - http://www.ninjatrader.com/support/f...d=6&linkid=533

        If you're not an advanced coder it might be a subject to pass on for now.

        Comment


          #5
          Bertrand,
          I have a similar question, and your website acted strange when I tried to start a new post, so I'm asking my question here. I want to access trade data as well, but from within ninjascript. What I want is to get the last actual executed order date, time and price. So for example, just before I ExitLong I want to get the last executed order date, time, price, which would in this example be the last EnterLong information.
          Questions:
          1) Do I have to get that from within OnExecute only?
          2) What call is there for the information of the last executed order?

          The goal is to get the same information (only a few fields) as is listed in the performance trade list.

          Comment


            #6
            That's correct tradetree, OnExection() with your entry IOrder would be a good place to get that info, alternatively the Performance class would hold the trade entry / exit execution as well, available to you for example in your OnBarUpdate() -

            Comment


              #7
              Ok thanks. Here is my intended code:

              protected override void OnExecution(IExecution execution)
              {
              if( execution.Order != null ){
              if( (execution.Order.OrderState == OrderState.PartFilled) | (execution.Order.OrderState == OrderState.Filled) ){
              if( (execution.Order.OrderAction == OrderAction.Sell) || (execution.Order.OrderAction == OrderAction.BuyToCover) ){
              Print( "Exit: " + execution.ToString() );
              }else{
              Print( "Entry: " + execution.ToString() );
              }
              }
              }
              }


              Originally posted by NinjaTrader_Bertrand View Post
              That's correct tradetree, OnExection() with your entry IOrder would be a good place to get that info, alternatively the Performance class would hold the trade entry / exit execution as well, available to you for example in your OnBarUpdate() -

              http://www.ninjatrader.com/support/h.../nt7/trade.htm

              Comment


                #8
                Hi tradetree,

                I wanted to mention that the code you have here would be for the execution as it is happening.

                So if you call ExitLong() the info in the execution that prints immediately would be for that ExitLong order.

                I mention this because it sounds like just before you ExitLong() you would like to print the information from the entry that this exit would be closing.

                Is this the case?
                Chelsea B.NinjaTrader Customer Service

                Comment


                  #9
                  Yes, I am trying to capture trades as they take place. So here is my updated and tested code. It functions just as I intended. It captures the trade entry into "trade_entry" global variable, then prints the entry and exit when the exit takes place. I actually store all these entry/exits into a file, but that's not shown here, just the printout of them.

                  Note that I found it strange and disturbing that I had to put "if( !Historical )" in to keep some random historical trades from showing up. I thought OnExecution would either be real-time only or historical and real-time. But what I found is that parts of historical trades would show up?


                  protected override void OnExecution(IExecution execution){
                  if( execution.Order != null ){
                  if( (execution.Order.OrderState == OrderState.PartFilled) | (execution.Order.OrderState == OrderState.Filled) ){
                  if( (execution.Order.OrderAction == OrderAction.Sell) || (execution.Order.OrderAction == OrderAction.BuyToCover) ){
                  trade_exit = "Exit:" + execution.ExecutionId.ToString() + ":" + execution.Instrument.FullName.Split(' ')[0].ToString() + ":" + execution.Order.OrderAction.ToString() + ":" + execution.Quantity.ToString() + ":" + execution.Price.ToString() + ":" + execution.Time.ToString();
                  if( !Historical ){
                  Print( trade_entry );
                  Print( trade_exit );
                  }
                  }else{
                  trade_entry = "Entry:" + execution.ExecutionId.ToString() + ":" + execution.Instrument.FullName.Split(' ')[0].ToString() + ":" + execution.Order.OrderAction.ToString() + ":" + execution.Quantity.ToString() + ":" + execution.Price.ToString() + ":" + execution.Time.ToString(); // #threefold
                  }
                  }
                  }
                  }

                  Comment


                    #10
                    Hi tradetree,

                    Yes, all historical trades will trigger OnOrderUpdate -> OnExecution -> OnPositionUpdate.

                    These method calls are not limited to real time data.

                    If you would like to prevent historical trades you can add the following to the beginning of OnBarUpdate():

                    If (Historical)
                    return;
                    Chelsea B.NinjaTrader Customer Service

                    Comment


                      #11
                      But I don't see how your answer explains what I wrote here,
                      "Note that I found it strange and disturbing that I had to put "if( !Historical )" in to keep some random historical trades from showing up. I thought OnExecution would either be real-time only or historical and real-time. But what I found is that parts of historical trades would show up?"
                      So let me clarify this. 95% of trades do not hit OnExecution if they are Historical. It is mainly just real-time live trades that hit OnExecution. But every now and then a Historical trade hits, about 5% of the time. There is no clear reason why only a few get through.

                      You say, "these method calls are not limited to real time data". So what IS the criteria? When do they hit OnExecution and when not? Also note that I'm not referring to OnBarUpdate at all, but OnExecution. OnBarUpdate always hits all bars. At least it is consistent. OnExecution is not. I would like to get the details for when OnExecution is hit.

                      Originally posted by NinjaTrader_ChelseaB View Post
                      Hi tradetree,

                      Yes, all historical trades will trigger OnOrderUpdate -> OnExecution -> OnPositionUpdate.

                      These method calls are not limited to real time data.

                      If you would like to prevent historical trades you can add the following to the beginning of OnBarUpdate():

                      If (Historical)
                      return;

                      Comment


                        #12
                        Hi tradetree,

                        The OnExecution will be triggered for every order submission in historical data.

                        Before using all of the exclusion statements to catch orders, I would recommend that you temporarily print the execution object to string and ensure that orders are not triggering the OnExecution.

                        I've attached a simple strategy that makes orders and prints the type of order to string as the execution happens. We can modify this to print any type of order.
                        Attached Files
                        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
                        646 views
                        0 likes
                        Last Post Geovanny Suaza  
                        Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                        0 responses
                        367 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
                        570 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