Hi Progster,
Appreciate your initiative, so let me give you some help to get you going
(further).
1) Remember that R has multiple ways of handling data, including multiple
arrays in matrix format. This is what the SMA function actually did. Using the
RMathDebugOn(), you can see that the MyMA-series shows a format like this:
[1230,] 99.0
[1231,] 100.0
etc.
It means that it is a matrix. See my code below on how to call the 1st column
of it (i.e. your array with data).
2) Remember that, unless you use R's timeseries functions, R will not recognize
AB's "bars to the right" settings. It means that in charts you'll see a
drop-off to zero for, say, the last 10 bars. This is why I use AB's Ref
function below.
3) Please have a look at the package Zoo, as well as related timeseries
packages. It allows you much more flexibility to link R's series to dates, etc.
On that note, I often use AB's Status("firstvisiblebar"), etc to manipulate R's
series by filling its matrix notation (i.e. [...,...]) with these "barnrs". You
need to play around with it, but you'll get the idea.
Here is the adjusted code which will correct the errors:
RMathSetArray(Close, "MyClose" ) ;
RMathProcedure( " MyClose <- MyClose + 1 " ) ; // to prove changes can be done
on R side, AND returned
RMathProcedure( " MyMA <- SMA(MyClose) " ) ; // seems to work on R side, based
on examination of saved workspace
RMathProcedure( " MA_Clean <- substituteNA(MyMA, type = <DQ>zeros<DQ>) " ) ;
rtnClose = RMathGetArray( "MyClose" ) ;
RMathDebugOn() ;
FVB = Status("firstvisiblebar");
LVB = Status("lastvisiblebar");
rtnMA = RMathGetArray( "MyMA" ) ;
rtnClean = RMathGetArray( "MA_Clean[,1]" ) ;
Plot( rtnClose, "rtnClose", colorBlue ) ;
Plot( Ref(rtnMA,-10), "rtnMA", colorRed ) ;
Plot(Ref(rtnClean,-10), "rtnClean", colorGreen ) ;
Finally, you are right: the link with R opens a new universe. Still, I don't
have time to support the plugin to a larger extent. Again, your initiative is
much appreciated in that regard. But at the end of the day, people have to be
willing to spend time learning it, and thus R. It took me a few weeks, but now
I can anything (except the coffee thing ;-)).
best,
PS
--- In [email protected], "progster01" <progs...@...> wrote:
>
> 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" ) ;
>