kernc / backtesting.py

:mag_right: :chart_with_upwards_trend: :snake: :moneybag: Backtest trading strategies in Python.
https://kernc.github.io/backtesting.py/
GNU Affero General Public License v3.0
5.41k stars 1.06k forks source link

Is there a way to backtest more instruments at once #20

Open ghost opened 5 years ago

ghost commented 5 years ago

Im trying to backtest a pairs trading strategy but i don't know how to add multiple data frames to the backtester in order to add positions to each instruments according to the hedge ratio.

Is there a way to do this?

chhudson commented 5 years ago

@jetychill Definitely!

You just need to setup a for statement to pull from a list of instruments IE. `instruments = ['USD_JPY', 'EUR_USD']

for instrument in instruments:
    data = run api calls to get data or load csv's
    bt_long = Backtest(`data`, `your strategy`, margin=0.002, cash=500)

    stats_long = bt_long.optimize(
        whatever optimization fields,
        maximize='Equity Final [$]'
    )

Obviously there is likely a bunch of data cleanup but that is something left to the user.

Hope this helps!

ghost commented 5 years ago

Thanks for the help! Another question, does the back tester has some sort of default maximum holding time where it closes the position after n amount of time has passed I'm comparing results to my back tester and they seem identical, but when there is a long holding period the the results vary significantly, could also be a bug on my side but just asking if the back tester by default closes positions earlier.

Thanks

kernc commented 5 years ago

Multiple positions/instruments aren't yet supported. Welcome a discussion if someone would have a look.

does the back tester has some sort of default maximum holding time where it closes the position after n amount of time

@jetychill By default it doesn't. Can you share a minimal working example?

kernc commented 5 years ago

does the back tester has some sort of default maximum holding time where it closes the position after n amount of time has passed

Sounds like a good idea for a composable base strategy, HoldNBarsStrategy. :bulb:

ridulfo commented 4 years ago

Multiple positions/instruments aren't yet supported. Welcome a discussion if someone would have a look.

I would also be interested in being able to have multiple instruments at the same. This could possibly be implemented using a pandas panel.

In this way arbitrage strategies would be possible to backtest.

kernc commented 4 years ago

@nicoloridulfo Can't one backtest arbitrage opportunities by providing the strategy with extra data?

ridulfo commented 4 years ago

@kernc Thanks for answering.

I believe that is not the case. Correct me if I am wrong. I have studied the code and from what i see: one backtest = one instrument. It is not possible to look at multiple instruments in the same backtest and e.g. go long one instrument and short an other. If the order placing would be instrument specific and with some minor changes to the broker and strategy classes, I believe that it could be possible. The rest of the code is written in such a way that it seems like it was made to support this kind of feature.

I would be interested in trying to implement such a feature if you'll assign me to it.

kernc commented 4 years ago

Ah, pairs arbitrage. :sweat_smile: I don't know how multiple instruments fit into the existing API. I'd foremost like to keep it simple and straightforward for the 95% use case. If you do care to look into it, however, please certainly base your findings on PR https://github.com/kernc/backtesting.py/pull/47 as that's about to get merged soon.

ridulfo commented 4 years ago

Not only for arbitrage! I was thinking multi instrument portfolios. One of the most important parts of trading is risk management. Portfolio optimization through asset diversification is one way. I'll look into it 👍

sdmovie commented 4 years ago

Would suggest first make the single equity finished(which is 90+% od the use case) then considering enhance to support multiple equities in one strategy. Can imaging things will go complex towards what backtrader looks like. Ther are still gaps for single equity case to fill or to verify, suppoorting of Future/Stocks with options of: Margin/NonMargin Shortable/NonShortable T+0 or T+N If putting everything in one backtest too complicated, possible split another parallel backtestbroker to deal with subset of cases ?

diegolovison commented 4 years ago

You also will need to support TP or SP per group of instruments. Example: buy: A, B, and C TP 5% [A,B,C] SL 10% [A,B,C]

ttfreeman commented 3 years ago

Is this assigned?

kernc commented 3 years ago

Not even decided. Its API implications not even discussed.

ttfreeman commented 3 years ago

I also was looking at one of the other issues that has suggested adding support for stock options. I think this would be a prerequisite for that, because option strategies are usually a combination of one, two , or more long or short trades on Put or Call options.

arisliang commented 3 years ago

What is the API implication to implement this? Though I tend to think this is a feature very worth to have.

lawrence910426 commented 1 year ago

It would be really helpful if we could do pair tradings. Thank you so much :D

truongthanh96 commented 1 year ago

Pair trading seems like a popular algorithm yet no one bothers to develop a proper backtest API for its. Fine, I will do it myself

BradKML commented 7 months ago

@truongthanh96 how is the progress?