(This article was first published on

In my explorations with R, Mathematica, FreeMat, MatLab, and RapidMiner (now with R support! Yay!), I’m seeing integration of R to be quite useful in building a trading app, as technical analysis is one of R’s fortés. For the sake of brevity, I’m including comments in the code instead of using paragraphs…**Naught Not Knot**, and kindly contributed to R-bloggers)*use the source, Luke.*

Note that I'm

*not*using the R(D)COM package, but the RdotNET package found here. It's open source (thanks for the correction, Carlitos)

The Source, Luke:

1: using System;

2: using System.Linq;

3: using RDotNet;

` 4: `

5: namespace R.NET_Wrapper

` 6: {`

7: class Program

` 8: {`

9: public static void Main(string[] args)

` 10: {`

11: //Console.WriteLine("Hello World!");

12: // Point the R Engine to the dll dir

` 13: RDotNet`

` 14: .REngine`

` 15: .SetDllDirectory(`

16: @"C:\Program Files\R\R-2.12.1\bin\i386"

` 17: );`

` 18: `

19: // make an instance, go ahead, do it

20: using

` 21: (REngine engine =`

` 22: REngine`

23: .CreateInstance("RInstance")

` 24: )`

` 25: {`

26: // Let's see what it'll do.

27: // Let's create a numeric vector with a double[]

28: // .NET framework array to vector

` 29: NumericVector group1 =`

` 30: engine.CreateNumericVector(`

31: new double[] {

` 32: 30.02,`

` 33: 29.99,`

` 34: 30.11,`

` 35: 29.97,`

` 36: 30.01,`

` 37: 29.99`

` 38: });`

` 39: engine`

40: .SetSymbol("group1", group1); // Dont forget this!

` 41: `

42: // Here's the sssllooww way

` 43: NumericVector group2 =`

` 44: engine`

` 45: .EagerEvaluate(`

46: "group2 <- c(29.89, 29.93, 29.72, 29.98, 30.02, 29.98)")

` 47: .AsNumeric();`

48: // EagerEvaluate will also accept IO.Stream (R scripts, anyone?)

` 49: `

50: // Test difference of mean (student's t-test) and get P-value

` 51: GenericVector testResult =`

` 52: engine`

53: .EagerEvaluate("t.test(group1, group2)")

` 54: .AsList();`

55: double p =

56: testResult["p.value"]

` 57: .AsNumeric()`

` 58: .First();`

` 59: `

` 60: Console.WriteLine(`

61: "Group 1 [{0}]",

62: string.Join(

63: ", ",

` 64: group1.Select(i => i.ToString()))`

` 65: );`

` 66: Console.WriteLine(`

67: "Group 2 [{0}]",

68: string.Join(

69: ", ",

` 70: group2.Select(i => i.ToString())`

` 71: )`

` 72: );`

73: Console.WriteLine("P-value = {0:0.000}", p);

` 74: }`

` 75: `

` 76: `

77: //+ TODO: finish getting data into managed space

` 78: `

79: Console.Write("Press any key to continue . . . ");

80: Console.ReadKey(true);

` 81: }`

` 82: }`

` 83: }`

To

**leave a comment**for the author, please follow the link and comment on his blog:**Naught Not Knot**.R-bloggers.com offers

**daily e-mail updates**about R news and tutorials on topics such as: visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...