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

Unrealized Profit and Loss

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

    Unrealized Profit and Loss

    Hello,

    I am trying to find the unrealized profit/ loss for open trades. More specifically, if a trade has a loss of greater than a certain percentage of my account, I would exit the trade. This is for a Back Test with multiple trades open simultaneously.

    The below code seems to get the profit or loss from the last trade, but not from necessarily an open trade. How do I get the profit/loss from an open trade?

    It seems that I can look through all trades by looping Performance.RealtimeTrades.Count - 2 etc ...

    Thank you,
    Ken



    if (Performance.AllTrades.Count > 0)
    {
    // Get the last completed real-time trade (at index 0)
    Trade lastTrade = Performance.AllTrades[Performance.RealtimeTrades.Count - 1];

    // Calculate the PnL for the last completed real-time trade
    double lastProfit = lastTrade.ProfitCurrency * lastTrade.Quantity;

    // Pring the PnL to the Output window
    Print("The last trade's profit is " + lastProfit);
    }
    Last edited by krubin; 10-23-2012, 10:05 PM.

    #2
    Hi Ken, correct you could loop through all trades, however a trade would be a completed roundturn, so not what you seek here - for this try working with GetProfitLoss() please > http://www.ninjatrader.com/support/h...profitloss.htm
    BertrandNinjaTrader Customer Service

    Comment


      #3
      Hi Bertrand,

      Thank you for pointing me in the right direction! If I have four trades open at the same time in my back testing, is there a way to itterate to the one trade, which is a looser so I can close it and keep the other 3 trades open. Thank you again for the quick response.

      Best Wishes,,
      Ken

      Comment


        #4
        You're welcome - would this be in one strategy with four individual entries then? The position would always cover your overall strategy position, it would not differentiate per entry. For this you would need to work with the IOrder objects to get access to the individual fills to monitor - http://www.ninjatrader.com/support/h...nt7/iorder.htm
        BertrandNinjaTrader Customer Service

        Comment


          #5
          Order at market

          Hi Bertrand,

          If my strategy buys an order at the market, not a limit order, which property in IOrder would I use to see the price that I bought the order at? Would limit price still work?

          Thank you again!

          Ken

          Comment


            #6
            Ken, you could then check the .AvgFillPrice of your IOrder object.
            BertrandNinjaTrader Customer Service

            Comment


              #7
              If I create an Iorder object by executing:

              entryOrder = EnterLong() and save this Iorder object in a collection.
              Say later my strategy does an ExitLong() for this order, does anything change in the Iorder object to tell me that the order is no longer active (closed)? Maybe the Iorder object is nulled out, since I'm saving a reference?

              The reason I ask this is that I wanted to have a strategy, which traded multiple orders at once and I wanted to exit an order, which was doing poorly.I was going to put the Iorder objects in a collection and wanted to understand how a closed order would be different, so I would not try to do anything with it. I realize I might be a little confused on what is actually going on here.



              Thank you,
              Ken
              Last edited by krubin; 10-26-2012, 02:36 AM.

              Comment


                #8
                Ken, no worries - this would just be the entry IOrder object you have in your collection then, the same would do then for example for exit IOrder - you can then access the fill prices for both individual entries / exits and compare / run your calcs. You would need to take care in your code that the objects are nullified when in terminal states, we have a reference on those more advanced order management methods which should be very helpful -

                The OnOrderUpdate() and OnExecution() methods are reserved for experienced programmers. Instead of using Set() methods to submit stop-loss and profit target orders, you can submit and update them manually through the use of IOrder and IExecution objects in the OnOrderUpdate() and OnExecution() methods. The OnOrderUpdate()
                BertrandNinjaTrader Customer Service

                Comment


                  #9
                  Originally posted by NinjaTrader_Bertrand View Post
                  For this you would need to work with the IOrder objects to get access to the individual fills to monitor - http://www.ninjatrader.com/support/h...nt7/iorder.htm
                  I am currently evaluating whether Ninjatrader will provide the framework I need to move forward on my project. I just wanted to weigh in on this thread (and a few others) to identify this point as a barrier to using Ninjatrader for my project.

                  I need Ninjatrader to tell me unrealized profit/loss for each open position, independently, without requiring me to replicate Ninjatrader's internal calculations.

                  Comment


                    #10
                    You can use the GetProfitLoss() mentioned in the thread for each position you have - however if you're interested of the Pnl of the individual orders / scale ins that make up this position then you would have to monitor via the custom code IOrder route already discussed.
                    BertrandNinjaTrader Customer Service

                    Comment


                      #11
                      Originally posted by GPEngine View Post
                      I am currently evaluating whether Ninjatrader will provide the framework I need to move forward on my project. I just wanted to weigh in on this thread (and a few others) to identify this point as a barrier to using Ninjatrader for my project.

                      I need Ninjatrader to tell me unrealized profit/loss for each open position, independently, without requiring me to replicate Ninjatrader's internal calculations.
                      That information is available in the NT Control Panel if you are looking for the ability to visually monitor the positions, which is what I think you are asking. Did I misunderstand you?
                      Attached Files

                      Comment


                        #12
                        I am new to this forum. But I think I am at the right thread to ask my question.
                        I have tried GetProfitLoss() method
                        but my problem is I cannot figure out how I will calculate UnRealizedPnL in Tick s of Signal10 <= -50
                        I want to calculate profit loss uniquely for every signal.
                        How can I accomplish this in NT.
                        Can you please help?

                        Comment


                          #13
                          abuomararman, so you run multiple entry signals combined in your strategy script and wish to calculate the open PnL for each signal? Is that correct in understanding?
                          BertrandNinjaTrader Customer Service

                          Comment


                            #14
                            PnL calculation Problem

                            Ninjattrader_Bertrand,Thank you so much to take time to review my question.

                            Actually
                            I need to make a series of 10 trades.
                            if market is flat,first trade.
                            if trade 1 is -50 ticks, 2nd trade
                            if 2nd trade is -50 ticks, third trade
                            till 10 steps.

                            in mt4 I calculate it very easily with magic number.
                            In Ninjatrader,I am not getting the clue how can I accomplish that.

                            I want to write the syntax

                            If Order 01 is -50 ticks, open a sell order.

                            Set StopLoss() function is not what I want.
                            I need to track each position independently.
                            based on
                            1. Profit Loss
                            2. Order Position.

                            If I can track market order individually for profit loss,i will go for the next steps.

                            Thanks

                            Comment


                              #15
                              Hello abuomararman,

                              Welcome to the forums!

                              You can manually track your orders to accomplish what you wish, and you can add another data series to calculate the difference in price for each tick that iterates through your strategy.

                              Essentially, you will want to add an additional data series that will allow OnBarUpdate() to be calculated for each tick that comes through. You may also use CalculateOnBarClose or Calculate.OnEachTick to accomplish this as well, but those settings have other implications. I suggest to add an additional data series for single tick data and add a condition in OnBarUpdate() that will only iterate for each tick. This block would be where you can recalculate the Profit Loss for your orders. You can use an IOrder object to hold the price for the order when it gets filled.

                              You will want to use BarsInProgress to detect when OnBarUpdate() is being run against your primary or additional data series. BarsArray is used to reference the data series of the specified series number. The Primary data series is always 0 and each additional data series will be 1, 2, 3, etc. I will reference the snippet in the BarsInProgress documentation as an example:

                              Code:
                              protected override void OnBarUpdate() 
                              { 
                                  // Check which Bars object is calling the OnBarUpdate() method 
                                  if (BarsInProgress == 0) 
                                  { 
                                      // A value of zero represents the primary Bars which is the ES 09-14 
                                      // 1 minute chart. 
                                      // Do something within the context of the 1 minute Bars here 
                                  } 
                                  else if (BarsInProgress == 1) 
                                  { 
                                      // A value of 1 represents the secondary data series added in the Initialize() 
                                      // If this is the per Tick data series, you may place your logic to check order losses here
                                  } 
                              }
                              In the above example the primary data series is the ES 09-14 (Noted BarsInProgress == 0). When an additional data series is added with AddDataSeries(), it becomes the secondary series (Noted BarsInProgress == 1).

                              The code block marked (BarsInProgress == 1) will be executed when the first additional data series is called. (BarsInProgress == 0) represents the primary data series that the strategy is attached to.

                              There is a sample strategy on our forums that outlines this which may be of use to you:
                              You can submit orders to different Bars objects. This allows you the flexibility of submitting orders to different timeframes. Like in live trading, taking entry conditions from a 5min chart means executing your order as soon as possible instead of waiting until the next 5min bar starts building. You can achieve this by


                              The components used are as follows:

                              Please also take the time to read the documentation on creating NinjaScripts for Multi-Time Frame & Instruments:



                              To be able to track your orders, I suggest using IOrders to hold the price data for the price the order entered.

                              We have a reference sample that demonstrates how to place protective orders using OnOrderUpdate() and OnExecution(). It also details how you can use IOrders within NinjaScript. You will want to use an IOrder to hold the price that the order was filled at so you can properly calculate the loss for that order.

                              Here is a link to the OnOrderUpdate() sample - http://ninjatrader.com/support/forum...ead.php?t=7499

                              Please also review the IOrder documentation for full syntax and usage:

                              IOrder - http://www.ninjatrader.com/support/h...nt7/iorder.htm

                              If you have any additional questions, please don't hesitate to ask.
                              JimNinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by DJ888, 04-16-2024, 06:09 PM
                              6 responses
                              18 views
                              0 likes
                              Last Post DJ888
                              by DJ888
                               
                              Started by Jon17, Today, 04:33 PM
                              0 responses
                              1 view
                              0 likes
                              Last Post Jon17
                              by Jon17
                               
                              Started by Javierw.ok, Today, 04:12 PM
                              0 responses
                              6 views
                              0 likes
                              Last Post Javierw.ok  
                              Started by timmbbo, Today, 08:59 AM
                              2 responses
                              10 views
                              0 likes
                              Last Post bltdavid  
                              Started by alifarahani, Today, 09:40 AM
                              6 responses
                              41 views
                              0 likes
                              Last Post alifarahani  
                              Working...
                              X