已经和股票打交道十来年了,一直梦想找到一个能指导买卖的软件。先后接触过海融资讯、钱龙、大智慧、分析家、指南针等等各类股票分析软件,加上大量的自编公式,每每都是惊喜到失落。目前看来,大部分方法都只适合马后炮式的研究,每只股票都有自己的行业特点,每只股票后面的参与人群都不相同,每只股票的运行环境都不尽相同,因此想拿出一个放之四海而皆准的指标来预测股票的运行,基本是无异于痴人说梦。但是为了大作业,为了实战下R和C# 的知识,还是勉为其难的做一下研究吧。
操作系统:Winfows 7旗舰版 64位。
编程软件VS2012+DevExpress3.25控件。
R 3.0.2 + R Studio 0.97
数据库系统:MySQL 5.1 (运行在CentOS 6.0下),条件允许的情况下再试验下MongoDB数据库。
1. 基础部分之一 c#和R的交互
对于c#和R的交互,在开始学习R的时候就开始一直关注这方面的资料。C#在在开发应用程序方面十分有优势,R语言强大的统计计算功能和简洁的语法让人十分心动,但是R对普通用户来说,使用难度是非常大的,而且交互性相对c#应用程序来说也是比较差的。所以如果能将这两者结合起来,那么就能碰撞出美妙的应用程序出来。
网上的资料基本都是下面几种应用方式:
1. R/Scilab (D)COM Server。
http://www.codeproject.com/KB/cs/RtoCSharp.aspx
2. 利用
http://rcom.univie.ac.at/提供的组件,有免费版和商业版
3. R.NET,
https://rdotnet.codeplex.com/
经过一番研究,最终选定了第三个方案,目前存在的问题是无法在WinForm界面绘图和在同一程序中进行Multiple initialization。其中第一个问题有了测试方案,因为目前来说这个不是重点,如果时间允许的话再进行下相关的研究。
在程序中引用非常简单,因为R.NET 1.5的版本在NuGet Gallery中,我们只需使用"Install-Package R.NET"安装即可。
设置R路径
public static void SetupPath()
{
var oldPath = System.Environment.GetEnvironmentVariable("PATH");
var rPath = System.Environment.Is64BitProcess ? @"C:\Program Files\R\R-3.0.2\bin\x64" : @"C:\Program Files\R\R-3.0.2\bin\i386";
// Mac OS X
//var rPath = "/Library/Frameworks/R.framework/Libraries";
// Linux (in case of libR.so exists in the directory)
//var rPath = "/usr/lib";
if (Directory.Exists(rPath) == false)
throw new DirectoryNotFoundException(string.Format("Could not found the specified path to the directory containing R.dll: {0}", rPath));
var newPath = string.Format("{0}{1}{2}", rPath, System.IO.Path.PathSeparator, oldPath);
System.Environment.SetEnvironmentVariable("PATH", newPath);
// NOTE: you may need to set up R_HOME manually also on some machines
string rHome = "";
var platform = Environment.OSVersion.Platform;
switch (platform)
{
case PlatformID.Win32NT:
break; // R on Windows seems to have a way to deduce its R_HOME if its R.dll is in the PATH
case PlatformID.MacOSX:
rHome = "/Library/Frameworks/R.framework/Resources";
break;
case PlatformID.Unix:
rHome = "/usr/lib/R";
break;
default:
throw new NotSupportedException(platform.ToString());
}
if (!string.IsNullOrEmpty(rHome))
Environment.SetEnvironmentVariable("R_HOME", rHome);
}
复制代码
例子1:在c#中调用R,用一个Hello R 来开胃吧
static void Main(string[] args)
{
//REngine.SetEnvironmentVariables(); // Currently under development - coming soon
SetupPath(); // current process, soon to be deprecated
using (REngine engine = REngine.CreateInstance("RDotNet"))
{
engine.Initialize(); // required since v1.5
CharacterVector charVec = engine.CreateCharacterVector(new[] { "Hello, R world!, .NET speaking" });
engine.SetSymbol("greetings", charVec);
engine.Evaluate("str(greetings)"); // print out in the console
string[] a = engine.Evaluate("'Hi there .NET, from the R engine'").AsCharacter().ToArray();
Console.WriteLine("R answered: '{0}'", a[0]);
Console.WriteLine("Press any key to exit the program");
Console.ReadKey();
}
}
复制代码
例子2:c#调用Iris数据集
public enum IrisSpecies
{
setosa = 1,
versicolor = 2,
virginica = 3,
}
[DataFrameRow]
public class IrisData
{
[DataFrameColumn("Sepal.Length")]
public double SepalLength { get; set; }
[DataFrameColumn("Sepal.Width")]
public double SepalWidth { get; set; }
[DataFrameColumn("Petal.Length")]
public double PetalLength { get; set; }
[DataFrameColumn("Petal.Width")]
public double PetalWidth { get; set; }
[DataFrameColumn("Species")]
public IrisSpecies Species { get; set; }
var iris = engine.Evaluate("iris").AsDataFrame();
foreach (var row in iris.GetRows<IrisData>())
Console.WriteLine(row.Species);