| Overall Statistics |
|
Total Trades 8 Average Win 0% Average Loss 0% Compounding Annual Return 0% Drawdown 0% Expectancy 0 Net Profit 0% Sharpe Ratio 0 Probabilistic 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 $180.62 Estimated 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.AddUniverse(self.CoarseSelectionFunction)
self.buys = []
self.sells = []
def OnData(self, data):
buys = self.buys.copy()
for symbol in buys:
if data.ContainsKey(symbol) and data[symbol] is not None:
self.SetHoldings(symbol, 0.1)
self.buys.remove(symbol)
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]
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[: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)
for security in changes.AddedSecurities:
self.buys.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)