I am a new QC beginner.
In the `research.ipynb`, I can easily extract and manipulate the historical VIX data.
However, when I use the same logic code in `main.py`, many errors come out (in code lines #40 and #56).
For example:
During the algorithm initialization, the following exception has occurred: 'DataFrame' object has no attribute 'close'
at __getattr__
return object.__getattribute__(self, name)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
in generic.py: line 6203
at initialize
vix_data = self.history(vix_symbol, 1).close
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
in main.py: line 40
'DataFrame' object has no attribute 'close'
Here is my `research.ipynb` code and this works:
qb = QuantBook()
vix_symbol = qb.add_data(CBOE, "VIX", Resolution.DAILY).symbol
df = qb.history(vix_symbol, 1)
df

Here is my `main.py` code and this does not work:
from AlgorithmImports import *
import datetime
class SectorMomentumAlgorithm(QCAlgorithm):
def initialize(self):
self.set_start_date(2007, 1, 1)
self.set_account_currency("USD")
self.set_cash(100000)
self.set_time_zone(TimeZones.EASTERN_STANDARD)
self.set_warm_up(timedelta(7))
# create a dictionary to store momentum indicators for all symbols
self._dataMOM_period = {}
self._dataMOM_1D = {}
period = 252 # 1 year trading days
# choose ten sector ETFs
symbols = ["XLB", # Vanguard Real Estate Index Fund
"XLV", # Technology Select Sector SPDR Fund
"XLC", # Energy Select Sector SPDR Fund
"XLK", # Health Care Select Sector SPDR Fund
"XLF", # Financial Select Sector SPDR Fund
"XLP", # SPDR S&P Bank ETF
"XLI", # Vanguard Materials ETF
"XLU", # Consumer Discretionary Select Sector SPDR Fund
"XLY", # Consumer Staples Select Sector SPDR Fund
"XLRE", # Real Estate Select Sector SPDR Fund
"XLE"] # Energy Select Sector SPDR Fund
# warm up the MOM indicator
self.set_warm_up(period)
for symbol in symbols:
self.add_equity(symbol, Resolution.DAILY)
self._dataMOM_period[symbol] = self.mom(symbol, period, Resolution.DAILY)
self._dataMOM_1D[symbol] = self.mom(symbol, 1, Resolution.DAILY)
vix_symbol = self.add_data(CBOE, "VIX", Resolution.DAILY).symbol
vix_data = self.history(vix_symbol, 1).close
self.debug = True
self.Debug(self.vix_data.columns)
# shcedule the function to fire at the day start
self.schedule.on(self.date_rules.every_day("XLK"), self.time_rules.after_market_open("XLK"), self._rebalance)
def _rebalance(self):
if self.is_warming_up:
return
top1 = pd.Series(self._dataMOM_period).sort_values(ascending=False)[:1]
for symbol, security_hold in self.portfolio.items():
if (security_hold.invested and (symbol.value not in top1.index) and vix_data<20) or (vix_data>20 and float(self._dataMOM_1D[symbol].Current.Value)>1.5):
self.liquidate(symbol)
for symbol in top1.index:
if (vix_data<20 and (symbol.value in top1.index)) or (vix_data>20 and float(self._dataMOM_1D[symbol].Current.Value)<-1.5):
self.set_holdings(symbol, 1/len(top1))
I have already read through the whole QC History Requests Documentation (History Requests - QuantConnect.com), but I have no idea how to solve the errors.
Cheuk hin Poon
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!