-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Backtesting multiple assets (and timeframes) #1205
-
Hi,
I am currently trying to explore a multitude of different strategies from various sources. I believe something is a little different about what I'm doing than use cases you may have seen in the past: I only have a fairly limited amount of data for each asset I want to backtest (days to maybe a few months at most), but in all cases I do have multiple timeframes available should I want to use them.
I'm wondering how I could run a backtest for more than one asset. I want to see how the strategy performs on all of them, not a single asset at a time. For one, compiling the results could be a bit of work... but it's also possible that a strategy performs well on one asset, but not at all on another. I want to see the results of any particular strategy on the combination of these assets.
Is there a way to do this?
I found this: #364 which doesn't really provide much of an answer so far
Beta Was this translation helpful? Give feedback.
All reactions
Replies: 1 comment 2 replies
-
Please check if the following code meets your expectations. If it does, you can use a For loop to replace 'GOOG' or 'EURUSD' with your desired assets.
from backtesting import Backtest, Strategy
from backtesting.lib import crossover
from backtesting.test import SMA, GOOG, EURUSD
class SmaCross(Strategy):
n1 = 10
n2 = 20
def init(self):
close = self.data.Close
self.sma1 = self.I(SMA, close, self.n1)
self.sma2 = self.I(SMA, close, self.n2)
def next(self):
if crossover(self.sma1, self.sma2):
self.buy()
elif crossover(self.sma2, self.sma1):
self.sell()
# You can use For loop to cover the code below:
bt = Backtest(GOOG, SmaCross,
cash=10000, commission=.002,
exclusive_orders=True)
output = bt.run()
bt.plot()
print(output.iloc[6])
bt = Backtest(EURUSD, SmaCross,
cash=10000, commission=.002,
exclusive_orders=True)
output = bt.run()
bt.plot()
print(output.iloc[6])
Beta Was this translation helpful? Give feedback.
All reactions
-
@iimka well, not quite. A for loop like this simply runs two backtests. I was hoping to run a single one and truly compile the results across different coins.
Imagine if I have data from January 1 to 14 for one asset, and another file has data from January 6th to 14th... implying that on the 6th I'd essentially turn on my strategy for a new asset. I'd want a single backtest so that I can correctly size my positions. If I only ever use my full portfolio for each position, that would mean that if I'm in a position on asset 1 on the 6th, I couldn't enter a new position on the second asset.
I'm essentially trying to mimic what would happen in a real world scenario where a strategy runs automatically across different assets.
A separate backtest will return different results. Your example has 682% final for one asset, and 59% for the other. I want to find out what the asset does for both of them combined, without somehow wrangling the results together if I can help it.
Beta Was this translation helpful? Give feedback.
All reactions
-
I understand your point now. Referring to #20 and #104, I believe that backtesting.py still does not support dynamically adjusting position sizes for a portfolio, and you may need another backtesting system. Alternatively, a clumsy method would be to use the backtesting.py results and put them into Excel, then add formulas to calculate the positions for assets A and B, as well as the total capital.
Additionally, my English is not very strong, and I am trying to understand the information on #254, but I'm not sure if it's relevant.
Beta Was this translation helpful? Give feedback.