Overall Statistics
Total Trades
923
Average Win
0.07%
Average Loss
-0.06%
Compounding Annual Return
-57.380%
Drawdown
4.200%
Expectancy
-0.098
Net Profit
-2.537%
Sharpe Ratio
-5.737
Loss Rate
58%
Win Rate
42%
Profit-Loss Ratio
1.14
Alpha
-0.728
Beta
0.541
Annual Standard Deviation
0.112
Annual Variance
0.012
Information Ratio
-7.197
Tracking Error
0.111
Treynor Ratio
-1.184
Total Fees
$2722.62
from QuantConnect.Data.Market import TradeBar
from datetime import *
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, 10, 14)  # Set Start Date
        self.SetEndDate(2017, 10, 24)
        self.SetCash(100000)  # Set Strategy Cash
        
        self.symbolData = dict()
        
        for ticker in ["AAPL", "FB"]:
            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.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))
                        
    def OnMinuteData(self, sender, bar):
        self.symbolData[bar.Symbol].minute_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
            
    def Tensec(self):

            for symbol in self.symbolData:
                window = self.symbolData[symbol].window
                minute = self.symbolData[symbol].minute_rw
                if not (window.IsReady and minute.IsReady): continue
                if (self.Securities[symbol].Exchange.ExchangeOpen):
                        
                    if self.Time.second == 10:
                    
                        if minute[0].Volume > minute[1].Volume > minute[2].Volume:
                            self.SetHoldings(symbol, 1) 
                            
                        if self.Portfolio[symbol].IsLong and minute[0].Volume < minute[1].Volume < minute[2].Volume:
                            self.Liquidate(symbol)

    def End_of_day_liquidate(self):
        self.Liquidate()
            
class SymbolData(object):
            
    def __init__(self):
        self.minute_rw = RollingWindow[TradeBar](15)
        self.window = RollingWindow[TradeBar](5)