Back

A little trick to plot the Benchmark

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

InnoQuantivity.com

Update Backtest








0

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.


Nice visualization thanks

0

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)?

0

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

1

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.

0

Great to hear Kyle Unknown !

It is indeed a very usefull feature to have.

Emilio

InnoQuantivity.com

1

Update Backtest





0

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.


Loading...

This discussion is closed