Overall Statistics
Total Trades
73
Average Win
2.54%
Average Loss
-1.84%
Compounding Annual Return
4.027%
Drawdown
22.100%
Expectancy
-0.058
Net Profit
8.189%
Sharpe Ratio
0.019
Sortino Ratio
0.024
Probabilistic Sharpe Ratio
10.559%
Loss Rate
60%
Win Rate
40%
Profit-Loss Ratio
1.38
Alpha
0.009
Beta
0.577
Annual Standard Deviation
0.141
Annual Variance
0.02
Information Ratio
0.107
Tracking Error
0.126
Treynor Ratio
0.005
Total Fees
$73.00
Estimated Strategy Capacity
$84000000.00
Lowest Capacity Asset
SPY R735QTJ8XC9X
Portfolio Turnover
11.56%
# region imports
from AlgorithmImports import *
# endregion

class MACDTrendAlgorithm(QCAlgorithm):

    def Initialize(self):
        self.SetStartDate(2022, 1, 1)  
        self.SetEndDate(2024, 1, 1)    
        self.SetCash(1000)         # Find more symbols here: http://quantconnect.com/data
        self.symbol=self.AddEquity("SPY", Resolution.Hour)

        # define our daily macd(12,26) with a 9 day signal
        self.__macd = self.MACD("SPY", 12, 26, 9, MovingAverageType.Exponential, Resolution.Hour)
        self.__previous = datetime.min
        self.PlotIndicator("MACD", True, self.__macd, self.__macd.Signal)
        self.PlotIndicator("SPY", self.__macd.Fast, self.__macd.Slow)

        self.crossupTOLERANCE = 0.001
        self.crossDownTOLERANCE= 0.0025
        stockPlot = Chart('Trade Plot')

        from System.Drawing import Color
        stockPlot.AddSeries(Series('Price', SeriesType.Line, '$', Color.Green))
        stockPlot.AddSeries(Series('Buy', SeriesType.Scatter, '$', Color.Red, ScatterMarkerSymbol.Triangle))
        stockPlot.AddSeries(Series('Sell', SeriesType.Scatter, '$', Color.Blue, ScatterMarkerSymbol.TriangleDown))
        self.AddChart(stockPlot)

    def OnData(self, data):
        if not self.__macd.IsReady: return

        if data[self.symbol.Symbol]:
            self.Plot("TEST", "CLOSE", data[self.symbol.Symbol].Close)
            tolerance = 0.0025
            holdings = self.Portfolio["SPY"].Quantity
            price=data["SPY"].Close
            signalDeltaPercent = (self.__macd.Current.Value - self.__macd.Signal.Current.Value)/self.__macd.Fast.Current.Value

            if holdings <= 0 and signalDeltaPercent > self.crossupTOLERANCE:  # 0.01%
                self.Log(f"Buying SPY | Holding: {holdings} | Delta%:{signalDeltaPercent} | UP_Tolerance{self.crossupTOLERANCE}")
                self.SetHoldings("SPY", 1.0)
                self.Plot("SPY", "Buy", price)

            elif holdings >= 0 and signalDeltaPercent < -self.crossDownTOLERANCE:
                self.Log(f"Selling SPY | Holding: {holdings} | Delta%:{signalDeltaPercent} | DOWN_Tolerance{self.crossDownTOLERANCE}")
                self.Liquidate("SPY")
                self.SetHoldings("SPY", -1.0)
                self.Plot("SPY", "Sell", price)
            else:
                self.Log(f"Nothing | Holding: {holdings} | Delta%:{signalDeltaPercent} | UP_Tolerance{self.crossupTOLERANCE} | DOWN_Tolerance{self.crossDownTOLERANCE}")
        
        else:
            self.Log(f"Data Missing on: {self.Time}")