Finding an account (and its values) by Name

Questions about MultiCharts .NET and user contributed studies.
Talky_Zebra
Posts: 52
Joined: Mar 07 2024
Has thanked: 16 times
Been thanked: 1 time

Jun 09 2024

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);

HellGhostEvocatorX
Posts: 143
Joined: Feb 10 2022
Has thanked: 68 times
Been thanked: 23 times

Jun 09 2024

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"); }

HellGhostEvocatorX
Posts: 143
Joined: Feb 10 2022
Has thanked: 68 times
Been thanked: 23 times

Jun 09 2024

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.

BD.
Posts: 23
Joined: Feb 02 2024
Has thanked: 4 times
Been thanked: 5 times

Jun 14 2024

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.