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

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 f.saeidi, Today, 05:56 AM
          1 response
          3 views
          0 likes
          Last Post Jltarrau  
          Started by Jltarrau, Today, 05:57 AM
          0 responses
          4 views
          0 likes
          Last Post Jltarrau  
          Started by Stanfillirenfro, Yesterday, 09:19 AM
          7 responses
          51 views
          0 likes
          Last Post NinjaTrader_Gaby  
          Started by TraderCro, 04-12-2024, 11:36 AM
          4 responses
          70 views
          0 likes
          Last Post Mindset
          by Mindset
           
          Started by Mindset, Yesterday, 02:04 AM
          1 response
          15 views
          0 likes
          Last Post Mindset
          by Mindset
           
          Working...
          X