Overall Statistics
Total Trades
465
Average Win
1.08%
Average Loss
-0.14%
Compounding Annual Return
6.744%
Drawdown
14.200%
Expectancy
0.856
Net Profit
45.571%
Sharpe Ratio
0.571
Loss Rate
78%
Win Rate
22%
Profit-Loss Ratio
7.49
Alpha
0.056
Beta
0.017
Annual Standard Deviation
0.103
Annual Variance
0.011
Information Ratio
-0.448
Tracking Error
0.182
Treynor Ratio
3.477
Total Fees
$0.00
class AlphaFiveTechnologyUniverse(QCAlgorithm):

    def Initialize(self):
        #1. Required: Five years of backtest history
        self.SetStartDate(2014, 1, 1) 
        
        #2. Required: Alpha Streams Models:
        self.SetBrokerageModel(BrokerageName.AlphaStreams)
        
        #3. Required: Significant AUM Capacity
        self.SetCash(1000000)
        
        #4. Select Desired ETF Universe
        # See more: https://www.quantconnect.com/docs/algorithm-reference/universes
        self.UniverseSettings.Resolution = Resolution.Hour
        self.SetUniverseSelection(TechnologyETFUniverse()) 
        self.universe = { }
        
        #5. Set Relevent Benchmark
        self.reference = "XLK"
        self.AddEquity(self.reference, Resolution.Hour)
        self.SetBenchmark("SPY")

        # Demonstration: Consolidation
        # See more: https://www.quantconnect.com/docs/algorithm-reference/consolidating-data
        self.Consolidate(self.reference, CalendarType.Weekly, self.ConsolidationDemo);
        
        # Demonstration: Scheduled Events
        # See more: https://www.quantconnect.com/docs/algorithm-reference/scheduled-events
        self.Schedule.On(self.DateRules.Every(DayOfWeek.Monday), self.TimeRules.AfterMarketOpen(self.reference, 30), self.ScheduleDemo)
        
        # --------
        # Optional: Framework Algorithm Models
        self.SetPortfolioConstruction(EqualWeightingPortfolioConstructionModel())
        self.SetExecution(ImmediateExecutionModel())
        # --------
        
    def OnData(self, data):
        # Manually update the Indicators
        for symbol in self.universe.keys():
            if data.Bars.ContainsKey(symbol):
                self.universe[symbol].update(data[symbol].EndTime, data[symbol].Close)
    
    def ScheduleDemo(self):
        for symbol, assetData in self.universe.items():
            price = self.ActiveSecurities[symbol].Price
            if assetData.is_ready() and assetData.deviating(price):
                # Demonstration: Ensure to emit Insights to clearly signal intent to fund.
                self.EmitInsights(Insight.Price(symbol, timedelta(3), InsightDirection.Up))
                
    def ConsolidationDemo(self, bar):
        self.Debug(f'{self.Time} :: {bar.Time} {bar.Close}')
            
    # Initializing ETF Universe Securities
    def OnSecuritiesChanged(self, changes):
        for s in changes.AddedSecurities:
            if s.Symbol not in self.universe:
                history = self.History(s.Symbol, 30, Resolution.Hour)
                self.universe[s.Symbol] = AssetData(s.Symbol, history)

# Indicators+Universe Demonstration
class AssetData(object):
    def __init__(self, symbol, history):
        self.std = StandardDeviation(30)
        self.mean = SimpleMovingAverage(7) 
        
        for bar in history.itertuples():
            self.update(bar.Index[1], bar.close)
        
    def is_ready(self):
        return self.std.IsReady
        
    def update(self, time, price):
        self.std.Update(time, price)
        self.mean.Update(time, price)
        
    def deviating(self, price):
        if self.std.Current.Value == 0:
            return False
        return ( (price - self.mean.Current.Value) / self.std.Current.Value ) < -3