Hello, all.
I need some help. I’ve been working with using arrays to monitor the status of multiple accounts. Everything seems to work, except this:
else if (State == State.DataLoaded)
{
int loopCount = 1;
foreach (Account sampleAccount in Account.All)
{
if (sampleAccount.ConnectionStatus == ConnectionStatus.Connected)
{
Print(String.Format("The account {0} is connected to {1}.", sampleAccount.Name, sampleAccount.Connection));
activeAccountsArray = new List<Account>();
activeAccountsArray.Add(sampleAccount);
myAccount[loopCount] = sampleAccount;
}
loopCount++;
}
}
Compiling crashes out saying “myAccount doesn’t exist in the current context.” But that line isn’t assigning myAccount, it’s assigning myAccount1 or myAccount2 or myAccount(however-many-accounts-are-in-the-Array).
Why doesn’t this work? I’ve researched it, and it seems like this is the way to include a variable within a variable’s name.
Thanks in advance!
Probably because myAccount doesn’t exist? Where is it declared and initialized?
1 Like
Code is a mess, why so reluctant to. use AI to help you debug ? It would explain the an
activeAccountsArray = new List<Account>();
activeAccountsArray.Add(sampleAccount);
Is pointless, you’re creating a new list every time and then immediately overwriting it. But this list never seems to be used anyway.
myAccount[loopCount] = sampleAccount;
This suggests myAccount is defined as an array or List. But then you’re adding to it based on a counter that is relative to something else which increments regardless of whether the condition is true.. If it’s a list; Unless myAccount[0] already exists you cannot add myAccount[1] . If it’s an array, Unless the array is defined and slot [1] exists and can be assigned, you cannot add to it there.
Also, you said you’re ‘monitoring status of accounts’ but any code in state data loaded in simply a snapshot of the accounts at the time the indicator loads. If a connection/account changes, this code here will not update.
myAccount1 and myAccount2 are defined at the beginning, right under “public class”
private Account myAccount1 = null;
private Account myAccount2 = null;
I wondered about that. I borrowed this code from an example indicator that I downloaded from the old forum.
Now that you’ve pointed that out, I realize why it was written that way in the indicator I borrowed. Moving the line that creates the array out of that loop is easy enough.
It’s true that this code snippet doesn’t actually do anything with the items in the array. I’m just trying to learn how to manipulate and use list items, which I haven’t done before in C#.
After doing some more research, I tried this:
private Dictionary<int, string> varDict = new Dictionary<int, string>();
else if (State == State.DataLoaded)
{
activeAccountsArray = new List<Account>();
int loopCount = 1;
foreach (Account sampleAccount in Account.All)
{
if (sampleAccount.ConnectionStatus == ConnectionStatus.Connected)
{
varDict.Add(loopCount, "myAccount" + loopCount.ToString()); // Assign values with dynamic keys
Print(String.Format("The account {0} is connected to {1}.", sampleAccount.Name, sampleAccount.Connection));
activeAccountsArray.Add(sampleAccount);
Print(varDict[loopCount] + " = " + sampleAccount.ToString());
varDict[loopCount] = sampleAccount;
loopCount++;
}
}
}
This does not compile, complaining about converting “from string to Ninjatrader.Cbi.Account,” but if I REM out the variable assignment line it compiles, and the Print statement produces the following output:
The account Sim101 is connected to NinjaTrader.Cbi.Connection.
myAccount1 = Sim101
The account Sim102 is connected to NinjaTrader.Cbi.Connection.
myAccount2 = Sim102
This is exactly what I want. myAccount1 is Sim101, myAccount2 is Sim102, etc. This is perfect!
So, how do I use the Dictionary items as the variable names to assign with the various accounts?
Your code was looking for myAccount. Not myAccount1 and myAccount2.
You can’t. Try asking AI to help with some of your issues.
Using the Dictionary to hold dynamic variable names is exactly what AI told me to do. What’s the point of the Dictionary if I can’t use the variable name that it’s holding?
Anyway, you’re wrong. If I use sampleAccount.ToString() in the assignment, it works. But then it’s useless as an Account variable.
Ok… maybe just keep using AI instead of asking here.
You specifically asked how to use dictionary items as variable names. The answer is you can’t. So I’m not wrong.
Using a dictionary to hold dynamic variable names is different than using dictionary items as variable names.
You seem confused here too. You don’t understand that you can use a dictionary to hold dynamic names, but you can’t turn those into actual variables.
Anyways, good luck.
It’s actually working the way I programmed it, except that it refuses to assign the account name from the activeAccountsArray to the variable name retrieved from the Dictionary. I can’t figure out how to get it to see the Dictionary item as an Account type. I can create the Dictionary so that it is holding items of Account type, but then I don’t know how to populate the Dictionary with the appropriate information.
I decided to stop banging my head against this wall and just created several separate “if” statements to handle the items returned by the loop.
Thanks for contributing!
Earlier you had a dictionary with int & string and you were trying to add an account object to it, which is why you got an error.
myAccount = Account Object
myAccount.Name = String
You can’t do this.:
private Dictionary<int, string> varDict = new Dictionary<int, string>();
varDict[loopCount] = sampleAccount;
But you can do this (storing just the account name as a string):
private Dictionary<int, string> varDict = new Dictionary<int, string>();
varDict[loopCount] = sampleAccount.Name;
or this (storing the Account object, which probably what you want:
private Dictionary<int, Account> varDict = new Dictionary<int, Account>();
varDict[loopCount] = sampleAccount;
I tried this. This is what gives me the error about converting “string to Ninjatrader.Cbi.Account” because I don’t know how to add ’ “myAccount” + loopCount ’ as the name for the iterative variable to the Dictionary as an Account object.
Is there a way to retrieve values from the Array each time an Account object is required? Would I be using varDict[1], varDict[2], etc. instead of myAccount1, my Account2, etc?
Okay, I decided to try that approach.
private List<Account> activeAccountsArray = new List<Account>();
foreach (Account sampleAccount in Account.All)
{
if (sampleAccount.ConnectionStatus == ConnectionStatus.Connected)
{
Print(String.Format("The account {0} is connected to {1}.", sampleAccount.Name, sampleAccount.Connection));
activeAccountsArray.Add(sampleAccount);
loopCount++;
}
Print(activeAccountsArray[loopCount].ToString() + " = " + sampleAccount.ToString());
}
order = activeAccountsArray[0].CreateOrder(instr, OrderAction.Buy, OrderType.StopMarket, OrderEntry.Automated, TimeInForce.Day, qty, 0, newVariable + 2, "", "Entry", Core.Globals.MaxDate, null);
It actually works great! It took me a few minutes to figure out that the first item in an array is actually [0] and not [1], but once I figured that out, it worked!
Thank you!