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.6k stars 1.08k forks source link

what about multiple stocks/data inputs? #1105

Open JustinGuese opened 10 months ago

JustinGuese commented 10 months ago

Hi, I like the direction where backtesting.py is going, but I am missing some backtrader functionality... Maybe the most important - currently only one stock is supported right?

Because if I have a strategy which uses 2 stocks, how would I deal with that? The backtrader way was using self.datas instead of data, which then becomes an array with all the stock datas, and then obviously the buy/sell functions would need a ticker parameter... Or is there already a way to achieve this?

JustinGuese commented 10 months ago

or is that functionality actually already there?

class _Data:
    """
    A data array accessor. Provides access to OHLCV "columns"
    as a standard `pd.DataFrame` would, except it's not a DataFrame
    and the returned "series" are _not_ `pd.Series` but `np.ndarray`
    for performance reasons.
    """
    def __init__(self, df: pd.DataFrame):
        self.__df = df
        self.__i = len(df)
        self.__pip: Optional[float] = None
        self.__cache: Dict[str, _Array] = {}
        self.__arrays: Dict[str, _Array] = {}
        self._update()

so would I pass something like data = { "MSFT" : yf.download("MSFT"), "AAPL" : ...?

PabloCanovas commented 8 months ago

Check out https://github.com/dodid/minitrade, a fork from this project which allows for multiasset backtesting. Would love to have this functionality here under @kernc project though!

BradKML commented 6 months ago

Seconding this since it is getting more important to mess with pair trading. Also @PabloCanovas do you know of any equivalents for bt?

s-kust commented 3 months ago

You may want to take a look at https://github.com/s-kust/python-backtesting-template/

You can easily run backtests of your strategy for several (or several dozen) tickers simultaneously. The results of these backtests are combined and saved in the output.xlsx file.