• 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

Last Exit (Day) in Backtest

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

    Last Exit (Day) in Backtest

    Dear Experts,


    The last trade in a backtest always closes with "Exit on Session Close".


    Can you please let me know how I can check if the last trade really was the last trade of a backtest and then do something, for example perform a final custom calculation and print to Output window?


    If Time[0].Date == To.Date, && ... do something, does not (always) do the trick, since the End Date entered by the user can be a Saturday, Sunday or a public holiday.


    If Time[0]Date >= To.Date && whatever ... doesn't work either, since the Backtest will already have stopped at that point in time.


    How does NT itself determine that it has to close the last open position on let's say Friday Sept 21, 2018, if the user runs a backtest until Sunday Sept 23, 2018?


    Thanks for letting me know.


    Regards
    NT-Roland

    #2
    Hello NT-Roland,

    Thanks for your inquiry.

    The Strategy Analyzer closes any open positions at the end of a backtest. There isn't a way to determine if the last trade is the last trade of a backtest through strategy execution, but you could perform your calculations on the last historical bar before the Strategy Analyzer closes the position.

    if (CurrentBar == Bars.Count-2)

    You may then use a TradeCollection to process performance information on the trades the strategy has made in the backtest. The last trade can be referenced from a TradeCollection in the following manner:

    Trade lastTrade = SystemPerformance.AllTrades[SystemPerformance.AllTrades.Count - 1];

    I've included documentation on TradeCollections, Trades, and SystemPerformance where you can get a collection of trades the strategy has made.

    TradeCollections - https://ninjatrader.com/support/help...collection.htm

    Trades - https://ninjatrader.com/support/help...n-us/trade.htm

    SystemPerformance - https://ninjatrader.com/support/help...erformance.htm

    Please let us know if we can be of further assistance.
    JimNinjaTrader Customer Service

    Comment


      #3
      Hi Jim,

      if (CurrentBar == Bars.Count-2) works like a charm UP TO the last trade even if the date of the last trade and the End Date (To.Date) are not the same, which is a step in the right direction, because I don't have to worry anymore if the End Date of a Backtest is a Saturday, Sunday or public holiday.

      However, I'm still not able to access the unrealized profit of the very last trade itself right before it is closed via "Exit on Session Close". I'm only missing this last piece.

      if (CurrentBar == Bars.Count - 2)
      {
      UnrealizedProfit = + PositionAccount.GetUnrealizedProfitLoss(Performanc eUnit.Currency, Close[0]);
      }

      does not seem to work.

      Can you please let me know what I'm doing wrong?

      Thank you.
      NT-Roland

      Comment


        #4
        Hello NT-Roland,

        Position represents the position of the strategy while PositionAccount represents the position of the account that the strategy is attached to. Additionally, the Strategy Analyzer does not have a regular account. For these reasons, I would recommend using Position.GetUnrealizedProfitLoss(). For example:

        Code:
        if (CurrentBar == Bars.Count - 2) 
        {
        	Print(Position.GetUnrealizedProfitLoss(PerformanceUnit.Currency, Close[0]));
        }
        When I test this, I see the result I would expect. I've included some documentation on strategy position vs account position for further reference.

        Strategy Position vs. Account Position - https://ninjatrader.com/support/help..._account_p.htm

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

        Comment


          #5
          Hi Jim,

          That makes perfect sense. Thank you.

          KR
          NT-Roland

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by Botan626, Today, 04:43 AM
          0 responses
          4 views
          0 likes
          Last Post Botan626  
          Started by xsgex, Yesterday, 09:46 PM
          0 responses
          3 views
          0 likes
          Last Post xsgex
          by xsgex
           
          Started by Steve L, Yesterday, 09:36 PM
          0 responses
          6 views
          0 likes
          Last Post Steve L
          by Steve L
           
          Started by krugman25, Yesterday, 09:34 PM
          0 responses
          4 views
          0 likes
          Last Post krugman25  
          Started by indy73, Yesterday, 04:42 PM
          0 responses
          15 views
          0 likes
          Last Post indy73
          by indy73
           
          Working...
          X