Even if the simulator plug-in offers many features and can perfectly suits the need of the majority of traders and investors, it isn't sufficient to pundits and traders who want to create sophisticated simulations and backtests.
You may want to create several strategies in a single portfolio and then adjust the equity size of each strategy depending on how it has performed during the last quarter. Or you may want to hedge a long portfolio with some specific stocks or hedge your portfolio against inflation or a particular currency. There are a lot of advanced backtesting scenarios that simply could be performed using the basic simulator, they require the money management tool.
The money management plug-in is composed of five events. Each event is executed during a particular period. As an example, the OnStartSimulation event is executed just before the simulation starts, and the OnEndPeriod event is executed just after a period ends. In a daily timeframe, a period is a day.
Trader can type some line of codes for one or several of these events. The code will tell and instruct the trading software on the course of action to take. You can for example create a position sizing script by catching the OnNewPosition event and updating the number of share to buy or short.
The tool requires some practice and knowledge of the basic functions. In the rest of the article, we will show you how to write a money management script that creates a simple long trading system using buy and sell rules. The trading system will be exactly the same as the one you can create with the basic simulator. However, it will be created entirely by the money management tool and it will be the good example to show you the basic functions of the money management scripting language.
You can open the money management tool by selecting "Analysis" then "Advanced Money Management". Or you can update a trading system, select the money management tab and then create a new script.
Either way, you will end up with a form that looks like this.
Most of the code will be contained in the "OnEndPeriod" event.
This first thing to do is to loop through the symbols (stocks, futures or whatever asset you are trading) that you have or will specify in your trading system.
The list of symbols can be retrieved using the following line:
string symbols = Data.GetSymbols();
For each symbol, we must check if the portfolio contains a long position for this symbol and if there is a pending order to sell the security.
Portfolio.IsInPortfolio(symbol, true) -> Check whether a long position for the specified symbol exists in the portfolio
Portfolio.IsInPendingOrders(symbol, "sell") -> Check whether a sell pending order exists
At any time, you can click on CONTROL + SPACE to display the list of available variables. Besides, each time you type the opening parenthesis of a function, a tooltip displays a description of the function and enumerates the parameters the function accepts.
To be continued.