Overall Statistics
Total Orders
44
Average Win
2.57%
Average Loss
-3.49%
Compounding Annual Return
48.919%
Drawdown
11.800%
Expectancy
0.421
Start Equity
1000
End Equity
1359.7
Net Profit
35.970%
Sharpe Ratio
1.448
Sortino Ratio
1.225
Probabilistic Sharpe Ratio
69.731%
Loss Rate
18%
Win Rate
82%
Profit-Loss Ratio
0.74
Alpha
0.148
Beta
0.976
Annual Standard Deviation
0.199
Annual Variance
0.04
Information Ratio
0.842
Tracking Error
0.171
Treynor Ratio
0.295
Total Fees
$44.00
Estimated Strategy Capacity
$89000000.00
Lowest Capacity Asset
SMH V2LT3QH97TYD
Portfolio Turnover
14.39%
from AlgorithmImports import *

class RSIStrategyAlgorithm(QCAlgorithm):
    def Initialize(self):
        self.SetStartDate(2024, 1, 1)  # Set Start Date
        self.SetCash(1000)           # Set Strategy Cash
        
        # Add equity with daily resolution
        self.spy = self.AddEquity("SMH", Resolution.Daily)
        
        # Initialize RSI(2) indicator
        self.rsi = self.RSI("SMH", 2, MovingAverageType.Simple, Resolution.Daily)
        
        # Variable to keep track of previous day's high
        self.previousHigh = None

    def OnData(self, data):
        # Skip if RSI data is not yet ready
        if not self.rsi.IsReady:
            return
        
        # Update previousHigh if it's not None
        if self.previousHigh is not None:
            # Exit condition: today's close is higher than yesterday's high
            if self.spy.Close > self.previousHigh and self.Portfolio["SMH"].Invested:
                self.Liquidate("SMH")
                
        # Update previousHigh with today's high at the end of the day
        self.previousHigh = self.spy.High
        
        # Entry condition: RSI(2) < 15 and not already invested
        if self.rsi.Current.Value < 20 and not self.Portfolio["SMH"].Invested:
            self.SetHoldings("SMH", 1)  # Use SetHoldings for simplicity; adjust position size as needed

# Note: Deploy this script within the QuantConnect environment to run the backtest.