Overall Statistics
Total Trades
21
Average Win
10.16%
Average Loss
-2.37%
Compounding Annual Return
18.822%
Drawdown
37.400%
Expectancy
0.320
Net Profit
5.932%
Sharpe Ratio
0.552
Probabilistic Sharpe Ratio
35.879%
Loss Rate
75%
Win Rate
25%
Profit-Loss Ratio
4.28
Alpha
0.294
Beta
0.557
Annual Standard Deviation
0.532
Annual Variance
0.283
Information Ratio
0.576
Tracking Error
0.511
Treynor Ratio
0.527
Total Fees
$600.09
Estimated Strategy Capacity
$6000.00
Lowest Capacity Asset
CERU VPLW2D47KBXH
from Execution.ImmediateExecutionModel import ImmediateExecutionModel
from Portfolio.EqualWeightingPortfolioConstructionModel import EqualWeightingPortfolioConstructionModel
from Risk.MaximumDrawdownPercentPerSecurity import MaximumDrawdownPercentPerSecurity

from UniverseSelectionModel import *
from EmaCrossAlphaModel import *

class GoldenCrossAlgo(QCAlgorithm):

    def Initialize(self):
        self.SetStartDate(2020, 2, 1)  # Set Start Date
        self.SetEndDate(2020, 6, 1)# Set Start Date
        self.SetCash(10000)  # Set Strategy Cash
        self.SetBenchmark("SPY")
        
        """
        self.AddEquity("CMCO", Resolution.Daily)
        self.AddEquity("AY", Resolution.Daily)
        self.AddEquity("CFB", Resolution.Daily)
        self.AddEquity("AGC", Resolution.Daily)
        self.AddEquity("DSWL", Resolution.Daily)
        self.AddEquity("DARE", Resolution.Daily)
        self.AddEquity("DLTR", Resolution.Daily)
        self.AddEquity("FLXN", Resolution.Daily)
        self.AddEquity("FORM", Resolution.Daily)
        self.AddEquity("AAME", Resolution.Daily)
        """
        self.AddEquity("DARE", Resolution.Daily)
       
        self.model = EmaCrossAlphaModel(50, 200, Resolution.Daily)
        self.AddAlpha(self.model)
    
        self.SetExecution(ImmediateExecutionModel())

        self.SetPortfolioConstruction(EqualWeightingPortfolioConstructionModel())

        self.SetRiskManagement(MaximumDrawdownPercentPerSecurity(0.02))

        self.__numberOfSymbols = 100
        self.__numberOfSymbolsFine = 5
        # self.SetUniverseSelection(EverythingUniverseSelectionModel(True, Resolution.Hour))
        
        self.Log("Initialize complete")


    def OnData(self, data):
        '''OnData event is the primary entry point for your algorithm. Each new data point will be pumped in here.
            Arguments:
                data: Slice object keyed by symbol containing the stock data
        '''
        
        self.Log ("OnData() data.len=" + str(len(data))+ ", model.symbolDataBySymbol.len=" + str(len(self.model.symbolDataBySymbol)))

        # if not self.Portfolio.Invested:
        #    self.SetHoldings("SPY", 1)
# QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
# Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from AlgorithmImports import *
from Selection.FundamentalUniverseSelectionModel import FundamentalUniverseSelectionModel
from itertools import groupby
from math import ceil

class EverythingUniverseSelectionModel(FundamentalUniverseSelectionModel):
    '''Defines the QC500 universe as a universe selection model for framework algorithm
    For details: https://github.com/QuantConnect/Lean/pull/1663'''

    def __init__(self, filterFineData = True, universeSettings = None):
        '''Initializes a new default instance of the QC500UniverseSelectionModel'''
        super().__init__(filterFineData, universeSettings)
        self.numberOfSymbolsCoarse = 1000
        self.numberOfSymbolsFine = 20 #500
        self.dollarVolumeBySymbol = {}
        self.lastMonth = -1

    def SelectCoarse(self, algorithm, coarse):
        '''Performs coarse selection for the QC500 constituents.
        The stocks 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.
        # A new selection will be attempted on the next trading day as self.lastMonth is not updated
        if len(self.dollarVolumeBySymbol) == 0:
            return Universe.Unchanged

        # return the symbol objects our sorted collection
        return list(self.dollarVolumeBySymbol.keys())

    def SelectFine(self, algorithm, fine):
        '''Performs fine selection for the QC500 constituents
        The company's headquarter must in the U.S.
        The stock must be traded on either the NYSE or NASDAQ
        At least half a year since its initial public offering
        The stock's market cap must be greater than 500 million'''

        sortedBySector = sorted([x for x in fine if x.CompanyReference.CountryId == "USA"
                                        and x.CompanyReference.PrimaryExchangeID in ["NYS","NAS"]
                                        and (algorithm.Time - x.SecurityReference.IPODate).days > 180
                                        and x.MarketCap > 5e8],
                               key = lambda x: x.CompanyReference.IndustryTemplateCode)

        count = len(sortedBySector)

        # If no security has met the QC500 criteria, the universe is unchanged.
        # A new selection will be attempted on the next trading day as self.lastMonth is not updated
        if count == 0:
            return Universe.Unchanged

        # Update self.lastMonth after all QC500 criteria checks passed
        self.lastMonth = algorithm.Time.month

        percent = self.numberOfSymbolsFine / count
        sortedByDollarVolume = []

        # select stocks with top dollar volume in every single sector
        for code, g in groupby(sortedBySector, lambda x: x.CompanyReference.IndustryTemplateCode):
            y = sorted(g, key = lambda x: self.dollarVolumeBySymbol[x.Symbol], reverse = True)
            c = ceil(len(y) * percent)
            sortedByDollarVolume.extend(y[:c])

        sortedByDollarVolume = sorted(sortedByDollarVolume, key = lambda x: self.dollarVolumeBySymbol[x.Symbol], reverse=True)
        return [x.Symbol for x in sortedByDollarVolume[:self.numberOfSymbolsFine]]
# QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
# Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from AlgorithmImports import *

class EmaCrossAlphaModel(AlphaModel):
    '''Alpha model that uses an EMA cross to create insights'''

    def __init__(self,
                 fastPeriod = 12,
                 slowPeriod = 26,
                 resolution = Resolution.Daily):
        '''Initializes a new instance of the EmaCrossAlphaModel class
        Args:
            fastPeriod: The fast EMA period
            slowPeriod: The slow EMA period'''
        self.fastPeriod = fastPeriod
        self.slowPeriod = slowPeriod
        self.resolution = resolution
        self.predictionInterval = Time.Multiply(Extensions.ToTimeSpan(resolution), fastPeriod)
        self.symbolDataBySymbol = {}

        resolutionString = Extensions.GetEnumString(resolution, Resolution)
        self.Name = '{}({},{},{})'.format(self.__class__.__name__, fastPeriod, slowPeriod, resolutionString)


    def Update(self, algorithm, data):
        '''Updates this alpha model with the latest data from the algorithm.
        This is called each time the algorithm receives data for subscribed securities
        Args:
            algorithm: The algorithm instance
            data: The new data available
        Returns:
            The new insights generated'''
            
        algorithm.Log("EmaCrossAlphaModel.Update() - data.len=" + str(len(data)) + ", symbolDataBySymbol.len=" + str(len(self.symbolDataBySymbol)))

        insights = []
        for symbol, symbolData in self.symbolDataBySymbol.items():
            algorithm.Log(str(algorithm.Time) + " " + str(symbolData.Symbol) + " symbolData.Fast.IsReady=" + str(symbolData.Fast.IsReady) + ", symbolData.Slow.IsReady=" + str(symbolData.Slow.IsReady))
            if symbolData.Fast.IsReady and symbolData.Slow.IsReady:

                if symbolData.FastIsOverSlow:
                    if symbolData.Slow > symbolData.Fast:
                        insights.append(Insight.Price(symbolData.Symbol, self.predictionInterval, InsightDirection.Down))

                elif symbolData.SlowIsOverFast:
                    if symbolData.Fast > symbolData.Slow:
                        insights.append(Insight.Price(symbolData.Symbol, self.predictionInterval, InsightDirection.Up))

            symbolData.FastIsOverSlow = symbolData.Fast > symbolData.Slow

        algorithm.Log("returning insights, len=" + str(len(insights)))
        for x in insights:
            algorithm.Log("     " + str(x.Symbol) + " direction=" + str(x.Direction))
        return insights

    def OnSecuritiesChanged(self, algorithm, changes):
        '''Event fired each time the we add/remove securities from the data feed
        Args:
            algorithm: The algorithm instance that experienced the change in securities
            changes: The security additions and removals from the algorithm'''
            
        algorithm.Log("EmaCrossAlphaModel.OnSecuritiesChanged()")
            
        for added in changes.AddedSecurities:
            symbolData = self.symbolDataBySymbol.get(added.Symbol)
            if symbolData is None:
                algorithm.Log("AddedSecurities new " + str(added.Symbol))
                symbolData = SymbolData(added)
                
                # create fast/slow EMAs
                symbolData.Fast = algorithm.EMA(added.Symbol, self.fastPeriod, self.resolution)
                algorithm.WarmUpIndicator(added.Symbol, symbolData.Fast, self.resolution)
                
                symbolData.Slow = algorithm.EMA(added.Symbol, self.slowPeriod, self.resolution)
                algorithm.WarmUpIndicator(added.Symbol, symbolData.Slow, self.resolution)

                self.symbolDataBySymbol[added.Symbol] = symbolData
            else:
                # a security that was already initialized was re-added, reset the indicators
                algorithm.Log("AddedSecurities reset " + str(added.Symbol))
                symbolData.Fast.Reset()
                symbolData.Slow.Reset()


class SymbolData:
    '''Contains data specific to a symbol required by this model'''
    def __init__(self, security):
        self.Security = security
        self.Symbol = security.Symbol
        self.Fast = None
        self.Slow = None

        # True if the fast is above the slow, otherwise false.
        # This is used to prevent emitting the same signal repeatedly
        self.FastIsOverSlow = False

    @property
    def SlowIsOverFast(self):
        return not self.FastIsOverSlow