@Z, check out the bt.analyzers.Return code. That already does what you want. Just give it years as timeframe. Or write your own analyzer with TimeFrameAnalyzerBase as base class and use the _on_dt_over()
method, which is called on first trading day of a year if you use years as timeframe.
Best posts made by Reinke
-
RE: Identifying the First Trading Day of a Year
-
RE: Where Should I Get Data From?
@Z I think in most jurisdictions tax treatment of dividends vs unrealized gains or even vs realized gains is different. So if you simulate cash outflows for taxes it makes a difference for some strategies. If your commission is calculated on per share basis, it’s important to know when the split happened.
It might not be your biggest priority right now. For feeding the indicators I mostly use
the adjusted close as well. -
RE: Trying to Trade with Varying Date Ranges of Data
@Z , In the method prenext you would need to directly call self.next(). This way bt will start with the first bit of data you have. However, your indicators might not be ready, so you need to manually check that you have no garbage data.
Another important consideration is that this backtest will have a strong survivorship bias, which means you will invest in the 350 stocks that survived in the S&P 500 for 25 years. These stocks certainly did better than the 150 stocks that were dropped from the index during that period. For this reason the results of this backtest are not very meaningful.There is a good article in the blog about this method. https://www.backtrader.com/blog/2019-05-20-momentum-strategy/momentum-strategy/
-
RE: Where Should I Get Data From?
@Z , I download my data from Alphavantage, because I can get splits and dividends
explicitly called out on the days even in the free version. I don’t like using adjusted data. But due to the API call limit of 5 per minute, I wrote a background downloader. And data quality is not great either.
Latest posts made by Reinke
-
RE: Identifying the First Trading Day of a Year
@Z, check out the bt.analyzers.Return code. That already does what you want. Just give it years as timeframe. Or write your own analyzer with TimeFrameAnalyzerBase as base class and use the
_on_dt_over()
method, which is called on first trading day of a year if you use years as timeframe. -
RE: Dynamic multi asset allocation trading
@guoz14, as a starting point, why don't you use the code from this blog post:
https://www.backtrader.com/blog/2019-07-19-rebalancing-conservative/rebalancing-conservative/
This strategy has an equal-weighting the portfolio, but since you already have the volatility indicator defined in the strategy, you could use that to risk balance the portfolio. -
RE: Momentum indicator, arrays and linregress
@Jérémy-A, a lot if the stuff that the original poster did in init() need to be done in next(). There is a very good blog article that describes how to implement a linregress indicator in next() and how to implement the same indicator in init(). https://www.backtrader.com/blog/2019-05-20-momentum-strategy/momentum-strategy/
-
RE: Custom broker/exchange integration
@Suraz-Negi, not sure if I understand your question correctly. I think you want to connect to a different broker, not exchange. Your broker gives you access to different exchanges.
In order to connect to a different broker, I would recommend to look at the existing interfaces and copy as much as you can. You would probably need to implement a store, a data feed, and a broker. Most likely you will have to implement a different order class as well, depending on the requirements of your broker.
-
RE: Add orders Strat with time depency
@Azizi-Yassine, I’m not sure what answer you expect here. You could either check the timestamp in the next() method and then issue your orders when your timestamp is reached.
Or, in some brokers you could even pass timing conditions to the broker, e.g. the Interactive Broker interface simply passes through any arguments you throw at it. However the backtest broker as implemented today does not take a timing condition. -
RE: How to pass data to Stra
@Mohsin-Shabbir, you need to define strategy parameters as parameters.
params = dict(stoploss=1.0, sell=1.0)
Then you can access these parameters in the strategy with self.p.stoploss and self.p.sell.
Backtrader wraps the strategy class in a meta class, so some things are a little different from what you know if you haven’t worked with meta classes before. It confused me a lot in the beginning, but now I appreciate the flexibility.
I recommend that you reread some of the documentation and follow the examples in there. It really helps, because some concepts are a little counter-intuitive. (Probably more a problem with my intuition, as I found again and again that the author of BT is much smarter than me.)
-
RE: Where Should I Get Data From?
@Z I think in most jurisdictions tax treatment of dividends vs unrealized gains or even vs realized gains is different. So if you simulate cash outflows for taxes it makes a difference for some strategies. If your commission is calculated on per share basis, it’s important to know when the split happened.
It might not be your biggest priority right now. For feeding the indicators I mostly use
the adjusted close as well. -
RE: Where Should I Get Data From?
@Z , I download my data from Alphavantage, because I can get splits and dividends
explicitly called out on the days even in the free version. I don’t like using adjusted data. But due to the API call limit of 5 per minute, I wrote a background downloader. And data quality is not great either. -
RE: Trying to Trade with Varying Date Ranges of Data
@Z , In the method prenext you would need to directly call self.next(). This way bt will start with the first bit of data you have. However, your indicators might not be ready, so you need to manually check that you have no garbage data.
Another important consideration is that this backtest will have a strong survivorship bias, which means you will invest in the 350 stocks that survived in the S&P 500 for 25 years. These stocks certainly did better than the 150 stocks that were dropped from the index during that period. For this reason the results of this backtest are not very meaningful.There is a good article in the blog about this method. https://www.backtrader.com/blog/2019-05-20-momentum-strategy/momentum-strategy/