Overall Statistics |
Total Trades
31
Average Win
0%
Average Loss
0%
Compounding Annual Return
-46.509%
Drawdown
6.400%
Expectancy
0
Net Profit
-5.337%
Sharpe Ratio
-4.661
Loss Rate
0%
Win Rate
0%
Profit-Loss Ratio
0
Alpha
-0.837
Beta
18.395
Annual Standard Deviation
0.113
Annual Variance
0.013
Information Ratio
-4.814
Tracking Error
0.113
Treynor Ratio
-0.029
Total Fees
$103.52
|
# https://www.quantconnect.com/forum/discussion/2607/important-universe-selection-in-python-algorithms from clr import AddReference AddReference("System") AddReference("QuantConnect.Algorithm") AddReference("QuantConnect.Indicators") AddReference("QuantConnect.Common") 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 decimal as d class EmaCrossUniverseSelectionAlgorithm(QCAlgorithm): def Initialize(self): self.SetCash(100000) self.SetStartDate(2017,10,1) self.SetEndDate( 2017,11,1) self.UniverseSettings.Resolution = Resolution.Daily self.UniverseSettings.Leverage = 2 self.coarse_max = 10 self.averages = {} self.AddUniverse(self.CoarseSelectionFunction) def CoarseSelectionFunction(self, coarse): for cf in coarse: if cf.Symbol not in self.averages: self.averages[cf.Symbol] = SymbolData(cf.Symbol) # Update the SymbolData object with current EOD price avg = self.averages[cf.Symbol] avg.update(cf) prc = 0.0 if self.Securities.ContainsKey(cf.Symbol): prc = self.Securities[cf.Symbol].Price sma1 = self.averages[cf.Symbol].sma1.Current.Value sma2 = self.averages[cf.Symbol].sma2.Current.Value sma1 = sma1 if sma1 else 0.0 sma2 = sma2 if sma2 else 0.0 diff = sma1 - sma2 self.Log('{} prc {} sma1 {} sma2 {} diff {} '.format(cf.Symbol, '%.2f' % prc, '%.3f' % sma1, '%.3f' % sma2, '%.5f' % diff)) ''' TODO Find out why log is this limited ... 2017-10-11 00:00:00 Z UYE69C59FN8L prc 42.14 sma1 41.987 sma2 41.752 diff 0.23467 2017-10-11 00:00:00 Z UYE69C59FN8L prc 42.14 sma1 41.810 sma2 41.829 diff -0.01900 2017-10-12 00:00:00 Z UYE69C59FN8L prc 41.87 sma1 41.633 sma2 41.783 diff -0.14967 2017-10-12 00:00:00 Z UYE69C59FN8L prc 41.87 sma1 41.433 sma2 41.745 diff -0.31167 2017-10-13 00:00:00 Z UYE69C59FN8L prc 41.42 sma1 41.463 sma2 41.747 diff -0.28367 2017-10-13 00:00:00 Z UYE69C59FN8L prc 41.42 sma1 41.467 sma2 41.741 diff -0.27433 2017-10-14 00:00:00 Z UYE69C59FN8L prc 41.70 sma1 41.550 sma2 41.680 diff -0.13000 2017-10-14 00:00:00 Z UYE69C59FN8L prc 41.70 sma1 41.460 sma2 41.634 diff -0.17400 2017-10-17 00:00:00 Z UYE69C59FN8L prc 41.52 sma1 41.363 sma2 41.534 diff -0.17067 2017-10-17 00:00:00 Z UYE69C59FN8L prc 41.52 sma1 41.213 sma2 41.446 diff -0.23267 2017-10-17 00:00:00 Z UYE69C59FN8L prc 41.14 sma1 41.283 sma2 41.423 diff -0.13967 2017-10-17 00:00:00 Z UYE69C59FN8L prc 41.14 sma1 41.407 sma2 41.413 diff -0.00633 Place diff's in a sortable object Sort them Select top or bottom coarse_max ''' # Filter the values of the dict: wait for indicator to be ready vals = filter(lambda x: x.is_ready, self.averages.values()) # need to return only the symbol objects return [ x.symbol for x in vals ] # Error: 'filter' object is not subscriptable return [ x.symbol for x in vals[:self.coarse_max] ] # this event fires whenever have changes to universe def OnSecuritiesChanged(self, changes): # want n% allocation in each security in universe for security in changes.AddedSecurities: self.SetHoldings(security.Symbol, 0.1) return # liquidate removed securities for security in changes.RemovedSecurities: if security.Invested: self.Liquidate(security.Symbol) class SymbolData(object): def __init__(self, symbol): self.symbol = symbol self.sma1 = SimpleMovingAverage(3) self.sma2 = SimpleMovingAverage(10) self.is_ready = False def update(self, value): self.is_ready = self.sma1.Update(value.EndTime, value.Price) and self.sma2.Update(value.EndTime, value.Price)