Click here to Login

Speed Up Optimizations by Saving Ranking Data into a Custom Database

Updated on 2014-04-19

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.


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.


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.


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.

4 comments (Log in)

QuantShare Blog
Search Posts

Recent Posts

Create Graphs using the Grid Tool
Posted 1318 days ago

Profile Graphs
Posted 1423 days ago

Previous Posts

How Does QuantShare Work?
Posted 3795 days ago

Troubleshooting a Trading System
Posted 3836 days ago

More Posts


Create an account
Affiliate Program
Contact Us
Trading Forum
How-to Lessons
About Us
Terms of Use

Copyright © 2024
Social Media
Follow us on Facebook
Twitter Follow us on Twitter
Follow us on Google+
RSS Trading Items

Trading financial instruments, including foreign exchange on margin, carries a high level of risk and is not suitable for all investors. The high degree of leverage can work against you as well as for you. Before deciding to invest in financial instruments or foreign exchange you should carefully consider your investment objectives, level of experience, and risk appetite. The possibility exists that you could sustain a loss of some or all of your initial investment and therefore you should not invest money that you cannot afford to lose. You should be aware of all the risks associated with trading and seek advice from an independent financial advisor if you have any doubts.