I'm getting runtime errors on the basic futures template.

Runtime Error: AttributeError : 'datetime.datetime' object has no attribute 'Date' at OnData
 

def OnData(self, slice):
if not self.Portfolio.Invested:
for chain in slice.FutureChains:
# find the front contract expiring no earlier than in 90 days
contracts = [ i for i in chain.Value if i.Expiry > self.Time.Date.AddDays(180) ]

# if thre is more than one contract, trade the one woth the closes expire date
if len(contracts) > 0:
contract = sorted(contracts, key=lambda x: x.Volume, reverse=False)[0]
self.MarketOrder(contract.Symbol, 1);

Then, if I modify the line to remedy the error (simply putting contracts = chain.Values.Contracts), I get the following TypeError:

Runtime Error: TypeError : object of type 'FuturesContracts' has no len()


    def OnData(self, slice):
        if not self.Portfolio.Invested:
            for chain in slice.FutureChains:
contracts = chain.Values.Contracts
                if len(contracts) > 0:
                    contract = sorted(contracts, key=lambda x: x.Volume, reverse=False)[0]
                    self.MarketOrder(contract.Symbol, 1);
        else:
            self.Liquidate();

 

I'm just trying to change the code from sorting by expiry to sorting by volume. Seems like it should be simple but I'm running into some difficulties, obviously.