Overall Statistics
Total Trades
104
Average Win
0.83%
Average Loss
-0.77%
Compounding Annual Return
8.746%
Drawdown
7.000%
Expectancy
0.323
Net Profit
13.414%
Sharpe Ratio
1.238
Probabilistic Sharpe Ratio
60.680%
Loss Rate
37%
Win Rate
63%
Profit-Loss Ratio
1.09
Alpha
0.074
Beta
-0.004
Annual Standard Deviation
0.059
Annual Variance
0.003
Information Ratio
-0.135
Tracking Error
0.262
Treynor Ratio
-16.784
Total Fees
$198.37
import pandas as pd

class DynamicOptimizedCircuit(QCAlgorithm):

    def Initialize(self):
        starting_equity = 100000
        self.SetStartDate(2018, 11, 28)
        self.SetCash(starting_equity)

        symbols = [ Symbol.Create("SPY", SecurityType.Equity, Market.USA) ]
        self.SetUniverseSelection( ManualUniverseSelectionModel(symbols) )
        self.UniverseSettings.Resolution = Resolution.Daily

        self.AddAlpha(MyAlphaModel())

        self.SetPortfolioConstruction(EqualWeightingPortfolioConstructionModel())

        self.SetExecution(ImmediateExecutionModel())
        
        self.SetRiskManagement(MyRiskManagementModel(starting_equity))


class MyAlphaModel(AlphaModel):
    
    def Update(self, algorithm, data):
        if not algorithm.Portfolio.Invested:
            return [Insight.Price("SPY", timedelta(1), InsightDirection.Up)]
        return []


class MyRiskManagementModel(RiskManagementModel):
    
    def __init__(self, starting_equity):
        self.trading = True
        self.previous_equity = starting_equity
        self.equity_changes = pd.Series([])
        self.equity_lookback = 100
        self.std_multiple = 2

    def ManageRisk(self, algorithm, targets):
        risk_adjusted_targets = []
        
        if self.trading:
            equity_change = algorithm.Portfolio.TotalPortfolioValue - self.previous_equity
            self.previous_equity = algorithm.Portfolio.TotalPortfolioValue
            self.equity_changes = self.equity_changes.append(pd.Series(equity_change, index=[algorithm.Time])).iloc[-self.equity_lookback:]
            if len(self.equity_changes) == self.equity_lookback and abs(equity_change) > self.equity_changes.std() * self.std_multiple:
                self.trading = False
        
        for kvp in algorithm.Securities:
            security = kvp.Value

            if not self.trading:
                risk_adjusted_targets.append(PortfolioTarget(security.Symbol, 0))
                
        return risk_adjusted_targets