[R-SIG-Finance] Backtesting trade systems

Robert Sams robert at sanctumfi.com
Thu Jul 16 16:41:25 CEST 2009


Hi Mark,
 
I have started a package called tradesys which, I think, is a clean
solution. The project is registered on r-forge
https://r-forge.r-project.org/projects/tradesys/ and the initial code
base with documentation will be checked-in by the end of the day London
time. As a taster:
 
> library(tradesys)
> data(spx)
> tail(spx)
             Open   High    Low  Close     Volume
2009-05-12 910.52 915.57 896.46 908.35 6871750400
2009-05-13 905.40 905.40 882.80 883.92 7091820000
2009-05-14 884.24 898.36 882.52 893.07 6134870000
2009-05-15 892.76 896.97 878.94 882.88 5439720000
2009-05-18 886.07 910.00 886.07 909.71 5702150000
2009-05-19 909.67 916.39 905.22 908.13 6616270000
> x <- tradesys(spx, el=MA(Close, 60) > MA(Close, 120), es=MA(Close, 60)
<= MA(Close, 120))
> tail(trades(x, uselog=TRUE))
    phase      etime      xtime time nobs  eprice  xprice    pnl
ror
107    EL 2006-09-21 2007-09-12  356  244 1324.89 1471.10 146.21
0.264117052
108    ES 2007-09-12 2007-11-09   58   42 1471.10 1467.59   3.51
0.006027153
109    EL 2007-11-09 2008-01-03   55   36 1467.59 1447.55 -20.04
-0.034689959
110    ES 2008-01-03 2008-06-10  159  109 1447.55 1358.98  88.57
0.159301490
111    EL 2008-06-10 2008-07-21   41   28 1358.98 1261.82 -97.16
-0.187159273
112    ES 2008-07-21 2009-05-19  302  209 1261.82  909.67 352.15
0.825619186
> tail(equity(x, uselog=TRUE))
           trade states    delta    price          ror   equity
2009-05-12   112     -1 1.331220 6.814016  0.018107970 38.02601
2009-05-13   112     -1 1.307543 6.808377  0.007373275 38.30638
2009-05-14   112     -1 1.297973 6.784729  0.030694874 39.48219
2009-05-15   112     -1 1.259318 6.794318 -0.012075942 39.00541
2009-05-18   112     -1 1.274712 6.786796  0.009588169 39.37940
2009-05-19   112     -1 1.262606 6.813082 -0.033188777 38.07244
 
Please checkout and play with the code at your leisure. Anyone
interested in write-access to the repository should contact me directly.


Robert 


________________________________

	From: r-sig-finance-bounces at stat.math.ethz.ch
[mailto:r-sig-finance-bounces at stat.math.ethz.ch] On Behalf Of Mark
Breman
	Sent: 16 July 2009 15:15
	To: r-sig-finance at stat.math.ethz.ch
	Subject: [R-SIG-Finance] Backtesting trade systems
	
	
	Hello, 

	I have spend quit some time now looking for a package that
allows me to backtest (technical) trading systems based on single
financial instruments with R.

	I had a look at Rmetrics, blotter, fTrading,
PerformanceAnalytics, backtest, quantmod, TTR etc, but not one of these
fill my requirements. It's not that they are not usefull, on the
contrary, they are all filled with terrific statistical stuff, but it's
not the simple, practical and straightforward approach that I am looking
for as a trader rather than as a statisticus.

	So I have decided to build my own solution, reusing as much as
possible from these existing packages. (As a former software engineer I
know how much time and effort goes into buiding reliable software, so
the more reuse the better). As I am quite new to R and statistics in
general, there is a lot to learn for me here...
	
	
	What I have build so far is a very basic set of functions called
"tradesim.R" (I have attached it to this post). A very basic example of
how these functions can be used for a backtest-run can be found in
"tradesim_example.R". The example runs a backtest with end-of-day data
from AAPL, using a (rather poor) trading system based on the RSI
indicator (from the TTR package). 

	Now I have read in some older post on this list that others were
also searching for a backtesting package. I even read a post proposing
to start a group effort creating such a package. I suspect that some of
you might be interested in what I made so far and maybe would like to
put in a effort creating such a package together. I certainly know that
it's a lot easier to create good software as a group, rather than by a
single person...

	So if you are interested have a look at what I got so far and
let me know what you think.

	Regards,

	-Mark- 

	   



More information about the R-SIG-Finance mailing list