R for datamining/backtesting/trading

Discussion in 'App Development' started by caementarius, Mar 6, 2012.

  1. At its base, the IB API is just a socket message protocal. So you can use any language to implement it. Check out the PosixSocketClient source plus supporting files (EClientSocket and EClientSocketBase) under /Jts to see how it is done.
     
    #31     Oct 27, 2012
  2. sf631

    sf631

    6 months later, but I'm just re-reading this thread and realizing that I'm considering a very similar approach as I've started to get increasingly excited about using R for pattern exploration.

    The approach I'm currently pursuing (it's a work in process) is to develop an order execution layer in C# that receives its trading instructions from a SQL Server database table such that the database maintains a list of the target portfolio state (+1000 shares of ABC, -500 shares XYZ, etc...) and executes trades as needed to close the gap between the current portfolio state and the target state, placing and "working" orders as needed to reach the target state in an optimal way (trading off execution price and immediacy as appropriate). The target portfolio positions table can be populated/updated by another class in the C# code, or can be populated by R via RODBC or other database connector.

    As long as you're careful to record NBBO (midpoint or whatever) at the moment when the trading signal is updated, you can measure the performance of your signal generation (in R or any other point of origin) separately from your execution. In my situation, trading signals are low/medium frequency so any latency introduced by the R layer or database I/O really isn't material, but execution is where it's much more important to be high frequency and event driven. This by default eliminates the risk of model drift

    Curious whether the OP (or anyone else) has taken this approach for these
     
    #32     Oct 28, 2012
  3. sle

    sle

    Out of curiosity, what public R packages have you found to be useful for your work? I barely use anything beyond time series and definitely do not use anything financial, aside from the timeDate package.
     
    #33     Oct 29, 2012
  4. sf631

    sf631

    I'm just getting up to speed on R and thus far have just waded around, but here's my current list of interesting packages for financial applications.

    I'd be very interested in other suggestions. It seems that some packages are not in CRAN but on R-Forge and therefore require a few extra steps to build into the package library

    RODBC (CRAN) - good connector to ODBC databases
    XTS / ZOO (CRAN) - absolutely necessary for working with financial time series
    RQuantLib (CRAN) - haven't used yet, but a wrapper to some of the QuantLib functions, probably especially good for options pricing etc...
    RTAQ (CRAN) - haven't used yet, but really promising for working with tick/quote data in R
    PerformanceAnalytics (CRAN) - just starting to use. Seems helpful for calculating perfomance measures (sharpe etc...)
    stockPortfolio (CRAN) - haven't used at all, looks interesting
    IBrokers (CRAN) - connector to IB TWS APIs
    portfolioSim (CRAN) - haven't used, seems similar to stockPortfolio
    portfolio (CRAN) - required classes for portfolioSim

    Blotter / QuantStrat / FinancialInstrument (R-Forge) - haven't used yet but they seem promising to simulate systems

    IQFeed (GitHub - https://github.com/bwlewis/iqfeed) - haven't been able to build the package yet, but very promising as a connector to an IQFeed data source
     
    #34     Oct 29, 2012
  5. hft_boy

    hft_boy

    What about quantmod?
     
    #35     Nov 1, 2012
  6. sf631

    sf631

    Good point, I am looking at that as well, missed adding it to the list. Quantmod is a must-use, and I think a requirement for QuantStrat and several others
     
    #36     Nov 1, 2012
  7. Mr_You

    Mr_You

    So far I've been using R to analyze Ninja Traders Walk Forward Optimization (saved in csv format) because they don't provide much for analysis. Just getting into it, but enjoying it so far. As a programmer I prefer it over Excel.

    Regarding using R for testing trading models... I haven't found anything useful beyond day/monthly/yearly time frames. So far I think I'm going to stick to a trading platform for backtesting and live trading and use R for analysis and as a programmable interface to the trading platforms GUI interface.

    Any comments are appreciated.
     
    #37     Nov 1, 2012
  8. sf631

    sf631

    Before I started thinking about using R for a backtesting platform (daily signals, and I agree with Mr You on that point) I initially got interested in it for performance attribution analysis.

    My execution platform generates lots of logs about NBBO at the point of order decision (when the system decides to place an order) and for executions once the order has filled, dumped into a SQL database. I think R would be a natural environment for measuring slippage, market impact etc... and for understanding whether certain symbols are better than others regarding trade execution. I'm fairly certain that I have some leaks in my actual performance due to this sort of slippage.

    Other than RODBC, which is a very solid way to directly query a datasource and pull the data into an R data frame, I think this will all require custom functions and scripts
     
    #38     Nov 1, 2012
    Oysteryx likes this.
  9. Mr_You

    Mr_You

    This is exactly it. Using R you can really dig in and get the big picture of your trading system. RStudio is a very nice and flexible IDE.

    I agree I think custom R scripting combined with the ability to programmaticly interact with apps (even GUI desktop apps unfortunately using AutoIt) and get data are really valuable skills for this type of analysis.
     
    #39     Nov 1, 2012
  10. That is very reasonable.

    See also
    http://www.rinfinance.com/agenda/2012/talk/JeffRyan.pdf

    for discussion of ways to extend R to higher frequency data.
     
    #40     Nov 1, 2012