In this third and final part of the series (Part 1, Part 2), I am going to continue shaping the examples in the previous posts by quickly building a small application - a simple Trading Desk application. In the example I will use the same C# and R interface method together with a specific Quantitative Financial Modelling R library called Quantmod.
Example Objective
In the example the objective is to build a C# Web Form which acts as the main application controller that captures various user option parameters with the main functions being:
1. To automatically kick off an R routine to download data from Yahoo Finance.
2. To select specific dates of interest
3. To add or udpate different charts
4. To add different chart indicators
5. To calculate Period Return Statistics
5. To calculate Period Return Statistics
Most of these functions are provided through the set of R functions exposed by the Quantmod R library.
Additions under the hood
Below I am showing some of the salient additions done to the code over and above the code in the previous posts. Since the underlying program structure is in line with the explanations in the previous posts I am not going to delve again into detailed explanations. The code should be pretty self explanatory.
//My RFacade Class - note the new addition to load Quantmod Library
public RFacade()
{
rconn = new STATCONNECTORSRVLib.StatConnector();
rconn.Init("R");
//load R Quantmod Library
rconn.EvaluateNoReturn("library(quantmod)");
}
//Get data from yahoo finance
public void RGetData(String symbol)
{
String RCommand = "getSymbols('"+symbol+"',src='yahoo')";
rconn.EvaluateNoReturn(RCommand);
}
//Draw Candle Chart for specific company symbol and date range
public void RCandleChart(String symbol, DateTime from, DateTime to)
{
String dateRange = FormatDateRange(from, to);
String RCommand = "candleChart(" + symbol +dateRange+",multi.col=TRUE,theme='white')";
rconn.EvaluateNoReturn(RCommand);
}
//Draw Bar Chart for specific company symbol and date range
public void RBarChart(String symbol, DateTime from, DateTime to)
{
String dateRange = FormatDateRange(from, to);
String RCommand = "barChart(" + symbol + dateRange + ")";
rconn.EvaluateNoReturn(RCommand);
}
//Draw ChartSeries plot for specific company symbol and date range
public void RTimeSeries(String symbol, DateTime from, DateTime to)
{
String dateRange = FormatDateRange(from, to);
String RCommand = "chartSeries(" + symbol + dateRange + ")";
rconn.EvaluateNoReturn(RCommand);
}
//Create new R grahics window (/device)
public void RNewChart()
{
String RCommand = "windows()";
rconn.EvaluateNoReturn(RCommand);
}
//Add Indicators
public void addIndicatorSMA()
{
String RCommand = "addSMA(20)";
rconn.EvaluateNoReturn(RCommand);
}
public void addIndicatorDPO()
{
String RCommand = "addDPO()";
rconn.EvaluateNoReturn(RCommand);
}
public void addIndicatorBBands()
{
String RCommand = "addBBands()";
rconn.EvaluateNoReturn(RCommand);
}
//Get Mean Return
public double RGetMeanReturn(String symbol, DateTime from, DateTime to)
{
String dateRange = FormatDateRange(from, to);
String RCommand = "logRet<-diff(log(" + symbol + "))";
rconn.EvaluateNoReturn(RCommand);
RCommand = "mean(logRet" + dateRange + "[,4])";
return (double)rconn.Evaluate(RCommand);
}
How it Looks
As you can see from the screen shot below, with this approach one can quickly start to mash up interesting applications by benefeting from the rich R platform and the flexible C# front end.
Hope you found this series of articles on C# and R interfacing using R(D)COM interesting ... and please anyone who wishes to continue discussing or collaborate or share experiences just drop me an email or post a comment.
Click to Enlarge |
Comments
What is the way to pass a table of time-series data of multiple stocks to R ? How do you pass a matrix ?
Thanks for your post.
Very nice blog and extremely useful! I am wondering if you have suggestions on how to capture the contents of a table using the GetSymbol function. For example, I have an R variable called 'locations' that has 150 entries and each row has 4 values (x,y,date,time). I'm trying to do something like:
object locs = RConn.GetSymbol("locations");
But locs only comes back with the first row. I've tried casting to a multidimensional object array:
object[,] locs = (object [,])RConn.GetSymbol("locations");
but that throws an error. Any ideas on how-to cast an R table into an equivalent C# object?
Thanks,
Jake
Do you by any chance know how can I populate a ms Access query or a listbox with data from R using StatConnector?
I am able to connect and send data
to R but I can't find the way to display it. This is what I am trying to do:
Dim myApp As New StatConnector
'initialize R server and transfer df array to R variable
myApp.Init ("R")
myApp.SetSymbol "df", myAccessTable
a = myApp.GetSymbol("df")
'Pseudo code below
listbox.rowsource=a
or
myquery.datasource=a
Thanks
Many thanks to your blog& work!!
I use R in ASP.NET and it works apart from the code below.
s <- read.table("testfile.csv", header=TRUE, sep=",")
reg <- function(d){
reg.u <- unique(d$o)
reg.df <- data.frame(id=0,return=0)
for(i in 1:length(reg.u)){
reg.sub <- subset(d, o==reg.u[i])
x <- reg.sub$x
y <- reg.sub$y
z <- reg.sub$z
}
Maybe you know why subset is not working, I always get a: "Linked object's source class has changed (Exception from HRESULT: 0x80040008 (OLE_E_CLASSDIFF))"
Many thanks in advance Vincent
may I ask, where is the FormatDateRange function from?