Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Account Details During Historical Strategy Execution

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

    Account Details During Historical Strategy Execution

    I've searched the forums and none of the threads offers a complete answer for me.

    In the OnBarsUpdate method of a strategy, I want to get the cash balance of the current account that is being used for the strategy so that I can calculate the number of contracts I can purchase.

    I understand that Account is the current account associated with the strategy. I try to get account details as follows as instructed in some other posts.

    Code:
      var cash = Account.Get(AccountItem.CashValue, Currency.UsDollar);
    But this only ever returns the initial cash value of the account regardless of how many trades I've entered and exited. That initial account value is also the same as Account.SimulatorInitialCash.

    What is the proper way to access or calculate the current account value while a strategy is running on historical data?

    I am only backtesting on historical data at this point so I'm interested particularly in behavior during a backtest, not real-time.

    Thanks,

    #2
    Hello BarzTrading,

    Thank you for your note.

    The account object is not going to be updated historically, only in real time.

    I have attached a sample which demonstrates how you could dynamically adjust trade size based on account size historically.

    Please let us know if you need further assistance.
    Attached Files
    Alan P.NinjaTrader Customer Service

    Comment


      #3
      Thanks, Alan.

      I believe your example just needs one little addition. The market position should be tested in the OnPositionUpdate method so that we're only updating the balance when the position is flat, that is, when the update is a completed trade rather than entering a new position.

      Code:
              protected override void OnPositionUpdate(Cbi.Position position, double averagePrice, int quantity, Cbi.MarketPosition marketPosition)
              {
                  // Check to make sure there is at least one trade in the collection
                  if (SystemPerformance.AllTrades.Count > 0)
                  {
                      if (marketPosition == MarketPosition.Flat)
                      {
                          // Get the last completed real-time trade (at index 0)
                          Trade lastTrade = SystemPerformance.AllTrades[SystemPerformance.AllTrades.Count - 1];
      
                          // Calculate the PnL for the last completed real-time trade
                          double lastProfit = lastTrade.ProfitCurrency ;
                          
                          xAccountSize= xAccountSize + lastProfit;
                          Print(xAccountSize.ToString()); //Prints Account Size
                          Print("The last trade's profit is " + lastProfit);
                      }                
                  }
              }

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by Mindset, 04-21-2026, 06:46 AM
      0 responses
      88 views
      0 likes
      Last Post Mindset
      by Mindset
       
      Started by M4ndoo, 04-20-2026, 05:21 PM
      0 responses
      135 views
      0 likes
      Last Post M4ndoo
      by M4ndoo
       
      Started by M4ndoo, 04-19-2026, 05:54 PM
      0 responses
      68 views
      0 likes
      Last Post M4ndoo
      by M4ndoo
       
      Started by cmoran13, 04-16-2026, 01:02 PM
      0 responses
      119 views
      0 likes
      Last Post cmoran13  
      Started by PaulMohn, 04-10-2026, 11:11 AM
      0 responses
      69 views
      0 likes
      Last Post PaulMohn  
      Working...
      X