Overall Statistics
Total Trades
8
Average Win
6.14%
Average Loss
-0.73%
Compounding Annual Return
32.757%
Drawdown
3.300%
Expectancy
3.695
Net Profit
10.794%
Sharpe Ratio
2.966
Probabilistic Sharpe Ratio
83.213%
Loss Rate
50%
Win Rate
50%
Profit-Loss Ratio
8.39
Alpha
0.217
Beta
0.393
Annual Standard Deviation
0.114
Annual Variance
0.013
Information Ratio
0.221
Tracking Error
0.141
Treynor Ratio
0.857
Total Fees
$8.00
from clr import AddReference
AddReference("System")
AddReference("QuantConnect.Algorithm")
AddReference("QuantConnect.Indicators")
AddReference("QuantConnect.Common")

from System import *
from QuantConnect import *
from QuantConnect.Indicators import *
from QuantConnect.Algorithm import *
from collections import deque
from datetime import datetime, timedelta
from numpy import sum

class OptimizedVerticalContainmentField(QCAlgorithm):

    def Initialize(self):
        self.SetStartDate(2020,8,24)  # Set Start Date
        self.SetEndDate(2020,12,31)
        self.spy = self.AddEquity("SPY", Resolution.Minute)
        self.SetCash(10000)  # Set Strategy Cash
        self.SetWarmUp(200)
        
        self.Firststock = "SPY"
        
        #Indicators that can be optimized
        self.FirstBuyIndicator = .21
        self.FirstSellIndicator = -.24
        self.FirstHMAPeriod = 35
       
        #Initializing the Hull Moving Average of the First Stock
        self.Firsthma = self.HMA(self.Firststock, self.FirstHMAPeriod, Resolution.Minute)
        #Initializing the rolling window (array) to hold historical data of the hull moving average
        self.Firsthma.Updated += self.HmaUpdated
        self.hmaWindow = RollingWindow[float](3)
        
     
        self.window = RollingWindow[TradeBar](3)
        
        
    #updates the rolling window for the First stock's Hull Moving Average Data    
    def HmaUpdated(self, sender, updated):
        self.hmaWindow.Add(self.Firsthma.Current.Value)
    
    #Where actions take place and you enter in the criteria to follow
    def OnData(self, data):
        #warming up the data, starts running the algorithm before the actual start data
        if self.IsWarmingUp: return
        self.window.Add(data[self.Firststock])
        
        #Ensures that the rolling windows (arrays) have the required data to be used
        if not (self.window.IsReady and self.hmaWindow.IsReady): 
            return
    
        #Criteria value-2 minute slope of the first stock hull moving average
        Firstslope = ((self.hmaWindow[0]-self.hmaWindow[2])/self.hmaWindow[2])*100
        
        #First Stock Buy Action Area
        if Firstslope > self.FirstBuyIndicator:
            if (not self.Portfolio.Invested):
                self.SetHoldings(self.Firststock, 1, True)
        
        #First Stock Sell Action Area
        if Firstslope < self.FirstSellIndicator:
            if (self.Portfolio.Invested):
                self.Liquidate(self.Firststock)