R for datamining/backtesting/trading

Discussion in 'Programming' started by caementarius, Mar 6, 2012.

  1. I've used a few platforms for strategy development. I always seem to come up against obstacles where I might as well write my own stuff rather than go through the gymnastics to get what I want out of the platform. For example, NinjaTrader felt weird to try to access more than 1-2 securities prices at a time (needed to wrap them into a derived indicator, if I remember correctly). With tradelink, not everything worked out of the box - so there was tweaking to be done plus data management was clunky.

    With R/quantmod and other modules, there is some nice open-source work mostly approached from an academic angle.

    The academic angle uses vector operations and can make for some nice succinct functional code. See this backtest in just a few lines of R script:
    http://blog.fosstrading.com/2011/03/how-to-backtest-strategy-in-r.html

    But, using vector operations isn't great when you want to switch to an event-driven model that responds to new bars or new ticks - like what happens when you want to run on live data and generate signals. And, more path-dependent trade logic expands the number of vectors needed until it's more messy than it's worth. The nice thing about NT and tradelink is that they are built from the ground up to run the same code whether you are backtesting or running live.

    Overarching all of this is my belief that I am likely better served developing systems by first exploring relationships in data and then seeing what might be a trade.

    So, I think R might be the way - but when I use it, iterating over bar data, it feels like I'm doing it wrong by introducing flow control into a functional language.

    Has anyone made R their "home" for everything (mining+testing+live signals)? Or - If you've been down the same road building your own tools for the whole process, what technologies did you settle on?
     
  2. "Overarching all of this is my belief that I am likely better served developing systems by first exploring relationships in data and then seeing what might be a trade.

    A pretty common thing among tradelinkers is to use R or matlab for analysis and modeling, then use tradelink for heavy backtesting and live trading.

    this is why tradelink has R/matlab/excel hooks in every application.

    "So, I think R might be the way - but when I use it, iterating over bar data, it feels like I'm doing it wrong by introducing flow control into a functional language."

    It shouldn't be necessary to iterate

    tlindicators<-read.csv("indicatorsFromGauntletRun.csv")

    price<-tlindicators[,3]
    time<-tlindicators[,1]

    plot(time,price)

    linearmodel <- lm(price~time)
    abline(linearmodel)
     
  3. This isn't really "a few lines of R," it's a script invoking the functionality of several very good finance-specific R add-on packages with their own quirks and fiddly bits, that are quite complex internally.
     
  4. correct:

    I like R for exploration, like how tradelink just posted. My point is that the trade logic and order matching are clunky and unnatural (that's where I'm iterating over bars, keeping orders in data frames, bleah).

    What I'm trying to find, or simply just lamenting about, is exploration using nice tools and ability to deploy a model with trading logic without rewriting it in tradelink (or NT, or..) for realtime use.
     
  5. Once you have found a relationship that is stable and exploitable, it's fairly easy to port this to tradelink.

    One option is R embed, and calling your routines via a dll in TL.

    Another option is calling R routines via a scripted interface. There's a plan to import a module we use to simplify this in TL (import it into the TL project).

    A third option if you really want to do actual trading in R, sent you a PM about that.
     
  6. Craig66

    Craig66

    I've been down this road, event driven back testing in R is a PITA. Simple toy strats can be vectorised, but anything beyond that becomes way too complex (and slow). R is good at slicing and dicing data, but I wouldn't be using it as a live trading environment. I ended up having a back-end in C++, a front end in C# and a bunch of R scripts which I use to visualize stuff. Writing custom back testing/live trading environment is a major, but once your done you'll be able to move quickly.
     
  7. Mr_You

    Mr_You

    Every trader I've asked who uses R doesn't use it for live trading.

    Have you looked at AlgoTrader?
     
  8. I haven't. Man, I thought I had searched everything out. I will check it out, looks interesting.
     
    #10     Mar 6, 2012