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

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 gemify, 11-11-2022, 11:52 AM
      6 responses
      803 views
      2 likes
      Last Post ultls
      by ultls
       
      Started by ScottWalsh, Today, 04:52 PM
      0 responses
      3 views
      0 likes
      Last Post ScottWalsh  
      Started by ScottWalsh, Today, 04:29 PM
      0 responses
      7 views
      0 likes
      Last Post ScottWalsh  
      Started by rtwave, 04-12-2024, 09:30 AM
      2 responses
      22 views
      0 likes
      Last Post rtwave
      by rtwave
       
      Started by tsantospinto, 04-12-2024, 07:04 PM
      5 responses
      70 views
      0 likes
      Last Post tsantospinto  
      Working...
      X