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!
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
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,
With regard to the Margin values, are there any plans to have them included in future releases?
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.
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 trilliantrader, Today, 08:16 AM
|
0 responses
3 views
0 likes
|
Last Post Today, 08:16 AM | ||
Started by AttiM, 02-14-2024, 05:20 PM
|
9 responses
174 views
0 likes
|
Last Post Today, 08:14 AM | ||
Started by funk10101, Today, 08:14 AM
|
0 responses
2 views
0 likes
|
Last Post
by funk10101
Today, 08:14 AM
|
||
Started by adeelshahzad, Today, 03:54 AM
|
1 response
13 views
0 likes
|
Last Post Today, 08:12 AM | ||
Started by RookieTrader, Today, 07:41 AM
|
1 response
6 views
0 likes
|
Last Post Today, 07:59 AM |
Comment