Hi,
Is there documentation for QuantBook API methods and members?
I am trying to play around with "VIX predicts stock index returns" strategy from the Strategy library in a research notebook. When I try to execute this cell (QuantBook object qb is initialized in a previous cell):
import numpy as np
from QuantConnect.Python import PythonQuandl
oef = qb.AddEquity("OEF", Resolution.Daily)
vix = 'CBOE/VIX'
qb.AddData(QuandlVix, self.vix, Resolution.Daily)
window = qb.RollingWindow[float](252*2)
hist = qb.History([self.vix], 1000, Resolution.Daily)
for close in hist.loc[self.vix]['vix close']:
self.window.Add(close)
I get a "NameError: name 'QuandlVix' is not defined" when I run this. Any suggestions?
Also, when I run the backtest for this strategy
class VIXPredictsStockIndexReturns(QCAlgorithm): def Initialize(self): self.SetStartDate(2006, 1, 1) self.SetEndDate(2018, 8, 1) self.SetCash(100000) self.AddEquity("OEF", Resolution.Daily) self.vix = 'CBOE/VIX' self.AddData(QuandlVix, self.vix, Resolution.Daily) self.window = RollingWindow[float](252*2) hist = self.History([self.vix], 1000, Resolution.Daily) for close in hist.loc[self.vix]['vix close']: self.window.Add(close) def OnData(self, data): if not data.ContainsKey(self.vix): return self.window.Add(self.Securities[self.vix].Price) if not self.window.IsReady: return history_close = [i for i in self.window] if self.Securities[self.vix].Price > np.percentile(history_close, 90): self.SetHoldings("OEF", 1) elif self.Securities[self.vix].Price < np.percentile(history_close, 10): self.SetHoldings("OEF", -1) class QuandlVix(PythonQuandl): def __init__(self): self.ValueColumnName = "VIX Close"
I am a little unsure of how the SetHoldings function works, because the first orders that are executed in this backtest are sell orders. Can I develop short positions in my portfolio even though I might not have "OEF" to begin with? Also, what do the +1 and -1 in the SetHoldings parameters mean? Do they mean 1 unit of security? Because the orders that get executed are orders with quantity greater than 1?
Adam W
The API for QuantBook behaves almost identically to the typical algorithm API, with the exception of certain methods. For the first error, don't forget to define QuandlVix before you call it.
Take a look here for docs on position sizing. The sign indicates long/short, and the number indicates percentage of unlevered buying power. For instance, SetHoldings('OEF', 1) means to allocate 100% of your portfolio value into OEF. To calculate units of securities instead, use CalculateOrderQuantity().
Yes, you can short a stock without holding it. Conceptually, this means that you are "borrowing" the stock from the broker with the agreement that you will "re-purchase" the stock from market when closing the short position in the future.
Â
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!