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

getting position data once closed

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

    getting position data once closed

    Hi there,

    I am attempting to do some Machine Learning so i need to know the success/failure once a position closes. I did not see how to do this using method onexecution.

    Also, is there a way to know once the strategy has finished so i can loop through all my trades?

    Thanks!

    #2
    Hello jaboo,

    Thank you for your post.

    Executions of Exit orders may be checked within OnExecutionUpdate if your Exits are given a unique signal name, or you may check when Position.MarketPostition == MarketPosition.Flat in OnPositionUpdate.

    Here are Help Guide links for more information about OnExecutionUpdate and MarketPosition.

    OnExecutionUpdate - https://ninjatrader.com/support/help...tionupdate.htm

    OnPositionUpdate - https://ninjatrader.com/support/help...tionupdate.htm

    MarketPosition - https://ninjatrader.com/support/help...etposition.htm

    SystemPerformance may be used to check the strategy's performance programmatically. SystemPerformance object holds all trades and trade performance data generated by a strategy.

    Please reference the Help Guide link below for information about using SystemPerformance.
    SystemPerformance - https://ninjatrader.com/support/help...erformance.htm

    Please let us know if we may further assist.

    Brandon H.NinjaTrader Customer Service

    Comment


      #3
      Appreciate you getting back to me! I may not have explained correctly.

      If i have two orders

      Order A = LongMarket("Order A");
      Order B = LongMarket("Order B");

      For whatever reason Order A and Order B close at the same time (profit of sl hit). How can i know that the profit for Order A is X and profit for Order B is X.

      If i use Trade lastTrade = SystemPerformance.AllTrades[SystemPerformance.AllTrades.Count - 1]; <- this will give me the last Order but it does not tell me if its Order A or Order B.

      Thanks!

      Comment


        #4
        to add. I realized a possible solution would be to track the OrderID and Order in a dictionary eg Dictionary<orderID, Order> and then in OnExecution grab the order by the OrderId. However in the docs it says that the OrderID is not unique and that it changes.If this is the case, at what point does the orderID change so i know to update the dictionary?

        Comment


          #5
          Hello jaboo,

          Thank you for your reply.

          The orderids change when the id generated by the broker comes back and when historical orders become realtime. Each trade has an Entry and Exit Execution property. We recommend checking the order name from the Exit Execution in the Trade. Please refer to the following example.

          SystemPerformance.AllTrades[SystemPerformance.AllTrades.Count - 1].Exit.Order.Name

          Here are Help Guide links about Trade, Execution, and Order in NinjaTrader.

          Trade - https://ninjatrader.com/support/help...nt8/?trade.htm

          Execution - https://ninjatrader.com/support/help...?execution.htm

          Order -https://ninjatrader.com/support/help...nt8/?order.htm

          Please let us know if we may assist further.


          Brandon H.NinjaTrader Customer Service

          Comment


            #6
            Thank you for the reply. I'm willing to bet i am just not explaining what im asking clearly enough so i'll give it another shot.

            at a very base level i want to know when an order exits with a profit for loss for that SPECIFIC order.

            Order x;
            Order y;

            if(a) x = enterLong("longA");
            if(b) y = enterLong("longB")

            ....a period of time elapses and the SL is hit on an order so it closes and therefor fires a tradeexecution.

            question -
            1. How can i tell that the order that JUST exited and fired the trade execution is either Order x or Order y. Because the ID is not unique i cannot reference order x or order y.


            SystemPerformance.AllTrades[SystemPerformance.AllTrades.Count - 1].Exit.Order.Name <- this just gives me "Stop loss" or "Profit target" but again says nothing about which order (Order x, Order y) was exited.

            for added context, given certain conditions are met I will enter a trade. When i decide to enter a trade there is a lot of properties I want to save. Once this order is complete (exits) I want to save the result/

            example.

            class MyOrder : Order

            bool condition1;
            bool condition2;
            double pnl;



            Onbarupdate()

            if(condtionXYZ) MyOrder o = new MyOrder(true,false)


            .......time goes by

            OrderExecution() o.pnl = trade.profitpoints


            if i could get a unique id i could easily just reference the order by id.

            Comment


              #7
              Hello jaboo,

              Thank you for the clarification.

              Please reference the following example that gets the entry order name of the trade that was just closed.

              SystemPerformance.AllTrades[SystemPerformance.AllTrades.Count - 1].Entry.Order.Name

              Please let us know if you have any more inquiries.
              Brandon H.NinjaTrader Customer Service

              Comment


                #8
                beautiful! that makes complete sense thank you!

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by sidlercom80, 10-28-2023, 08:49 AM
                168 responses
                2,262 views
                0 likes
                Last Post sidlercom80  
                Started by Barry Milan, Yesterday, 10:35 PM
                3 responses
                10 views
                0 likes
                Last Post NinjaTrader_Manfred  
                Started by WeyldFalcon, 12-10-2020, 06:48 PM
                14 responses
                1,429 views
                0 likes
                Last Post Handclap0241  
                Started by DJ888, 04-16-2024, 06:09 PM
                2 responses
                9 views
                0 likes
                Last Post DJ888
                by DJ888
                 
                Started by jeronymite, 04-12-2024, 04:26 PM
                3 responses
                41 views
                0 likes
                Last Post jeronymite  
                Working...
                X