If you have an advanced ranking logic in your trading system and needs to do a lot of optimizations then you should probably take a look at this article. You will find here instructions on how to make optimization run faster, much faster in some cases. How? Simply by creating a custom database to store ranking data and using that database to retrieve ranks for each symbol. Instead of recalculating ranking for each optimization (In case ranking formula doesn’t contain any optimizable variable), your formula will read ranking data from a custom database where you stored that same ranking data previously. Why? Because reading ready-to-use data from a database is faster (around 5 times) than creating composites and ranking securities on fly. The higher the number of securities used in ranking the faster this solution would be compared to ranking on fly. How to store ranking data in a custom database? To do this, we will have to create a custom script. - Create a new script using (Tools -> Script Editor) - Copy the content of the script located here: Click to Get Script - Add one or several ranking formulas, update few variables - Click on "Execute" to calculate ranking and store them in a custom database Here is what you should update in the script: "databaseName" variable: Gets the database name "symbols" variable: Gets the symbols used in the ranking. In the parameter you can define a symbols filter (Symbol -> Symbols View) To create a new ranking formula, simply type: Formula.Create("field name", "[formula]", min, max, step); The min, max and step variables are useful if you want to create several variations of the same formula. Example: Formula.Create("Perf", "Perf(close, [])", 10, 100, 20); // [] is replaced by the appropriate value If you want to create a single variable (example: Performance over the last 50 bars) Formula.Create("Perf", "Perf(close, 50)", 50, 50, 50); How to display ranking data? After you set up your formulas and execute the above script, QuantShare starts calculating ranking and saving them in your custom database. Here is how to display that data: - Select "Data" then "Edit Databases" - Select "Custom" next to "Choose database" - Select your custom database then select a symbol (that was part of the symbols filter specified in the above script) You can compare that data by running a screen at a particular date and using the same formula. How to get ranking in my trading system? Let us use the following basic trading system: Rank NASDAQ 100 stocks based on their 5-bar performance and buy the top 5 ones if they have a volume higher than N times previous day volume. Our QS language formula would look like this: Optimize("n", 0, 5, 1); p = perf(close, 5); r = comp(p, "rank"); buy = r <= 5 and volume > n*volume[1]; We previously created a ranking database (MyDb -> Perf) using the following formula: string databaseName = "MyDb"; Formula.Create("Perf", "Perf(close, 50)", 5, 5, 5); Now, how can we change our trading system formula to use the custom database instead of calculating ranking on each optimization? Very simply, just replace "comp" line by: r = GetData("MyDb", "Perf"); Note: It is important to use the same symbols filter in your trading system than the one you used in the ranking script.
|