Overall Statistics
Total Trades
9991
Average Win
0.42%
Average Loss
-0.46%
Compounding Annual Return
3.456%
Drawdown
31.500%
Expectancy
0.035
Net Profit
97.474%
Sharpe Ratio
0.303
Probabilistic Sharpe Ratio
0.010%
Loss Rate
46%
Win Rate
54%
Profit-Loss Ratio
0.92
Alpha
-0.002
Beta
0.364
Annual Standard Deviation
0.093
Annual Variance
0.009
Information Ratio
-0.433
Tracking Error
0.124
Treynor Ratio
0.078
Total Fees
$458009.51
Estimated Strategy Capacity
$170000000.00
Lowest Capacity Asset
SPY R735QTJ8XC9X
from AlgorithmImports import *


class PTLbuySellOpen(QCAlgorithm):

    def Initialize(self):
        self.SetStartDate(2002, 8, 1)# set start and end date for backtest
        self.SetEndDate(2022, 8, 5)
        self.SetCash(1000000) # initialize cash balance
        self.security = self.AddEquity("SPY", Resolution.Minute)# add an equity
        self.security.SetFeeModel(ConstantFeeModel(0))
        self.security.SetFeeModel(SecurityMarginModel(3))
        self.closingOrderSent = False # defualt false

        # use Interactive Brokers model for fees
        self.SetBrokerageModel(BrokerageName.InteractiveBrokersBrokerage, AccountType.Margin)
    

        self.Schedule.On(self.DateRules.EveryDay(self.security.Symbol),
                         self.TimeRules.AfterMarketOpen(self.security.Symbol, 1), self.SellOpen)

        # benchmark against S&P 500
        self.SetBenchmark("SPY")

    def SellOpen(self):
        if self.Portfolio.Invested:
            self.Liquidate()
            self.closingOrderSent = False


    def OnData(self, data):
        """OnData event is the primary entry point for your algorithm. Each new data point will be pumped in here.
        Arguments:
            data: Slice object keyed by symbol containing the stock data
        """
        if self.Time.hour == 15 and not self.Portfolio.Invested and not self.closingOrderSent:
            quantity = self.CalculateOrderQuantity(self.security.Symbol, 1)

            self.MarketOnCloseOrder(self.security.Symbol, quantity)
            self.closingOrderSent = True
            self.Debug("Purchased Stock")

    def OnEndOfAlgorithm(self):
        self.Debug("Final portfolio value: " + str(self.Portfolio.TotalPortfolioValue))