In my recent articles I was focusing on strategy hypotheses checking and strategies paper-trading in R. So all of these activities with so much of development effort in it bring a valid question. Is there any value in such backtesting and hypothesis checking? My answer is definitely yes, and I'm going try to persuade you about it.

Why doing back test in R when my strategy runs in Java/JForex?

This is valid question. Why anyone would be bothering writing thousand lines of code without any possible reuse?
First of all R is good for quick strategy prototyping. Before we would be bothering to write single line of code in Java, it's better to assure that our strategy is profitable. And R is very good tool for it. Ok, fair enough so far. However this would lead us to justified worries about rewriting whole strategy that we potentially have in R to Java. This could be potentially quite big issue since libraries we are used to use in R usually do not have their opposite siblings in Java world. So my personal intention about strategies being written in R is to be able to reuse as much from the code base in R as possible. How?

Reuse R code in Java/JForex? Use Renjin!

When I was starting investigation about possibilities how to reuse my code written in R from JForex strategy I've found library called Renjin [1]. I was really excited with Renjin at the beginning because effectively it's R and R libraries rewritten into Java and so runnable in Java Virtual Machine. So the idea to overcome JavaR with pure Java code run withing same process on JVM is great. However after several hours of playing with Renjin I've came to conclusion that it's not mature enough for using it in automation strategies. E.g. I'm used to use the XTS objects in R and Renjin does not support those objects fully yet. There more obstacles even with more simpler things on Renjin side like e.g. printing to console and also Renjin does not meet my main requirement on R code re-usage. Simply I'd be eventually force to rewrite my R code quite extensively with Renjin, so I've postponed Renjine indefinitely even though I do really think that it's very promising framework.

Forget Renjin, go with JRI!

Fortunately there are more options how to call R from Java. Because after my initial testing of JRI[2] I;ve realized that JRI fully meets my requirement about R code reuse, I've choose JRI as my interface between R and Java.

JRI installation

There are two installation steps that must be done to make JRI running. First step is installation rJava, which JRI is partof, into R by following code snippet : install.packages("rJava",dependencies = T)


If you see message about successful installation as on above image we can continue with step two and make JRI to be usable from our Java strategy. To do this add 2 paths in the system Path variable through Control Panel \ System and Security \ System and option Advanced Settings (see below screenshot).
First path is path to your R bin, in my case it's on D drive and 64 bit D:\Program Files\R\R-3.3.1\bin\x64;
The second path is reference path to the rJava we installed from R D:\Program Files\R\R-3.3.1\library\rJava\jri\x64



After that you can check PATH from command prompt and you should find both path listed in the path


The last step is downloading JRI.zip package and place it in the lib path of your local JForex project installation. On below image you can see JRI.zip within other Dukascopy libraries in my IntelliJ Idea.


So we should now have everything in place and can test some call from R to Java.

Calling R script from IStrategy.OnBar

To test JRI we first need to create some test script in R. I've created simple script test.R and included the script file as embedded resource in my local JForex strategy tester (see below screenshot). The script itself does not do anything useful. It just prints content of msg and then perform linear regression over artificially created time series




The last part is create strategy calling my R script from Java. For this purpose I've created IStrategy implementation called JRITestStrategy (see source code on github [3]) and just for testing purposes test.R script is called each 5 minute bar. The main code is shown below:


So at the beginning REngine is instantiated and checked. It's effectively looking into the path we recently added in our sytem path and tries to create instance of R. If it fails, it prints our "Cannot load R" and nothing happens.
When R is successfully loaded it first pass message "Hi there" to the local variable msg and then our test.R script is triggered and all output is captured in the string array. So when strategy is run you should see below output in the Java console on each 5 minutes bar:

Though this particular sample does not do anything usable, with the same approach we are able to make extended prototypes and e.g. call a script performing neural net prediction over array of Close prices passed from strategy. But this is subject for another article

Have fun with JRI, Java, and R and do not hesitate to ask questions below the article.

[1] Renjin
[2]
JRI
[3] JRITestStrategy.java
Translate to English Show original