Overall Statistics
Total Trades
52
Average Win
2.35%
Average Loss
-0.21%
Compounding Annual Return
22.393%
Drawdown
49.200%
Expectancy
10.025
Net Profit
238.896%
Sharpe Ratio
0.867
Probabilistic Sharpe Ratio
27.912%
Loss Rate
8%
Win Rate
92%
Profit-Loss Ratio
10.98
Alpha
0.193
Beta
1.213
Annual Standard Deviation
0.347
Annual Variance
0.12
Information Ratio
1.055
Tracking Error
0.201
Treynor Ratio
0.248
Total Fees
$149.07
# Equal-weighted portfolio of stocks with different participation dates
# -----------------------------------------------
STOCKS = ['QQQ', 'XIV', 'ARKK']; TARGET_LEV = 1.0
# -----------------------------------------------
class Equal_weighted_portfolio(QCAlgorithm):

    def Initialize(self):
        
        self.SetStartDate(2008, 1, 1)  
        self.SetEndDate(2014, 1, 13)  
        self.InitCash = 100000
        self.SetCash(self.InitCash)  
        self.MKT = self.AddEquity("SPY", Resolution.Hour).Symbol
        self.mkt = []
        
        self.assets = [self.AddEquity(ticker, Resolution.Minute).Symbol for ticker in STOCKS]
        self.wt = {}
        self.real_wt = {}
        
        self.Schedule.On(self.DateRules.MonthStart(), self.TimeRules.AfterMarketOpen(self.MKT, 60), 
            self.trade)
            

    def trade(self):
        tradable = []
        
        for sec in self.assets:
            if self.CurrentSlice.ContainsKey(sec) and self.CurrentSlice[sec] is not None:
                tradable.append(sec)
            else:
                continue

        for sec in self.Portfolio.Keys:
            if sec not in tradable:                
                self.wt[sec] = 0.                
                
        for sec in tradable:        
            self.wt[sec] = TARGET_LEV / len(tradable)
                
        for sec, weight in self.wt.items():
            if weight == 0. and self.Portfolio[sec].IsLong:
                self.Liquidate(sec)
                
        for sec, weight in self.wt.items(): 
            if weight != 0.:
                self.SetHoldings(sec, weight)          
        
        
    def OnEndOfDay(self): 
        
        mkt_price = self.Securities[self.MKT].Close
        self.mkt.append(mkt_price)
        mkt_perf = self.InitCash * self.mkt[-1] / self.mkt[0] 
        self.Plot('Strategy Equity', self.MKT, mkt_perf)     
        
        account_leverage = self.Portfolio.TotalHoldingsValue / self.Portfolio.TotalPortfolioValue
        
        self.Plot('Holdings', 'leverage', round(account_leverage, 2))
        self.Plot('Holdings', 'Target Leverage', TARGET_LEV)

        for sec, weight in self.wt.items(): 
            self.real_wt[sec] = round(self.ActiveSecurities[sec].Holdings.Quantity * self.Securities[sec].Price / self.Portfolio.TotalPortfolioValue,4)
            # self.Log(str(self.Time) +" "+ str(self.Securities[sec].Symbol) +" "+ str(self.real_wt[sec]))
            self.Plot('Holdings', self.Securities[sec].Symbol, round(self.real_wt[sec], 4))