Overall Statistics |
Total Trades 8 Average Win 0.41% Average Loss 0% Compounding Annual Return 798.757% Drawdown 0.800% Expectancy 0 Net Profit 2.436% Sharpe Ratio 9.538 Loss Rate 0% Win Rate 100% Profit-Loss Ratio 0 Alpha 0 Beta 110.715 Annual Standard Deviation 0.128 Annual Variance 0.016 Information Ratio 9.461 Tracking Error 0.128 Treynor Ratio 0.011 Total Fees $9.63 |
import pandas as pd class CoarseFundamentalTop3Algorithm(QCAlgorithm): def Initialize(self): self.SetStartDate(2019,6,4) self.SetEndDate(2019,6,8) self.SetCash(50000) self.UniverseSettings.Resolution = Resolution.Daily self.AddUniverse(self.CoarseSelectionFunction, self.SelectFine) self.IndustryCode = {} self.__numberOfSymbols = 3 self.__numberOfSymbolsFine = 3 self._changes = None def OnData(self, data): if self._changes is None: return for security in self._changes.RemovedSecurities: if security.Invested: self.Liquidate(security.Symbol) for security in self._changes.AddedSecurities: self.SetHoldings(security.Symbol, 1 / self.__numberOfSymbols) self._changes = None def OnSecuritiesChanged(self, changes): self._changes = changes def CoarseSelectionFunction(self, coarse): CoarseWithFundamental = [x for x in coarse if x.HasFundamentalData] self.Log(coarse[-1].Time) sortedByVolume = sorted(CoarseWithFundamental, key=lambda x: x.DollarVolume, reverse=True) for x in sortedByVolume[:self.__numberOfSymbols]: self.Log(x.Symbol.Value) self.Log(x.DollarVolume) return [ x.Symbol for x in sortedByVolume[:self.__numberOfSymbols] ] def SelectFine(self, fine): IndustryCode = {} for x in fine: if x.AssetClassification.MorningstarIndustryGroupCode not in IndustryCode: IndustryCode[x.AssetClassification.MorningstarIndustryGroupCode] = [x.Symbol] else: IndustryCode[x.AssetClassification.MorningstarIndustryGroupCode].append(x.Symbol) history_price = {} pre_symbols = [] for value in IndustryCode.values(): for k in set(value): pre_symbols.append(self.AddEquity(str(k.Value), Resolution.Minute).Symbol) symbols = [] for value in IndustryCode.values(): for j in set(value): hist = self.History(j, datetime(2019, 6, 4, 0), datetime(2019, 6, 4, 23), Resolution.Minute) self.Log(hist) if hist.empty: value.remove(j) else: symbols.append(j) history_price[str(j.Value)] = [] for tuple in hist.loc[str(j.Value)].itertuples(): history_price[str(j.Value)].append(float(tuple.close)) if len(history_price[str(j.Value)]) < 390: value.remove(j) symbols.remove(j) history_price.pop(str(j.Value)) df_history_price = pd.DataFrame.from_dict(history_price) return symbols