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

Realized Profit Issues While In Trade

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

    Realized Profit Issues While In Trade

    Hi,

    I have a Strategy that takes an initial position (long or Short) and then adds additional contracts and then covers them throughout the trade all while keeping the initial position open. I use Named orders like LE2, LE3 to establish additional position and then I use named Exits from Entries like LX2, LX3 which match to the entry orders when covering.

    My problem is that my Relized profit Calulations don't match what is show in Strategy Tab of Control Center while the trade is still open and don't seem to make sense to me at all.

    I think it may have to do with the FIFO and LIFO alogrithims that the functions use, I have the Options-->General--> Use FIFO unchecked

    What I do it take the realized profit(loss) + the unrealized profit(loss) and if it is above a certain level I want to exit and stop trading for the day.

    Any help would be appreciated.....

    Here is my code:

    DoneTrading = false;
    TdyTotalRealizedProfitLoss = Performance.RealtimeTrades.TradesPerformance.Curre ncy.CumProfit;
    if ( Position.Quantity > 0 )
    CurUnRealizedProfitLoss = Position.GetProfitLoss(Close[0], PerformanceUnit.Currency);
    else
    CurUnRealizedProfitLoss = 0;

    TdyTotalNetProfitLoss = (TdyTotalRealizedProfitLoss + CurUnRealizedProfitLoss );

    if ( TdyTotalNetProfitLoss < 0 && Math.Abs(TdyTotalNetProfitLoss) >= DailyLossLimit )
    {
    OkToTradeNow = false;
    DoneTrading = true;
    if ( Position.MarketPosition == MarketPosition.Long )
    ExitLong();
    if ( Position.MarketPosition == MarketPosition.Short )
    ExitShort();
    Alert("LossLimit", NinjaTrader.Cbi.Priority.High, "Loss Limit Exceeded, Done Trading Today", "Alert3.wav", 3, Color.Black, Color.Yellow);

    }

    #2
    Someone will look into this later today.
    RayNinjaTrader Customer Service

    Comment


      #3
      Ray,

      I have not heard from anyone on this issue.

      Can you let me know the status?

      Thanks....

      Mark

      Comment


        #4
        This maybe something to do with the commission schedule you have set in your NinjaTrader. Are you currently applying commissions to the PnL calculations?

        Tools->Option->Apply commission to PnL calculations

        Also, please give this attachment a try. Open up the Output Window. You should be able to watch the PnL print match the unrealized PnL in Control Center.
        Attached Files
        Last edited by NinjaTrader_JoshP; 05-09-2008, 11:23 AM.
        Josh P.NinjaTrader Customer Service

        Comment


          #5
          Josh,

          Thanks, the problem I am having is with Realized P&L in the middle of a trade not the unrealized P&L that matches fine (see my code).

          The following line of code (does not match Strategy Manager Tab) and does not give the correct value (nor do other similar functions I have tried), which is why I beleive that it may have to do with LIFO or FIFO settings because of the buying and covering while the main trade is on, which affects the Average Entry Calculations on which Relized profit is based (i think)

          TdyTotalRealizedProfitLoss = Performance.RealtimeTrades.TradesPerformance.Curre ncy.CumProfit

          The problem is not with commissions.

          BTW When all trades are closed (and flat) the calculation above returns the same value as the Strategy Manager Tab, it is only in the middle of the trade that the problem arises.

          Thanks....

          Mark

          Comment


            #6
            Mark,

            Please confirm that the attached strategy works. PnL (realized and unrealized) match up for me on my end. If this does not match up for you with the attached strategy then we will proceed further. If it does than some debugging will be needed in your particular strategy.

            The first printed number will be realized PnL and the second is unrealized PnL. Connect to the Simulated Data Feed and open a 2 second empty chart of any instrument. Throw the strategy on and let it run. Watch the Output Window in relation to the Control Center.
            Attached Files
            Josh P.NinjaTrader Customer Service

            Comment


              #7
              Josh,

              Your strategy works fine as I expected it would.

              However, your straetegy does not nearly do the same types of orders as mine. Also my straetgy shows the correct Realized and unrealized when it is flat.

              As I indicated I have a Strategy that takes an initial position (long or Short) and then adds additional contracts and then covers them throughout the trade all while keeping the initial position open. I use Named orders like LE2, LE3 to establish additional position and then I use named Exits from Entries like LX2, LX3 which match to the entry orders when covering. This could go on fro hours adding LE2 and LE3 then covering LE3 then adding LE3 then cover LE3 and then LE2 then add LE2 then cover LE2, etc all while LE1 is open. In this way the Position never gets flat (until stopped out) so the relized P&L is very much affected by the FIFO & LIFO rules.

              My case is way more complicated and uses named orders, etc.

              As I stated before I think it may have to do with the FIFO and LIFO alogrithims that the functions use, I have the Options-->General--> Use FIFO unchecked

              In every other way my strategy works fine and there is no bug at all while this is going on, the only problem is the realized $ number which I have no programatic control over.

              I don't see this as a problem with my strategy, only that it is active and with the interpretation of it's intra-trade P&L.

              Any other suggestions?

              Mark

              Comment


                #8
                To double check,

                Are you saying that the following values:

                TdyTotalRealizedProfitLoss = Performance.RealtimeTrades.TradesPerformance.Curre ncy.CumProfit;

                does NOT match the Strategies tab realized PnL column?
                RayNinjaTrader Customer Service

                Comment


                  #9
                  Ray,

                  Correct!

                  Within my strategy while in an open trade as described in my posts, they don't match.

                  They match when the strategy is flat.

                  Mark

                  Comment


                    #10
                    Not sure they are supposed to match or not, will get clarification from development.
                    RayNinjaTrader Customer Service

                    Comment


                      #11
                      The bigger problem is I need an actual Realized + Unrealized to stop trading if I am off more than x dollars in the account for the strategy.

                      So if realized is 300 and unrealized is 400 then the loss is 700 right now (in the middle of a trade) and I need to stop trading and turn off the system immediately.

                      It is critical that this calulation is correct so I can take appropriate steps.

                      It is not correct as it is now and so I may ending losing way more than $700 dollars because my realized number is not right (and often less then the actual loss in the account).

                      DO you see the issue. I really don't care what Strategy Tab says I need to know what my current loss is on the day at any given moment.

                      Thanks....

                      Mark

                      Comment


                        #12
                        I guess then you would have to create a reference sample that demonstrates the problem you see.

                        - I just created a simple strategy that took two entries with a stop and target
                        - I added code to ensure that it will only execute in real-time
                        - I reset my Sim101 account so the PnL is zero
                        - I printed out the realized PnL of the strategy on each bar (5 second bars)
                        - In every possible scenario, the realized PnL printed out by the strategy, the strategy tab PnL and the Accounts tab PnL were in sync as I would expect

                        Not sure if this may be relevant but if your strategy starts with an open position (virtually created) of 1 long (for example) and your flat in your actual acount. The strategy real-time PnL will include a virtual execution and for sure would not match your actual account PnL.
                        RayNinjaTrader Customer Service

                        Comment


                          #13
                          Ray, thanks for the effort!

                          So what is the next step? You want a sample that demonstrates the issue?

                          That will not be so easy, because of the complex nature of the orders. I would end up having to give you the strategy itself.


                          Mark

                          Comment


                            #14
                            What I first suggest is to run an isolated test like I just did.

                            Add at the top of OnBarUpdate() and any other event driven method:

                            if (Historical) return;

                            then reset your sim account so everything is at zero. Then add one line of code in OnBarUpdate();

                            Print(Performance.RealtimeTrades.TradesPerformance .Currency.CumProfit);
                            Run in real-time tomorrow and see what happens.
                            RayNinjaTrader Customer Service

                            Comment


                              #15
                              Ok, I built a new strategy that just does the most simple version of my orders.

                              The problem surfaced in four 5 minute bars based on a replay of the June Contract Mini Dow for 5/12 data starting at 8:30 am.

                              If you watch throughout the day the output window realized PnL really gets way out of sync.

                              I would be happy to send you the Strategy so you can see for yourself.

                              Is there a private e-mail I can use? Also, I should send my replay file of the 5/12 so you can do exactly what I did.

                              Mark

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by cre8able, Today, 03:20 PM
                              0 responses
                              5 views
                              0 likes
                              Last Post cre8able  
                              Started by Fran888, 02-16-2024, 10:48 AM
                              3 responses
                              47 views
                              0 likes
                              Last Post Sam2515
                              by Sam2515
                               
                              Started by martin70, 03-24-2023, 04:58 AM
                              15 responses
                              114 views
                              0 likes
                              Last Post NinjaTrader_Jesse  
                              Started by The_Sec, Today, 02:29 PM
                              1 response
                              7 views
                              0 likes
                              Last Post NinjaTrader_Jesse  
                              Started by jeronymite, 04-12-2024, 04:26 PM
                              2 responses
                              31 views
                              0 likes
                              Last Post NinjaTrader_BrandonH  
                              Working...
                              X