View Full Version : Account Size - Safety Measure
03-29-2007, 09:56 AM
I'm looking to put a stop-gap safety net on my strategy. Something that will determine my account status, like if the account has lost 10% or something, flatten all trades (and make a martini). Could you point me in the right direction? Also, AccountSize() I see in the docs, but no info. Maybe that's what I need?
03-29-2007, 10:41 AM
We actually do not support account level properties in the NinjaScript layer at this time, however, I can see the value in what you ask. The code sample belows dives into our framework layer. This should do the trick but double check in testing on your live account. You will have to self calculate what the % loss value is against a user defined account cash balance. Mostbroker API's do not provide cash balance so its safer to enter the starting cash balance of your account when running the strategy. What I have provided below is conceptual.
Declare in variables section:
private bool safetyStop = false;
// If flat and safety stop is hit, stop process strategy logic
if (safetyStop && Position.MarketPosition == MarketPosition.Flat)
// In real-time only and the safety stop not yet hit
if (!Historical && !safetyStop)
// Check the account realized PnL and if the max loss exceeded
if (((AccountItem) Account.GetAccountItem(Cbi.AccountItemType.Realize dProftLoss, Cbi.Currency.Unknown)).Value < -100);
// Close any open position
safetyStop = true;
03-29-2007, 10:59 AM
Cool, understood will check it out. Thanks. -> So, what is that AccountSize() I see in the docs?
03-30-2007, 12:58 AM
That sets a user defined account balance. This can be used to dynamically generate position size. See the Help Guide for more granularity.
03-30-2007, 03:00 AM
I ammended the sample code below to just exit the OnBarUpdate() method as a suggested approach to prevent your strategy from running when account threshholds are breached.
04-28-2008, 02:40 PM
Does this work in NinjaTrader 6.5 and was this an example from inside a NinjaScript? I am wondering why I could not use a similar method of ((AccountItem) Account.GetAccountItem(Cbi.AccountItemType.BuyingP ower, Cbi.Currency.Unknown)).Value to get BuyingPower in the same way?
04-28-2008, 02:44 PM
It likely works in 6.5 but as this thread stated, this is in our framework layer and is actually outside the scope of what we provide support for.
04-28-2008, 02:50 PM
@Ray, thanks for the reply. I understand this is not formally supported, however this information would be very useful to a number of people on these forums (just from the searches I have done) and it seems you have had to answer this inquiry many times. So if there was an unsupported way (via the Framework) to obtain this information that may work (but still of course not be formally supported) I think there are many that would appreciate it. I will reply to this thread if I am able to make it work myself, as of yet, no luck :)