Overall Statistics
Total Trades
90
Average Win
0.04%
Average Loss
-0.04%
Compounding Annual Return
0.635%
Drawdown
0.300%
Expectancy
0.148
Net Profit
0.262%
Sharpe Ratio
1.216
Loss Rate
44%
Win Rate
56%
Profit-Loss Ratio
1.07
Alpha
0.006
Beta
0.001
Annual Standard Deviation
0.005
Annual Variance
0
Information Ratio
-1.894
Tracking Error
0.128
Treynor Ratio
4.481
Total Fees
$90.00
from datetime import timedelta

class Algorithm(QCAlgorithm):

    def Initialize(self):
        self.SetStartDate(2019, 1, 1)
        self.SetEndDate(2019, 5, 31)
        self.SetCash(100000)
        self.syl = 'SPY'
        self.equity = self.AddEquity(self.syl, Resolution.Minute)
        #equity.SetDataNormalizationMode(DataNormalizationMode.Raw)
        self.SetBenchmark(self.equity.Symbol)
        
        self.PrevClose = None
        
        self.Schedule.On(self.DateRules.EveryDay("SPY"), \
            self.TimeRules.BeforeMarketClose("SPY", 45), \
            Action(self.Open))
            
        self.Schedule.On(self.DateRules.EveryDay("SPY"), \
            self.TimeRules.BeforeMarketClose("SPY", 5), \
            Action(self.Close))
            
        self.Schedule.On(self.DateRules.EveryDay("SPY"), \
            self.TimeRules.BeforeMarketClose("SPY", 0), \
            Action(self.Update))
     
    def Open(self):
        #if self.UtcTime.weekday()==4:
        #    self.Log(self.UtcTime)
        #    return
        if self.PrevClose != None:
            if (self.equity.Price - self.PrevClose)/self.PrevClose > 0.005:
                self.MarketOrder(self.equity.Symbol, -100)
                self.StopMarketOrder(self.equity.Symbol, 100, self.equity.Price*1.002)
                return
            elif (self.equity.Price - self.PrevClose)/self.PrevClose < -0.005:
                self.MarketOrder(self.equity.Symbol, 100)
                self.StopMarketOrder(self.equity.Symbol, -100, self.equity.Price*0.998)
                return

    def Close(self):
        self.Liquidate()
        openOrders = self.Transactions.GetOpenOrders()
        
        if len(openOrders) > 0:
            for x in openOrders:
                self.Transactions.CancelOrder(x.Id)
                self.Log(str(x.Id) + " cancelled")
                
    def Update(self):
        self.PrevClose = self.equity.Price

    def OnData(self,data):
        self.data = data

        pass