pmorissette / bt

bt - flexible backtesting for Python
http://pmorissette.github.io/bt
MIT License
2.13k stars 411 forks source link

General questions #7

Closed Fractally closed 9 years ago

Fractally commented 9 years ago

I have a few general questions regarding bt that I would like to ask. I hope it is acceptable to ask questions like this here.

1) For what type of investment style was this project created? It seems to me that bt is focussed on longer-term portfolios with concentration on rebalancing. Would bt also work well for short-term investments? I am not referring to HFT, but daily trading or perhaps hourly trading.

2) Is it at all possible to use bt for live trading through extending the code? I assume it is not since it seems that bt is not event-driven. Could you provide some clarity?

3) The roadmap does not provide any specific features that could be implemented, but is a little vague. What are the current shortcomings of this library? What would be valuable features to add other than providing more algos?

I have scanned through the code and would just like to complement you on your neat and thorough coding style. Your use of comments is better than most of the code I see these days...

pmorissette commented 9 years ago

Hey @Fractally

Thanks for the kind words.

As you mentioned, this library was indeed created around longer term portfolio-type strategies (daily rebalancing and longer). However, it should work fine for shorter term strategies as well. I have yet to test the code at the intraday level though, so there might be some extra work needed to get that working correctly.

Although this library was not designed for live trading, it could be adapted to it easily enough and in the worst-case, parts of the code could be reused. Although it might not appear to be event-driven, it kind of is. The only problem is that I have spent some time optimizing the code so it is not as straightforward as something like on_bar_update(new_data). You basically would have to update the strategy tree yourself (when you receive new data, for example) and then run the strategy logic. If you look at the Backtest.run method, you will see how it works.

As for the shortcomings, speed is always an issue when running backtests with Python. I have tried to speed up the code while maintaining the flexibility of the framework (tree structure & algo stacks) but I am sure there still are improvements to make. For my purposes it performs quite well, but if you backtested intraday data for a large number of stocks, it would definitely be quite slow.

More algos are always needed as I am sure everyone has different ideas and requirements. For example, stop-loss algos have yet to be added and I'm sure they would be quite useful for others using the library - I just haven't had the need for them (nor have I had the time to code them up) but I will gladly accept pull requests!

Hope this helps and let me know if you have any other questions.

Cheers, P

Fractally commented 9 years ago

Hi @pmorissette

Thanks for your reply. In terms of speed considerations, does the compilation of the core from python to c code make a significant difference? I'm just curious if you have done any comparisons. I'll look a little into what improvements could be made to speed up the backtests. The current performance is not bad as far as I've seen.

pmorissette commented 9 years ago

Hey @Fractally,

During my testing, the compilation to C indeed made a big difference even though all I did was add cython type hinting! I also made other types of optimization along the way (such as passing in the whole DataFrame from the start as opposed to having a more traditional on_bars_updated event that would have to append data to a internal DataFrame).

Anyways, I'd be nice to have someone else take a look at it.

Thanks for your interest, P

fja05680 commented 9 years ago

Hi @pmorissette

I agree with @Fractally, this is some of the cleaner and well commented code I've seen in a while. For the long term investor, it appears to be a very nice tool for backtesting. For short term back testing, I would need to access all of the time series (open, low, high, and close) for a security, and be able to set stops and limits. The default behavior appears to deal with only Adj Close. I know some python but have limited free time (like everyone else). That said, what type of effort would these changes require?

thanks, Farrell

pmorissette commented 9 years ago

Hey @fja05680,

Thanks for the kind words :-)

When you say you would need access to open/high/low/close, does this mean you would potentially trade at a price different than the close? If so, then yes there would indeed be some work to do at the core framework level because as it stands, you cannot specify a trade price - the framework automatically trades at the close price. If not, then you could just download this data and save it in Strategy.perm. This way, your algos could access the data to make their investment decisions.

As for the stop loss / limit orders, this shouldn't be difficult. Basically, it would just be like any other Algo the only difference being that you would have to make sure it runs on each loop (best way to achieve that would be to set the attribute run_always=True on your Algo). A limit order would be similar in this regard.

Basically, the main hurdle here is if you want to trade at a price other than the close. Also, keep in mind that I have not used this for intra-day backtesting so there might be some bugs I haven't stumbled across yet!

Hope this helps, Phil

fja05680 commented 9 years ago

Hi @pmorissette,

Thanks for your detailed quick reply. Yes, I would like to trade at prices other than the close price. There are all sorts of trading techniques, for example gap up/down, that trade on limits that may fire during the trading day. Additionally, stop loss and and limit orders also need to be able to execute intraday to minimize slippage. Slippage isn't very important for long term trading but for short term trading could cause any profits to evaporate. Unfortunately, I've tried every python library out there and haven't found any non-event based libraries that accomplish this. I prefer to not use event based frameworks because they require minute or tick data for intraday trading. It sounds possible to adapt your library. For the time being though, I'll write each algorithm from scratch with a simple loop for the trades. I would love to contribute to your project, but paramount for me right now is testing my algorithms. I'll keep a watch for updates to your awesome libraries.

thanks, Farrell

1vory commented 1 month ago

Hi everyone,

just stumbled on bt and found it to be much more elegant than e.g. backtrader!

As I also focus on intra and swing trading, I was wondering whether someone could share some experiences using bt for that?

Any experience / opinions are appreciated

Thanks Kai

fja05680 commented 1 month ago

Kai,

Can't believe someone posted on this old thread. Shortly after posting this, I started my own open source project for back testing called pinkfish. I just couldn't find any others that did what I wanted. Anyways, that's obviously what I use and it meets my needs. There are a lot of choices. Best wishes, Farrell

1vory commented 1 month ago

Hey Farrell, hehe, nearly a decade. Thanks for the reply and the repo. Will have a look!! :)

fja05680 commented 1 month ago

Kai,

Yea, I wrote the initial version over Christmas break many years ago. It would do simple trading with one stock. It's not hard to do that in python. But then adding portfolio trading, shorting, leverage, various indicators, and more advanced stats too a lot of time. Now I cringe when I hear someone say they are going to write their own backtester.

Farrell