Page 1 of 1

Finding an account (and its values) by Name

Posted: Jun 09 2024
by Talky_Zebra
I am trying to find account values by the name of the account (or any other identifier... Name seems good.)

I have tried using Lambda functionality similar to this:

Code: Select all

string account_ID = "MY_DEMO_ACCT"; var accounts = this.TradeManager.TradingData.Accounts.Items; string matchingName = accounts.FirstOrDefault(a => a.Name == account_ID )?.Name; if (matchingName != null) { Console.WriteLine("Found matching name: " + matchingName); } else { Console.WriteLine("No matching name found"); }
But I am getting a syntax error that "Operator '?' cannot be applied to operand of type 'Account'" in the third line.
Is there a way out of this error?

I tried the long hand alternative, where I iterate through the .Items. It works, but it looks a bit tortured. Is there a better way?

Code: Select all

string account_ID = "MY_DEMO_ACCT"; var accounts = this.TradeManager.TradingData.Accounts.Items; Account this_account; int accounts_len = accounts.Length; int account_idx=-1; for(int i = 0; i < accounts_len; i++) { Output.WriteLine("accounts[{0}].Name: {1}, account_ID {2}", i, accounts[i].Name, account_ID); if (account_ID == accounts[i].Name) { account_idx = i; this_account = accounts[account_idx]; } else { Output.WriteLine("Could not find account"); Dispose(); } } // Finally I get account info here Output.WriteLine(accounts[account_idx].Name);

Re: Finding an account (and its values) by Name

Posted: Jun 09 2024
by HellGhostEvocatorX
the ? Operator is not available in the deprecated framework 4.0 used by multicharts.net. This actually only means that a value can also be zero. You could also just if (XYZ != null) instead

chatgpt conversion:

Code: Select all

string account_ID = "MY_DEMO_ACCT"; var accounts = this.TradeManager.TradingData.Accounts.Items; string matchingName = null; foreach (var account in accounts) { if (account.Name == account_ID) { matchingName = account.Name; break; } } if (matchingName != null) { Console.WriteLine("Found matching name: " + matchingName); } else { Console.WriteLine("No matching name found"); }

Re: Finding an account (and its values) by Name

Posted: Jun 09 2024
by HellGhostEvocatorX
and if you ever have the problem that neither the PL editor nor visual studio output a compiler error, then it is often precisely because the framework is outdated and you have to write it differently. According to multicharts, the framework will be upgraded with the next updates.

Re: Finding an account (and its values) by Name

Posted: Jun 14 2024
by BD.
According to multicharts, the framework will be upgraded with the next updates.
This is great news! I kept seeing "we will consider this" for a while. Hope they will do something about PL editor as well.