Overall Statistics
import numpy as np
from QuantConnect.Python import PythonQuandl
from QuantConnect.Data import SubscriptionDataSource
from QuantConnect.Python import PythonData
from datetime import date, timedelta, datetime

class WorldCup2(QCAlgorithm):
    
    def Initialize(self):
        self.SetStartDate(2018,5, 14)  
        self.SetEndDate(2018,7,12)   
        self.SetCash(100000)
        
        #find all data tickers
        self.adidas = "FSE/ADS_X" #adidas
        self.daimler = "FSE/DAI_X" #mercedes-benz (owner)
        self.dpost = "FSE/DPW_X" #deutche post
        self.luft = "FSE/LHA_X" #lufthansa
        self.sap = "FSE/SAP_X" #SAP
        self.aca = "EURONEXT/ACA" #credit agricole
        self.vaux = "EURONEXT/UG" #vauxhall (owner)
        self.mobi = "SSE/MOS" #mobistar
        self.volks = "FSE/VOW3_X" #volkswagen
        self.bmw = "FSE/BMW_X" #bmw
        self.edf = "EURONEXT/EDF" #edf
        self.carre = "EURONEXT/CA" #carrefour
        self.hein = "EURONEXT/HEIA" #heinekin
        self.nike = "NKE" #nike
        self.telefonica = "TEF" #telefonica
        self.coke = "KO"
        self.euro_coke = "CCE"
        self.carlsberg = "SSE/CBGB"
        self.ee = "BT"
        self.cogniz = "CTSH"
        self.ab = "BUD"
        
        #subscribe to all data
        self.AddEquity(self.ab, Resolution.Daily)
        self.AddEquity(self.coke, Resolution.Daily)
        self.AddEquity(self.euro_coke, Resolution.Daily)
        self.AddEquity(self.cogniz, Resolution.Daily)
        self.AddEquity(self.nike, Resolution.Daily)
        self.AddEquity(self.telefonica, Resolution.Daily)
        self.AddEquity(self.ee, Resolution.Daily)
        self.AddData(Frankfurt, self.adidas, Resolution.Daily, TimeZones.NewYork, True)
        self.AddData(Frankfurt, self.daimler, Resolution.Daily, TimeZones.NewYork, True)
        self.AddData(Frankfurt, self.dpost, Resolution.Daily, TimeZones.NewYork, True)
        self.AddData(Frankfurt, self.luft, Resolution.Daily, TimeZones.NewYork, True)
        self.AddData(Frankfurt, self.sap, Resolution.Daily, TimeZones.NewYork, True)
        self.AddData(Frankfurt, self.volks, Resolution.Daily, TimeZones.NewYork, True)
        self.AddData(Frankfurt, self.bmw, Resolution.Daily, TimeZones.NewYork, True)
        self.AddData(Boerse, self.carlsberg, Resolution.Daily, TimeZones.NewYork, True)
        self.AddData(Boerse, self.mobi, Resolution.Daily, TimeZones.NewYork, True)
        self.AddData(Euronext, self.aca, Resolution.Daily, TimeZones.NewYork, True)
        self.AddData(Euronext, self.vaux, Resolution.Daily, TimeZones.NewYork, True)
        self.AddData(Euronext, self.edf, Resolution.Daily, TimeZones.NewYork, True)
        self.AddData(Euronext, self.carre, Resolution.Daily, TimeZones.NewYork, True)
        self.AddData(Euronext, self.hein, Resolution.Daily, TimeZones.NewYork, True)
        
        self.AddData(Games, "WC", Resolution.Daily)
        self.game = self.Securities["WC"].Symbol
        
        #create a map of portfolios
        self.contenders = {}
        self.contenders["Germany"] = [self.adidas,self.daimler,self.dpost,self.luft,self.sap,self.euro_coke] 
        self.contenders["England"] = [self.vaux,self.carlsberg,self.nike,self.ee,self.cogniz]
        self.contenders["France"] = [self.aca,self.nike,self.mobi,self.volks,self.edf]
        self.contenders["Spain"] = [self.adidas,self.volks,self.hein,self.telefonica]
        self.contenders["Belgium"] = [self.adidas,self.bmw,self.carre,self.euro_coke,self.ab]

    def OnData(self, data):
        #conveniently all countries have adidas or nike sponsorships so this is the initial condition
        if not self.Portfolio[self.adidas].Invested or not self.Portfolio[self.nike].Invested:
            size = len(self.contenders)
            for country in self.contenders:
                allocation = float((1/size)/len(self.contenders[country]))*float(self.Portfolio.Cash)
                for sponsor in self.contenders[country]:
                    price = float(self.Securities[sponsor].Price)
                    quantity = int(allocation/price)
                    self.MarketOrder(sponsor,quantity)
        else:
            need_liquidate = False
            if not data.ContainsKey(self.game): return
            loser = data[self.game].Loser
            self.Debug(loser)
            if loser in self.contenders.keys():
                need_liquidate = True
                del self.contenders[loser]
            if need_liquidate == True:    
                self.Liquidate()
                size = len(self.contenders)
                for country in self.contenders:
                    allocation = float((1/size)/len(self.contenders[country]))*float(self.Portfolio.Cash)
                    for sponsor in self.contenders[country]:
                        price = float(self.Securities[sponsor].Price)
                        quantity = int(allocation/price)
                        self.MarketOrder(sponsor,quantity)
            
class Euronext(PythonQuandl):
    def __init__(self):
        self.ValueColumnName = "last"

class Boerse(PythonQuandl):
    def __init__(self):
        self.ValueColumnName = "previous day price"

class Frankfurt(PythonQuandl):
    def __init__(self):
        self.ValueColumnName = "close"
        
class Games(PythonData):

    def GetSource(self, config, date, isLiveMode):
        return SubscriptionDataSource("https://www.dropbox.com/s/pnpzpfsdpp5a6a0/loser.csv?dl=1", SubscriptionTransportMedium.RemoteFile)
    
    def Reader(self, config, line, date, isLiveMode):
        game = Games()
        game.Symbol = config.Symbol
        data = line.split(',')
        game.Time = datetime.strptime(data[0], '%Y-%m-%d') + timedelta(hours=20)
        game["Loser"] = data[1]
        return game