[Rprotobuf-commits] r337 - pkg/inst/examples/HighFrequencyFinance

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Sat Jul 17 22:20:36 CEST 2010


Author: edd
Date: 2010-07-17 22:20:36 +0200 (Sat, 17 Jul 2010)
New Revision: 337

Modified:
   pkg/inst/examples/HighFrequencyFinance/loadInR.r
   pkg/inst/examples/HighFrequencyFinance/protoModule.cpp
Log:
some improvements but still no working Module


Modified: pkg/inst/examples/HighFrequencyFinance/loadInR.r
===================================================================
--- pkg/inst/examples/HighFrequencyFinance/loadInR.r	2010-07-17 19:14:36 UTC (rev 336)
+++ pkg/inst/examples/HighFrequencyFinance/loadInR.r	2010-07-17 20:20:36 UTC (rev 337)
@@ -67,8 +67,6 @@
 }
 
 compiled <- function(verbose=FALSE, file="trades.pb") {
-    suppressMessages(library(utils))
-    suppressMessages(library(Rcpp))
 
     stopifnot(file.exists(file))
 
@@ -79,31 +77,38 @@
     invisible(df)
 }
 
-moduled <- function(file="trades.pb") {
-    suppressMessages(library(utils))
-    suppressMessages(library(Rcpp))
+moduled <- function(verbose=FALSE, file="trades.pb", dll) {
 
     stopifnot(file.exists(file))
+
     dll <- dyn.load("protoModule.so")
+    trds <- Module("trades", dll)
+    td <- new( trds$Trades )
+    td$init(file)
+    #n <- td$numberOfFills()
+    df <- NULL #td$getData()
 
-    yada <- Module("yada", dll)
-    yada$init("trades.pb")
-    print( yada$numberOfFills() )
-    invisible(NULL)
+    if (verbose) print(summary(df))
+
+    invisible(df)
 }
 
 suppressMessages(library(stats))
 suppressMessages(library(RProtoBuf))
+suppressMessages(library(utils))
+suppressMessages(library(Rcpp))
 suppressMessages(library(rbenchmark))
 
-moduled(); q()
-
 dyn.load("protoLoadForR.so")
 
-print(benchmark(basicUse  = basicUse(FALSE),
+#print(summary(moduled())); q()
+
+
+print(benchmark(compiled  = compiled(FALSE),
+                #moduled   = moduled(FALSE),
+                basicUse  = basicUse(FALSE),
                 betterUs  = betterUse(FALSE),
                 preAlloc  = preAlloc(FALSE),
-                compiled  = compiled(FALSE),
                 order = "elapsed",
                 columns = c("test", "replications", "elapsed", "relative", "user.self", "sys.self"),
                 replications  = 3))

Modified: pkg/inst/examples/HighFrequencyFinance/protoModule.cpp
===================================================================
--- pkg/inst/examples/HighFrequencyFinance/protoModule.cpp	2010-07-17 19:14:36 UTC (rev 336)
+++ pkg/inst/examples/HighFrequencyFinance/protoModule.cpp	2010-07-17 20:20:36 UTC (rev 337)
@@ -5,7 +5,7 @@
 #include <Rcpp.h>
 #include <fstream>
 
-class TradeModule : public TradeData::Trades 
+class TradeModule 
 {
 private:
     TradeData::Trades tr;
@@ -23,14 +23,37 @@
 	int numberOfFills(void) {
 		return tr.fill_size();
 	}
+
+	Rcpp::DataFrame getData(void) {
+		int n = tr.fill_size();
+		Rcpp::DatetimeVector timestamp(n);
+		Rcpp::CharacterVector tsym(n);
+		Rcpp::NumericVector tprice(n);
+		Rcpp::IntegerVector tsize(n);
+
+		for (int i=0; i<n; i++) {
+			const TradeData::Fill &fill = tr.fill(i);
+			timestamp[i] = fill.timestamp();
+			tsym[i]      = fill.symbol();
+			tprice[i]    = fill.price();
+			tsize[i]     = fill.size();
+		}
+
+ 		return Rcpp::DataFrame::create(Rcpp::Named("times")  = timestamp,
+									   Rcpp::Named("symbol") = tsym,
+									   Rcpp::Named("price")  = tprice,
+									   Rcpp::Named("size")   = tsize);
+	}
+
 };
 
-RCPP_MODULE(yada){
+RCPP_MODULE(trades){
 	using namespace Rcpp ;
 	                  
 	class_<TradeModule>( "Trades" )
 		.method( "init",          &TradeModule::init )
 		.method( "numberOfFills", &TradeModule::numberOfFills )
+		.method( "getData",       &TradeModule::getData )
 		;
 	
 }                     



More information about the Rprotobuf-commits mailing list