Overall Statistics
Total Trades
28
Average Win
0.44%
Average Loss
-0.46%
Compounding Annual Return
-76.044%
Drawdown
11.800%
Expectancy
-0.286
Net Profit
-11.775%
Sharpe Ratio
-8.625
Loss Rate
64%
Win Rate
36%
Profit-Loss Ratio
0.96
Alpha
-1.733
Beta
0.622
Annual Standard Deviation
0.157
Annual Variance
0.025
Information Ratio
-12.92
Tracking Error
0.152
Treynor Ratio
-2.182
Total Fees
$5007.74
from clr import AddReference
AddReference("System")
AddReference("QuantConnect.Algorithm")
AddReference("QuantConnect.Common")
AddReference("QuantConnect.Indicators")

from System import *
from QuantConnect import *
from QuantConnect.Algorithm import *
from QuantConnect.Indicators import *
from QuantConnect.Data.UniverseSelection import *

class SmaCrossUniverseSelectionAlgorithm(QCAlgorithm):
    '''Provides an example where WarmUpIndicator method is used to warm up indicators
    after their security is added and before (Universe Selection scenario)'''

    count = 10;
    tolerance = 0.01
    targetPercent = 1 / count
    averages = dict()

    def Initialize(self):

        self.UniverseSettings.Leverage = 2
        self.UniverseSettings.Resolution = Resolution.Daily

        self.SetStartDate(2018, 1, 1)
        self.SetEndDate(2018, 2, 1)
        self.SetCash(1000000)

        self.EnableAutomaticIndicatorWarmUp = True

        #ibm = self.AddEquity("IBM", Resolution.Hour).Symbol
        #ibmSma = self.SMA(ibm, 40)
        #self.Log(f"{ibmSma.Name}: {ibmSma.Current.Time} - {ibmSma}. IsReady? {ibmSma.IsReady}")

        self.AddUniverse(self.CoarseSmaSelector)

        # Since the indicators are ready, we will receive error messages
        # reporting that the algorithm manager is trying to add old information
        self.SetWarmUp(10)

    def CoarseSmaSelector(self, coarse):

        score = dict()
        for cf in coarse:
            if not cf.HasFundamentalData:
               continue
            symbol = cf.Symbol
            price = cf.AdjustedPrice
            # grab the SMA instance for this symbol
            avg = self.averages.setdefault(symbol,
                self.WarmUpIndicator(symbol, SimpleMovingAverage(100), Resolution.Daily))
            # Update returns true when the indicators are ready, so don't accept until they are
            if avg.Update(cf.EndTime, price):
               value = avg.Current.Value
               # only pick symbols who have their price over their 100 day sma
               if value > price * self.tolerance:
                    score[symbol] = (value - price) / ((value + price) / 2)

        # prefer symbols with a larger delta by percentage between the two averages
        sortedScore = sorted(score.items(), key=lambda kvp: kvp[1], reverse=True)
        return [x[0] for x in sortedScore[:self.count]]

    def OnSecuritiesChanged(self, changes):
        for security in changes.RemovedSecurities:
            if security.Invested:
                self.Liquidate(security.Symbol)

        for security in changes.AddedSecurities:
            self.SetHoldings(security.Symbol, self.targetPercent)