Overall Statistics
Total Trades
852
Average Win
1.05%
Average Loss
-1.08%
Compounding Annual Return
7.568%
Drawdown
23.500%
Expectancy
0.233
Net Profit
158.251%
Sharpe Ratio
0.57
Probabilistic Sharpe Ratio
2.915%
Loss Rate
38%
Win Rate
62%
Profit-Loss Ratio
0.97
Alpha
0.012
Beta
0.476
Annual Standard Deviation
0.1
Annual Variance
0.01
Information Ratio
-0.349
Tracking Error
0.105
Treynor Ratio
0.12
Total Fees
$3344.64
Estimated Strategy Capacity
$760000000.00
Lowest Capacity Asset
SPY R735QTJ8XC9X
Portfolio Turnover
15.23%
import numpy as np
from QuantConnect.Algorithm import QCAlgorithm
from QuantConnect.Indicators import *
from QuantConnect import Resolution
from QuantConnect.Data.Market import TradeBar

class RsiCloseAlgorithm(QCAlgorithm):
    def Initialize(self):
        self.SetStartDate(2010, 1, 1) # Set the start date for backtesting
        self.SetEndDate(2023, 1, 1) # Set the end date for backtesting
        self.SetCash(100000) # Set the initial cash balance for backtesting

        # Define the symbol we want to trade
        self.symbol = self.AddEquity("SPY", Resolution.Daily).Symbol

        # RSI parameters
        self.rsi_period = 2
        self.rsi_lower = 15

        # Set up the RSI indicator
        self.rsi = self.RSI(self.symbol, self.rsi_period, MovingAverageType.Simple, Resolution.Daily)

        # Initialize a RollingWindow to keep track of past TradeBars
        self.barWindow = RollingWindow[TradeBar](2)

    def OnData(self, data):
        # Add the current TradeBar to the RollingWindow
        self.barWindow.Add(data[self.symbol])

        # Skip if the RollingWindow is not ready
        if not self.barWindow.IsReady:
            return

        # Get the past high price from the RollingWindow
        pastHigh = self.barWindow[1].High

        # Check if RSI is lower than the defined threshold
        if self.rsi.Current.Value < self.rsi_lower:
            self.SetHoldings(self.symbol, 1.0)

        # Check if today's close is higher than yesterday's high
        if self.Portfolio[self.symbol].Invested and self.Securities[self.symbol].Close > pastHigh:
            self.Liquidate(self.symbol)

        # Plot RSI on chart
        self.Plot("Indicators", "RSI", self.rsi.Current.Value)
        self.Plot("Indicators", "Oversold Level", self.rsi_lower)