Overall Statistics Total Trades8Average Win0%Average Loss0%Compounding Annual Return0%Drawdown0%Expectancy0Net Profit0%Sharpe Ratio0Probabilistic Sharpe Ratio0%Loss Rate0%Win Rate0%Profit-Loss Ratio0Alpha0Beta0Annual Standard Deviation0Annual Variance0Information Ratio0Tracking Error0Treynor Ratio0Total Fees$180.62Estimated Strategy Capacity$11.00
class EmaCrossUniverseSelectionAlgorithm(QCAlgorithm):

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(2020,1,1)  #Set Start Date
self.SetEndDate(2021,3,1)    #Set End Date
self.SetCash(100000)           #Set Strategy Cash

self.UniverseSettings.Resolution = Resolution.Minute
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.sells = []

def OnData(self, data):
if data.ContainsKey(symbol) and data[symbol] is not None:
self.SetHoldings(symbol, 0.1)

sells = self.sells.copy()
for symbol in sells:
if data.ContainsKey(symbol) and data[symbol] is not None:
self.Liquidate(symbol)
self.sells.remove(symbol)

self.Quit()

# 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, self)

# Updates the SymbolData object with current EOD price
avg = self.averages[cf.Symbol]

# 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[:self.coarse_count]:
#    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[:self.coarse_count] ]

# this event fires whenever we have changes to our universe
def OnSecuritiesChanged(self, changes):
for security in changes.RemovedSecurities:
self.sells.append(security.Symbol)

class SymbolData(object):
def __init__(self, symbol, algorithm):
self.symbol = symbol
self.tolerance = 1.01
self.fast = ExponentialMovingAverage(5)
self.slow = ExponentialMovingAverage(10)
self.is_uptrend = False
self.scale = 0

## Warm up EMAs
history = algorithm.History(symbol, self.slow.WarmUpPeriod, Resolution.Daily)
if history.empty or 'close' not in history.columns:
return
closes = history.loc[symbol].close
for time, close in closes.iteritems():
self.fast.Update(time, close)
self.slow.Update(time, close)

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 * self.tolerance

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