Overall Statistics
Total Trades
48
Average Win
0.25%
Average Loss
-0.21%
Compounding Annual Return
0.298%
Drawdown
1.600%
Expectancy
0.008
Net Profit
0.029%
Sharpe Ratio
0.076
Loss Rate
54%
Win Rate
46%
Profit-Loss Ratio
1.20
Alpha
0.066
Beta
-0.213
Annual Standard Deviation
0.043
Annual Variance
0.002
Information Ratio
-3.701
Tracking Error
0.079
Treynor Ratio
-0.016
Total Fees
$79.94
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, 1, 14)  # Set Start Date
        self.SetEndDate(2017, 02, 17)
        self.SetCash(100000)  # Set Strategy Cash
        
        self.symbolData = dict()
        self.vwap = dict()
        
        for ticker in ["BABA"]:
            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)
            
            
            self.symbolData[symbol] = SymbolData()
            
            self.vwap[symbol]= self.VWAP(symbol, 20000)


        self.Schedule.On(self.DateRules.EveryDay(), 
                        self.TimeRules.Every(timedelta(seconds=10)), 
                        Action(self.Tensec))
        
        self.Schedule.On(self.DateRules.EveryDay(),
                        self.TimeRules.At(15, 59, 52),
                        Action(self.End_of_day_liquidate))
                        
        self.Schedule.On(self.DateRules.EveryDay(),
                        self.TimeRules.At(9, 30, 00),
                        Action(self.Reset_counter))
                        

    # 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 Reset_counter(self):
        self.xe = 0

    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
                        
    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:
                #if self.xe > 0: self.xe = self.xe - 1 
                last_bar = minute[0].Close - minute[0].Open
                last_bar_wicks = minute[0].High - minute[0].Low
                second_bar = minute[1].Close - minute[1].Open
                third_bar = minute[2].Close - minute[2].Open
                fourth_bar = minute[3].Close - minute[3].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
                vwap = self.vwap[symbol]
                if (last_bar > 0 and second_bar > 0 and third_bar > 0 and last_min_open < last_close):  
                    self.SetHoldings(symbol, 1.0/3.0)
                    self.Log('shorting')
                    self.Log(str(symbol)+", "+str(last_bar)+", "+str(second_bar)+", "+str(symbol)+
                    ", "+str(symbol)+", "+str(symbol)+", "+str(symbol)+", "+str(vwap))
                    #self.xe = self.xe + 5
                if self.Portfolio[symbol].Invested:
                    if self.xe > 0: self.xe = self.xe - 1 
                    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 *d.Decimal(1.005) < self.Portfolio[symbol].AveragePrice  or
                    last_bar > 0 and last_min_open - last_close > last_bar *d.Decimal(2))):
                        self.Liquidate(symbol)
                       # self.xe = self.xe + 5

                        
    def End_of_day_liquidate(self):
        self.Liquidate()
            
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)