Overall Statistics
Total Trades
10
Average Win
10.04%
Average Loss
0%
Compounding Annual Return
170.323%
Drawdown
7.900%
Expectancy
0
Net Profit
52.149%
Sharpe Ratio
4.769
Probabilistic Sharpe Ratio
94.086%
Loss Rate
0%
Win Rate
100%
Profit-Loss Ratio
0
Alpha
1.211
Beta
-0.41
Annual Standard Deviation
0.259
Annual Variance
0.067
Information Ratio
3.423
Tracking Error
0.38
Treynor Ratio
-3.018
Total Fees
$338.55
from Selection.FutureUniverseSelectionModel import FutureUniverseSelectionModel
from datetime import date, timedelta

class FrontMonthUniverseSelectionModel(FutureUniverseSelectionModel):
    
    def __init__(self, select_future_chain_symbols):
        super().__init__(timedelta(1), select_future_chain_symbols)

    def Filter(self, filter):
        return (filter.FrontMonth())
from math import floor

class FuturesHarryBrownStyle(QCAlgorithm):

    def Initialize(self):
        self.SetStartDate(2010, 4, 20) 
        self.SetEndDate(2010, 9, 20)
        self.SetCash(1000000) 
        self.Settings.FreePortfolioValuePercentage = 0.3
        
        self.gold = self.AddSecurity(SecurityType.Future, "GC", Resolution.Minute) 
        self.gold.SetFilter(0, 90)
        self.spEmini = self.AddFuture(Futures.Indices.SP500EMini)  
        self.spEmini.SetFilter(0, 90)
        self.bonds = self.AddFuture(Futures.Financials.Y30TreasuryBond)  
        self.bonds.SetFilter(0, 90)
        
        self.currentHoldings = {}

    
    def OnMarginCallWarning(self):
        self.Error("You received a margin call warning..")


    def OnData(self, slice):
        for chain in slice.FutureChains:
            self.popularContracts = [contract for contract in chain.Value if contract.LastPrice > 0 ]#and contract.OpenInterest > 10]
  
            if len(self.popularContracts) == 0:
                continue
        
            sortedByOIContracts = sorted(self.popularContracts, key=lambda k : k.OpenInterest, reverse=True)
            self.liquidContract = sortedByOIContracts[0]
            underlying = str((self.liquidContract))[:2]
            
            if underlying not in self.currentHoldings:
                self.SetHoldings(self.liquidContract.Symbol, 0.08)
                #self.Debug(f"New Position: {self.liquidContract.Symbol}. Expiry: {self.liquidContract.Expiry}")
                self.currentHoldings[underlying] = self.liquidContract
                #self.Portfolio.LogMarginInformation
                
            elif self.liquidContract.Expiry > self.currentHoldings[underlying].Expiry:
                # Sell the old & Delete asset from currentHoldings (for cleancode)
                self.Liquidate(self.currentHoldings[underlying].Symbol) 
                #self.Debug(f"Old contract {self.currentHoldings[underlying].Symbol} liquidated. Expiry: {self.currentHoldings[underlying].Expiry}")
                del self.currentHoldings[underlying] 
                
                # Buy the new, and update currentHoldingsDict
                self.SetHoldings(self.liquidContract.Symbol, 0.08)
                #self.Debug(f"New Position: {self.liquidContract.Symbol}. Expiry: {self.liquidContract.Expiry}")
                self.currentHoldings[underlying] = self.liquidContract
                #self.Portfolio.LogMarginInformation