Overall Statistics
from datetime import timedelta

### <summary>
### This example demonstrates how to get access to futures history for a given root symbol.
### It also shows how you can prefilter contracts easily based on expirations, and inspect the futures
### chain to pick a specific contract to trade.
### </summary>
class BasicTemplateFuturesHistoryAlgorithm(QCAlgorithm):

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

        # Subscribe and set our expiry filter for the futures chain
        # find the front contract expiring no earlier than in 90 days
        futureES = self.AddFuture(Futures.Indices.SP500EMini, Resolution.Minute)
        futureES.SetFilter(timedelta(0), timedelta(182))

        futureGC = self.AddFuture(Futures.Metals.Gold, Resolution.Minute)
        futureGC.SetFilter(timedelta(0), timedelta(182))
        self.SetBenchmark("SPY")

    def OnData(self,slice):
        if not self.Portfolio.Invested:
            for chain in slice.FutureChains:
                for contract in chain.Value:
                    """
                    self.Log("{0},Bid={1} Ask={2} Last={3} OI={4}".format(
                            contract.Symbol.Value,
                            contract.BidPrice,
                            contract.AskPrice,
                            contract.LastPrice,
                            contract.OpenInterest))
                    """


    def OnSecuritiesChanged(self, changes):
        #if changes == SecurityChanges.None: return
        for change in changes.AddedSecurities:
            history = self.History(change.Symbol, 10, Resolution.Daily).sort_index(level='time', ascending=False)[:3]
            self.Log(change.Symbol)
        
            for i in range(len(history)):
                self.Log("History: " + str(history.iloc[i].name[0])
                        + ": " + str(history.iloc[i].name[1].strftime("%m/%d/%Y %I:%M:%S %p"))
                        + " > " + str(history.iloc[i]['close']))
                        
    def OnOrderEvent(self, orderEvent):
        # Order fill event handler. On an order fill update the resulting information is passed to this method.
        # Order event details containing details of the events
        self.Log(str(orderEvent))