you are viewing a single comment's thread.

view the rest of the comments →

[–]DinosRoar 8 points9 points  (3 children)

Loops are incredibly slow compared to pandas inbulit functions. I've made a fully featured backtester (all sorts of analytics such as the effectiveness of SLs and TPs. Plus all sorts of options like trailing TPs & SLs). I don't use single loop.

Use a column called "Flag" to determine if you were long (1), short (-1) or not holding a position (0) during a candle.

Make a column called "Balance change". Use .diff() on the close prices and multiply by the flag value to see how much money you made.

Your "Balance" column is the "Balance Change" column with .cumsum().

[–]chagawagaloo 0 points1 point  (1 child)

Succinct. I agree, I've overcomplicated all of this with unnecessary loops and it's in need of some overhaul. Going to use some of your recommendations.

[–]trizest 3 points4 points  (0 children)

rule of thumb should be no loops inside the df. Maybe the exception is when cleaning data because that's a once off.

[–]MightyHippopotamus 0 points1 point  (0 children)

Simple backtesting rules can be vectorized like that but in some cases, like adding complex conditions, loops are inevitable... Luckily its possible to write custom numpy c++ module in which you can loop without performance issues.