Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

backtesting - simulate account size with internal calculations

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

    backtesting - simulate account size with internal calculations

    Hi,
    As I understand it we can't update the sim account size per historical trade in backtesting in the analyzer.
    I'd like to use an AccountSize variable which updates at the end of each trade, in order to apply money management techniques to the strategy. For instance, my variable begins at $100,000 and updates with each trade to match the final result of the backtest (minus 100K, obviously).

    I've been working with:

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

    and

    lastTrade.ProfitCurrency;

    However, I'm unable to grasp how to use these in the proper place to update my AccountSize variable only once per trade in the backtest. If you could help me with a clue or point to a method group to clarify, that would be great. Any advice to get me where my simulated account growth to match the final sim101 account growth would suffice, and I could take it from there.

    thanks,
    David

    #2
    Hello trader3000a,

    Thanks for your post.

    The Strategy Analyzer does not have a specific account that is used when backtesting the strategy. We are tracking interest in an existing feature request to add a fully functional backtesting account to the Strategy Analyzer. Please let me know if you would like me to add your vote to this feature request.

    Something you could consider for a workaround is to create a class-level double variable called accountSize and assign it a value of 100,000. Then, you could try checking if State == State.Terminated in OnStateChange(), add accountSize to SystemPerformance.AllTrades.TradesPerformance.Curr ency.CumProfit, then print out accountSize after your calculation. This would add the total net profit calculated by the backtest at the end of the backtest to the accountSize variable and print the calculated accountSize value to the Output window.

    Currency.CumProfit: https://ninjatrader.com/support/help.../cumprofit.htm
    <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

    Comment


      #3
      Hi Brandon,
      Would this be something I could adapt in order to get a new AccountSize value after every trade closed? I need a running value to make position size adjustments every time the AccountSize value changes after a trade.
      Please add my vote for the account.
      thanks,
      David

      Comment


        #4
        Hello David,

        Thanks for your notes.

        I have added your vote to the feature request. This request is being tracked under the number SFT-137.

        As with all feature requests, interest is tracked before implementation is considered, so we cannot offer an ETA or promise of fulfillment. If implemented, it will be noted on the Release Notes page of the Help Guide.

        Release Notes — https://ninjatrader.com/support/help...ease_notes.htm

        If you want to increment the variable in the way you described then this would require coming up with your own custom logic to retrieve Trade information from the last trade and calculate the AccountSize using the data retrieved from the TradeCollection

        Something you could consider creating a Series<int> variable that tracks the TradeNumber of the lastTrade made. You could create a bool called something like doOnce (initially set to true), check if the bool is true, and check if the lastTrade.TradeNumber is equal to 0 and increment AccountSize by lastTrade.ProfitCurrency and set the bool to false. Then you could try incrementing the AccountSize by the lastTrade.ProfitCurrency when the previous TradeNumber assigned to your previous custom Series<int> variable value (mySeries[1]) is not equal to the TradeNumber assigned to the current Series<int> variable value (mySeries[0]). Ultimately, it would be up to you to come up with the specific custom logic to accomplish your overall goal.

        See the help guide documentation below for more information.

        Trade: https://ninjatrader.com/support/help.../nt8/trade.htm
        TradeCollection: https://ninjatrader.com/support/help...collection.htm
        TradesPerformance: https://ninjatrader.com/support/help...erformance.htm
        Last edited by NinjaTrader_BrandonH; 10-08-2023, 07:47 PM.
        <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

        Comment


          #5
          Hi Brandon,
          This works within OnBarUpdate():

          if ( SystemPerformance.AllTrades.Count == 0)
          {
          StartingAccountSize = 100000;
          }

          AccountSize = StartingAccountSize + SystemPerformance.AllTrades.TradesPerformance.Curr ency.CumProfit;



          There is just one question remaining. If there is a trade in progress on the last bar of the backtest, SystemPerformance naturally doesn't add that PnL.

          I'd like to grap that last in-progress trade's unrealized pnl somehow. Is there some function whereby I can ? Is if(is last bar on chart) {get unrealized pnl} possible somehow?
          thanks,
          David​

          Comment


            #6
            Hello David,

            Thanks for your notes.

            If the Strategy Analyzer calculates that the last trade in the backtest exits by means of Exit On Session Close or if the trade is exited on the last bar then there would be no further processing by the Strategy Analyzer and you would not be able to retrieve the TradesPerformance information for that last trade.

            You would need to program your strategy to exit the trade before the end of the data being processed in the backtest.

            For example, you could consider checking if CurrentBar is equal to Count - 5 to check if the current bar index is 5 bars before the end of the data, check if you are in a long or short market position ysing Position.MarketPosition and call ExitLong() or ExitShort() to exit the position before the end of the last bar processed in the backtest. By doing so, the trade would exit before the last bar and you would be able to retrieve TradesPerformance information for that final trade.

            Position.MarketPosition: https://ninjatrader.com/support/help...etposition.htm
            <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

            Comment


              #7
              Hi Brandon,
              Cool idea, thanks!
              David

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by NullPointStrategies, Yesterday, 05:17 AM
              0 responses
              59 views
              0 likes
              Last Post NullPointStrategies  
              Started by argusthome, 03-08-2026, 10:06 AM
              0 responses
              133 views
              0 likes
              Last Post argusthome  
              Started by NabilKhattabi, 03-06-2026, 11:18 AM
              0 responses
              73 views
              0 likes
              Last Post NabilKhattabi  
              Started by Deep42, 03-06-2026, 12:28 AM
              0 responses
              45 views
              0 likes
              Last Post Deep42
              by Deep42
               
              Started by TheRealMorford, 03-05-2026, 06:15 PM
              0 responses
              50 views
              0 likes
              Last Post TheRealMorford  
              Working...
              X