Before continuing, please read the first part of the article here: Create a trading strategy using the money management tool - Part 1 After passing the last two tests (Is it in the portfolio? Is it in the pending orders list?), we check if there is market data for the security on the current date (The current date can be retrieved using the "Divers.CurrentDate" function). After that, we must verify that the security passes our buy rules. To do that, we will have to parse the formula that will contain the buy rules, execute it, extract the result and then verify it. Look at the following code: MMParser parser = Data.ParseFormula("a = " + buyrule + ";"); TimeSeries a = parser.GetTimeSeries(symbol, "a"); if(a[0] == 1) { // Pass } In the first line, we create the formula parser, we then extract the appropriate time series in the second line and finally we verify that the current value of the time series is equal to 1. Note that "a[0]" gets the current value of the time series "a", while "a[n]" gets the time series value N bars ago. Negative values of N reference future bars. Once the security meets the rule's criteria, we create our order by calling the "Functions.AddLongPosition" function. Functions.AddLongPosition(symbol, null); If we don't specify the number of shares to buy, the trading software will automatically assign a number of shares that depends on the maximum number of positions allowed for the category (Long Positions) and the available amount of cash. We set the Trading Order parameter (the second parameter) to null in order to use the default order type that was defined in the first screen of the trading system editor. Instead of the previous line, you can type the following code to assign one or several stops to the long position: MMPositionSettings settings = Functions.CreatePositionSettings("long"); settings.AddNBarsStop(10); Functions.AddLongPosition(symbol, null, settings); You can debug any money management script, using the "Divers.Output" method. The output or log is visible in the simulation report, under the "Details" tab. A slightly more advanced version of this money management script can be downloaded here Long strategy created with the money management tool.
|