Profile Picture

R.Net REngine.GetInstance() only work works for the first simulation

Posted By andvac 4 Years Ago

R.Net REngine.GetInstance() only work works for the first simulation

Posted Thursday May 28 2015
Hey guys,

I really need your help on this one. I've been banging my head for hours. Here's the problem: I'm trying to use R.Net to execute some r functions as part of my trading system. If a open RightEdge and start a simulation, everything works as expected, the simulation ends, and there are no errors. However, if I start a second simulation, RightEdge hangs on the call to REngine.GetInstance() and even clicking cancel won't work. If I open RightEdge again and start another simulation, everything works again.

It's important to note that R.Net is expected to work as a singleton. The only way to create an REngine instance is to call REngine.GetInstance(). I initially tought that my problem was in my r code, so I started logging all output and error message from R to a file. It turns out that my code runs properly, however after the simulation is completed the log file cannot be deleted because it's still being used by RightEdge. I suspect that the problem is that RightEdge doesn't completely dispose and release the trading system at the end of a simulation.

You can easily repro this problem as follows.

1. Create a new RightEdge trading system with the default (empty) template
2. Download R.Net 1.6 and Dynamic Interop 0.7.4 and unpack the packages
3. Reference RDotNet.dll, RDotNet.NativeLibrary.dll, and DynamicInterop.dll in your project
4. Replace MySystem with the class definition below
5. Select a short time range a run the simulation one time
6. Try to delete temp.txt in the RightEdge program folder, you should get an error message that the file is being used
7. Run the simulation again, this time RightEdge should hang forever
8. Become my hero by fixing this problem!

public class MySystem : MySystemBase
    public override void Startup()
    REngine engine = REngine.GetInstance();
    engine.Evaluate(@"logger = file('test.txt', open='w')");
    engine.Evaluate(@"sink(logger, type='message')");
    engine.Evaluate(@"print('hello world')");
    // engine.Dispose();

Please note that I played with the Dispose() but I couldn't find anything that works. I really hope you guys can help me with this!
Posted Sunday May 31 2015
Each time you run a system in RightEdge, it is run in a new AppDomain.  This is probably what is causing the issue.  I've posted a question on StackOverflow asking how to deal with multiple AppDomains in R.NET.

Posted Monday February 29 2016
Hi, I am now sitting with the same problem.  Has any workaround been found, as it seems that once an REngine  project has been initialized, it can not be reused, even after it has been disposed of.  A pity, as R really has good algo's, not available in commercially available C# libraries.

Posted Sunday March 13 2016
There's still no easy solution for this.  A possible solution would be to launch a separate process from your trading system and do your calls into R from the separate process.  That's not going to be simple though.


Similar Topics

Reading This Topic

2005-2019 © RightEdge Systems