Announcement
Collapse
No announcement yet.
Partner 728x90
Collapse
NinjaTrader
Getting AccountItem Values from my custom Strategy
Collapse
X
-
Getting AccountItem Values from my custom Strategy
I tried to get the AccountItem Values for my live account with Rithmic without success. The CashValue does not seem to be up to date and the different kinds of Margins return zero. Why is this so? -
Hi JoshG
I can only see the Cash Value on the Accounts tab.
My code is a simple modification of what is in the HelpGuide and the relevant part is as follows:
public class MarginChecker : Strategy
{
#region Variables
private Account myAccount;
#endregion
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = @"checks the margin values of the account.";
Name = "MarginChecker";
Calculate = Calculate.OnPriceChange;
EntriesPerDirection = 20; //Default Position Limits from NT
EntryHandling = EntryHandling.AllEntries;
IsExitOnSessionCloseStrategy = false;
ExitOnSessionCloseSeconds = 0;
IsFillLimitOnTouch = false;
MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;
OrderFillResolution = OrderFillResolution.Standard;
Slippage = 0;
StartBehavior = StartBehavior.WaitUntilFlat;
TimeInForce = TimeInForce.Gtc;
TraceOrders = false;
RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose;
StopTargetHandling = StopTargetHandling.PerEntryExecution;
BarsRequiredToTrade = 0;
// Disable this property for performance gains in Strategy Analyzer optimizations
// See the Help Guide for additional information
IsInstantiatedOnEachOptimizationIteration = true;
// Find live account
lock (Account.All)
myAccount = Account.All.FirstOrDefault(a => a.Name == "XXXXXXX");
// Subscribe to static events. Remember to unsubscribe with -= when you are done
Account.AccountStatusUpdate += OnAccountStatusUpdate;
if (myAccount != null)
{
// Print some information about our account using the AccountItem indexer
Print("STATE_DEFAULTS-------------------------------------------");
Print("" + DateTime.Now.ToLongDateString() + " " + DateTime.Now.ToShortTimeString());
Print("Account Name: " + myAccount.Name);
Print("Item 1: " + myAccount.Get(AccountItem.BuyingPower, Currency.UsDollar));
Print("Item 2: " + myAccount.Get(AccountItem.CashValue, Currency.UsDollar));
Print("Item 3: " + myAccount.Get(AccountItem.NetLiquidation, Currency.UsDollar));
Print("Item 4: " + myAccount.Get(AccountItem.NetLiquidationByCurrency , Currency.UsDollar));
Print("Item 5: " + myAccount.Get(AccountItem.PositionMargin, Currency.UsDollar));
Print("Item 6: " + myAccount.Get(AccountItem.TotalCashBalance, Currency.UsDollar));
Print("Item 7: " + myAccount.Get(AccountItem.ExcessInitialMargin, Currency.UsDollar));
Print("Item 8: " + myAccount.Get(AccountItem.ExcessPositionMargin, Currency.UsDollar));
Print("-------------------------------------------");
// Subscribe to events. Remember to unsubscribe with -= when you are done
myAccount.AccountItemUpdate += OnAccountItemUpdate;
myAccount.OrderUpdate += OnOrderUpdate;
}
}
else if (State == State.Configure)
{
}
else if (State == State.Terminated)
{
// Unsubscribe to events
myAccount.AccountItemUpdate -= OnAccountItemUpdate;
myAccount.OrderUpdate -= OnOrderUpdate;
Account.AccountStatusUpdate -= OnAccountStatusUpdate;
}
}
Comment
-
mbesha,
If you are not getting the different Margin values on your Accounts tab I would not expect to get them through NinjaScript either.
In regards to the CashValue that you see in your Print() -- that would only get called once inside State.SetDefaults, so I would not expect it to stay in sync. You would need to use OnBarUpdate() or any of the other event driven methods to get a more up to date CashValue.Josh G.NinjaTrader Customer Service
Comment
-
Hi JoshG
With regard to the Margin values, are there any plans to have them included in future releases?
As for the Cash Value, it is not a simple matter of today's value being different from yesterday's value for instance. The value I get through NinjaScript is weeks if not months off. The Accounts Tab shows the latest value but whatever value is coming through NinjaScript is totally different.
Comment
-
mbesha,
Like the Accounts tab, the Margin Values are already accessible from within the platform if your broker supports them.With regard to the Margin values, are there any plans to have them included in future releases?
Let me know if relocating your CashValue print to OnBarUpdate() does not give you something more expected.The value I get through NinjaScript is weeks if not months off.Josh G.NinjaTrader Customer Service
Comment
-
mbesha,
Yeh, I suspect Rithmic does not support that information if you do not see it on your Accounts tab.
I'm able to see a difference when I Print that value from OnBarUpdate() in my strategy. Can we see your OnBarUpdate() code that does not update with the current CashValue?With regard to the Cash Value, there is no difference even when I print using OnBarUpdate().Josh G.NinjaTrader Customer Service
Comment
-
Hi JoshG
This is my code for OnBarUpdate()
protected override void OnBarUpdate()
{
//Add your custom strategy logic here.
if(State != State.Realtime || myAccount == null)
return;
//For printing out the Cash Value
Print("ONBARUPDATE-------------------------------------------");
Print("" + DateTime.Now.ToLongDateString() + " " + DateTime.Now.ToShortTimeString());
Print("Account Name: " + myAccount.Name);
Print("Cash Value: " + myAccount.Get(AccountItem.CashValue, Currency.UsDollar));
}
Comment
-
mbesha,
I do not see anything that sticks out as an error. Something is not correct though here if the prints for CashValue are not updating. Are there errors on the Logs tab of your Control Center?
I was able to get the following snippet to lock onto my SIM101 account and print the cash value as it changed. Please modify this and see if it works with your live account.
Code:public class SampleStrat : Strategy { private Account myAccount; protected override void OnStateChange() { if (State == State.SetDefaults) { Calculate = Calculate.OnPriceChange; lock (Account.All) myAccount = Account.All.FirstOrDefault(a => a.Name == "Sim101"); } } protected override void OnBarUpdate() { Print("Cash Value: " + myAccount.Get(AccountItem.CashValue, Currency.UsDollar)); } }Josh G.NinjaTrader Customer Service
Comment
-
mbesha,
It looks like you should expect to see CashValue, Relaized PnL, and Unrealized PnL while connected to a Rithmic account. If you want to see what all would be expected for account values please see the grid of Account values by data provider in the following link:
Josh G.NinjaTrader Customer Service
Comment
-
Hi JoshG
As you mentioned, I should expect to see those values but unfortunately I am not. And if the Ninjascript is in order, what then could be the problem? Could the fact that I use another trading platform to place and sometimes manage my orders be a factor here? Please help me to get to the bottom of this.
Comment
Latest Posts
Collapse
| Topics | Statistics | Last Post | ||
|---|---|---|---|---|
|
Started by argusthome, 03-08-2026, 10:06 AM
|
0 responses
94 views
0 likes
|
Last Post
by argusthome
03-08-2026, 10:06 AM
|
||
|
Started by NabilKhattabi, 03-06-2026, 11:18 AM
|
0 responses
49 views
0 likes
|
Last Post
|
||
|
Started by Deep42, 03-06-2026, 12:28 AM
|
0 responses
31 views
0 likes
|
Last Post
by Deep42
03-06-2026, 12:28 AM
|
||
|
Started by TheRealMorford, 03-05-2026, 06:15 PM
|
0 responses
34 views
0 likes
|
Last Post
|
||
|
Started by Mindset, 02-28-2026, 06:16 AM
|
0 responses
70 views
0 likes
|
Last Post
by Mindset
02-28-2026, 06:16 AM
|

Comment