Overall Statistics
Total Trades
3970
Average Win
0%
Average Loss
-0.01%
Compounding Annual Return
-2.114%
Drawdown
15.600%
Expectancy
-1
Net Profit
-15.570%
Sharpe Ratio
-19.749
Probabilistic Sharpe Ratio
0%
Loss Rate
100%
Win Rate
0%
Profit-Loss Ratio
0
Alpha
-0.015
Beta
-0
Annual Standard Deviation
0.001
Annual Variance
0
Information Ratio
-0.662
Tracking Error
0.155
Treynor Ratio
141.505
Total Fees
$7187.29
Estimated Strategy Capacity
$6900000.00
Lowest Capacity Asset
SPY R735QTJ8XC9X
# region imports
from AlgorithmImports import *
# endregion
# Import necessary libraries

class GapTradingAlgorithm(QCAlgorithm):
    def Initialize(self):
        self.SetStartDate(2015, 1, 1)
        self.SetEndDate(2022, 11, 30)
        # Set the cash we'd like to use for our strategy
        self.SetCash(100000)

        # Subscribe to the desired asset
        self.symbol=self.AddEquity("SPY", Resolution.Minute).Symbol

        # Set the trade bar consolidator to consolidate data at the end of the day
        self.consolidator = TradeBarConsolidator(timedelta(days=1))
        self.consolidator.DataConsolidated += self.OnDataConsolidated
        self.SubscriptionManager.AddConsolidator( self.symbol, self.consolidator)

        # Create a rolling window to store the previous day's closing price
        self.previous_close = RollingWindow[TradeBar](1)

    def OnDataConsolidated(self, sender, bar):
        # Add the current bar to the rolling window
        self.previous_close.Add(bar)

        # Check if the rolling window is ready
        if not self.previous_close.IsReady:
            return

        # Get the current day's opening price
        opening_price = self.Securities[self.symbol].Open

        # Get the previous day's closing price
        previous_close = self.previous_close[0].Close

        # Calculate the gap between the two prices
        gap = opening_price - previous_close

        # Check if the gap is positive or negative
        if gap > 0:
            # Enter a short position
            self.SetHoldings( self.symbol, -1)
        elif gap < 0:
            # Enter a long position
            self.SetHoldings( self.symbol, 1)
        else:
            # No gap, do nothing
            pass

        # Exit the position before market close
        self.Liquidate( self.symbol)