Overall Statistics
Total Trades
4303
Average Win
0.19%
Average Loss
-0.19%
Compounding Annual Return
-4.139%
Drawdown
26.600%
Expectancy
-0.063
Net Profit
-11.941%
Sharpe Ratio
-0.233
Loss Rate
53%
Win Rate
47%
Profit-Loss Ratio
0.98
Alpha
-0.11
Beta
0.593
Annual Standard Deviation
0.118
Annual Variance
0.014
Information Ratio
-1.536
Tracking Error
0.109
Treynor Ratio
-0.046
Total Fees
$26938.36
from System.Collections.Generic import List
from QuantConnect.Data.UniverseSelection import *
import decimal as d

class EmaCrossUniverseSelectionAlgorithm(QCAlgorithm):
    '''In this algorithm we demonstrate how to define a universe as a combination of use the coarse fundamental data and fine fundamental data'''
    def Initialize(self):
        '''Initialise the data and resolution required, as well as the cash and start-end dates for your algorithm. All algorithms must initialized.'''
        
        self.SetStartDate(2012,01,01)  #Set Start Date
        self.SetEndDate(2015,01,03)    #Set End Date
        self.SetCash(100000)           #Set Strategy Cash
        
        self.UniverseSettings.Resolution = Resolution.Daily        
        self.UniverseSettings.Leverage = 2        

        self.coarse_count = 10
        self.averages = { };

        # this add universe method accepts two parameters:
        # - coarse selection function: accepts an IEnumerable<CoarseFundamental> and returns an IEnumerable<Symbol>
        self.AddUniverse(self.CoarseSelectionFunction)
        

    # sort the data by daily dollar volume and take the top 'NumberOfSymbols'
    def CoarseSelectionFunction(self, coarse):
        
        # We are going to use a dictionary to refer the object that will keep the moving averages 
        for cf in coarse:
            if cf.Symbol not in self.averages: 
                self.averages[cf.Symbol] = SymbolData(cf.Symbol)

            # Updates the SymbolData object with current EOD price
            avg = self.averages[cf.Symbol]
            avg.update(cf.EndTime, cf.Price)

        # Filter the values of the dict: we only want up-trending securities 
        values = filter(lambda x: x.is_uptrend, self.averages.values())
        
        # Sorts the values of the dict: we want those with greater difference between the moving averages
        values.sort(key=lambda x: x.scale, reverse=False)
        
        # we need to return only the symbol objects
        list = List[Symbol]()
        for x in values[:self.coarse_count]:
            list.Add(x.symbol)

        return list

    # this event fires whenever we have changes to our universe
    def OnSecuritiesChanged(self, changes):
        # liquidate removed securities
        for security in changes.RemovedSecurities:
            if security.Invested:
                self.Liquidate(security.Symbol)
         
        # we want 20% allocation in each security in our universe
        for security in changes.AddedSecurities:
            self.SetHoldings(security.Symbol, 0.2)


class SymbolData(object):
    def __init__(self, symbol):
        self.symbol = symbol
        self.tolerance = d.Decimal(1.01)
        self.fast = ExponentialMovingAverage(100)
        self.slow = ExponentialMovingAverage(300)
        self.is_uptrend = False
        self.scale = 0

    def update(self, time, value):
        datapoint = IndicatorDataPoint(time, value)
        
        if self.fast.Update(datapoint) and self.slow.Update(datapoint):
            fast = self.fast.Current.Value
            slow = self.slow.Current.Value
            self.is_uptrend = fast > slow * self.tolerance

        if self.is_uptrend:
            self.scale = (fast - slow) / ((fast + slow) / 2)