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

Error on subscribe to OnAccountUpdate()

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

    Error on subscribe to OnAccountUpdate()

    Hi NT8 Support,

    I am seeking to print the AccountItems in my strategy. Here is my code:




    protected override void OnAccountItemUpdate(Account Sim101, AccountItem.TotalCashBalance accountItem, double value)

    {
    Print(string.Format("{0} {1} {2}", Sim101.Name, AccountItem.TotalCashBalance, value));
    }





    But I get this error: "TotalCashBalance does not exist in the type NinjaTrader.Cbi.AccountItem

    Please let me know what I am doing wrong. Also, I'd like to subscribe to get all account items, not just the total cash balance. How can I do this?

    #2
    Hello Austiner87,

    Thanks for your post.

    Please see the Help Guide for syntax on using this method.

    OnAccountItemUpdate - https://ninjatrader.com/support/help...itemupdate.htm

    Available AccountItems - https://ninjatrader.com/support/help...ccountitem.htm

    To see which AccountItems will be available with your connection, please see the grid at the bottom of the Accounts tab documentation.

    Accounts tab - https://ninjatrader.com/support/help...TheAccountsTab

    Checking the documentation we can see the following as the correct syntax.

    Code:
    protected override void OnAccountItemUpdate(Account account, AccountItem accountItem, double value)
    If you want to print out TotalCashBalance, that would look like:

    Code:
    protected override void OnAccountItemUpdate(Account account, AccountItem accountItem, double value)
    {
        if (accountItem == AccountItem.TotalCashBalance)
            Print(String.Format("Acct: {0} TotalCashValue: {1}", account.Name, value));
    }
    If you want to check for different account items, you can set up conditions to check for those account items like I have demonstrated for AccountItem.TotalCashBalance above.

    We look forward to assisting.
    JimNinjaTrader Customer Service

    Comment


      #3
      Hi NT8 support!

      I still cannot get this to print in the output window for TotalCashBalance. I pasted in the code above exactly into my code but no output appears for TotalCashBalance.

      Do I need to declare variables for TotalCashBalance?
      Does any of the text above such as Account account or account.Name need to be replaced with the actual account name (Playback101 for testing purposes)?

      I am looking to use the account items for calculations within the strategy with other variables, so if I have a variable named "VariableX" how can I build a formula in the OnBarUpdate() that has syntax:

      VariableX = TotalCashBalance - SystemPerformance.AllTrades.TradesPerformance.Curr ency.CumProfit

      Thank you!

      Comment


        #4
        Hello Austiner87,

        We will need to get an AccountItemUpdate for TotalCashBalance to see an update with the snippet I gave in post #2.

        Do you see this field change in the Accounts tab of the Control Center when you have a Total Cash Balance column added when you expect to see your print? If this value is unused with your account, are you looking for something else, like AccountItem.CashValue?

        If you are looking to get a snapshot of an AccountItem, I suggest using Account.Get instead. OnAccountItemUpdate would be used to check when these AccountItems change.

        I.E.

        Code:
        Print(Account.Get(AccountItem.CashValue, Currency.UsDollar));
        
        Print(Account.Get(AccountItem.TotalCashBalance, Currency.UsDollar));
        You could use Account.Get in OnBarUpdate to get a snapshot and use that in your code, or you can save your TotalCashBalance/CashValue AccountItems from OnAccountItemUpdate to a class level variable to use it in OnBarUpdate.

        You can learn more about variable scope from educational C# resources that can be found external to NinjaTrader. A publicly available resource is linked below.

        Scope - https://www.geeksforgeeks.org/scope-...es-in-c-sharp/

        Please also note that AccountItems are only going to be relevant when the strategy is trading live with realtime data. If you include reference account items when the strategy processes historical data, it would not be relevant. Additionally, the AllTrades collection includes both historical and realtime trades, and the historical trades would not be relevant to the current account standing since they are virtual trades. RealtimeTrades may be recommended instead when comparing against account values.

        RealtimeTrades collection - https://ninjatrader.com/support/help...timetrades.htm

        You could consider separating logic that should be processed with Realtime data only by making State checks in your code.

        For example:

        Code:
        if (State == State.Historical)
            return;
        The above will skip processing historical data for all code underneath the return; line of code.

        States - https://ninjatrader.com/support/help...nt8/?state.htm

        We look forward to assisting.
        JimNinjaTrader Customer Service

        Comment


          #5
          Hello Jim,

          when using

          Code:
          Print(Account.Get(AccountItem.CashValue, Currency.UsDollar));
          Print(Account.Get(AccountItem.TotalCashBalance, Currency.UsDollar));
          with a Sim101 account and a simulated data feed, I receive 0 for the two AccountItems.

          Why?

          Kind regards
          Gerik

          Comment


            #6
            Hello Gerik,

            Thanks for your message.

            When I test this in a strategy that is applied to a Sim101 account, I get the same prints that are seen in the Accounts tab of the Control Center. (See attached screenshot.)

            Please test the same prints in a separate strategy and apply the strategy against the Sim101 account and compare with the Accounts tab to test.
            Attached Files
            JimNinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by Mindset, 05-06-2023, 09:03 PM
            10 responses
            262 views
            0 likes
            Last Post NinjaTrader_BrandonH  
            Started by michi08, 10-05-2018, 09:31 AM
            5 responses
            741 views
            0 likes
            Last Post NinjaTrader_ChelseaB  
            Started by The_Sec, Today, 02:29 PM
            0 responses
            2 views
            0 likes
            Last Post The_Sec
            by The_Sec
             
            Started by tsantospinto, 04-12-2024, 07:04 PM
            4 responses
            62 views
            0 likes
            Last Post aligator  
            Started by sightcareclickhere, Today, 01:55 PM
            0 responses
            1 view
            0 likes
            Last Post sightcareclickhere  
            Working...
            X