Overall Statistics
Total Trades
66
Average Win
0.07%
Average Loss
-0.12%
Compounding Annual Return
3.225%
Drawdown
0.500%
Expectancy
0.077
Net Profit
0.305%
Sharpe Ratio
1.697
Loss Rate
30%
Win Rate
70%
Profit-Loss Ratio
0.55
Alpha
0.018
Beta
0.026
Annual Standard Deviation
0.015
Annual Variance
0
Information Ratio
-4.836
Tracking Error
0.056
Treynor Ratio
1.003
Total Fees
$109.46
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_minute = TradeBarConsolidator(60)
            consolidator_minute.DataConsolidated += self.OnMinuteData
            self.SubscriptionManager.AddConsolidator(symbol, consolidator_minute)
            
            
            self.symbolData[symbol] = SymbolData()
            
            self.vwap[symbol]= self.VWAP(symbol, 23400)


        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(16, 00, 00),
                        Action(self.Resetting))
        
    def OnMinuteData(self, sender, bar):
        self.symbolData[bar.Symbol].minute_rw.Add(bar)

    def Resetting(self):
        for symbol in self.symbolData:
            self.vwap[symbol].Reset()

    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
                        
    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 self.Securities[symbol].Exchange.ExchangeOpen:
                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
                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
                thrd_min_close = minute[2].Close
                vwap = self.vwap[symbol].Current.Value
                if not self.Portfolio[symbol].Invested:
                    if (last_bar > 0 and second_bar > 0 and third_bar > 0 and last_min_open < vwap < last_close):  
                        self.SetHoldings(symbol, 1.0/3.0)
                        self.Log('long')
                        self.Log(str(symbol)+", "+str(last_bar)+", "+str(second_bar)+", "+str(symbol)+
                    ", "+str(symbol)+", "+str(symbol)+", "+str(symbol)+", "+str(vwap))
                if self.Portfolio[symbol].Invested:
                    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))):
                    last_min_close * d.Decimal(1.002) < vwap or
                    last_close > self.Portfolio[symbol].AveragePrice * d.Decimal(1.0015)):
                        self.Liquidate(symbol)
                        
    def End_of_day_liquidate(self):
        self.Liquidate()
            
class SymbolData(object):
            
    def __init__(self):
        self.minute_rw = RollingWindow[TradeBar](5)
        self.window = RollingWindow[TradeBar](5)