Documentation

Last updated on 19. 6. 2015 by Mark Fric

Create a new What-If function

QuantAnalyzer 4 can be extended to use your own custom statistical values, What If, Equity or Monte Carlo methods, many other parts of the program can be also extended by snippets..

All these methods in the program are fully dynamic, each method (including the default ones) is an independent Java class – snippet – implementing 1 or 2 simple functions that manipulate the orders.
You can take inspiration on how the existing methods are made when implementing your own idea.

To extend QuantAnalyzer you have to use the build-in QuantEditor. It is a code editor that allows you to create and edit classes that will be then used in QuantAnalyzer.
The principle is very similar to working with custom indicators in your trading platform, but the coding is much simpler.

Example – creating a new What -If function

What If cases allow you to test what-if scenarios – for example what if you’d take trades only from Monday to Thursday? Or take only 1 trade per day?

With QuantEditor you can create your own cases like these. In our example we’ll make a simple case that takes every second trade.
It is not very useful scenario, but it shows how simple it is to add new feature to What-If scenarios.

We’ll start by opening QuantEditor from tool bar.

QuantEditor will open in new window. Because we want to cerate a new What-If case, we click on new in QE toolbar. There we’ll choose What If option and name our file TakeEverySecondTrade.

After clicking OK the editor will create new class using a working template for What If class that already contains the generic code for What-If class.

The class has only two methods – constructor TakeEverySecondTrade() and function filter() that filters the list of orders according to our criteria.

Let’s look at the constructor first.
It is named as same as the name of our file and it is called in the beginning when this class is loaded.

Generally we shoud set the class name – we’ll see it in What-If options in EA Analyzer by this name. In our case we’ll use “Take Every Second Trade”.
If our class requires some parameters we can define them here using calls like addIntParameter(), addFloatParameter, addComboBoxParameter().
You can find description of all these functions in Help section in QuantEditor.

In our case we don’t need any parameters so our constructor will look like this:

public TakeEverySecondTrade() {
   setName("Take every second trade"); 
   setFormatedName("Take every second trade");
}

Now we need to define our filter() function. This is the function where we should specify what we want from the class.
This function is called when you select this What-If case. It receives list of all orders sorted by open time. You can manipulate with the orders list or a particular order; or remove an order from the list if you want to.

The basic code (shown below) does nothing, it just goes through list of orders without any action.

public void filter(SQOrderList originalOrders) throws Exception {
   int parameter = getIntParameterValue("_PARAMETER_");

for(Iterator<SQOrder> i = originalOrders.listIterator(); i.hasNext();) {
SQOrder order = i.next();

// todo – your custom action
// orders can be skipped or manipulated here
}
}

In our case we want to remove every second order in the list, so that only half of the orders are left in the list and we can do it with the following code:

public void filter(SQOrderList originalOrders) {
   boolean isSecondOrder = false;

for(Iterator<SQOrder> i = originalOrders.listIterator(); i.hasNext();) {
SQOrder order = i.next();

// if variable isSecondOrder = true it will remove the order from results
if(isSecondOrder) i.remove();

// set isSecondOrder variable to opposite value, from true to false, from false to true
isSecondOrder = !isSecondOrder;
}
}

Code explanation:
– we first create isSecondOrder variabele and set it to false
– then as we go through the orders we check if this variable is true and if yes we remove the order
– after every check we set the variable to opposite value (from false to true, from true to false) and go to next order

This code will effectively remove every second order from the list of orders.

This is it. We are done.

Now we can click on Compile button and our new class should be compiled without any errors.

Now switch back to EA Analyzer, go to What If scenarios and you should see the new functionality that we just implemented there:

Apply it on some strategy and you should see that it will have only half the trades

In this example we shown how EA Analyzer 3 makes it possible to add new custom What-If function. You can create new Monte Carlo or Equity Control functionality in a very siimilar way.

If you’ll create some interesting function you are welcome to share it on our forum with others.

Was this article helpful? The article was useful The article was not useful

Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments