Overall Statistics
Total Trades
0
Average Win
0%
Average Loss
0%
Compounding Annual Return
0%
Drawdown
0%
Expectancy
0
Net Profit
0%
Sharpe Ratio
0
Loss Rate
0%
Win Rate
0%
Profit-Loss Ratio
0
Alpha
0
Beta
0
Annual Standard Deviation
0
Annual Variance
0
Information Ratio
0
Tracking Error
0
Treynor Ratio
0
Total Fees
$0.00
from clr import AddReference
AddReference("System")
AddReference("QuantConnect.Algorithm")
AddReference("QuantConnect.Indicators")
AddReference("QuantConnect.Common")
AddReference("QuantConnect.Algorithm.Framework")

from System import *
from QuantConnect import *
from QuantConnect.Data import *
from QuantConnect.Algorithm import *
from QuantConnect.Indicators import *
from System.Collections.Generic import List
import pandas as pd


class PublicHelp(QCAlgorithm):

    def Initialize(self):
    
        self.SetStartDate(2017,1,1) #Set Start Date
        self.SetEndDate(2017, 1, 20) # Set End Date
        #self.SetEndDate(datetime.now().date() - timedelta(1)) #Set End Date
        #self.SetEndDate(2013,1,1) #Set End Date
        self.SetCash(150000) #Set Strategy Cash
        
        self.UniverseSettings.Resolution = Resolution.Hour
        
        self.averages = {};
        
        self.AddEquity("SPY", Resolution.Hour)
        self.AddUniverse(self.CoarseSelectionFunction)
        
    #Universe Filter
    # sort the data by volume and price, apply the moving average crossver, and take the top 24 sorted results based on breakout magnitude
    def CoarseSelectionFunction(self, coarse):
    
        filtered = [ x for x in coarse if (x.DollarVolume > 50000000) ]
        
        # We are going to use a dictionary to refer the object that will keep the moving averages
        for cf in filtered:
            if cf.Symbol not in self.averages:
                self.averages[cf.Symbol] = SymbolData(cf.Symbol, self)
        
            # Updates the SymbolData object with current EOD price
            avg = self.averages[cf.Symbol]
            history = self.History(cf.Symbol, 16)
            if str(cf.Symbol) in history.index:
                avg.WarmUpIndicators(history.loc[str(cf.Symbol)])
                avg.update(cf.EndTime, cf.AdjustedPrice)
        
        # Filter the values of the dict: we only want up-trending securities
        values = list(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=True)
        
        for x in values[:200]:
            self.Log('symbol: ' + str(x.symbol.Value) + ' scale: ' + str(x.scale))
        
        # we need to return only the symbol objects
        return [ x.symbol for x in values[:200] ]
    
    
    # this event fires whenever we have changes to our universe
    def OnSecuritiesChanged(self, changes):
        self.changes = changes
        
        # liquidate removed securities
        for security in changes.RemovedSecurities:
            if security.Invested:
                self.Liquidate(security.Symbol)
    
    #EMA Crossover Class
class SymbolData(object):
    
    def __init__(self, symbol, algo):
        self.symbol = symbol
        self.fast = ExponentialMovingAverage(50)
        self.slow = ExponentialMovingAverage(200)
        self.is_uptrend = False
        self.scale = None
        self.algo = algo
    
    def update(self, time, value):
        if self.fast.Update(time, value) and self.slow.Update(time, value):
            fast = self.fast.Current.Value
            slow = self.slow.Current.Value
            self.is_uptrend = (fast / slow) > 1.00
    
        if self.is_uptrend:
            self.scale = (fast - slow) / ((fast + slow) / 2.0)
    
    def WarmUpIndicators(self, history):
        for index in history.index:
            self.fast.Update(index, history.loc[index].close)
            self.slow.Update(index, history.loc[index].close)