Overall Statistics
from clr import AddReference
AddReference("System")
AddReference("QuantConnect.Algorithm")
AddReference("QuantConnect.Common")

from System import *
from QuantConnect import *
from QuantConnect.Data import *
from QuantConnect.Algorithm import *
from QuantConnect.Indicators import *
from QuantConnect.Securities import *
from QuantConnect.Data.Consolidators import *
from datetime import timedelta

class BasicTemplateFuturesConsolidationAlgorithm(QCAlgorithm):
    
    _futureContracts = []

    def Initialize(self):
        self.SetStartDate(2018, 11, 1)
        self.SetEndDate(2018, 12, 31)
        self.SetCash(1000000)

        # Subscribe and set our expiry filter for the futures chain
        future = self.AddFuture(Futures.Indices.SP500EMini)
        #future.SetFilter(timedelta(0), timedelta(182))
        #future = self.AddSecurity(SecurityType.Future, "ES", Resolution.Minute);
        #future = self.AddFuture("ES",Resolution.Minute)
        

    def OnData(self,slice):
        for chain in slice.FutureChains:
            for contract in chain.Value:
                if contract.Symbol not in self._futureContracts:
                    self._futureContracts.append(contract.Symbol)

                    consolidator = QuoteBarConsolidator(timedelta(minutes=5))
                    consolidator.DataConsolidated += self.OnDataConsolidated
                    self.SubscriptionManager.AddConsolidator(contract.Symbol, consolidator)
          
        self.Log("Data" + str(slice))

    def OnDataConsolidated(self, sender, quoteBar):
        self.Log("OnDataConsolidated called on " + str(self.Time))
        self.Log(str(quoteBar))
from clr import AddReference
AddReference("System")
AddReference("QuantConnect.Algorithm")
AddReference("QuantConnect.Common")

from System import *
from QuantConnect import *
from QuantConnect.Data import *
from QuantConnect.Algorithm import *
from QuantConnect.Indicators import *
from QuantConnect.Securities import *
from QuantConnect.Data.Consolidators import *
from datetime import timedelta
import decimal

class BasicTemplateFuturesConsolidationAlgorithm(QCAlgorithm):
    
    vState = 'FLAT'
    symbol = '/ESZ18'
    ha = HeikinAshi
    
    def Initialize(self):
        self.SetStartDate(2018, 12, 1)
        self.SetEndDate(2018, 12, 2)
        self.SetCash(1000000)

        # Subscribe and set our expiry filter for the futures chain
        future = self.AddFuture(Futures.Indices.SP500EMini) 
        self.window = RollingWindow[QuoteBar](2)
        
        self.HA_open_win = RollingWindow[IndicatorDataPoint](10)
        self.HA_close_win = RollingWindow[IndicatorDataPoint](10)
        
        self._futureContracts = []

    def OnData(self,slice):
        for chain in slice.FutureChains:
            for contract in chain.Value:
                if ((contract.Symbol not in self._futureContracts)):
                    self._futureContracts.append(contract.Symbol)
                    self.Log("symbol: {0}".format(contract))
                    
                    self.HA = HeikinAshi(contract.Symbol)
                    
                    consolidator = QuoteBarConsolidator(timedelta(minutes=2))
                    consolidator.DataConsolidated += self.OnDataConsolidated
                    self.SubscriptionManager.AddConsolidator(contract.Symbol, consolidator)
                    self.RegisterIndicator(contract.Symbol, self.HA,  consolidator)


    def OnDataConsolidated(self, sender, data):
        #self.Log("OnDataConsolidated called on " + str(self.Time))
        #self.Log(f"BAR - {data.Time} - {data.Open} {data.Close} {data.High} {data.Low}")
        self.Log("Data: {0}".format(data))
        
        
        if not (self.HA.IsReady): return
        self.window.Add(data)
    
        #self.HA_open_win.Add(data) 
        #self.HA_close_win.Add(data)
        
        if not (self.HA_open_win.IsReady):return
        if not (self.HA_close_win.IsReady):return
    
        self.Log(self.HA_open_win[0])
        
        currBar = data[0]                     # Current bar had index zero.
        pastBar = data[1]                     # Past bar has index one.
        
        #self.Log("Price: {0} -> {1} ... {2} -> {3}".format(pastBar.Time, pastBar.Close, currBar.Time, currBar.Close))
              
        if self.vState == "FLAT":
            if(currBar.Close > pastBar.Close + 1):
                ### = BTO signal
                self.MarketOrder(currBar.Symbol , 1)
                self.vState = "LONG"
                self.Log("vState: {0}".format(self.vState))
            elif(currBar.Close < pastBar.Close - 1):
                ### = STO signal
                self.MarketOrder(currBar.Symbol , -1)
                self.vState = "SHORT"
                self.Log("vState: {0}".format(self.vState))
        elif self.vState == "LONG":
            if(currBar.Close < currBar.Open):
                ### = STC signal
                self.MarketOrder(currBar.Symbol , -1)
                self.vState = "FLAT"
                self.Log("vState: {0}".format(self.vState))
        elif self.vState == "SHORT":
            if(currBar.Close > currBar.Open):
                ### = BTC signal
                self.MarketOrder(currBar.Symbol , 1)
                self.vState = "FLAT"
                self.Log("vState: {0}".format(self.vState))
### set before initiak=lise function

vState = "FLAT"
symbol = 'ES'



### include in:  OnData(self,slice):
### you previously had it in:  def OnDataConsolidated(self, sender, data): don't know if that is correct

global vState
        
        if vState == "FLAT":
            if(currBar.Open > (pastBar.Open + 1) and currBar.Close > pastBar.Close):
                ### = BTO signal
                self.MarketOrder(currBar.Symbol , 1)
                vState = "LONG"
            #elif(currBar.Close < pastBar.Close - 1):
            elif(currBar.Open < (pastBar.Open - 1) and currBar.Close < pastBar.Close):
                ### = STO signal
                self.MarketOrder(currBar.Symbol , -1)
                vState = "SHORT"
                
        elif vState == "LONG":
            if(currBar.Close < currBar.Open):
                ### = STC signal
                self.MarketOrder(currBar.Symbol , -1)
                vState = "FLAT"
                
        elif vState == "SHORT":
            if(currBar.Close > currBar.Open):
                ### = BTC signal
                self.MarketOrder(currBar.Symbol , 1)
                vState = "FLAT"### set before initiak=lise function

vState = "FLAT"
symbol = 'ES'



### include in:  OnData(self,slice):
### you previously had it in:  def OnDataConsolidated(self, sender, data): don't know if that is correct

global vState
        
        if vState == "FLAT":
            if(currBar.Open > (pastBar.Open + 1) and currBar.Close > pastBar.Close):
                ### = BTO signal
                self.MarketOrder(currBar.Symbol , 1)
                vState = "LONG"
            #elif(currBar.Close < pastBar.Close - 1):
            elif(currBar.Open < (pastBar.Open - 1) and currBar.Close < pastBar.Close):
                ### = STO signal
                self.MarketOrder(currBar.Symbol , -1)
                vState = "SHORT"
                
        elif vState == "LONG":
            if(currBar.Close < currBar.Open):
                ### = STC signal
                self.MarketOrder(currBar.Symbol , -1)
                vState = "FLAT"
                
        elif vState == "SHORT":
            if(currBar.Close > currBar.Open):
                ### = BTC signal
                self.MarketOrder(currBar.Symbol , 1)
                vState = "FLAT"### set before initiak=lise function

vState = "FLAT"
symbol = 'ES'



### include in:  OnData(self,slice):
### you previously had it in:  def OnDataConsolidated(self, sender, data): don't know if that is correct

global vState
        
        if vState == "FLAT":
            if(currBar.Open > (pastBar.Open + 1) and currBar.Close > pastBar.Close):
                ### = BTO signal
                self.MarketOrder(currBar.Symbol , 1)
                vState = "LONG"
            #elif(currBar.Close < pastBar.Close - 1):
            elif(currBar.Open < (pastBar.Open - 1) and currBar.Close < pastBar.Close):
                ### = STO signal
                self.MarketOrder(currBar.Symbol , -1)
                vState = "SHORT"
                
        elif vState == "LONG":
            if(currBar.Close < currBar.Open):
                ### = STC signal
                self.MarketOrder(currBar.Symbol , -1)
                vState = "FLAT"
                
        elif vState == "SHORT":
            if(currBar.Close > currBar.Open):
                ### = BTC signal
                self.MarketOrder(currBar.Symbol , 1)
                vState = "FLAT"### set before initiak=lise function

vState = "FLAT"
symbol = 'ES'



### include in:  OnData(self,slice):
### you previously had it in:  def OnDataConsolidated(self, sender, data): don't know if that is correct

global vState
        
        if vState == "FLAT":
            if(currBar.Open > (pastBar.Open + 1) and currBar.Close > pastBar.Close):
                ### = BTO signal
                self.MarketOrder(currBar.Symbol , 1)
                vState = "LONG"
            #elif(currBar.Close < pastBar.Close - 1):
            elif(currBar.Open < (pastBar.Open - 1) and currBar.Close < pastBar.Close):
                ### = STO signal
                self.MarketOrder(currBar.Symbol , -1)
                vState = "SHORT"
                
        elif vState == "LONG":
            if(currBar.Close < currBar.Open):
                ### = STC signal
                self.MarketOrder(currBar.Symbol , -1)
                vState = "FLAT"
                
        elif vState == "SHORT":
            if(currBar.Close > currBar.Open):
                ### = BTC signal
                self.MarketOrder(currBar.Symbol , 1)
                vState = "FLAT"
from clr import AddReference
AddReference("System")
AddReference("QuantConnect.Algorithm")
AddReference("QuantConnect.Common")

from System import *
from QuantConnect import *
from QuantConnect.Data import *
from QuantConnect.Algorithm import *
from QuantConnect.Indicators import *
from QuantConnect.Securities import *
from QuantConnect.Data.Consolidators import *
from datetime import timedelta

class BasicTemplateFuturesConsolidationAlgorithm(QCAlgorithm):
    def Initialize(self):
        self.SetStartDate(2018, 11, 1)
        self.SetEndDate(2018, 12, 31)
        self.SetCash(1000000)
        
        #future = self.AddFuture(Futures.Indices.SP500EMini)
        future = self.AddFuture("ES",Resolution.Minute)
        consolidator = TradeBarConsolidator(timedelta(minutes=2))
        consolidator.DataConsolidated += self.OnDataConsolidated
        self.SubscriptionManager.AddConsolidator(future, consolidator)
        
    def OnDataConsolidated(self, sender, bar):
        self.Debug(str(self.Time) + " > New Bar!")
### set before initiak=lise function

vState = "FLAT"
symbol = 'ES'



### include in:  OnData(self,slice):
### you previously had it in:  def OnDataConsolidated(self, sender, data): don't know if that is correct

global vState
        
        if vState == "FLAT":
            if(currBar.Open > (pastBar.Open + 1) and currBar.Close > pastBar.Close):
                ### = BTO signal
                self.MarketOrder(currBar.Symbol , 1)
                vState = "LONG"
            #elif(currBar.Close < pastBar.Close - 1):
            elif(currBar.Open < (pastBar.Open - 1) and currBar.Close < pastBar.Close):
                ### = STO signal
                self.MarketOrder(currBar.Symbol , -1)
                vState = "SHORT"
                
        elif vState == "LONG":
            if(currBar.Close < currBar.Open):
                ### = STC signal
                self.MarketOrder(currBar.Symbol , -1)
                vState = "FLAT"
                
        elif vState == "SHORT":
            if(currBar.Close > currBar.Open):
                ### = BTC signal
                self.MarketOrder(currBar.Symbol , 1)
                vState = "FLAT"### set before initiak=lise function

vState = "FLAT"
symbol = 'ES'



### include in:  OnData(self,slice):
### you previously had it in:  def OnDataConsolidated(self, sender, data): don't know if that is correct

global vState
        
        if vState == "FLAT":
            if(currBar.Open > (pastBar.Open + 1) and currBar.Close > pastBar.Close):
                ### = BTO signal
                self.MarketOrder(currBar.Symbol , 1)
                vState = "LONG"
            #elif(currBar.Close < pastBar.Close - 1):
            elif(currBar.Open < (pastBar.Open - 1) and currBar.Close < pastBar.Close):
                ### = STO signal
                self.MarketOrder(currBar.Symbol , -1)
                vState = "SHORT"
                
        elif vState == "LONG":
            if(currBar.Close < currBar.Open):
                ### = STC signal
                self.MarketOrder(currBar.Symbol , -1)
                vState = "FLAT"
                
        elif vState == "SHORT":
            if(currBar.Close > currBar.Open):
                ### = BTC signal
                self.MarketOrder(currBar.Symbol , 1)
                vState = "FLAT"### set before initiak=lise function

vState = "FLAT"
symbol = 'ES'



### include in:  OnData(self,slice):
### you previously had it in:  def OnDataConsolidated(self, sender, data): don't know if that is correct

global vState
        
        if vState == "FLAT":
            if(currBar.Open > (pastBar.Open + 1) and currBar.Close > pastBar.Close):
                ### = BTO signal
                self.MarketOrder(currBar.Symbol , 1)
                vState = "LONG"
            #elif(currBar.Close < pastBar.Close - 1):
            elif(currBar.Open < (pastBar.Open - 1) and currBar.Close < pastBar.Close):
                ### = STO signal
                self.MarketOrder(currBar.Symbol , -1)
                vState = "SHORT"
                
        elif vState == "LONG":
            if(currBar.Close < currBar.Open):
                ### = STC signal
                self.MarketOrder(currBar.Symbol , -1)
                vState = "FLAT"
                
        elif vState == "SHORT":
            if(currBar.Close > currBar.Open):
                ### = BTC signal
                self.MarketOrder(currBar.Symbol , 1)
                vState = "FLAT"### set before initiak=lise function

vState = "FLAT"
symbol = 'ES'



### include in:  OnData(self,slice):
### you previously had it in:  def OnDataConsolidated(self, sender, data): don't know if that is correct

global vState
        
        if vState == "FLAT":
            if(currBar.Open > (pastBar.Open + 1) and currBar.Close > pastBar.Close):
                ### = BTO signal
                self.MarketOrder(currBar.Symbol , 1)
                vState = "LONG"
            #elif(currBar.Close < pastBar.Close - 1):
            elif(currBar.Open < (pastBar.Open - 1) and currBar.Close < pastBar.Close):
                ### = STO signal
                self.MarketOrder(currBar.Symbol , -1)
                vState = "SHORT"
                
        elif vState == "LONG":
            if(currBar.Close < currBar.Open):
                ### = STC signal
                self.MarketOrder(currBar.Symbol , -1)
                vState = "FLAT"
                
        elif vState == "SHORT":
            if(currBar.Close > currBar.Open):
                ### = BTC signal
                self.MarketOrder(currBar.Symbol , 1)
                vState = "FLAT"