Overall Statistics
Total Trades
69
Average Win
0.61%
Average Loss
-0.24%
Compounding Annual Return
9.110%
Drawdown
7.600%
Expectancy
1.323
Net Profit
12.335%
Sharpe Ratio
0.916
Probabilistic Sharpe Ratio
44.255%
Loss Rate
34%
Win Rate
66%
Profit-Loss Ratio
2.54
Alpha
0.077
Beta
0.01
Annual Standard Deviation
0.085
Annual Variance
0.007
Information Ratio
-0.201
Tracking Error
0.264
Treynor Ratio
7.894
Total Fees
$71.73
Estimated Strategy Capacity
$230000000.00
from datetime import timedelta
from QuantConnect.Data.UniverseSelection import * 
from Selection.FundamentalUniverseSelectionModel import FundamentalUniverseSelectionModel

class UglyAsparagusSheep(QCAlgorithm):

    def Initialize(self):
        self.SetStartDate(2019, 1, 1)
        self.SetEndDate(2020, 5, 1)
        self.SetCash(100000)
        self.SetWarmUp(200)
        self.UniverseSettings.Resolution = Resolution.Daily
        self.UniverseSettings.Leverage = 1 #self.GetParameter('Leverage')
        #self.AddUniverseSelection(LiquidValueUniverseSelectionModel())
        self.AddAlpha(LongShortEYAlphaModel())
        self.SetPortfolioConstruction(EqualWeightingPortfolioConstructionModel())
        self.SetExecution(ImmediateExecutionModel())
        self.AddEquity("IWM", Resolution.Daily)
        self.AddEquity("SPY", Resolution.Daily)
        
  
class LongShortEYAlphaModel(AlphaModel):

    def __init__(self):
        self.lastChange = None
        self.warmedUp = False
        self.bearMarket = False
        self.hedgeSignal = False
        self.ema_slow = None
        self.ema_fast = None
        self.ema_faster = None
        self.symbols = []
        
    def Update(self, algorithm, data):
        insights = []
        
        if self.ema_slow is None or not self.ema_slow.IsReady:
            return insights
            
        for symbol in self.symbols:
            if not (data.ContainsKey(symbol) and data[symbol] is not None):
                return []
        
        #2. If else statement to emit signals once a day
        if self.lastChange == algorithm.Time.day:
            return insights
        self.lastChange = algorithm.Time.day
        
        self.bearMarket = data["IWM"].Close < self.ema_slow.Current.Value 
        self.hedgeSignal = self.bearMarket and self.ema_faster.Current.Value < self.ema_fast.Current.Value
        
        # Not using the LiquidValueUniverseSelectionModel output for testing purpose
        insights.append(Insight.Price("SPY", timedelta(days=2), InsightDirection.Up))
        
        if self.hedgeSignal:
            insights.append(Insight.Price("IWM", timedelta(days=2), InsightDirection.Down))
            
        return insights

    def OnSecuritiesChanged(self, algorithm, changes):
        for security in changes.AddedSecurities:
            if str(security.Symbol) == 'IWM':
                self.ema_slow = algorithm.EMA("IWM", 200, Resolution.Daily)
                self.ema_fast = algorithm.EMA("IWM", 10, Resolution.Daily)
                self.ema_faster = algorithm.EMA("IWM", 5, Resolution.Daily)
            self.symbols.append(security.Symbol)