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

Retrieving cash values for all local accounts

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

    Retrieving cash values for all local accounts

    I'm running into an unexpected problem in NinjaTrader and wonder if someone may point me toward a working solution. In a nutshell what I need to do is to extract all cash values of all local trading accounts. The code I'm calling to do that is similar to what I'm doing for the active trading account:

    Code:
    double accountValue = Cbi.Globals.Accounts.FindByName([B]name[/B]).GetAccountValue(AccountItem.CashValue, Currency.UsDollar).Value;
    Where 'name' is basically replaced during a loop through all local accounts. The code compiles but prints '$0.00'. Now I AM calling this in historical mode, and perhaps in live it would actually print the real values. It's weekend and we are just about to roll over. But if there is a better/other way to get to those values. I really need to extract the cash value of all trading accounts, not just the cash value of the currently active one (which is the same method but without the Currency.UsDollar param).

    I dug around a little and found this ancient thread:

    Hi maxi You don't have to use the code in the Plot section as I did in the bTPAnaked indicator that I did PM to you. This is just the fastest way to place the orders, but OnBarUpdate should work too. But have a look at my code in the indicator or in the code in Post#3. This has nothing to do with your code in your last post in OnBarUpdate. I would start with something like that: protected override void OnBarUpdate() { int c = a.Orders.Count; Order order = a.Orders[c-1]; if (order.OrderState == OrderState.Filled) { // …


    Seems like one can make this call which returns a bunch of stuff in one long string. But I wouldn't know how to parse that out.

    Yes NinjaTrader Support - I know it's not a supported API call. But if anyone can offer any unsupported help I would be very thankful. The alternative would be a very ugly hack.

    Many thanks in advance.
    Last edited by molecool; 07-12-2015, 04:00 PM.

    #2
    Found a solution....

    Well, I did figure it out but clearly it's a hack. So I'm not sure if I'm free to post this here for everyone to see - perhaps NT Support may have an issue with that. If they don't then I'd be happy to share it

    Comment


      #3
      Hello,

      What you are currently using would not work in historical specifically so that would be part of why you are getting 0's.

      On the main GetAccountValue page, http://ninjatrader.com/support/helpG...etAccountValue

      It lists that this returns 0 on historical bars.

      I used the Simulated Data Feed to test this, I get 0's in realtime using the Globals.Account vs the strategies Inherited account.

      In the test strategy I used:

      Code:
      GetAccountValue(AccountItem.CashValue)
      And this returned 0 as expected for historical but in realtime it returned the strategies accounts cash value. I tried a few variations of using the Globals.Account way but it appears that the CashValue is not updated in this variable when used this way.

      I am unsure on this of a different way to access all of the accounts values at once so I am sorry I cant be of more help but this at least this should explain what is going on currently. I will see if any other support members know of a way during office hours tomorrow, otherwise this will remain open for others to comment on.

      I look forward to being of further assistance.
      JesseNinjaTrader Customer Service

      Comment


        #4
        Originally posted by NinjaTrader_Jesse View Post
        Hello,

        What you are currently using would not work in historical specifically so that would be part of why you are getting 0's.

        On the main GetAccountValue page, http://ninjatrader.com/support/helpG...etAccountValue

        It lists that this returns 0 on historical bars.

        I used the Simulated Data Feed to test this, I get 0's in realtime using the Globals.Account vs the strategies Inherited account.

        In the test strategy I used:

        Code:
        GetAccountValue(AccountItem.CashValue)
        And this returned 0 as expected for historical but in realtime it returned the strategies accounts cash value. I tried a few variations of using the Globals.Account way but it appears that the CashValue is not updated in this variable when used this way.

        I am unsure on this of a different way to access all of the accounts values at once so I am sorry I cant be of more help but this at least this should explain what is going on currently. I will see if any other support members know of a way during office hours tomorrow, otherwise this will remain open for others to comment on.

        I look forward to being of further assistance.
        Hey Jesse - I am familiar with that call but that's just for the currently active account in that particular strategy. My solution is able to pull the cash values for ALL present accounts - even the ones which are not currently selected for this strategy, e.g.:

        Demo1: 50000
        Demo2: 35000
        Demo3: 150000
        etc...

        Hope this makes sense. The default method I found online does not work, even in live tape:

        Code:
        Cbi.Globals.Accounts.FindByName("Demo3").GetAccountValue(AccountItem.CashValue, Currency.UsDollar).Value;
        If you call this in any strategy it will not work and only return $0.00.

        Anyway, I found a solution which I hope will work when the account cash values are being updated. If you have anything to add to this I'd be all ears ;-)
        Last edited by molecool; 07-12-2015, 05:04 PM.

        Comment


          #5
          Hello,

          Just for clarification, the multiple accounts was understood. I used GetAccountValue(AccountItem.CashValue) as a test to ensure I was getting a value for the current account while running the strategy on the simulated data feed. Once I verified that was working I tested the other syntax a few different ways in which none of them provided a result.

          If you have found a work around this would be a great place to post your results for future reference.

          Please let me know if I may be of further assistance.
          JesseNinjaTrader Customer Service

          Comment


            #6
            The Solution

            Originally posted by NinjaTrader_Jesse View Post
            Hello,

            Just for clarification, the multiple accounts was understood. I used GetAccountValue(AccountItem.CashValue) as a test to ensure I was getting a value for the current account while running the strategy on the simulated data feed. Once I verified that was working I tested the other syntax a few different ways in which none of them provided a result.

            If you have found a work around this would be a great place to post your results for future reference.

            Please let me know if I may be of further assistance.
            Alright, just wanted to make sure I don't step on anyone's feet by sharing this hack. If so please feel free to delete this thread:

            Code:
            	/// <summary>
            		/// A little NT7 hack allowing us to pull the respective account values.
            		/// </summary>
            		/// <param name="acct">The account object to be parsed.</param>
            		/// <returns>A dictionary with the following keys: 'Buying Power', 'Cash Value', and 'Excess Equity'.</returns>
            		protected static Dictionary<string, double> GetAccountValues(Account acct) {
            			Dictionary<string, double> dict = new Dictionary<string, double>();
            			String rawVal = acct.UserData.DocumentElement.InnerText.ToString();
            			string[] delimiters = new string[] { "099", "199", "299", "399" };
            			string[] digits =  rawVal.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
            			dict.Add("Buying Power", Convert.ToDouble(digits[0]));
            			dict.Add("Cash Value", Convert.ToDouble(digits[1]));
            			dict.Add("Excess Equity", Convert.ToDouble(digits[2]));
            			return dict;
            		}
            What can I say - it's ugly but it works. Hope it helps someone else further down the line.

            BTW, will there be a working/supported call for NT8? I'm in the beta but haven't tested it yet.

            Comment


              #7
              A very interesting workaround indeed.

              Thanks for sharing that, although not supported this does seem to return the value of a given account from that string.

              I will have to look into this for NT8 and see if there is a way. Once I find out if that is possible I will post my findings.

              I look forward to being of further assistance.
              JesseNinjaTrader Customer Service

              Comment


                #8
                Originally posted by molecool View Post
                Alright, just wanted to make sure I don't step on anyone's feet by sharing this hack. If so please feel free to delete this thread:

                Code:
                	/// <summary>
                		/// A little NT7 hack allowing us to pull the respective account values.
                		/// </summary>
                		/// <param name="acct">The account object to be parsed.</param>
                		/// <returns>A dictionary with the following keys: 'Buying Power', 'Cash Value', and 'Excess Equity'.</returns>
                		protected static Dictionary<string, double> GetAccountValues(Account acct) {
                			Dictionary<string, double> dict = new Dictionary<string, double>();
                			String rawVal = acct.UserData.DocumentElement.InnerText.ToString();
                			string[] delimiters = new string[] { "099", "199", "299", "399" };
                			string[] digits =  rawVal.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
                			dict.Add("Buying Power", Convert.ToDouble(digits[0]));
                			dict.Add("Cash Value", Convert.ToDouble(digits[1]));
                			dict.Add("Excess Equity", Convert.ToDouble(digits[2]));
                			return dict;
                		}
                What can I say - it's ugly but it works. Hope it helps someone else further down the line.

                BTW, will there be a working/supported call for NT8? I'm in the beta but haven't tested it yet.
                Or you can just call with the correct syntax:
                Code:
                			Print("Accounts Enumerated from 'for' loop");
                			for (int i = 0; i <= NinjaTrader.Cbi.Globals.Accounts.Count - 1; ++i)
                			{
                				Print(String.Format("{0}: {1}: {2}"
                				, NinjaTrader.Cbi.Globals.Accounts[i].Name
                				, NinjaTrader.Cbi.Globals.Accounts[i].GetAccountValue(AccountItem.CashValue, Currency.Unknown)
                				, NinjaTrader.Cbi.Globals.Accounts[i].ToString()));
                			}

                Comment


                  #9
                  Brilliant!

                  Originally posted by koganam View Post
                  Or you can just call with the correct syntax:
                  Code:
                  			Print("Accounts Enumerated from 'for' loop");
                  			for (int i = 0; i <= NinjaTrader.Cbi.Globals.Accounts.Count - 1; ++i)
                  			{
                  				Print(String.Format("{0}: {1}: {2}"
                  				, NinjaTrader.Cbi.Globals.Accounts[i].Name
                  				, NinjaTrader.Cbi.Globals.Accounts[i].GetAccountValue(AccountItem.CashValue, Currency.Unknown)
                  				, NinjaTrader.Cbi.Globals.Accounts[i].ToString()));
                  			}
                  I be damned - that actually works! Little detail was the 'Currency.Unknown' param instead of using the Dollar. Didn't try that and of course it's not documented. Well done, thanks a bunch.

                  Comment


                    #10
                    Is there a similar way to retrieve PnL of all open positions (unrealized) or is Account Value always including all unrealized PnL.

                    I ask that because of I look at Cash Value in Accounts I only get an update when a position is opened or closed but perhaps that´s only the refresh of the display,

                    Comment


                      #11
                      Problems with FXCM

                      A little follow up on this thread after we put the code into production. It seems that there is an issue with FXCM accounts in that they do not report anything. I used Koganam's call:

                      GetAccountValue(AccountItem.CashValue, Currency.Unknown).Value;

                      And also the hack I reported below. Neither of them are able to pull live account details (i.e. cash, buying power, excess equity) for FXCM accounts. Works fine for CQG/Continuum accounts for some reason.

                      Has anyone else run into this issue? Is there a solution?

                      Comment


                        #12
                        Anyone?

                        No further thoughts on the issue?

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by MarianApalaghiei, Today, 10:49 PM
                        2 responses
                        8 views
                        0 likes
                        Last Post MarianApalaghiei  
                        Started by love2code2trade, Yesterday, 01:45 PM
                        4 responses
                        28 views
                        0 likes
                        Last Post love2code2trade  
                        Started by funk10101, Today, 09:43 PM
                        0 responses
                        8 views
                        0 likes
                        Last Post funk10101  
                        Started by pkefal, 04-11-2024, 07:39 AM
                        11 responses
                        37 views
                        0 likes
                        Last Post jeronymite  
                        Started by bill2023, Yesterday, 08:51 AM
                        8 responses
                        46 views
                        0 likes
                        Last Post bill2023  
                        Working...
                        X