R for datamining/backtesting/trading

Quote from ssrrkk:

The main reason I use Java is because the TWS API comes in Java or C++ only.
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.
 
Quote from caementarius:

Thanks, I appreciate you pointing that out. I have looked at it. The challenge is building something that you don't have to rewrite in order to deploy in an event-driven fashion (responding to new bars or ticks as they arrive).

But - I had a kind of "well, duh" idea.

I suppose if a strategy is not high frequency and operates say once per minute, you could just update the historical data with a new minute bar each minute, rerun your backtest on however much data you need to go back through, and see if any new signals were generated since the last test. This way you don't need to convert your backtest strategy into an event-driven strategy that is keeping track of states.

This could be practical for a medium-frequency bot or greybox. The bot just collects bars, updates bar data in R and adjusts backtest timeframe, runs the backtest and checks for signals/orders.

Anyone approach it like that?

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
 
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.
 
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
 
Quote from 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

What about quantmod?
 
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
 
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.
 
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
 
Quote from sf631:
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.

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.

Quote from sf631:
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

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.
 
Quote from 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.

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.
 
Back
Top