Overall Statistics
Total Trades
0
Average Win
0%
Average Loss
0%
Compounding Annual Return
0%
Drawdown
0%
Expectancy
0
Net Profit
0%
Sharpe Ratio
0
Loss Rate
0%
Win Rate
0%
Profit-Loss Ratio
0
Alpha
0
Beta
0
Annual Standard Deviation
0
Annual Variance
0
Information Ratio
0
Tracking Error
0
Treynor Ratio
0
Total Fees
$0.00
from clr import AddReference
AddReference("System")
AddReference("QuantConnect.Algorithm")
AddReference("QuantConnect.Common")

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


class TestFuturesAlgorithm(QCAlgorithm):

    def Initialize(self):
        self.SetStartDate(2013, 10, 7)
        self.SetEndDate(2013, 10, 8)
        self.SetCash(1000)
        
        # spy_equity = self.AddSecurity(SecurityType.Equity, "SPY", Resolution.Tick).Symbol

        # for futures, one needs to retrieve data from Contracts rather than Symbol.
        # This is different from Equity.
        self.es_future = self.AddFuture(Futures.Indices.SP500EMini, Resolution.Tick)
        self.es_future.SetFilter(timedelta(0), timedelta(182))
        
        ## Dictionary to store the consolidators by contract symbol -- allows us to access them in other methods
        self.renkoConsolidators = {}


    def OnData(self,data):
        pass
    
    def HandleRenkoClose(self, sender, data):
        '''This function is called by our renkoClose consolidator defined in Initialize()
        Args:
            data: The new renko bar produced by the consolidator'''
        #if not self.Portfolio.Invested:
        #    self.SetHoldings(data.Symbol, 1)

        self.Log(f"CLOSE - {data.Time} - {data.Open} {data.Close}")
        
    def OnSecuritiesChanged(self, changes):
        for security in changes.AddedSecurities:
            consolidator = RenkoConsolidator(0.5, RenkoType.Wicked)  ## Create Renko consolidator
            consolidator.DataConsolidated += self.HandleRenkoClose  ## Define the handler function
            self.SubscriptionManager.AddConsolidator(security.Symbol, consolidator)  ## Add the consolidator to our main data handler
            self.renkoConsolidators[security.Symbol] = consolidator  ## Add consolidator to the dictionary, indexed by symbol
            
        for security in changes.RemovedSecurities:
            consolidator = self.renkoConsolidators.pop(security.Symbol)  ## Remove/assign consolidator
            self.SubscriptionManager.RemoveConsolidator(security.Symbol, consolidator)  ## Remove the consolidator from our data handler to keep memory usage low
            consolidator.DataConsolidated -= self.HandleRenkoClose  ## Remove handler function