Overall Statistics
Total Orders
0
Average Win
0%
Average Loss
0%
Compounding Annual Return
0%
Drawdown
0%
Expectancy
0
Start Equity
100000
End Equity
100000
Net Profit
0%
Sharpe Ratio
0
Sortino 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
-4.826
Tracking Error
0.092
Treynor Ratio
0
Total Fees
$0.00
Estimated Strategy Capacity
$0
Lowest Capacity Asset
Portfolio Turnover
0%
# region imports
from AlgorithmImports import *
# endregion


class EMAMomentumUniverse(QCAlgorithm):
    

    def Initialize(self):
        self.set_start_date(2019, 1, 7)
        self.set_end_date(2019, 4, 1)
        self.set_cash(100000)
        self.universe_settings.resolution = Resolution.DAILY
        self.add_universe(self.coarse_selection_function) 
        self.averages = {}
        self.set_warm_up(200)
    

    def coarse_selection_function(self, universe):  
        selected = []
        universe = sorted(universe, key=lambda c: c.dollar_volume, reverse=True)  
        universe = [c for c in universe if c.price > 10][:100]

        for coarse in universe:  
            symbol = coarse.symbol
            
            if symbol not in self.averages:
                # 1. Call history to get an array of 200 days of history data
                history = self.history(symbol,200,Resolution.DAILY)
                
                #2. Adjust SelectionData to pass in the history result
                self.averages[symbol] = SelectionData(history) 

            self.averages[symbol].update(self.time, coarse.adjusted_price)
            
            if  self.averages[symbol].is_ready() and self.averages[symbol].fast > self.averages[symbol].slow:
                selected.append(symbol)
        return selected[:10]

    def on_securities_changed(self, changes):
        for security in changes.removed_securities:
            self.liquidate(security.symbol)
       
        for security in changes.added_securities:
            self.set_holdings(security.symbol, 0.10)
            
class SelectionData():
    #3. Update the constructor to accept a history array
    def __init__(self,history):
        # self.history=history
        self.slow = ExponentialMovingAverage(200)
        self.fast = ExponentialMovingAverage(50)
        for bar in history.itertuples():
            self.fast.update(bar.Index[1], bar.close)
            self.slow.update(bar.Index[1], bar.close)
        #4. Loop over the history data and update the indicators
    
    def is_ready(self):
        return self.slow.is_ready and self.fast.is_ready
    
    def update(self, time, price):
        self.fast.update(time, price)
        self.slow.update(time, price)