• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

discrepancy in open position profit

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

    discrepancy in open position profit

    I have attached a code example demonstrating a discrepancy in the calculation of the open position profit. There is lots of comments inside the code to explain why I believe this to be a bug.

    Please let me know if there is any additional information I can provide.


    Thank you,
    best regards, Felix
    Attached Files

    #2
    Hello fbertram,

    Thanks for your post, code and steps to reproduce.
    I was able to reproduce.

    We will investigate and update this thread when we have further information.

    Thanks for reporting.
    Paul H.NinjaTrader Customer Service

    Comment


      #3
      Hello fbertram,

      Thank you for your post.

      The last bar is not processed as the Strategy Analyzer is running over historical data. The only way a bar knows it is closed is by the opening of another bar. As we are running on historical data the bar calculates on the close and as the last bar has not closed technically then it does not call OnBarUpdate().

      You could force this by writing Calculate.OnEachTick or Calculate.OnEachPriceChange in your code. Or you could use Tick Replay with Calcualte.OnPriceChange.

      For information on Calculate and Tick Replay please visit the following links:
      Patrick H.NinjaTrader Customer Service

      Comment


        #4
        Your item on the Unrealized PnL and Position Average Price has been reported to development.
        Patrick H.NinjaTrader Customer Service

        Comment


          #5
          Hi Patrick,

          thank you for explaining this. While I can follow you from the technical point of view, I would still disagree. As I am running from historic bars, it is quite well known that the last bar I downloaded has closed, especially as I am not simulating until today, but until yesterday. Even though I disagree, I can live with that.

          Having said that, we should make sure that things are consistent. If we really don't process the last bar, then the backtester should also close all open positions on the close of the last bar processed. Right now, that is not the case: the backtester closes all open positions one bar later (the very bar we didn't process). I would consider this inconsistent behavior a bug.

          Thank you,
          best regards, Felix

          Comment


            #6
            Hello fbertram

            Development has passed back your code with modifications to show how the value would be calculated manually inside a strategy.

            If this is unclear, please be more specific about what you are expecting in the calculation.
            Attached Files
            Paul H.NinjaTrader Customer Service

            Comment


              #7
              Hi Paul,

              thank you for your follow up.

              I appreciate your developers taking the time to disclose how NinjaTrader is internally calculating the open position profit. Unfortunately, that calculation is incorrect, which is why I started this thread in the first place.

              I believe we can all agree that this formula needs to be true at all times:
              <total portfolio value> = <initial capital> + <net profit loss> + <unrealized profit loss> - <total commission>

              Your way of calculating unrealized profit loss however leads to this criterion/ formula not being met.

              What you need to calculate the unrealized profit loss, is the open quantity, the purchase price of the open shares, and the closing price of the open shares. The entry price needs to be calculated by using a FIFO. The shares bought first are also sold first, and to calculate the average entry price, you need to go through the list and check the price at which you have purchased each share. Your method of trying to calculate an average entry price does not do that and leads to very wrong results.

              I have attached an Excel sheet showing your way of calculating in red and the correct method in green. Please also revisit the code that I have originally posted in this thread, which implements the FIFO method. I really hope we can get this sorted out, as I would expect a reputable tool like NinjaTrader to follow typical accounting methods.

              Thank you,
              best regards, Felix
              Attached Files

              Comment


                #8
                Hello fbertram,

                Thanks for your reply.

                My apologies for letting this slip by without responding.

                Please go to tool>Options>Trading and enable "Use FIFO for position average price calculations" and re run your original strategy with the expectation that the numbers will match.

                Reference: http://ninjatrader.com/support/helpG...ns_trading.htm
                Paul H.NinjaTrader Customer Service

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by rickyblah12, Today, 09:17 PM
                0 responses
                1 view
                0 likes
                Last Post rickyblah12  
                Started by rickyblah12, Today, 08:27 PM
                0 responses
                3 views
                0 likes
                Last Post rickyblah12  
                Started by giogio1, Today, 06:55 PM
                0 responses
                1 view
                0 likes
                Last Post giogio1
                by giogio1
                 
                Started by priceisking, Today, 06:39 PM
                0 responses
                2 views
                0 likes
                Last Post priceisking  
                Started by vivekniwas, Today, 05:59 PM
                0 responses
                1 view
                0 likes
                Last Post vivekniwas  
                Working...
                X