• 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

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 reedstickets, Today, 07:53 PM
      0 responses
      5 views
      0 likes
      Last Post reedstickets  
      Started by Atilla, Today, 04:05 PM
      0 responses
      17 views
      0 likes
      Last Post Atilla
      by Atilla
       
      Started by Displacer, Today, 02:57 PM
      2 responses
      13 views
      0 likes
      Last Post Displacer  
      Started by SAILIKITY, 05-22-2019, 05:41 PM
      1 response
      9 views
      0 likes
      Last Post NinjaTrader_PaulH  
      Started by rmillar, 01-09-2019, 06:02 PM
      6 responses
      75 views
      1 like
      Last Post eDanny
      by eDanny
       
      Working...
      X