Overall Statistics
Total Trades
2
Average Win
0%
Average Loss
-0.03%
Compounding Annual Return
-0.420%
Drawdown
0.000%
Expectancy
-1
Net Profit
-0.025%
Sharpe Ratio
-6.425
Probabilistic Sharpe Ratio
0.000%
Loss Rate
100%
Win Rate
0%
Profit-Loss Ratio
0
Alpha
-0.003
Beta
0
Annual Standard Deviation
0.001
Annual Variance
0
Information Ratio
1.626
Tracking Error
0.6
Treynor Ratio
347.646
Total Fees
$2.00
## (*) Denotes code changes

class VerticalNadionGearbox(QCAlgorithm):

    def Initialize(self):
        self.SetStartDate(2020, 2, 20)  # Set Start Date
        self.SetEndDate(2020, 3, 12)
        self.SetCash(100000)  # Set Strategy Cash
        
        symbols = [ Symbol.Create("SPY", SecurityType.Equity, Market.USA) ]
        self.SetUniverseSelection( ManualUniverseSelectionModel(symbols) )
        self.UniverseSettings.Resolution = Resolution.Daily
        
        self.AddAlpha(MyAlphaModel())
        
        self.SetPortfolioConstruction(MyPortfolioModel())  # (*)
        
        self.SetRiskManagement(MaximumDrawdownPercentPerSecurityCustom(0.05))
        
        self.SetExecution(ImmediateExecutionModel())


    def OnData(self, data):
        pass
        
class MyAlphaModel(AlphaModel):
    emited = False

    def Update(self, algorithm, data):
        if not self.emited:
            self.emited = True
            return [Insight.Price("SPY", timedelta(365), InsightDirection.Up)]
        return []
         
class MyPortfolioModel(EqualWeightingPortfolioConstructionModel):
    def __init__(self):
        pass
        
    def CreateTargets(self, algorithm, insights):
        
        # (*)
        targets = []
        for insight in insights:
            targ = PortfolioTarget(insight.Symbol, insight.Direction)
            targets.append(targ)
        return targets
            
class MaximumDrawdownPercentPerSecurityCustom(RiskManagementModel):

    def __init__(self, maximumDrawdownPercent = 0.01):
        self.maximumDrawdownPercent = -abs(maximumDrawdownPercent)
        self.liquidated = set()
        self.currentTargets = set()

    def ManageRisk(self, algorithm, targets):
        
        # (*)
        if (set(targets) != self.currentTargets) and len(targets)>0:
            algorithm.Log(f"Different Targets: Quantity {targets[0].Quantity}")
            self.currentTargets = set(targets)
            self.liquidated = set()
        
        targets = []
        for kvp in algorithm.Securities:
            security = kvp.Value

            pnl = security.Holdings.UnrealizedProfitPercent
            if pnl < self.maximumDrawdownPercent or security.Symbol in self.liquidated:
                # liquidate
                targets.append(PortfolioTarget(security.Symbol, 0))
                if algorithm.Securities[security.Symbol].Invested:
                    self.liquidated.add(security.Symbol)
                    algorithm.Log(f"Liquidating {security.Symbol}")

        return targets