Documentation

Last updated on 25. 6. 2015 by Mark Fric

Add new Monte Carlo column

An example of how to add new value in Monte Carlo method.

What is the task about?

By default, when you run Monte Carlo simulation you’ll get simulation results for given confidence levels.
MC simulation computes results computed like Net profit, % Net Profit, Return / DD etc. for given confidence level.

The user on the forum requested computing a new custom value – number of consecutive losing trades.

As many other things, values (columns ) in Monte Carlo report are computed using snippets, so it is possible to extend it and add your own.

In this article we’ll show how to add new MC value that computes the number of consecutive losing trades for all the Monte Carlo confidence levels.

Step 1 – Create new Monte Carlo Value snippet

As all the other snippets, Monte Carlo snippets are editable in QuantEditor, so we’ll start it:

Now we’ll create a new snippet that will compute our consecutive losing orders:

This will create a skeleton of a snippet that we can extend to do what we want. We’ll go through the functions in the snippet one by one.

In the constructor we have to set the column name and the ordering. If the best values are the highest ones then we should set it to MAXIMIZE.
In our case, the best values are the lowest ones (as little number of consecutive losing trades as possible), so we’ll set the order to MINIMIZE.

This is important so that the program knows how to sort the results when it will be computing results for each confidence level.

public ConsecutiveLosingOrders() {
   super("ConsecutiveLosingOrders", FitnessFunction.MINIMIZE);

 


setColumnName(L.t("Consecutive Losing Orders"));
setColumnWidth(100);
setTooltip(L.t("Consecutive Losing Orders"));
}
method compute() is the heart of our snippet, it contains the code that does the actual computation of maximum consecutive losing orders.
The algorithm to compute this is quite simple, it simply goes through the array of orders and remembers the biggest streak of losing orders.

protected double compute(MonteCarloStatValues statValues, double initialCapital, double[] orders, double avgTradesPerMonth) throws Exception {
   int maxConsecLosingOrders = 0;
   int consecLosingOrders = 0;

for(int i=0; i<orders.length; i++) {
if(orders[i] > 0) {
// it is winning order, reset the counter
consecLosingOrders = 0;
} else {
// it is losing order
consecLosingOrders++;

if(consecLosingOrders > maxConsecLosingOrders) {
maxConsecLosingOrders = consecLosingOrders;
}
}
}

return maxConsecLosingOrders;
}

And the last function is for formating the computed value

public String formatValue(double value) {
   return String.valueOf((int) value);
}

This is all, we can hit the Compile button and our new snippet should be compiled without any error.

Step 2 – add our new value to the view

If we’ll look at Monte Carlo table in Quant Analyzer we’ll see that our new column is still not visible there. It is because the visible columns are managed in Views.

We have to click on Configure view link on the top right corner of the table to add new column to the view:

Here we have to add the Consecutive Losing Orders column to the list of displayed columns on the right.

Once it is done we can see that our Monte Carlo results table now contains also consecutive losing orders column with computed values:

This is all that was required to add new value to the Monte Carlo results.

You can see it was simple, the only real challenge is to correctly implement the compute() function in a way that it computes what you want.

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

Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments