Go Back   NinjaTrader Support Forum > Historical Beta Archive > Version 8 Beta

Version 8 Beta Historical archive for NinjaTrader 8 beta.

NinjaTrader
Reply
 
Thread Tools Display Modes
Old 10-20-2016, 06:32 PM   #1
fbertram
Member
 
Join Date: Sep 2014
Location: Seattle
Posts: 57
Thanks: 20
Thanked 4 times in 4 posts
Default 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
File Type: zip issue_OpenPositionProfit.zip (2.2 KB, 6 views)
fbertram is offline  
Reply With Quote
Old 10-21-2016, 09:45 AM   #2
NinjaTrader_Paul
NinjaTrader Customer Service
 
NinjaTrader_Paul's Avatar
 
Join Date: May 2014
Posts: 2,853
Thanks: 335
Thanked 662 times in 639 posts
Default

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.
NinjaTrader_Paul is offline  
Reply With Quote
Old 10-26-2016, 12:57 PM   #3
NinjaTrader_PatrickH
NinjaTrader Customer Service
 
NinjaTrader_PatrickH's Avatar
 
Join Date: Jul 2011
Location: Denver, CO
Posts: 10,319
Thanks: 744
Thanked 1,744 times in 1,680 posts
Default

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:
NinjaTrader_PatrickH is offline  
Reply With Quote
Old 10-26-2016, 12:57 PM   #4
NinjaTrader_PatrickH
NinjaTrader Customer Service
 
NinjaTrader_PatrickH's Avatar
 
Join Date: Jul 2011
Location: Denver, CO
Posts: 10,319
Thanks: 744
Thanked 1,744 times in 1,680 posts
Default

Your item on the Unrealized PnL and Position Average Price has been reported to development.
NinjaTrader_PatrickH is offline  
Reply With Quote
The following user says thank you to NinjaTrader_PatrickH for this post:
Old 10-26-2016, 03:01 PM   #5
fbertram
Member
 
Join Date: Sep 2014
Location: Seattle
Posts: 57
Thanks: 20
Thanked 4 times in 4 posts
Default

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
fbertram is offline  
Reply With Quote
Old 11-02-2016, 12:40 PM   #6
NinjaTrader_Paul
NinjaTrader Customer Service
 
NinjaTrader_Paul's Avatar
 
Join Date: May 2014
Posts: 2,853
Thanks: 335
Thanked 662 times in 639 posts
Default

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
File Type: cs UpdatedIssueOpenPosition.cs (5.8 KB, 3 views)
NinjaTrader_Paul is offline  
Reply With Quote
Old 11-03-2016, 09:59 AM   #7
fbertram
Member
 
Join Date: Sep 2014
Location: Seattle
Posts: 57
Thanks: 20
Thanked 4 times in 4 posts
Default

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
File Type: zip OpenPositionProfitExcel.zip (8.6 KB, 6 views)
fbertram is offline  
Reply With Quote
Old 03-31-2017, 12:45 PM   #8
NinjaTrader_Paul
NinjaTrader Customer Service
 
NinjaTrader_Paul's Avatar
 
Join Date: May 2014
Posts: 2,853
Thanks: 335
Thanked 662 times in 639 posts
Default

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
NinjaTrader_Paul is offline  
Reply With Quote
The following user says thank you to NinjaTrader_Paul for this post:
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Profit target discrepancy sandman Platform Technical Support 3 10-03-2014 10:36 AM
Open position with take profit and stop loss shayhz Platform Technical Support 15 06-27-2013 12:14 PM
Open Position on daily Open Luigio Strategy Development 1 01-19-2013 02:21 PM
Open Position Profit/Loss 4x_Newbie Platform Technical Support 3 11-10-2009 11:57 AM
Position stays open after a profit target met kgillis23 Platform Technical Support 1 10-30-2007 04:58 AM


All times are GMT -6. The time now is 06:09 PM.

Futures, foreign currency and options trading contains substantial risk and is not for every investor. An investor could potentially lose all or more than the initial investment. Risk capital is money that can be lost without jeopardizing ones financial security or lifestyle. Only risk capital should be used for trading and only those with sufficient risk capital should consider trading. Past performance is not necessarily indicative of future results. View Full Risk Disclosure.

CFTC Rules 4.41 - Hypothetical or Simulated performance results have certain limitations, unlike an actual performance record, simulated results do not represent actual trading. Also, since the trades have not been executed, the results may have under-or-over compensated for the impact, if any, of certain market factors, such as lack of liquidity. Simulated trading programs in general are also subject to the fact that they are designed with the benefit of hindsight. No representation is being made that any account will or is likely to achieve profit or losses similar to those shown.

This website is hosted and operated by NinjaTrader, LLC (“NT”), a software development company which owns and supports all proprietary technology relating to and including the NinjaTrader trading platform. NT is an affiliated company to NinjaTrader Brokerage (“NTB”), which is a NFA registered introducing broker (NFA #0339976) providing brokerage services to traders of futures and foreign exchange products. This website is intended for educational and informational purposes only and should not be viewed as a solicitation or recommendation of any product, service or trading strategy. No offer or solicitation to buy or sell securities, securities derivative or futures products of any kind, or any type of trading or investment advice, recommendation or strategy, is made, given, or in any manner endorsed by any NT affiliate and the information made available on this Web site is not an offer or solicitation of any kind. Specific questions related to a brokerage account should be sent to your broker directly. The content and opinions expressed on this website are those of the authors and do not necessarily reflect the official policy or position of NT or any of its affiliates. 

Vendors along with their websites, products and services, collectively referred to as (“Vendor Content”), are independent persons or companies that are in no manner affiliated with NT or any if its affiliates. NT or any of its affiliates are not responsible for, do not approve, recommend or endorse any Vendor Content referenced on this website and it’s your sole responsibility to evaluate Vendor Content. Please be aware that any performance information provided by a vendor should be considered hypothetical and must contain the disclosures required by NFA Rule 2-29(c). If you are interested in learning more about, or investigating the quality of, any such Vendor Content you must contact the vendor, provider or seller of such Vendor Content. No person employed by, or associated with, NT or any of its affiliates is authorized to provide any information about any such Vendor Content. Visit the CFTC resources for education regarding the industry and signs of fraud.