Overall Statistics
# https://quantpedia.com/strategies/term-structure-effect-in-commodities/
#
# This simple strategy buys each month the 20% of commodities with the highest roll-returns and shorts the 20% of commodities with the lowest 
# roll-returns and holds the long-short positions for one month. The contracts in each quintile are equally-weighted. 
# The investment universe is all commodity futures contracts.

class TermStructureCommodities(QCAlgorithm):

    def Initialize(self):
        self.SetStartDate(2000, 1, 1)
        self.SetCash(100000)
        
        self.symbols = [
                        "CME_S",   # Soybean Futures, Continuous Contract
                        "CME_W",   # Wheat Futures, Continuous Contract
                        "CME_SM",  # Soybean Meal Futures, Continuous Contract
                        "CME_BO",  # Soybean Oil Futures, Continuous Contract
                        "CME_C",   # Corn Futures, Continuous Contract
                        "CME_O",   # Oats Futures, Continuous Contract
                        "CME_LC",  # Live Cattle Futures, Continuous Contract
                        "CME_FC",  # Feeder Cattle Futures, Continuous Contract
                        "CME_LN",  # Lean Hog Futures, Continuous Contract
                        "CME_GC",  # Gold Futures, Continuous Contract
                        "CME_SI",  # Silver Futures, Continuous Contract
                        "CME_PL",  # Platinum Futures, Continuous Contract
                        "CME_CL",  # Crude Oil Futures, Continuous Contract
                        "CME_HG",  # Copper Futures, Continuous Contract
                        "CME_LB",  # Random Length Lumber Futures, Continuous Contract
                        "CME_NG",  # Natural Gas (Henry Hub) Physical Futures, Continuous Contract
                        "CME_PA",  # Palladium Futures, Continuous Contract 
                        "CME_RR",  # Rough Rice Futures, Continuous Contract
                        "CME_CU",  # Chicago Ethanol (Platts) Futures
                        "CME_DA",  # Class III Milk Futures

                        "ICE_RS1",  # Canola Futures, Continuous Contract
                        "ICE_GO1",  # Gas Oil Futures, Continuous Contract
                        "CME_RB2",  # Gasoline Futures, Continuous Contract
                        "CME_KW2",  # Wheat Kansas, Continuous Contract
                        "ICE_WT1",  # WTI Crude Futures, Continuous Contract

                        "ICE_CC",  # Cocoa Futures, Continuous Contract 
                        "ICE_CT",  # Cotton No. 2 Futures, Continuous Contract
                        "ICE_KC",  # Coffee C Futures, Continuous Contract
                        "ICE_O",   # Heating Oil Futures, Continuous Contract
                        "ICE_OJ",  # Orange Juice Futures, Continuous Contract
                        "ICE_SB",  # Sugar No. 11 Futures, Continuous Contract
                        ]
        
        # True -> Quantpedia data
        # False -> Quandl free data
        self.use_quantpedia_data = True
        
        if self.use_quantpedia_data:
            for symbol in self.symbols:
                sym = symbol + '1'
                data = self.AddData(QuantpediaFutures, sym, Resolution.Daily)
                
                data.SetLeverage(5)
                data.SetFeeModel(CustomFeeModel(self))
        
        self.symbols2 = ['CHRIS/' + x for x in self.symbols]
        for symbol in self.symbols2:
            sym1 = symbol + '1'
            data = self.AddData(QuandlFutures, sym1, Resolution.Daily)
            if not self.use_quantpedia_data:
                data.SetLeverage(5)
                data.SetFeeModel(CustomFeeModel(self))
        
            sym2 = symbol + '2'
            self.AddData(QuandlFutures, sym2, Resolution.Daily)
                
        self.Schedule.On(self.DateRules.MonthStart(self.symbols2[0] + '1'), self.TimeRules.AfterMarketOpen(self.symbols2[0] + '1'), self.Rebalance)

    def Rebalance(self):
        # Roll return calc.
        roll_return = {}
        for symbol_index in range(0, len(self.symbols2)):
            symbol = self.symbols2[symbol_index]
            sym1 = symbol + '1'
            sym2 = symbol + '2'

            traded_symbol = ''
            if self.use_quantpedia_data: 
                traded_symbol = self.symbols[symbol_index] + '1'
            else: 
                traded_symbol = sym1
            
            price1 = self.Securities[sym1].Price
            price2 = self.Securities[sym2].Price
            if price1 != 0 and price2 != 0:
                roll_return[traded_symbol] = price1 / price2 - 1

        # Roll return sorting.
        long = []
        short = []
        if len(roll_return) != 0:
            sorted_by_roll = sorted(roll_return.items(), key=lambda x: x[1], reverse = True)
            quintile = int(len(sorted_by_roll) / 5)
            long = [x[0] for x in sorted_by_roll[:quintile]]
            short = [x[0] for x in sorted_by_roll[-quintile:]]
        
        # Trade execution.
        invested = [x.Key.Value for x in self.Portfolio if x.Value.Invested]
        for symbol in invested:
            if symbol not in long + short:
                self.Liquidate(symbol)

        for symbol in long:
            self.SetHoldings(symbol, 1 / len(long))
        
        for symbol in short:
            self.SetHoldings(symbol, -1 / len(short))

# Quantpedia data.
# NOTE: IMPORTANT: Data order must be ascending (datewise)
class QuantpediaFutures(PythonData):
    def GetSource(self, config, date, isLiveMode):
        return SubscriptionDataSource("data.quantpedia.com/backtesting_data/futures/{0}.csv".format(config.Symbol.Value), SubscriptionTransportMedium.RemoteFile, FileFormat.Csv)

    def Reader(self, config, line, date, isLiveMode):
        data = QuantpediaFutures()
        data.Symbol = config.Symbol
        
        if not line[0].isdigit(): return None
        split = line.split(';')
        
        data.Time = datetime.strptime(split[0], "%d.%m.%Y") + timedelta(days=1)
        data['back_adjusted'] = float(split[1])
        data['spliced'] = float(split[2])
        data.Value = float(split[1])

        return data

# Quandl free data
class QuandlFutures(PythonQuandl):
    def __init__(self):
        self.ValueColumnName = "settle"
        
# Custom fee model.
class CustomFeeModel(FeeModel):
    def GetOrderFee(self, parameters):
        fee = parameters.Security.Price * parameters.Order.AbsoluteQuantity * 0.00005
        return OrderFee(CashAmount(fee, "USD"))