Search
× Search
Wednesday, February 5, 2025

Archived Discussions

Recent member discussions

The Algorithmic Traders' Association prides itself on providing a forum for the publication and dissemination of its members' white papers, research, reflections, works in progress, and other contributions. Please Note that archive searches and some of our members' publications are reserved for members only, so please log in or sign up to gain the most from our members' contributions.

Backtesting tool for loops

photo

 Saurabh Maloo, CFA, EM Rates Trader

 Friday, December 5, 2014

Hello, I am looking to optimize on the speed of my backtesting, i tried using R, but thats also taking a lot of time. Can anyone please suggest me an efficient platform to use for multi-level loops in my code? Many thanks Saurabh..


Print

18 comments on article "Backtesting tool for loops"

photo

 Rob Terpilowski, Software Architect

 Friday, December 5, 2014



I think the tool will depend on your use case. Are you testing a large portfolio of instruments? How much historical data are you using and how many sources are you having to pull the data from?


photo

 Salah Elmorry, Founding Partner at Systemathics

 Saturday, December 6, 2014



Hi Saurabh, please have a look at our website and let me know if interested.


http://systemathics.com/backtest.htm


Best regards,


Salah


photo

 Manuele Marini, Project Manager and Software Engineer at Wakett (Malta) Limited

 Saturday, December 6, 2014



Hello, which problem do you see by using R ?


photo

 Saurabh Maloo, CFA, EM Rates Trader

 Saturday, December 6, 2014



Hi Rob, I am trying to backtest my algo on some 80k data points and each point has some 4-5 if then else loops. I take 40 mins and 1 hr on VB and R respectively. If there any other tool or language i can use to speed up my calc.

Dear Salah,I am more keen to do this myself and play with the variables at this stage of development and want to outsource the work.

regards


photo

 Salah Elmorry, Founding Partner at Systemathics

 Sunday, December 7, 2014



Hi Saurabh, it is curious that a backtesting on 80K*5 data points takes about one hour.

Did you try to isolate each step of the backtesting and see who does what ?

My recommendation is to split the process into 3 different steps :

1- normalize market data

2- simulation step (signals and portfolio building)

3- post-trade analysis step (perf, optimization ...)

Also, maybe you have to multi-thread some pieces ...

Hope this is helpful.

Best regards,

Salah


photo

 Jared Broad, Founder at QuantConnect

 Sunday, December 7, 2014



We're supporting R.NET inside QuantConnect soon. You should check us out. It would be the best of both worlds - local and cloud backtesting...


photo

 Avinash P., Software Engg at Integral Development Corp

 Sunday, December 7, 2014



Try quantopian.com for backtesting. If you have ur own data for backtest use zipline package in python for backtest (Given complexity you mentioned with data points i m sure it would take at max few mins for backtesting)


photo

 Aatos Heikkinen, Hired gun, data scientist, comp. physics PhD. R&D consulting: BI, econophysics, R, Haskell, Clojure, JS, AI, innovation.

 Sunday, December 7, 2014



Have you inspected GPU computing in R as a way to speed up your backtesting? http://www.r-bloggers.com/?s=GPU


photo

 Colin "Soup" Campbell, Trader at IFundTraders

 Sunday, December 7, 2014



I know you can do better. Just for fun, I tested 400 days each for 400 equity's for a simple strategy. 12 sec. C++ for the program, SQL for the data storage/retreval.


photo

 Sam Birnbaum, Experienced Software Developer

 Monday, December 8, 2014



First, question that I would ask is : What kind of code executes within each "If Then" condition?.

The reason for that question is that a simple loop with 4-5 simple conditions (i.e. add 1 to a counter) should take less then 1 second on a modern PC. So the problem is not in the loop but rather in the code that is executed within the condition statements. Frequently, I have found when debugging such code that the programmer reloaded the data within each iteration within the loop. That is not to say what is happening here, but there maybe something else like that happening within those condition statements.


photo

 Saurabh Maloo, CFA, EM Rates Trader

 Tuesday, December 9, 2014



Right Sam, I am applying counter on each loop, i should rather use one counter for all the loops.. this would save me some time..


photo

 Wang Q., Technical Manager at Perfect World Co., Ltd.

 Tuesday, December 9, 2014



OP, if you show some core code of the if-then-self and loops(?) which you think is the most time consuming, we may help to see how to optimize your code. 80K data takes one hour to process is not normal, unless you are doing very time consuming job.

I choose C++ to develop my backtesting/trade system because I can gain the best performance from C++.


photo

 Saurabh Maloo, CFA, EM Rates Trader

 Tuesday, December 9, 2014



The only problem is time Manuele, perhaps my code is not very efficient. I will try to refine it.

Thanks Salah, I will try and split n process to check the problem with my code.

Good to know about the GPU computing Aatos, i will read up on the same. Thanks very much..

Thanks Avinash for suggesting Python.

Right Colin, C++ is indeed a faster option available, i will switch to that if R doesn't serve the purpose..


photo

 Ram Nagpure, Vice President

 Tuesday, December 9, 2014



Just of out curiosity wondering what is it that you folks are backtesting? Exposure or MTM? Have you folks tried python using large numpy arrays (assuming you have robust data store for storage & retrieval of large time series based data)? Thanks


photo

 Manuele Marini, Portfolio Manager at Temple Asset Management Ltd

 Wednesday, December 10, 2014



@ Saurabh : I use R on multiples cores and servers. If you can't split your task on multiple cores but you must use only one core try in C but it takes time to code it. R is faster to setup at an higher level of research.

I usually run R for hours of computational time on multiple tests. To value the test you need time, so it is not clear to me which is the problem for 45 min ? Do you have to run it multiple times during the same day ?


photo

 William K. Wong, Ph.D., Algorithmic Trader, HFT, FX, Global Futures

 Wednesday, December 10, 2014



I agree with some of the folks here, the amount of time necessary for 80K data points x 4-5 conditions taking 1h for R and much less in VBA just don't seem right.

I would profile the R code and determine where the bottleneck is first. Then use R packages like Parallel/Multicore/Snow to parallelize your code. Quite often you'll find significant trimming down in execution time to only a small fraction of your original time.

If you need to run your code in real time, then it's best to translate it to C/C++. If it's acceptable to run your code every few minutes, then spend the effort to vectorize your algo and parallelize them. You are not talking about a lot of data after all. I wouldn't be surprised if your operation can be trimmed down to less than 1 or 2 minutes. For me, trimming it down to a minimum is lot of fun.

Good luck and have fun while doing it.


photo

 David Parker, CEO - HDForex.ru

 Thursday, December 11, 2014



Optimal Jforex platform ( java )


photo

 Alexandru B., Financial Software Developer at Bloomberg LP

 Saturday, December 13, 2014



You shouldn't be using loops in R if you can get away with it. Looping is horrendously inefficient in R. R is much much better at vector operations then looping. So if you can turn your loop into a vector operation the speed gains will be enormous. Trade of will be that your're using a lot more memory.

Example:

Increment the values of a vector x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] by 1

Solution 1: Use an if then else loop (slow)

Solution 2: Create a vector y = [1,1,1,1,1,1,1,1,1,1]. x = x + y. Much faster. Obviously also more memory intensive.

Please login or register to post comments.

TRADING FUTURES AND OPTIONS INVOLVES SUBSTANTIAL RISK OF LOSS AND IS NOT SUITABLE FOR ALL INVESTORS
Terms Of UsePrivacy StatementCopyright 2018 Algorithmic Traders Association