Overall Statistics
Total Trades
399
Average Win
0.10%
Average Loss
-0.06%
Compounding Annual Return
-83.947%
Drawdown
3.600%
Expectancy
-0.196
Net Profit
-2.475%
Sharpe Ratio
-11.416
Loss Rate
69%
Win Rate
31%
Profit-Loss Ratio
1.55
Alpha
-1.285
Beta
-0.31
Annual Standard Deviation
0.11
Annual Variance
0.012
Information Ratio
-9.933
Tracking Error
0.116
Treynor Ratio
4.047
Total Fees
$2646.93
from QuantConnect.Data.Market import TradeBar
from datetime import timedelta
from System import *
from QuantConnect import *
from QuantConnect.Algorithm import *
from QuantConnect.Indicators import *
import decimal as d

class MyAlgorithm(QCAlgorithm):
    
    def Initialize(self):
        self.SetStartDate(2017, 9, 14)  # Set Start Date
        self.SetEndDate(2017, 9, 18)
        self.SetCash(100000)  # Set Strategy Cash
        
        self.symbolData = dict()
        
        for ticker in ["NVDA", "AAPL", "NVDA", "FB", "BABA", "MU", "MYL", "JD", "TEVA", "TVIX", "AMD"]:
            symbol = self.AddEquity(ticker, Resolution.Second).Symbol

            consolidator_daily = TradeBarConsolidator(timedelta(1))
            consolidator_daily.DataConsolidated += self.OnDailyData
            self.SubscriptionManager.AddConsolidator(symbol, consolidator_daily)

            consolidator_minute = TradeBarConsolidator(60)
            consolidator_minute.DataConsolidated += self.OnMinuteData
            self.SubscriptionManager.AddConsolidator(symbol, consolidator_minute)
            
            #consolidator_tensec = TradeBarConsolidator(10)
            #consolidator_tensec.DataConsolidated += self.OnTenSecondData
            #self.SubscriptionManager.AddConsolidator(symbol, consolidator_tensec)
            
            self.symbolData[symbol] = SymbolData()

        #self.Schedule.On(self.DateRules.EveryDay(),
        #                 self.TimeRules.AfterMarketOpen('SPY', 1),
        #                 Action(self.one_minute_after_open_market))
                         
        #self.Schedule.On(self.DateRules.EveryDay(),
        #                 self.TimeRules.AfterMarketOpen('SPY', 5),
        #                 Action(self.before_close_market))
        
        self.Schedule.On(self.DateRules.EveryDay(), 
                        self.TimeRules.Every(timedelta(seconds=10)), 
                        Action(self.Tensec))

    # Add daily bar to daily rolling window
    def OnDailyData(self, sender, bar):
        self.symbolData[bar.Symbol].daily_rw.Add(bar)
        
    def OnMinuteData(self, sender, bar):
        self.symbolData[bar.Symbol].minute_rw.Add(bar)
    
    #def OnTenSecondData(self, sender, bar):
    #    self.symbolData[bar.Symbol].tensec_rw.Add(bar)


    def OnData(self, data):
        for symbol in data.Keys:
            if data[symbol] is None: continue
            # Create local variable to readability
            window = self.symbolData[symbol].window
            # Update the window. If not ready, continue
            window.Add(data[symbol])
            minute = self.symbolData[symbol].minute_rw
            if not (window.IsReady and minute.IsReady): continue
            #self.Log(str(window[0].Close))
            #if not (window[0].Close > 200):

                        
    def Tensec(self):
        
        for symbol in self.symbolData:
            # Create local variable to readability
            window = self.symbolData[symbol].window
            # Update the window. If not ready, continue
            minute = self.symbolData[symbol].minute_rw
            if not (window.IsReady and minute.IsReady): continue    
    
            if not self.Portfolio[symbol].Invested and self.Securities[symbol].Exchange.ExchangeOpen:
                last_bar = minute[0].Close - minute[0].Open
                l_high_div_open = minute[0].High / minute[0].Open
                l_low_div_close = minute[0].Low / minute[0].Close
                second_bar = minute[1].Close - minute[1].Open
                third_bar = minute[2].Close - minute[2].Open
                last_close = window[0].Close
                last_min_open = minute[0].Open
                last_min_close = minute[0].Close
                sec_min_open = minute[1].Open
                sec_min_close = minute[1].Close
                if (last_bar < 0 and second_bar < 0 and
                d.Decimal(0.9) < l_high_div_open < d.Decimal(1.1) and d.Decimal(0.9) < l_low_div_close < d.Decimal(1.1) and 
                last_close *d.Decimal(1.001) < minute[0].Close):  
                    self.SetHoldings(symbol, -1.0/3.0)
                    self.Log('shorting')
                    self.Log(str(symbol)+", "+str(last_bar)+", "+str(second_bar)+", "+str(last_min_open)+
                    ", "+str(last_min_close)+", "+str(sec_min_open)+", "+str(sec_min_close)+", "+str(last_close))
                    
            if self.Portfolio[symbol].Invested:
                    last_close = window[0].Close
                    last_min_open = minute[0].Open
                    last_min_close = minute[0].Close
                    last_bar = minute[0].Close - minute[0].Open
                    second_bar = minute[1].Close - minute[1].Open
                    if (last_close > last_min_close *d.Decimal(1.001)):
                        #self.Log(str(last_min_open)+", "+str(last_close))
                        self.Liquidate(symbol)
                        #self.Log('LIQUIDATE AT THRESHOLD REACHED.')
            
class SymbolData(object):
            
    def __init__(self):
        self.daily_rw = RollingWindow[TradeBar](5)
        self.minute_rw = RollingWindow[TradeBar](5)
        #self.tensec_rw = RollingWindow[TradeBar](5)
        self.window = RollingWindow[TradeBar](5)