Overall Statistics
Total Trades
1009
Average Win
0.17%
Average Loss
-0.20%
Compounding Annual Return
4.436%
Drawdown
8.900%
Expectancy
0.090
Net Profit
9.078%
Sharpe Ratio
0.752
Probabilistic Sharpe Ratio
33.968%
Loss Rate
41%
Win Rate
59%
Profit-Loss Ratio
0.86
Alpha
0.001
Beta
0.157
Annual Standard Deviation
0.042
Annual Variance
0.002
Information Ratio
-0.889
Tracking Error
0.179
Treynor Ratio
0.199
Total Fees
$5045.00
Estimated Strategy Capacity
$520000000.00
Lowest Capacity Asset
SPY R735QTJ8XC9X
if __name__ == "__main__":
    from myapi import *
else:
    from AlgorithmImports import *

class BuyCloseSellOpen(QCAlgorithm):
    def Initialize(self):
        # message = "\n\n\n********** Printing from Initialize."; self.Debug(message); self.Log(message)

        # set start and end date for backtest
        self.SetStartDate(2019, 1,  1 )
        self.SetEndDate(  2020, 12, 31)

        # initialize cash balance
        self.SetCash(1_000_000)

        # add an equity
        self.security = self.AddEquity("SPY", Resolution.Minute)

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

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

        # keep track of whether closing order was sent for the day or not
        self.closingOrderSent = False

    def OnData(self, data):
        # message = "\n\n\n********** Printing from OnData."; self.Debug(message); self.Log(message)
        if not self.closingOrderSent and not self.Portfolio.Invested:
            self.MarketOnCloseOrder("SPY", 1000)
            self.closingOrderSent = True

    def OnOrderEvent(self, orderEvent):
        # message = "\n\n\n********** Printing from OnOrderEvent."; self.Debug(message); self.Log(message)
        if orderEvent.Status == OrderStatus.Filled and orderEvent.FillQuantity > 0:
            self.MarketOnOpenOrder("SPY", -1000)
            self.closingOrderSent = False

if __name__ == "__main__":
    print("Running BuyCloseSellOpen unit tests.")
    import unittest

    class BIST(unittest.TestCase):
        def test(self):
            print("Running test.")
            algo = BuyCloseSellOpen()
            self.assertEqual( algo.Portfolio.Invested, False )
            algo.Initialize()
            algo.OnData(0)
            self.assertEqual( algo.Portfolio.Invested, True )

    unittest.main()