Topics
Replies

IncubusI
07 Sep 2015, 19:13

RE:

Paul_Hayes said:

Run this, tell me if it states in the log Initialized and then disposed or if it fails again.

protected override void Initialize()
        {
            RegistryKey registryKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\\R-core\\R");

            var envPath = Environment.GetEnvironmentVariable("PATH");
            rBinPath = (string)registryKey.GetValue("InstallPath");
            string rVersion = (string)registryKey.GetValue("Current Version");
            rBinPath = System.Environment.Is64BitProcess ? rBinPath + "\\bin\\x64" : rBinPath + "\\bin\\i386";
            Environment.SetEnvironmentVariable("PATH", envPath + Path.PathSeparator + rBinPath);

            REngine.SetEnvironmentVariables(rBinPath);
            r = REngine.GetInstance();

            r.Initialize();
            Print("Initialized...");

            // close engine
            r.Dispose();
            Print("Disposed...");
        }

 

It only worked the first time. I created a new indicator with this code. It worked. R was running and then stopped with dispose, but second time I tried to run it it failed again like in previous cases.


@IncubusI

IncubusI
07 Sep 2015, 18:42

RE:

Paul_Hayes said:

Did you install the R.NET other than with NuGet?

How did you setup your registry values?

Yes. I tried installing R.NET from source too but it didn't help.

What do you mean by registry values?


@IncubusI

IncubusI
07 Sep 2015, 18:15

Paul I am sending the code of my indicator. 

You can even disregard the Calculate() function since the code never reaches it. It fails at r = REngine.GetInstance(); Also disregard the while(true) loop - its just there because I was trying to catch where it fails and why isn't R running.

Here are the steps how I am running the program:

1. Edit the code in visual studio

2. Attach to the process cAlgo

3. Add instance 

 

Try it out for yourself and see how it works for you. Also you should use 1.6.5.0 version of R.NET (I don't know which one you downloaded from NuGet).

Thank you for helping out, its really frustrating since I am dealing with this issue for a long time now and I am running out of options and ideas.

 

using System;
using cAlgo.API;
using cAlgo.API.Internals;
using cAlgo.API.Indicators;
using cAlgo.Indicators;
using RDotNet;
using System.IO;
using Microsoft.Win32;

namespace cAlgo
{
    [Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.FullAccess)]
    public class indicator : Indicator
    {
        [Parameter(DefaultValue = 0.0)]
        public double Parameter { get; set; }

        [Output("avg")]
        public IndicatorDataSeries avg { get; set; }

        private static REngine r;

        private string rBinPath { get; set; }

        protected override void Initialize()
        {
            // Initialize and create nested indicators
            using (RegistryKey registryKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\\R-       core\\R"))
            {
                var envPath = Environment.GetEnvironmentVariable("PATH");
                rBinPath = (string)registryKey.GetValue("InstallPath");
                string rVersion = (string)registryKey.GetValue("Current Version");
                rBinPath = System.Environment.Is64BitProcess ? rBinPath + "\\bin\\x64" : rBinPath + "\\bin\\i386";
                Environment.SetEnvironmentVariable("PATH", envPath + Path.PathSeparator + rBinPath);
            }
            while (true)
            {
                try
                {
                    REngine.SetEnvironmentVariables(rBinPath);
                    r = REngine.GetInstance();
                    if (r.IsRunning)
                    {
                        break;
                    }
                    else
                    {
                        r.Initialize();
                    }
                } catch (Exception ex)
                {
                    Print("Error:" + ex.ToString());
                }
            }
        }

        public override void Calculate(int index)
        {
            // Calculate value at specified index
            // Result[index] = ...
            double open = MarketSeries.Open[index];
            double high = MarketSeries.High[index];
            double low = MarketSeries.Low[index];
            double close = MarketSeries.Close[index];

            avg[index] = avgPrice(open, high, low, close);
        }

        private double avgPrice(double open, double high, double low, double close)
        {
            r.SetSymbol("open", r.CreateNumeric(open));
            r.SetSymbol("high", r.CreateNumeric(high));
            r.SetSymbol("low", r.CreateNumeric(low));
            r.SetSymbol("close", r.CreateNumeric(close));
            NumericVector avg = r.Evaluate("(open + high + low + close)/4").AsNumeric();
            return (Convert.ToDouble(avg[0]));
        }
    }
}

 


@IncubusI

IncubusI
07 Sep 2015, 16:06

RE:

Paul_Hayes said:

ok, has it been working before and has it now suddenly stopped working or is this a new project?

Have you got it working with the cAlgo API before?

It is a new project. I have not used R (R.NET) with cAlgo before. Have you ever tried using R with cAlgo? 
It works only the first time when I make a new indicator. After that it fails every time. 

Also I have tried to put R.NET source as reference and debug it but didn't come up with anything useful.


@IncubusI

IncubusI
07 Sep 2015, 15:04

RE:

Paul_Hayes said:

Is this what you are using?

https://rdotnet.codeplex.com/documentation

 

Yes


@IncubusI