Overall Statistics
Total Trades
12
Average Win
9.06%
Average Loss
-4.59%
Compounding Annual Return
203.019%
Drawdown
8.600%
Expectancy
1.377
Net Profit
59.660%
Sharpe Ratio
5.073
Probabilistic Sharpe Ratio
94.582%
Loss Rate
20%
Win Rate
80%
Profit-Loss Ratio
1.97
Alpha
1.4
Beta
-0.465
Annual Standard Deviation
0.282
Annual Variance
0.079
Information Ratio
3.731
Tracking Error
0.4
Treynor Ratio
-3.075
Total Fees
$401.45
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.gold.SetFilter(lambda x: x.FrontMonth())
        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.Schedule.On(self.DateRules.MonthEnd("SPY"), \
         #        self.TimeRules.AfterMarketOpen("SPY"), \
          #       self.RebalancingCode)

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


    def OnData(self, slice):
       # Each asset we subscribe to will return a 'chain'.
       # Each chain will have the different expiry contracts for that asset. 
       # We access the chain with chain.Value
        for chain in slice.FutureChains:
            self.popularContracts = [contract for contract in chain.Value if contract.OpenInterest > 1000]
  
            if len(self.popularContracts) == 0:
                continue
    
            sortedByOIContracts = sorted(self.popularContracts, key=lambda k : k.OpenInterest, reverse=True)
            self.liquidContract = sortedByOIContracts[0]
            self.Debug(f"The contract with the greatest open interest is: {self.liquidContract} with an expiry: {self.liquidContract.Expiry}")
           
            # Check if the "asset" is in our portfolio.
            # A Futures asset can have multiple contracts... 
            # The problem arises where I already have a May ES contract and the algo goes and buys a June ES contract too
            # We need to check if ANY of those contracts are in our portfolio
            if not self.Portfolio[self.liquidContract.Symbol].Invested:
                self.Debug(f"The contract with the greatest open interest is: {self.liquidContract} with an expiry: {self.liquidContract.Expiry}")
                future = self.Securities[self.liquidContract.Symbol]
                self.notionalValue = self.liquidContract.AskPrice * future.SymbolProperties.ContractMultiplier 
                self.Debug(f"The value of one {self.liquidContract.Symbol} contract is: {str(self.notionalValue)}")
                self.SetHoldings(self.liquidContract.Symbol, 0.08)

                
#    def RebalancingCode(self):
        
 #       for asset in self.Portfolio.Invested:
  #          self.SetHoldings(asset.Symbol, 0.08)
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())