Hello RMath plug-in users!
Beginning work with the RMath plug-in for AB, I've had success with simple
tests such as passing Close to the R side, adding a number to it, passing it
back to the AB side, and then showing the results on a chart on in an
Exploration.
However, adding just a little more complexity seems to introduce problems.
Below is a complete code that loads a couple of extra packages and then
attempts to generate a simple moving average (SMA) on the R side and pass it
back to the AB side.
After saving the session on the R side (code provided, just un-comment it and
set the path as desired), the session can be reloaded in R interactively and it
can be confirmed that the SMA was generated correctly, incl. a few NA values at
the front of the array while it ramps up.
However, something is not right when passing the SMA back to AB, as the result
on the AB side is an array of zeros. This is true even after an attempt to
substitute 0 for NA (on the R side), on the theory that NA does not pass pack
to AB cleanly (?).
So, what appears as if it should work, does not.
Is the code incorrect?
Does RMath have some limitation in this respect?
Is there an aspect of series handling on the R side I need to learn more about?
Are there perhaps version incompatibilities in my installation?
I'm appealing to other users of the RMath plugin here to see if anyone can shed
some light on this, or report that this code works or does not work on their
installation.
I was hoping to make available a short suite of demos using RMath and AB, but I
seem to have driven directly into a hole!
Public or private responses are welcome. So far, it seems there are very few
postings here related to RMath. The potential of the AB-R connection is huge,
IMO, but it's starting to seem that a bit of user-to-user co-support may be
important to have available. I'll certainly throw in on this myself if I can
get "over the hump".
At this point, it doesn't look like there's any danger of swamping the
message-base here with RMath discussion. But if that becomes an issue, we can
take it to the CFT Forum or elsewhere.
Thanks to anyone who can take a look and comment.
- Progster
/*
R_SMA_01.p1 by Progster
Attempt to calculate SMA in R by using package:TTR .
Problem is, the rtnMA array is all zeros on the AB side,
even though it is calculated correctly on the R side.
This test failed with:
AB 5.3
RAndFriendsSetup2092V3.0-17-2.exe: R 2.9.2, statconnDCOM 3.1-2B6
RMath 1.0.0 per Tools -> Plug-ins ...
Note: package:TTR must be installed separately
*/
EnableTextOutput( False ) ;
RMathDebugOn() ;
RMathProcedure( "library( TTR )" ) ; // package:TTR
(Technical Trading Rules)
RMathProcedure( "library( fSeries )" ) ; // package:fSeries
RMathDebugOff() ;
RMathSetArray(Close, "MyClose" ) ;
RMathProcedure( " MyClose <- MyClose + 1 " ) ; // to
prove changes can be done on R side, and returned
RMathProcedure( " MyMA <- SMA(MyClose, n=10) " ) ; // seems
to work on R side, based on examination of saved workspace
// RMathProcedure( " MyMA <- filter(MyClose, rep(1/5,5), sides=1) "
) ; // http://tolstoy.newcastle.edu.au/R/help/04/09/4302.html
// RMathProcedure( " MyMA <- removeNA( MyMA ) " ) ;
// With or without this call, rtnMA does not come out correct on the AB
side
RMathProcedure( " MA_Clean <- substituteNA(MyMA, type = <DQ>zeros<DQ>) " ) ;
// fSeries:substituteNA
// RMathProcedure( " " ) ;
rtnClose = RMathGetArray( "MyClose" ) ;
rtnMA = RMathGetArray( "MA_Clean" ) ;
// Finished on the R side - save image if desired.
// You can save the image here, then reload it in an R session
// to confirm the R side calculation of MA_Clean.
/*
RMathDebugOn() ;
// Set desired output directory
RMathProcedure( " setwd( <DQ>C:/Users/Steve/Documents<DQ> ) " ) ;
// Save the workspace to specified file in the cwd
RMathProcedure( " save.image( file=<DQ>R_SMA_01.p1.Rdata<DQ>) " ) ;
// works!
RMathDebugOff() ;
*/
Plot( rtnClose, "rtnClose", colorBlue ) ;
Plot( rtnMA, "rtnMA", colorRed ) ;
EnableTextOutput( True ) ;
printf( "Close: " + NumToStr(Close, 6.4 ) + "\n" ) ;
printf( "rtnMA: " + NumToStr(rtnMA, 6.4 ) + "\n" ) ;
EnableTextOutput( False ) ;
// Show all results in the Exploration
Filter = 1 ;
AddColumn( Close, "Close" ) ;
AddColumn( rtnClose, "rtnClose" ) ;
AddColumn( rtnMA, "rtnMA" ) ;