Overall Statistics
Total Trades
28
Average Win
0.47%
Average Loss
-0.51%
Compounding Annual Return
37.414%
Drawdown
2.700%
Expectancy
0.438
Net Profit
5.547%
Sharpe Ratio
2.586
Probabilistic Sharpe Ratio
72.601%
Loss Rate
25%
Win Rate
75%
Profit-Loss Ratio
0.92
Alpha
0.28
Beta
0.096
Annual Standard Deviation
0.119
Annual Variance
0.014
Information Ratio
0.088
Tracking Error
0.145
Treynor Ratio
3.222
Total Fees
$33.71
from Selection.FundamentalUniverseSelectionModel import FundamentalUniverseSelectionModel

class NadionUncoupledPrism(QCAlgorithm):

    def Initialize(self):
        
        self.SetStartDate(2012, 8, 1)
        self.SetEndDate(2012, 10, 1)
        self.SetCash(100000)
        
        self.symbols = [Symbol.Create("SPY", SecurityType.Equity, Market.USA) for t in ['SPY', 'QQQ', 'TLT'] ]
        
        self.AddUniverseSelection(TechnologyUniverseModule())
        self.UniverseSettings.Resolution = Resolution.Daily
        
        self.SetWarmUp(200)
        

    def OnData(self, data):
        if self.IsWarmingUp:
            return
        
        self.Plot("Removed", "Len", len([s for s in self.changes.RemovedSecurities]))

        for security in self.removed:
            if security.Invested:
                self.Liquidate(security.Symbol)
        self.removed = []
                
                
        for security in self.added:
            if not security.Invested and security.Symbol not in self.symbols:
                self.SetHoldings(security.Symbol, 0.05)
        self.added = []
        


    def OnSecuritiesChanged(self, changes):
        self.Log("OnSecuritiesChanged")
        
        self.added = changes.AddedSecurities
        self.removed = changes.RemovedSecurities
        
        self.changes = changes


class TechnologyUniverseModule(FundamentalUniverseSelectionModel):
    
    #This module selects the most liquid stocks listed on the Nasdaq Stock Exchange.
    
    def __init__(self, filterFineData = True, universeSettings = None, securityInitializer = None):
        #Initializes a new default instance of the TechnologyUniverseModule
        super().__init__(filterFineData, universeSettings, securityInitializer)
        self.numberOfSymbolsCoarse = 1000
        self.numberOfSymbolsFine = 20
        self.dollarVolumeBySymbol = {}
        self.lastMonth = -1


    def SelectCoarse(self, algorithm, coarse):
        if algorithm.Time.month == self.lastMonth: 
            return Universe.Unchanged
            

        coarse = sorted([x for x in coarse if x.HasFundamentalData and x.Price > 10],
                               key = lambda x: x.DollarVolume, reverse=True)[:self.numberOfSymbolsCoarse]
        for security in coarse:
            symbol = security.Symbol
            self.dollarVolumeBySymbol[symbol] = security.DollarVolume
        
        # If no security has met the QC500 criteria, the universe is unchanged.
        if len(self.dollarVolumeBySymbol) == 0:
            return Universe.Unchanged
        
        return list(self.dollarVolumeBySymbol.keys())


    def SelectFine(self, algorithm, fine):

        sortedByDollarVolume =  sorted([x for x in fine if x.CompanyReference.CountryId == "USA" \
                                and x.CompanyReference.PrimaryExchangeID == "NAS" \
                                and x.CompanyReference.IndustryTemplateCode == "N" \
                                and (algorithm.Time - x.SecurityReference.IPODate).days > 180], \
            key = lambda x: self.dollarVolumeBySymbol[x.Symbol], reverse=True)

        #Industry Template Codes: N=Normal (Manufacturing), M=Mining, U=Utility, T=Transportation, B=Bank, I=Insurance  
        if len(sortedByDollarVolume) == 0:
            return Universe.Unchanged
            
        self.lastMonth = algorithm.Time.month
        
        #algorithm.Log()
        return [x.Symbol for x in sortedByDollarVolume[:self.numberOfSymbolsFine]]
from Selection.FundamentalUniverseSelectionModel import FundamentalUniverseSelectionModel

class TechnologyUniverseModule(FundamentalUniverseSelectionModel):
    '''
    This module selects the most liquid stocks listed on the Nasdaq Stock Exchange.
    '''
    def __init__(self, filterFineData = True, universeSettings = None, securityInitializer = None):
        '''Initializes a new default instance of the TechnologyUniverseModule'''
        super().__init__(filterFineData, universeSettings, securityInitializer)
        self.numberOfSymbolsCoarse = 1000
        self.numberOfSymbolsFine = 100
        self.dollarVolumeBySymbol = {}
        self.lastMonth = -1

    def SelectCoarse(self, algorithm, coarse):
        '''
        Performs a coarse selection:
        
        -The stock must have fundamental data
        -The stock must have positive previous-day close price
        -The stock must have positive volume on the previous trading day
        '''
        if algorithm.Time.month == self.lastMonth: 
            return Universe.Unchanged

        sortedByDollarVolume = sorted([x for x in coarse if x.HasFundamentalData and x.Volume > 0 and x.Price > 0],
            key = lambda x: x.DollarVolume, reverse=True)[:self.numberOfSymbolsCoarse]

        self.dollarVolumeBySymbol = {x.Symbol:x.DollarVolume for x in sortedByDollarVolume}
        
        # If no security has met the QC500 criteria, the universe is unchanged.
        if len(self.dollarVolumeBySymbol) == 0:
            return Universe.Unchanged

        return list(self.dollarVolumeBySymbol.keys())

    def SelectFine(self, algorithm, fine):
        '''
        Performs a fine selection:
        
        -The company's headquarter must in the U.S.
        -The stock must be traded on the NASDAQ stock exchange
        -The stock must be in the Industry Template Code catagory N
        -At least half a year since its initial public offering
        '''
        # Filter stocks and sort on dollar volume
        sortedByDollarVolume = sorted([x for x in fine if x.CompanyReference.CountryId == "USA"
                                        and x.CompanyReference.PrimaryExchangeID == "NAS"
                                        and x.CompanyReference.IndustryTemplateCode == "N"
                                        and (algorithm.Time - x.SecurityReference.IPODate).days > 180],
            key = lambda x: self.dollarVolumeBySymbol[x.Symbol], reverse=True)

        if len(sortedByDollarVolume) == 0:
            return Universe.Unchanged
            
        self.lastMonth = algorithm.Time.month

        return [x.Symbol for x in sortedByDollarVolume[:self.numberOfSymbolsFine]]