Back

Total Return on SPY

I'm testing a buy & hold strategy on the SPY as a total return symbol, but I'm having trouble coding this properly. Would you mind taking a look at my project for me?

The probable operative code follows, which is my only reference to "DataNormalizationMode.TotalReturn".

AddSecurity(SecurityType.Equity, symbol, Resolution.Minute);
Securities[symbol].SetDataNormalizationMode(DataNormalizationMode.TotalReturn);

When I run the buy & hold SPY test from Dec 28, 2009 through July 28, 2015 I generate less than the SPX Benchmark does on QC for the same period, and my ending portfolio value approximates that for the SPY on Dividend Channel (DC) without dividend reinvestment. The QC Benchmark for SPX generates $211,500 over this period which matches that on DC.

So I anticipate I am not buying the SPY as a total return symbol in my code. Can you show me how to do this? I suspect I am missing either an important reference or concept....

Thank you very much!
Update Backtest








Hey John Jay,

Are you referring to the benchmark that is overlayed on the equity plot? If so, this doesn't reflect the total return setting. It uses split and dividend adjusted data to compute how a buy and hold would have perform.

I've attached some edits to your algorithm that modifies the Benchmark chart (not the equity overlay, but the chart called 'Benchmark'). This does some fancy hacks to put the total return benchmark and equity on the same chart. Expect some small differences though due to differences in when the benchmark is sampled and when the equity is sampled.

Hope this helps!
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.


Thank you for taking a look at this!

My problem is that the equity SPY traded in my strategy does not seem to trade on a total return basis: I don't think I'm getting dividends automatically reinvested in my strategy.

The variance between your new benchmark and the SPY buy and hold strategy approximates $7K, same as before.

The SPY traded generates $204K and the benchmark $211K, when in fact they should be very much close to the same (the benchmark is the SPY Total Return, in your configuration, yes?). Your new benchmark generates the exact same ending value as the standard benchmark did for SPX.

Can you verify that the SPY in my code is receiving total return values?

Thank you!
0

Please click on the Benchmark chart on the right. When I run the algo it has a difference of under 1K. The standard overlay of SP500 with your equity is not what I changed.
2

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.


I can't find the Benchmark Chart "on the right" you mean.... Sorry about that!

But when I clone and run the code you have attached to the chart above I get a Benchmark value of $211,000 and an equity value of $203,000; this based on the hover over at the end of the chart when expanded to 1 month.

Should my code, as written, be giving me Total Return on the equity price? $

203,000 is the return over the period on the SPY without dividends reinvested according to Dividend Channel, and $211,000 is the total return of the index according to DC. So the equity return I get in my code as written of $203,000 is probably the return without dividends reinvested, yes? I expect a mistake in the calculation not of the benchmark but the equity returns.
0

Using the total return option adjusts the raw price of the security by adding in dividends since the start of the backtest. We don't actually model taking those dividends and purchasing new stock with the distribution, it is a means of normalizing the data and the strategy trades on the normalized data. Our default option normalizes for splits and dividends, albeit using a different formula. For higher fidelity I would recommend not using TotalReturn, but instead using Raw and manually reinvesting your dividends. This will remove upside bias from not factoring in transaction fees and errors in not accounting for delays from receiving the dividend to filling the order.

TotalReturn is very similar to dividend adjusted, but it happens in the opposite direction. Dividend adjusted data is back adjusted, but TotalReturn forward adjusts based on incoming dividends. I ran your strategy in both TotalReturn and default mode and confirmed that the prices are being scaled properly. In TotalReturn mode the price starts at the raw price and finishes ~228 which is the result of adding in the dividends. In default mode the price starts less than the raw price because it's been back adjusted for dividends and ends at the current raw price.

Here's the chart's I was referencing in previous posts.

If you click on the word 'Benchmark' on the right, highlighted in red:


When this chart loads you can see the plots I've added. The one is your total portfolio value ('equity') and the other is the 'Benchmark' that I've been referencing. The regular benchmark chart visible in the image above is based on split and dividend adjusted data from Yahoo, not based on the selected data normalization mode.
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.


Just a note -- We simplified the benchmark plot in the IDE to be a little more intuitive -- the light gray line under the equity plot is now your custom benchmark plot (set with SetBenchmark function as demo'd by Mike)

Because of this we also hid the large Benchmark button -- and the "Select Benchmark" dropdown is the only way to access this data.

Benchmarks can be custom in QC. (This allows statistics based on custom benchmarks).
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.


Thank you very much for the review and advice.

I will need manual dividend reinvestment. I was always going to have to do this for live trading - IB doesn't support DRIPs (Dividend Reinvestment Plans).

When I code for manual reinvestment of cash dividends, will I need to reference any special code to activate Dividend Events (I saw something about this on github).

Nonetheless, I will still need to rank the securities traded on the basis of Total Return, this to determine security selection, but I will then need to actually trade them based using manual dividend reinvestment, so I will need to use both methods in one strategy, .TotalReturn and .Raw. Do you think this presents a problem?

When I have formulated more detailed questions about this I will let you know.

Cheers!
0

You can check out the sample dividend algorithm here. It's very important to note that when doing live trading, data normalization modes aren't respected, it would cause issues with portfolio values and such. In live it's always the raw price.

As such, if you want to perform analysis on the total return value of a stock you'll need to keep track of the sum of the dividends in the algorithm and add it to Security.Price each time you do analysis. Indicators could be made to simplify this.

Let us know if you need help wiring up the dividend events!
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.


This is great feedback. Thanks!

So in live mode I can't run "Total Return" rankings on a list of securities in order to select from that list the top x symbols to trade live using Raw Price mode data?

Can I build indicators for each security using "Total Return" rankings and then use these indicator values (i.e. PCT Total Return over x period) to select my securities for live trading?
0

If you want to simulate total return in live mode you'll need to manually record all of the dividends received for each security. The total return price is equal to the raw price + the sum of the dividends. Once you have this working, you can use that data to modify the default indicator. So assuming you can track the dividends you end up with something like the following://class variable
decimal sumOfDividends = 0m;
string Symbol = "SPY";

// initialize
var priceIndicator = Identity(Symbol);
// modify priceIndicator with the dividends
var totalReturnPrice = new FunctionalIndicator(Symbol + "_TotalReturn",
input => input + sumOfDividends,
ind => true);

// OnData(Dividends data)
sumOfDividends += data[Symbol].Distribution;

Try this out and let me know how you make out, if you get stuck, please post an algorithm and I can take a peak.
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.


Extremely well done.

I will work on this and keep you posted.

Cheers!
0

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