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 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 -2.703 Tracking Error 0.135 Treynor Ratio 0 Total Fees $0.00 Estimated Strategy Capacity $0 |
from Alphas.MacdAlphaModel import MacdAlphaModel from datetime import datetime class CrawlingFluorescentOrangeBull(QCAlgorithm): def Initialize(self): self.SetStartDate(2021, 1, 1) # Set Start Date self.SetCash(10000) # Set Strategy Cash self.AddAlpha(MacdAlphaModel(12, 26, 9, MovingAverageType.Simple, Resolution.Daily)) self.SetUniverseSelection(CoarseFundamentalUniverseSelectionModel(self.CoarseSelectionFunction)) self.averages = { } self.UniverseSettings.Resolution = Resolution.Daily def CoarseSelectionFunction(self, universe): selected = [] universe = sorted(universe, key=lambda c: c.DollarVolume, reverse=True) universe = [c for c in universe if c.Price > 5][:100] for coarse in universe: self.symbol = coarse.Symbol if self.symbol not in self.averages: # 1. Call history to get an array of 200 days of history data history = self.History(self.symbol, 200, Resolution.Daily) #2. Adjust SelectionData to pass in the history result self.averages[self.symbol] = SelectionData(history) self.averages[self.symbol].update(self.Time, coarse.AdjustedPrice) if self.averages[self.symbol].is_ready() and self.averages[self.symbol].fast > self.averages[self.symbol].slow: selected.append(self.symbol) return selected[:5] def MACD20(self, coarse): self.macd = self.MACD(self.symbol, 12, 26, 9, MovingAverageType.Exponential, Resolution.Daily) self.__previous = datetime.min self.PlotIndicator("MACD", True, self.__macd, self.__macd.Signal) self.PlotIndicator(self.symbol, self.__macd.Fast, self.__macd.Slow) def Data(self, data): if not self.macd.IsReady: return if self.previous.date() == self.Time.date(): return holdings = self.Portfolio[self.symbol].Quantity self.__previous = self.Time class Holdings(): def buyShares(self, slice): if holdings == 0 and self.macd.Current.Value > self.macd.Signal.Value: self.Buy(self.symbol, 10) else: return def holdShares(self, slice): if holdings > 0 or self.macd.Current.Value > self.macd.Signal.Value: return if holdings < 0 or self.macd.Current.Value < self.macd.Signal.Value: return else: return def sellShares(self, slice): if holdings > 0 or self.macd.Current.Value < self.macd.Signal.Value: self.Liquidate() else: return class SelectionData(): #3. Update the constructor to accept a history array def __init__(self, history): self.slow = ExponentialMovingAverage(100) self.fast = ExponentialMovingAverage(20) #4. Loop over the history data and update the indicators for bar in history.itertuples(): self.fast.Update(bar.Index[1], bar.close) self.slow.Update(bar.Index[1], bar.close) def is_ready(self): return self.slow.IsReady and self.fast.IsReady def update(self, time, price): self.fast.Update(time, price) self.slow.Update(time, price)