Managing Market Studies in R

[This article was first published on Milk Trader, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

I’m currently working on seasonal studies for various markets and have decided it’s high time I got an organized workflow established. How does sugar behave in August every year? Is it random or are there some fundamental drivers that coerce its behavior? How about gold, silver and the gold/silver ratio? Or crude, heating oil and the crack spread? Or soybeans, soybean oil, soybean meal and the crush spread? You get the idea. One’s imagination can generate a crazy amount of combinations.

I’m doing my studies mostly in R, and with the quantmod package. The quantmod package is cool because if you load it first, you get zoo, xts and TTR with it. It’s a dependencies thing. This is important because I have decided the best way to treat time series data is to first convert it to an xts object. Once you’ve gotten past this hurdle, you can index time frames and do other data manipulations on your way to delusional curve-fitting adventures.

If you are using R with Windows, stop it. Get Ubuntu on your machine this weekend. You can install the VM Player, load Ubuntu on it and then install R and its packages (this will cost you zero dinero, by the way). Then you must familiarize yourself with svn (also gratis), since that is the version control software R package developers use. It’s best to learn how to use it from command line and that is what I’m doing here below.

R has this built in memory file that comes in handy for our purposes.  I think it’s called .RData (you see, you don’t need to know absolutely everything to get started). Where you store this file will make all the difference in organizing your studies. This is a trivial process, and I’ll get to it shortly.

Once you get your svn layout organized in your mind, you can create a special one for a particular study. I called my SeasonalStudy (pascal cased for the repo). My checkout folder is called desk.seasonalStudy (camel-cased with my unique ‘desk’ prepend to signify to me it’s a checkout folder). Once you get the folder setup with the standard trunk, branches, tags layout, you’re ready to get analyzing.

You need to get to the point where your command line looks something like this:

    milktrader@ubuntu:~/Dropbox/desk.seasonalStudy$ 

From here, you should have the trunk, branches, tags directories. Let’s switch to the trunk.

     milktrader@ubuntu:~/Dropbox/desk.seasonalStudy$ cd trunk
     milktrader@ubuntu:~/Dropbox/desk.seasonalStudy/trunk$

In here, I’ve placed some template type of R scripts. One to efficiently convert csv files to xts objects called makeXts.r and another to index months called getSection.r. These files are general, but we’re going to copy them into a branch and then make them market-specific. The svn branch theory is that it copies the trunk, so general is good. I created a sugar branch by doing the following (dropping milktrader@ubuntu to save space):

    :~/Dropbox/desk.seasonalStudy$ svn copy trunk branches/sugar
    :~/Dropbox/desk.seasonalStudy$ svn commit -m “sugar study”

There is a shortcut method to this, but I couldn’t get it to work. In any case, I now have an svn branch called sugar, which contains the template R scripts I uploaded into the trunk. I navigate to this directory and I get the following:

    :~/Dropbox/desk.seasonalStudy/branches/sugar$

On this prompt, I simply type R and the R programs opens up. After sourcing an R script that extracts sugar data, I now have objects in my R session with which to work. This is cool. When I exit the R session with q(), I get a prompt asking if I’d like to save the workspace image. You can guess my answer. I’ve gone to some length to make this a unique and organized study, so I press “y” (which stands for yes). A unique copy of  .RData has been saved on your behalf. Easy peasy, like I said.


You can follow the same procedure for other studies and every time you open R from a specific directory, it remembers the objects and you don’t get all mixed up with too many objects.


Alright, that was the easy part. Now the hard part. Making sense of all the seasonal data. Or more to the point: making money by making sense of all the seasonal data.





To leave a comment for the author, please follow the link and comment on their blog: Milk Trader.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)