Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

One-Off Error in Array for PnL

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

    One-Off Error in Array for PnL

    I'm using a sample code from help guide to print some trade profit https://ninjatrader.com/support/help...nt7/?trade.htm

    however, when I implemented this code here:

    Code:
    [COLOR=black]if( SystemPerformance.AllTrades.Count > 0 ) [/COLOR]
    [COLOR=black] [/COLOR]
    [COLOR=black]{ [/COLOR]
    [COLOR=black][/COLOR] 
    [COLOR=black]Trade lastTrade = SystemPerformance.AllTrades[SystemPerformance.AllTrades.Count - 1];[/COLOR]
    [COLOR=#000000][/COLOR] 
    [COLOR=black][/COLOR]
    [COLOR=black]ProfitLoss_LastTrade = lastTrade.ProfitCurrency; [/COLOR]
    [COLOR=black][/COLOR]
    [COLOR=black][/COLOR]
    [COLOR=black][/COLOR]
    [COLOR=black][/COLOR] 
    [COLOR=black]}[/COLOR]
    it gives me an one -off array error when I try to match the output with the analyzer. I know that array [0] is the first element but it somehow loses data for the first trade and the last. Please see attached screenshots for beginning of strategy analyzer and end as well as beginning of output to end.

    the 6:31:03 am trade should be trade 1 and there should be a loss of -100, but the trade count and profit info is "skipped" to the 7:50 trade.

    The last trade at 3:55:24 on 3/29 is "lost", never displayed.

    what is needed in the help guide code to correct the missing info?
    Attached Files

    #2
    Thanks for your post, Boonfly8.

    Let's consider the following as an example:

    Code:
    protected override void OnBarUpdate()
    {
    	if(State == State.Historical)
    	{
    		if(Position.MarketPosition == MarketPosition.Flat)
    			EnterLong();
    		
    		if(CurrentBar == Bars.Count-2)
    		{					
    			if( SystemPerformance.AllTrades.Count > 0 ) 
    			{ 
    				Trade lastTrade = SystemPerformance.AllTrades[SystemPerformance.AllTrades.Count - 1];
    				Print(String.Format("Trade #: {0} TradeProfit {1}", lastTrade.TradeNumber+1, lastTrade.ProfitCurrency));
    			}
    		}
    	}
    	
    }
    In this code, we will print the TradeNumber index for the Trade with 1 added so we start at 1 instead of 0 and then we will print the profit for the Trade.

    When backtesting 01/01/2018 - 07/12/2018 ES 09-18 minute data we will get the following output:

    Trade #: 315 TradeProfit -62.5
    The Strategy Analyzer reports 316 trades because the last trade is not closed by the strategy logic but by the Strategy Analyzer to close open positions after the backtest.

    Please let us know if you have any additional questions.
    JimNinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by Waxavi, Today, 02:10 AM
    0 responses
    6 views
    0 likes
    Last Post Waxavi
    by Waxavi
     
    Started by TradeForge, Today, 02:09 AM
    0 responses
    11 views
    0 likes
    Last Post TradeForge  
    Started by Waxavi, Today, 02:00 AM
    0 responses
    2 views
    0 likes
    Last Post Waxavi
    by Waxavi
     
    Started by elirion, Today, 01:36 AM
    0 responses
    4 views
    0 likes
    Last Post elirion
    by elirion
     
    Started by gentlebenthebear, Today, 01:30 AM
    0 responses
    5 views
    0 likes
    Last Post gentlebenthebear  
    Working...
    X