Hi Guys,
Just sharing a little function to plot the Benchmark curve along with the Strate Equity. See template code attached.
Essentially, when you add a benchmark using
self.SetBenchmark()
, you can retrieve its price during the backtest usingĀ
self.Benchmark.Evaluate(self.Time)
The function I wrote is just:
def UpdateBenchmarkValue(self):
''' Simulate buy and hold the Benchmark '''
if self.initBenchmarkPrice is None:
self.initBenchmarkCash = self.Portfolio.Cash
self.initBenchmarkPrice = self.Benchmark.Evaluate(self.Time)
self.benchmarkValue = self.initBenchmarkCash
else:
currentBenchmarkPrice = self.Benchmark.Evaluate(self.Time)
self.benchmarkValue = (currentBenchmarkPrice / self.initBenchmarkPrice) * self.initBenchmarkCash
Enjoy!
Emilio
Ofir Shmulevich
Nice visualization thanks
Kyle sn
Thanks for this. Presumably the benchmark data is using the same resolution as the symbol's resolution? What if you had an intraday strategy using minute data but wanted to graph the daily benchmark value (to abide by the 4000 data point limit)?
Emilio Freire
Thanks Guys!
Kyle UnknownĀ The data resolution doesn't matter. You can simply call the function once a day to plot the close to close of the benchmark. This self.Benchmark.Evaluate(self.Time) will give you the current value of the Benchmark at that time so it's up to you when and how often to call that function!
Emilio
InnoQuantivity.com
Kyle sn
That makes sense. I was able to make that work in a test algorithm and I am definitely going to add this to all my others.
Emilio Freire
Great to hear Kyle UnknownĀ !
It is indeed a very usefull feature to have.
Emilio
InnoQuantivity.com
Vncne
Hi! Your function worked perfectly fine when I cloned your algorithm, but when I tried incorporating it to one of mine that uses simple technical indicators, I encounter an error:
:: self.benchmarkValue = (currentBenchmarkPrice / self.initBenchmarkPrice) * self.initBenchmarkCash
ZeroDivisionError : float division by zero
I've no idea how it's ending up dividing by zero.
Btw in the attached backtest, I had to comment out the self.plot in OnData in order for the algorithm to produce a backtest.
Thanks!
Derek Melchin
Hi Vncne,
To resolve this, we need to place the `IsWarmingUp` guard above the call to `UpdateBenchmarkValue` in OnData.
See the attached backtest for reference.
Best,
Derek Melchin
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
Brandon Leonard
This is just what I was looking for and it is really helpful. Thanks for sharing!
Emilio Freire
Hi Brandon Leonard !
Glad you found it helpful :)
And even cooler, if you do something like the below, you can control the direction of the Benchmark (long/short) and its exposure as well. You could set self.benchmarkExposure to -1 to short the Benchmark or even -0.5 to simulate a short 50% exposure to it, etc.
Jaipal Tuttle
So cool! I learned a ton from this!!!
Thanks and best wishes
Emilio Freire
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!