Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Profile and improve the execution speed of the Drawdown Calculation #18

Open
mhallsmoore opened this issue Jun 4, 2015 · 1 comment
Assignees

Comments

@mhallsmoore
Copy link
Owner

The drawdown curve produced subsequent to the backtest takes a long time to calculate. Profile it and improve the speed (possibly by removing a direct for-loop).

@mhallsmoore mhallsmoore self-assigned this Jun 4, 2015
@femtotrader
Copy link

femtotrader commented Jun 21, 2016

Displaying number of ticks processed per second is something quite easy to add and a very informative metrics for profiling

in backtest.py you can add

class Backtest(object):
    ...
    def _run_backtest(self):
        ...
        self.ticks = 0
        while self.iters < self.max_iters and self.ticker.continue_backtest:
            ...
                    if event.type == 'TICK':
                        self.strategy.calculate_signals(event)
                        self.portfolio.update_portfolio(event)
                        self.ticks += 1

    def _output_performance(self):
        """
        Outputs the strategy performance from the backtest.
        """
        print("Calculating Performance Metrics...")
        print(self.s_speed())
        self.portfolio.output_results()

    @property
    def speed(self):
        return self.ticks / (time.time() - self.t0)

    def s_speed(self):
        return "%d ticks processed @ %f ticks/s" % (self.ticks, self.speed)

To my understanding of your code, a lot of time is lost by loading CSV file into memory using Pandas read_csv.
With my Macbook Air mi-2011 with random price tick for 1 day (2 symbols EURUSD and GBPUSD) and mac.py example I'm getting less than 2000 ticks per seconds processed.

You might make your price handler class an iterable class http://stackoverflow.com/questions/19151/how-to-make-class-iterable and use priority queue for tick events. With such improvements you can expect more than 12000 ticks per seconds (so a x6 factor).
With Julia and a "do nothing except print" strategy I'm processing more than 35000 ticks per second.

You might also be aware that printing all ticks reduce substantially tick processing speed. You might use modulo to only print ticks every n ticks (with a n_window window length)

        if i % n == 0 and i != 0:
            print(s_speed(i, t0))
            print("")
        if i % n in range(n_window):
            print("%d %s %s" % (i, dt, data))

I typically use n = 100000 and n_window = 5 for a 2 symbols example strategy

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants