Overall Statistics
Total Orders
310
Average Win
0.06%
Average Loss
-0.02%
Compounding Annual Return
6.736%
Drawdown
17.900%
Expectancy
2.334
Start Equity
100000
End Equity
123526.40
Net Profit
23.526%
Sharpe Ratio
0.243
Sortino Ratio
0.305
Probabilistic Sharpe Ratio
13.432%
Loss Rate
15%
Win Rate
85%
Profit-Loss Ratio
2.91
Alpha
-0.021
Beta
0.657
Annual Standard Deviation
0.103
Annual Variance
0.011
Information Ratio
-0.686
Tracking Error
0.065
Treynor Ratio
0.038
Total Fees
$310.16
Estimated Strategy Capacity
$1400000.00
Lowest Capacity Asset
WOOD U3RDKMG7QNHH
Portfolio Turnover
0.14%
#region imports
from AlgorithmImports import *
#endregion
from clr import AddReference
AddReference("QuantConnect.Common")
AddReference("QuantConnect.Algorithm")
AddReference("QuantConnect.Algorithm.Framework")

from QuantConnect import *
from QuantConnect.Algorithm import *
from QuantConnect.Algorithm.Framework import *
from QuantConnect.Algorithm.Framework.Alphas import AlphaModel, Insight, InsightType, InsightDirection


class constantAlphaModel(AlphaModel):
    

    def __init__(self, type, direction, period, magnitude = None, confidence = None):
        
        self.type = type
        self.direction = direction
        self.period = period
        self.magnitude = magnitude
        self.confidence = confidence
        self.securities = []
        self.insightsTimeBySymbol = {}

        typeString = Extensions.GetEnumString(type, InsightType)
        directionString = Extensions.GetEnumString(direction, InsightDirection)

        self.Name = '{}({},{},{}'.format(self.__class__.__name__, typeString, directionString, strfdelta(period))
        if magnitude is not None:
            self.Name += ',{}'.format(magnitude)
        if confidence is not None:
            self.Name += ',{}'.format(confidence)

        self.Name += ')';


    def Update(self, algorithm, data):
        
        insights = []

        for security in self.securities:
            if security.Price != 0 and self.ShouldEmitInsight(algorithm.UtcTime, security.Symbol):
                insights.append(Insight(security.Symbol, self.period, self.type, self.direction, self.magnitude, self.confidence))

        return insights


    def OnSecuritiesChanged(self, algorithm, changes):
        for added in changes.AddedSecurities:
            self.securities.append(added)

        for removed in changes.RemovedSecurities:
            if removed in self.securities:
                self.securities.remove(removed)
            if removed.Symbol in self.insightsTimeBySymbol:
                self.insightsTimeBySymbol.pop(removed.Symbol)


    def ShouldEmitInsight(self, utcTime, symbol):

        generatedTimeUtc = self.insightsTimeBySymbol.get(symbol)

        if generatedTimeUtc is not None:
            if utcTime - generatedTimeUtc < self.period:
                return False

        self.insightsTimeBySymbol[symbol] = utcTime
        return True

def strfdelta(tdelta):
    d = tdelta.days
    h, rem = divmod(tdelta.seconds, 3600)
    m, s = divmod(rem, 60)
    return "{}.{:02d}:{:02d}:{:02d}".format(d,h,m,s)
#region imports
from AlgorithmImports import *
#endregion
from Risk.MaximumDrawdownPercentPerSecurity import MaximumDrawdownPercentPerSecurity
from Constant_Alpha import constantAlphaModel

class EnergeticSkyBlueFrog(QCAlgorithm):

    def Initialize(self):
        
        self.SetStartDate(2021, 1, 1) 
        self.SetEndDate(2024,3,31)
        self._cash = 100000
        self.SetCash(self._cash)  
    
      
        # 1. UNIVERSE SELECTION MODEL
        self.UniverseSettings.Resolution = Resolution.Daily
        symbols = [ # OUR INVESTMENT UNIVERSE
                    # AGGREGATE INDICES
                    Symbol.Create("SPY", SecurityType.Equity, Market.USA),
                    Symbol.Create("IEFA", SecurityType.Equity, Market.USA),
                    Symbol.Create("AGG", SecurityType.Equity, Market.USA),
                    Symbol.Create("IWM", SecurityType.Equity, Market.USA),
                    Symbol.Create("EEM", SecurityType.Equity, Market.USA),
                    Symbol.Create("EWJ", SecurityType.Equity, Market.USA),
                    Symbol.Create("EPP", SecurityType.Equity, Market.USA),
                    # FIXED INCOME AND REAL ESTATE
                    Symbol.Create("IYR", SecurityType.Equity, Market.USA),
                    Symbol.Create("LQD", SecurityType.Equity, Market.USA),
                    Symbol.Create("EMB", SecurityType.Equity, Market.USA),
                    Symbol.Create("IEF", SecurityType.Equity, Market.USA),
                    Symbol.Create("IEI", SecurityType.Equity, Market.USA),
                    # COMMODITIES
                    Symbol.Create("IAU", SecurityType.Equity, Market.USA),
                    Symbol.Create("GSG", SecurityType.Equity, Market.USA),
                    Symbol.Create("COMT", SecurityType.Equity, Market.USA),
                    # FACTORS
                    Symbol.Create("USMV", SecurityType.Equity, Market.USA),
                    Symbol.Create("DGRO", SecurityType.Equity, Market.USA),
                    Symbol.Create("QUAL", SecurityType.Equity, Market.USA),
                    Symbol.Create("DVY", SecurityType.Equity, Market.USA),
                    Symbol.Create("MTUM", SecurityType.Equity, Market.USA),
                    Symbol.Create("VLUE", SecurityType.Equity, Market.USA),
                    Symbol.Create("EFAV", SecurityType.Equity, Market.USA),
                    Symbol.Create("EEMV", SecurityType.Equity, Market.USA),
                    Symbol.Create("IDV", SecurityType.Equity, Market.USA),
                    Symbol.Create("DVY", SecurityType.Equity, Market.USA),
                    Symbol.Create("IQLT", SecurityType.Equity, Market.USA),
                    # SECTORS AND INDUSTRIES
                    Symbol.Create("IBB", SecurityType.Equity, Market.USA),
                    Symbol.Create("IHI", SecurityType.Equity, Market.USA),
                    Symbol.Create("IYW", SecurityType.Equity, Market.USA),
                    Symbol.Create("IGF", SecurityType.Equity, Market.USA),
                    Symbol.Create("IYH", SecurityType.Equity, Market.USA),
                    Symbol.Create("VYF", SecurityType.Equity, Market.USA),
                    Symbol.Create("IXC", SecurityType.Equity, Market.USA),
                    Symbol.Create("PICK", SecurityType.Equity, Market.USA),
                    Symbol.Create("IYE", SecurityType.Equity, Market.USA),
                    Symbol.Create("KXI", SecurityType.Equity, Market.USA),
                    Symbol.Create("WOOD", SecurityType.Equity, Market.USA)]
        self.SetUniverseSelection(ManualUniverseSelectionModel(symbols))
        
        # 2. ALPHA MODEL
        self.AddAlpha(constantAlphaModel(InsightType.Price, InsightDirection.Up, timedelta(days = 1), 0.025, None))
        #self.AddAlpha(constantAlphaModel(InsightType.Price, InsightDirection.Up, timedelta(days = 1), 0.999))

        # 3. PORTFOLIO CONSTRUCTION MODEL
        self.SetPortfolioConstruction(EqualWeightingPortfolioConstructionModel())
        
        # 4. RISK MANAGEMENT
        self.SetRiskManagement(MaximumDrawdownPercentPortfolio(0.05))
        
        # 5. EXECUTION MODEL
        # If we say nothing about it, it is assumed as Immediate Execution (Market Orders)
        
        self._benchmark = self.AddEquity("SPY", Resolution.Daily).Symbol
        self._benchmarkInitial = self.History(self._benchmark, 1, Resolution.Daily)
        self._benchmarkPrice = self._benchmarkInitial['close'][0]
        
    def OnData(self,data): #customizable part of the sandwich truck
        
        self.Plot("Relative Performance", "SPY", self._cash*self.Securities["SPY"].Close/self._benchmarkPrice)
        self.Plot("Relative Performance", "Portfolio Value", self.Portfolio.TotalPortfolioValue)