How to get account equity & set position size in strategy  [SOLVED]

Questions about MultiCharts .NET and user contributed studies.
robbob
Posts: 46
Joined: 26 Jan 2013
Has thanked: 2 times
Been thanked: 3 times

How to get account equity & set position size in strategy

Postby robbob » 27 Feb 2013

Hi,

I would like to dynamically set the position size of my buy orders so that they are generated based on the current account equity, ATR value of the instrument being traded, as well as the tick size (in dollars) of the instrument being traded.

I've found a few easy language examples, but haven't found much in the way of .Net.

thanks,
-Rob

User avatar
Henry MultiСharts
Posts: 9165
Joined: 25 Aug 2011
Has thanked: 1264 times
Been thanked: 2957 times

Re: How to get account equity & set position size in strateg

Postby Henry MultiСharts » 27 Feb 2013

Hello Rob,

You can find a sample code that shows how to specify the amount of contracts in the code attached to this post.

markwit229
Posts: 14
Joined: 23 Jan 2013
Has thanked: 4 times

Re: How to get account equity & set position size in strateg

Postby markwit229 » 27 Feb 2013

Thanks, Henry. I had a similar question and would like to know if I'm approaching a solution in the best way. In backtesting, I would like to be able to dynamically calculate the number of contracts I should purchase at each trade depending on the level of risk I'm willing to take. As an example, the E-mini S&P 500 is trading at ~$1,500 with a big point value of 50. If I wanted to be 10x "levered", I would purchase one contract for every $7,500 of available capital. In my strategy script, I then would need to access total available capital for trading, which is my problem.

My current solution has been to do 'this.InitialCapital + this.NetProfit' (within the strategy script) to arrive at total capital available for trading. However, I believe this will not work if I have more than one signal on the chart (e.g., long-entry signal, short-entry signal, profit target signal, stop loss target, etc.) as I would need to know 'this.NetProfit' for every signal. Is there a better way to do this (I had assumed there would have been something like 'StrategyInfo.CurrentCapital' but there is not)? Also, if I am correct that my solution will not work across multiple signals, how could I go about doing so (aside from having every individual signal inside one script)?

Thanks,
Mark

robbob
Posts: 46
Joined: 26 Jan 2013
Has thanked: 2 times
Been thanked: 3 times

Re: How to get account equity & set position size in strateg

Postby robbob » 27 Feb 2013

Thanks Henry,
I am attempting to accomplish something similar to what Mark is doing, so I would need to be able to access the account balance, and the point value of the instrument in order to calculate the position size. The example you posted however uses a hard coded value to set the position size.

Thanks,
-Rob

User avatar
Henry MultiСharts
Posts: 9165
Joined: 25 Aug 2011
Has thanked: 1264 times
Been thanked: 2957 times

Re: How to get account equity & set position size in strateg  [SOLVED]

Postby Henry MultiСharts » 28 Feb 2013

I believe this will not work if I have more than one signal on the chart (e.g., long-entry signal, short-entry signal, profit target signal, stop loss target, etc.) as I would need to know 'this.NetProfit' for every signal.
You do not need to know it for every signal and you do not need to put every individual signal inside one script.
The values are returned per srategy. All of the signals applied to one chart form a single strategy.
My current solution has been to do 'this.InitialCapital + this.NetProfit' (within the strategy script) to arrive at total capital available for trading.
In order to access total available capital for trading you just need to add to your calculation IStrategyPerformance.OpenEquity: Returns potential profit, i.e. current Profit and Loss, aka PnL.

User avatar
Henry MultiСharts
Posts: 9165
Joined: 25 Aug 2011
Has thanked: 1264 times
Been thanked: 2957 times

Re: How to get account equity & set position size in strateg

Postby Henry MultiСharts » 28 Feb 2013

I am attempting to accomplish something similar to what Mark is doing, so I would need to be able to access the account balance, and the point value of the instrument in order to calculate the position size.

To access the account balance you can use Account.Balance Property
The big point point value of the instrument is returned by Bars.Info.BigPointValue
the tick size (in dollars)
Check this example.
The example you posted however uses a hard coded value to set the position size.
It just shows how to get the position size specified in the code. You can modify the code the way you need it. In case you have a problem achieving your goal with a certain code-please let me know and we will review the code.

robbob
Posts: 46
Joined: 26 Jan 2013
Has thanked: 2 times
Been thanked: 3 times

Re: How to get account equity & set position size in strateg

Postby robbob » 10 Mar 2013

I think I'm still missing something here. The "Account" property that is accessible from my strategy that extends SignalObject is a String, not an object or struct. How do I get a reference to the Account object that you mention in your reply which has a "Balance" property.


Thanks,
-Rob

User avatar
Henry MultiСharts
Posts: 9165
Joined: 25 Aug 2011
Has thanked: 1264 times
Been thanked: 2957 times

Re: How to get account equity & set position size in strateg

Postby Henry MultiСharts » 14 Mar 2013

The "Account" property that is accessible from my strategy that extends SignalObject is a String, not an object or struct.
You need to use Account.Balance Property. Example:

Code: Select all

TradeManager.ProcessEvents();
...
TradeManager.TradingData.Accounts.Items[0].Balance;


Return to “MultiCharts .NET”