Finally… How to Get The Size Of Your Account Inside Of A NinjaTrader Strategy
I have been using this for a while, since the early NinjaTrader 7 betas, which seems like forever ago and it has been very useful. Mainly used in risk management, which is usually overlooked by many.
Knowing the account size is very , very helpful for writing code for risk management. Risk management is so important in strategies that most of MY strategies have most of the code in risk management, not entries or exits. Developers really need to spend more time getting the risk management part of the code perfect.
THE OLD WAY
When I USED to do custom programming ( I have no time ) , what I would do would be to keep the starting value of the account and then use the running profit to calculate what the current value of the account was during the trading session.
Painful and convuluted, but it worked. It did have a couple of problems. Well, big problems. The first was that you have to input the starting account value at the start of the session. The second is when you are trading more than one automated system in your account. You are keeping the running total for THAT strategy only and you have no idea of what the other strategies are doing.
FIXED FRACTIONAL PRICING EXAMPLE
As an example, say you only want to risk 2% of your trade on an a trade. That is fixed fractional risk management which is very popular. You have a $20,000 account, are trading two strategies and you want to trade one contract per $5,000.
The old way, Say strategy #1 had lost $10,000. Strategy #1 will know about the loss and start trading 2 contracts instead of the previous 4 contracts. Strategy #2 should start to trade 2 contracts BUT it doesn’t know about the loss so it will continue to trade 4 contracts. This was typical using NinjaTrader 6.5, which frankly sucked.
TRADE SIZE
I think the biggest thing is that is helps position sizing and here are some code snippets to help get you started. Tons of people recommend you only risk a certain percentage of capital on each trade and the following code shows you how to do that.. You can use this to set trade size or the amount you risk by setting the stop loss.
Say you want to enter a trade one contract for every $5,000 in cash you have.
position_size = (int) GetAccountValue(AccountItem.CashValue)) / 5000 ;
Or you could calculate the max risk by calculating 2% of your cash.
max_risk = GetAccountValue(AccountItem.CashValue)) * 0.02 ;
Now, please note that futures and equities work differently in the amounts reported.
GET ACCOUNT VALUE
The function that does all the work is the GetAccountValue call. There are a couple of things to notice about this call. The first is that it will return 0 for historical data and that it returns the information on the account that the strategy is running on at the time. The other is that there are more items in Cbi.AccountItem then just the cash value, so you should explore those.
public double GetAccountValue(Cbi.AccountItem accountItem)
PUTTING IT ALL TOGETHER
So let us make a simple example. We will trade 10 contracts when backtesting, one contract per $5,000 running real time or replay.
protected override void OnBarUpdate()
{
if (Historical)
{
contracts_to_trade = 10 ; // trade 10 historically
}
else
{
if (GetAccountValue(AccountItem.CashValue) > 5000)
{
contracts_to_trade = (int) GetAccountValue(AccountItem.CashValue)) / 5000 ;
}
}
}
GetAccountValue() doesn’t require very much computing power so you can call it fairly often.
NOTE
The ability or inability for these functions to work depend on the information provided by the broker to NinjaTrader. So you should test using your brokers connection.
If you have any questions, just leave a comment.
Photo Courtesy John Althouse Cohen
PIMPING MY NEWSLETTER
I don’t post often, but I do have a mailing list you can subscribe to in the meantime. I usually write about Ninjatrader programming, but other trading topics as well. Subscribe to TraderWerks Blog by Email
Good to see you! If you are new here, you can subscribe to the RSS feed for updates


