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
Probabilistic 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
-5.684
Tracking Error
0.089
Treynor Ratio
0
Total Fees
$0.00
class EmaCrossFuturesFrontMonthAlgorithm(QCAlgorithm):

    def Initialize(self):
        self.SetStartDate(2013, 10, 8)
        self.SetEndDate(2013, 12, 10)
        self.SetCash(1000000)

        future = self.AddFuture(Futures.Metals.Gold);

        # Only consider the front month contract
        # Update the universe once per day to improve performance
        future.SetFilter(lambda x: x.FrontMonth().OnlyApplyFilterAtMarketOpen())

        # Symbol of the current contract
        self.symbol = None

        # Create two exponential moving averages
        self.fast = ExponentialMovingAverage(100)
        self.slow = ExponentialMovingAverage(300)
        self.tolerance = 0.001
        self.consolidator = None
        
        self.day = 0


    def OnData(self, data):
        if self.Time.day == self.day:
            return
        self.day = self.Time.day
        
        for chain in data.FutureChains.Values:
            contracts = [c for c in chain.Contracts]
            self.Plot("Contracts", "Number", len(contracts))
            

    def OnSecuritiesChanged(self, changes):
        if len(changes.RemovedSecurities) > 0:
            # Remove the consolidator for the previous contract
            # and reset the indicators
            if self.symbol is not None and self.consolidator is not None:
                self.SubscriptionManager.RemoveConsolidator(self.symbol, self.consolidator)
                self.fast.Reset()
                self.slow.Reset()
            # We don't need to call Liquidate(_symbol),
            # since its positions are liquidated because the contract has expired.

        # Only one security will be added: the new front contract
        self.symbol = changes.AddedSecurities[0].Symbol

        # Create a new consolidator and register the indicators to it
        self.consolidator = self.ResolveConsolidator(self.symbol, Resolution.Minute)
        self.RegisterIndicator(self.symbol, self.fast, self.consolidator)
        self.RegisterIndicator(self.symbol, self.slow, self.consolidator)

        #  Warm up the indicators
        self.WarmUpIndicator(self.symbol, self.fast, Resolution.Minute)
        self.WarmUpIndicator(self.symbol, self.slow, Resolution.Minute)