LEAN is the open source
algorithmic trading engine powering QuantConnect. Founded in 2013 LEAN has been built by a
global community of 80+ engineers and powers more than a dozen hedge funds today.
Alpha League Competition: $1,000 Weekly Prize Pool
Qualifying Alpha Streams Reentered Weekly Learn
more
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.
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.
Vladimir
94.7k Pro
,
Louis,
In your example code, when I replaced "SPY" with "QQQ" on line 7, I got an AttributeError: 'NoneType' object has no attribute 'Price'. Why?
1
Spacetime
10.8k Pro
,
need to check data before accessing it
below code can help
def OnData(self, data):
if not self.sma.IsReady or self.spy not in data or self.bnd not in data:
return
if data.Bars.ContainsKey("QQQ"):
price = data.Bars[self.spy].Price
if price >= self.sma.Current.Value:
# Either rebalance or rice has crossed above SMA
if self.Time >= self.rebalanceTime or not self.uptrend:
self.SetHoldings(self.spy, 0.8)
self.SetHoldings(self.bnd, 0.2)
self.uptrend = True
self.rebalanceTime = self.Time + timedelta(30)
# Either rebalance or price has crossed below SMA
elif self.Time >= self.rebalanceTime or self.uptrend:
self.SetHoldings(self.spy, 0.2)
self.SetHoldings(self.bnd, 0.8)
self.uptrend = False
self.rebalanceTime = self.Time + timedelta(30)
Spacetime Thank you for your response. Why the original data check if not self.sma.IsReady or self.spy not in data or self.bnd not in data: return is good for "SPY" or if I use price = self.Securities[self.spy].Price?
0
Spacetime
10.8k Pro
,
I do not know the reasons why. Maybe someone more knowledgeable can shed some light.
is still true Sometimes although the key for a security may be available in the slice data, the bar data for that security may not be available. For example, this may happen when non-price(splits,dividends, etc) data is available for that symbol while bar data is not. Best, Varad Kabade
1
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.
if data[self.spy].Price >= self.sma.Current.Value:
or
if self.Securities[self.spy].Price >= self.sma.Current.Value:
0
Varad Kabade
STAFF Pro
,
Hi Vladimir, We recommend using
if data[self.spy].Price >= self.sma.Current.Value:
instead of
if self.Securities[self.spy].Price >= self.sma.Current.Value:
because the latter contains the price at which the asset was traded at last, which may not be ideal if the current data does not contain any TradeBar for the given asset. However, in general, they have the same value, so we can use the latter if we only care about the security price no matter when it was recorded. Best, Varad Kabade
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.
if data[self.spy].Price >= self.sma.Current.Value:
instead of
if self.Securities[self.spy].Price >= self.sma.Current.Value:
then
why I get
AttributeError : 'NoneType' object has no attribute 'Price'
if I change on line 7 "SPY" to "QQQ"???
0
Edited by Vladimir
Spacetime
10.8k Pro
,
Vladimir , you still have to check data before accessing it
0
Varad Kabade
STAFF Pro
,
Hi Vladimir, The above error occurs because even though the current slice Object (data) consists of data for our given symbol, it is non-trade data (Splits, Dividends, Delistings, etc.) hence data.Bars[symbol] is a null-type object. Refer to the attached backtest for the above demo. Best, Varad Kabade
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.
I have never seen such a long data test in any public algorithm.
if not self.sma.IsReady or self.spy not in data or self.bnd not in data:
return
if self.spy in data.Splits or self.spy in data.Dividends or self.spy in data.Delistings and self.spy not in data.Bars:
return
Last two questions:
Why in the original algorithm published by Louis SPY does not issue a warning without this lengthy test?
Where in the documentation is it explained?
0
Daniel Wash
251 Pro
,
Thanks for doing these videos, really helpful 😊
1
Varad Kabade
STAFF Pro
,
Hi Vladimir,
Why in the original algorithm published by Louis SPY does not issue a warning without this lengthy test?
This happens because when self.spy in data.Dividends is true, consequently data[self.spy] is None. During the backtest interval in the original algorithm by Louis, there is no event of type Dividend. Therefore, we don't encounter the error. We don't need a long test; I was just describing how the TradeBars and QuoteBars are not the only data event in our Slice object. The following test is enough:
Where in the documentation is it explained?
Please refer to the following doc for information regarding different data events. Refer to the attached backtest. Best, Varad Kabade
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.
Mitch Christow
438 Pro
,
Hi Louis,
Absolutely amazing video series. Thanks for creating these great tutorials. Are you planning on creating more videos or is the series completed? I would really like to see a tutorial that covers how to write custom modules for the algorithm framework from scratch. It would be great to have a video on how to create a custom class for each framework module (universe selection, alpha creation, portfolio construction, execution, and risk management). Is that something that you are planning on creating? Thanks again for these fantastic tutorials.
Cheers, Mitch
1
Louis Szeto
STAFF Pro
,
Hi Mitch
While waiting for Louis’s video series updating, we also have examples of custom modules of our framework. They’re well-explained and could be easily mimicked. Please find them on the Alpha Framework docs’ top boxes on each modules’ page, or in this collection. Also, implementation of their structures could be found here (alpha, portfolio construction, execution, risk management). We encourage members to try and build their own ones. Practice makes perfect!
Best Louis Szeto
2
Edited by Louis Szeto
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.
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...
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!