Overall Statistics
Total Trades
323
Average Win
1.78%
Average Loss
-2.08%
Compounding Annual Return
12.092%
Drawdown
21.900%
Expectancy
0.229
Net Profit
101.772%
Sharpe Ratio
0.916
Probabilistic Sharpe Ratio
36.904%
Loss Rate
34%
Win Rate
66%
Profit-Loss Ratio
0.85
Alpha
0.036
Beta
0.647
Annual Standard Deviation
0.146
Annual Variance
0.021
Information Ratio
-0.16
Tracking Error
0.107
Treynor Ratio
0.206
Total Fees
$563.57
class OptimizationExample(QCAlgorithm):

    def Initialize(self):
        self.SetStartDate(2015, 1, 1)
        self.SetCash(75000)
        self.stocks = ["SPY"]
        
        symbols = []
        for i in self.stocks:
            symbols.append(Symbol.Create(i, SecurityType.Equity, Market.USA))
            
        take_profit = self.GetParameter("take-profit") # We grab our value from the Algorithm Parameters. GetParameter must be declared here for the optimization to work
        
        self.UniverseSettings.Resolution = Resolution.Hour
        self.SetUniverseSelection(ManualUniverseSelectionModel(symbols))
        self.SetAlpha(TradeSPY(self, take_profit)) # We want to pass in the value from our Algorithm Parameters, which we aim to optimize
        self.SetPortfolioConstruction(EqualWeightingPortfolioConstructionModel())
        self.SetExecution(ImmediateExecutionModel())
        
class TradeSPY(AlphaModel):   
        
    def __init__(self, algorithm, take_profit): # Our third parameter is the value we aim to optimize
            
        self.Trade = True
        self.stocks = []
        
        self.profit = float(take_profit) # Since the Algorithm Parameter is a string we must convert it to a float
        
        algorithm.Schedule.On(algorithm.DateRules.Every(DayOfWeek.Monday),
                 algorithm.TimeRules.At(9, 30),
                 self.TradeSet) # Enter a long position every Monday at 9:30 am
            
    def Update(self, algorithm, data):
        
        insights = []
        
        if self.Trade != True: # If self.Trade != True, we monitor our UnrealizedProfitPercent and liquidate our position if it is greater than our optimized variable
            for symbol in self.stocks:
                if algorithm.Portfolio[symbol].Invested and (algorithm.Securities[symbol].Holdings.UnrealizedProfitPercent > self.profit):
                    insights.append(Insight.Price(symbol,timedelta(days=6), InsightDirection.Flat))
        
        if self.Trade == True: # Enter long position
            for symbol in self.stocks:
                #symbol = ticker.Symbol
                if not algorithm.Portfolio[symbol].Invested:
                    insights.append(Insight.Price(symbol,timedelta(days=6), InsightDirection.Up))
            self.Trade = False
        
        return insights
            
    def OnSecuritiesChanged(self, algorithm, changes):
            
        for added in changes.AddedSecurities:
            self.stocks.append(added.Symbol)
                
    # Triggers every Monday at 9:30 am
    def TradeSet(self):
        self.Trade = True