Overall Statistics
Total Trades
320
Average Win
1.68%
Average Loss
-1.94%
Compounding Annual Return
10.027%
Drawdown
21.200%
Expectancy
0.200
Net Profit
76.890%
Sharpe Ratio
0.766
Probabilistic Sharpe Ratio
25.986%
Loss Rate
36%
Win Rate
64%
Profit-Loss Ratio
0.86
Alpha
0.019
Beta
0.648
Annual Standard Deviation
0.147
Annual Variance
0.021
Information Ratio
-0.292
Tracking Error
0.107
Treynor Ratio
0.173
Total Fees
$675.13
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