Overall Statistics
Total Trades
779
Average Win
-1.72%
Average Loss
0.44%
Compounding Annual Return
-24.001%
Drawdown
99.800%
Expectancy
-2.170
Net Profit
-99.740%
Sharpe Ratio
-0.611
Probabilistic Sharpe Ratio
0.000%
Loss Rate
60%
Win Rate
40%
Profit-Loss Ratio
-3.92
Alpha
0
Beta
0
Annual Standard Deviation
0.325
Annual Variance
0.105
Information Ratio
-0.611
Tracking Error
0.325
Treynor Ratio
0
Total Fees
$172001.31
Estimated Strategy Capacity
$370000000.00
Lowest Capacity Asset
AAPL R735QTJ8XC9X
from System.Drawing import Color


class SMAStrategy(QCAlgorithm):

    def Initialize(self):
        
        self.SetStartDate(2000, 1, 1)  # Set Start Date
        self.SetEndDate(2021, 8, 31)  # Set End Date
        self.SetCash(100000)  # Set Strategy Cash
        
        self.SetBrokerageModel(BrokerageName.InteractiveBrokersBrokerage) #, AccountType.Margin)
        
        self.aapl = self.AddEquity("AAPL", Resolution.Daily).Symbol
        
        lenght = self.GetParameter("sma_lenght")
        lenght = 5 if lenght is None else int(lenght)  
        
        #self.tolerance = self.GetParameter("crossover_tolerance")
        #self.tolerance = 0.0002 if self.tolerance is None else self.tolerance
        
        self.sma = self.SMA(self.aapl, lenght, Resolution.Daily)
        self.SetWarmUp(timedelta(lenght))

        self.Schedule.On(self.DateRules.On(2021,8, 31), self.TimeRules.At(15,45),
                        self.ExitPositions)
        
        stockPlot = Chart("Trade Plot")
        
        stockPlot.AddSeries(Series("Buy", SeriesType.Scatter, "$", Color.Green, ScatterMarkerSymbol.Triangle))
        stockPlot.AddSeries(Series("Sell", SeriesType.Scatter, "$", Color.Red, ScatterMarkerSymbol.TriangleDown))
        
        self.AddChart(stockPlot)

  
    def OnData(self, data):
        
        if not self.sma.IsReady:
            return
        
        price = self.Securities[self.aapl].Price
        self.Debug(str(self.Time) + " Purchasing AAPL: " + str(price))
        tolerance = 0.0000
        
        self.Plot("Trade Plot", "Price", price)
        self.Plot("Trade Plot", "SMA", self.sma.Current.Value)
        self.Debug(str(self.Time) + " SMA " + str(self.sma.Current.Value))
        holdings = self.Portfolio[self.aapl].Quantity
        
        if holdings <= 0:
            if self.sma.Current.Value > price * (1 + tolerance):
                self.SetHoldings(self.aapl,1)
                self.Plot("Trade Plot", "Buy", price)
        elif self.sma.Current.Value < price * (1 - tolerance):
            self.SetHoldings(self.aapl,-1)
            self.Plot("Trade Plot", "Sell", price)
        
        
    def ExitPositions(self):
        self.Liquidate()