Back

SMA Algo (Getting previous day's value?!)

So we've developed a strategy that needs to know if the opening price is above the 20-day SMA. My problem is that if I go look at a chart say in Scottrade, the 20 day SMA value that shows at market open is not the same as my QuantConnect value at the same time. In fact, we've noticed that the QuantConnect algo SMA value is the EXACT value that Scottrade (or any other historic chart) will show for the SMA from the PREVIOUS day.

Which brings me to the conclusion that the way my algo is coded is causing me to use the previous day's SMA value... Can someone take a look at my code and help me understand that? If there is documentation on the indicator classes, I would love to read up on them. I just don't see it under the documentation section of the site. I'm sure its possibly there, but I can't find it.

Update Backtest








If you're looking at a daily SMA plot then the SMA on a date, say 3/3/2016 is actually the SMA at the end of that same day. In LEAN, the daily SMA will be emitted when the first data point on the new day comes in. So in your example you're running second data, so the SMA value from 3/2/2016 will arrive in the algorithm at 9:30:01am on 3/3/2016. Daily charts from a broker or other service are misleading because it implies you can use the data on that same day, but in reality, you don't know what the daily value would be until after the day is over and the markets are closed. Does this make sense, or maybe I misunderstood your issue?

Here's the LEAN indicator documentation. Also, if you're extra curious, you can peruse the indicator code base. Here's another link to the consolidator implementations (consolidates small bars into bigger bars for auto updating indicators).

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.


Interesting... So the idea here is that the broker charts are showing data that isn't available if you were to view the chart live. I'll go back and check to see if that's the value we're looking at in a live example.

With that said, I would like to confirm that the SimpleMovingAverage value is calculated on the standard "close of day" price. Some brokerages allow you to modify the value to be on open, close, average, etc. The price we were using in our historic charts from the brokerage claimed to be based on "close"... so by checking the value at the beginning of the daily chart, we thought that would be the same as the SimpleMovingAverage indicator class (considering you don't know what the close of day will be yet).

0

I could be wrong but, what I think you're describing is look ahead bias. The second part of your question is kind of confusing and I read it like 10 times but still don't quite understand what you're trying to do. If you rephrase it maybe I can help.

0

@Chris, indicators can be configured to run with whichever fields you want, by specifying one when you create your indicator. Here's me making an SMA from the low values:var lowSma = SMA("SPY", 14, Field.Low);Here's the field class with various field selectors.

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.


@MichaelH, thanks for the field selection. That definitely provides the functionality that brokerage charts offer.

@Travis Simply put, I want to take the SMA (say 20 day for example) and on the FIRST tick of the day, I want to "update" that value to see what the 20 day SMA would be if this present day were to end at the current price of this tick. I've looked through the documentation and it looks like this is going to be rather difficult to do.

I can call SimpleMovingAverage.Update(tick.price), but that will contaminate the actual SMA value moving forward. Because the ComputeNextValue() method is protected, I reasoned I'd just need to copy the object, perform an update, and then repeat this on the first tick of every day to use the indicator as I intend.

Because there's apparently no standard way to copy objects in C#, it looks as though I'll need to instantiate a new object of SimpleMovingAverage (ie. SMA2), set SMA2.RollingSum = SMA1.RollingSum (effectively copying the rolling sum and hopefully the window of values), and then run SMA2.Update(firstTick.price) to get the answer I desire.

Have I reached the correct conclusion or is there a better way to approach this?

0


You're conclusions are correct but implementation looks flawed. Settinging SMA2.RollingSum = SMA1.RollingSum would copy a reference to the same Sum class object. This is, however, also not permitted since you're unable to set the RollingSum property on SimpleMovingAverage. It would probably be best to wrap this behavior up in a new indicator for organizational purposes. You'll need to manually maintain a window of values so you can populate the duplicate simple moving average.

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.


Michael,

After going down a long path attempting to add the functionality to an InstantSMA class I was attempting to write myself, I just tried to roll back to what I previously had by "cloning" the original project in this post. It comes up with three compiler errors. I am quite perplexed at how the code that worked just a couple of months ago fails to build and execute properly now.

Can you provide any guidance as to how to fix these three errors? (I can make the errors go away with small fixes, but I am unable to get the algo to buy at all.. Previously it definitely performed).

Build Warning: File: Main.cs Line:16 Column:33 - 'QCUParameterizedAlgorithm.SMA' hides inherited member 'QCAlgorithm.SMA(Symbol, int, Resolution?, Func)'. Use the new keyword if hiding was intended.

Build Warning: File: Main.cs Line:78 Column:59 - 'DataDictionary.Time' is obsolete: 'The DataDictionary Time property is now obsolete. All algorithms should use algorithm.Time instead.'

Build Warning: File: Main.cs Line:66 Column:19 - Field 'QCUParameterizedAlgorithm.previous' is never assigned to, and will always have its default value 

0

Hi Chris, we forced an upgrade on all algorithms on the platform. This probably caused a change in behavior you've seen.

> Change the name of your method to something other than SMA, we use this in the base class.> Use "Time" not "data.Time".> Remove previous variable

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.


Jared,

That fixed two of the compiler errors. The third one regarding the "previous" value remains. This is most likely the reason why it isn't trading (because of the login involving that value). What has this value been updated to or how can I obtain the date for the previous trading day?

Build Warning: File: Main.cs Line:66 Column:19 - Field 'QCUParameterizedAlgorithm.previous' is never assigned to, and will always have its default value

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