Back

TypeError exception with SetHoldings

I've been playing around with the tail hedge code provided in the most recent QC livestream on Youtube. After making some modifications, I'm getting some errors in the log I can't figure out. In an effort to drill down on the specific issue, I've deleted most of the code and really simplified it, such that now I just want it to allocate 90% of my portfolio and 10% in an option hedge on day 1. Just two trades and that's it, no rebalancing or liquidating positions. 

I'm getting an error message that there's a TypeError exception with SetHoldings. How do I correct this? Nothing I do seems to fix it. Your assistance is much appreciated.

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.


Hi there, can you please post the code of the issue you're having? It's not possible to guess the issue from your message above. 

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.


class ResistanceTransdimensionalSplitter(QCAlgorithm):

def Initialize(self):
self.SetStartDate(2009, 1, 1)
self.SetEndDate(2009, 6, 1)
self.SetCash(1000000)
spy = self.AddEquity("SPY", Resolution.Minute)
spy.SetDataNormalizationMode(DataNormalizationMode.Raw)
self.spy = spy.Symbol
self.contract = None
self.SetWarmUp(200)

def OnData(self, data):
if self.IsWarmingUp:
return

if not self.Portfolio[self.spy].Invested:
self.SetHoldings(self.spy, 0.9)
self.Debug(str(self.Portfolio[self.spy].HoldingsValue))

if self.contract is None:
self.contract = self.GetContract
self.SetHoldings(self.contract, 0.1)
return

def GetContract(self):
targetStrike = (self.Securities[self.spy].Price * 0.6) - (self.Securities[self.spy].Price * 0.6)%5
contracts = self.OptionChainProvider.GetOptionContractList(self.spy, self.Time)
puts = [x for x in contracts if x.ID.OptionRight == OptionRight.Put]
puts = sorted( sorted(puts, key = lambda x: x.ID.Date, reverse = True),
key = lambda x: x.ID.StrikePrice)
puts = [x for x in puts if x.ID.StrikePrice == targetStrike]
puts = [x for x in puts if 270 < (x.ID.Date - self.Time).days <= 420]
if len(puts) == 0:
return None
self.AddOptionContract(puts[0], Resolution.Minute)
return puts[0]

 

0

Above is the code. I tried attaching the backtest to the comment with the "Attach Backtest" button, but it's not working. The project shows up, but it shows no back tests, even though there are 4. Perhaps because I'm getting runtime errors with the backtests...?

0

Hi The Dude,

You are correct in that backtests with runtime errors can't be attached.On line 22, the parentheses were left off of the function call. I’ve included the fix in the attached backtest. For future reference, our debugger is a great way to resolve errors like these.

Best,
Shile Wen 

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.


I knew it would be something silly like that! Thanks for helping me out. AND I had no idea about this debugging tool. I look forward to using it a lot more going forward.

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