Overall Statistics
Total Trades
11
Average Win
23.34%
Average Loss
-9.00%
Compounding Annual Return
34.079%
Drawdown
19.300%
Expectancy
0.197
Net Profit
13.175%
Sharpe Ratio
0.619
Probabilistic Sharpe Ratio
37.691%
Loss Rate
67%
Win Rate
33%
Profit-Loss Ratio
2.59
Alpha
0.272
Beta
0.895
Annual Standard Deviation
0.345
Annual Variance
0.119
Information Ratio
0.954
Tracking Error
0.292
Treynor Ratio
0.239
Total Fees
$492.10
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.usd = self.AddFuture(Futures.Currencies.CAD, Resolution.Minute)
        self.usd.SetFilter(30, 90)
        self.spEmini = self.AddFuture(Futures.Indices.SP500EMini)  
        self.spEmini.SetFilter(30, 90)
        self.bonds = self.AddFuture(Futures.Financials.Y30TreasuryBond)  
        self.bonds.SetFilter(30, 90)
        
        self.currentHoldings = {}

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


    def OnData(self, slice):
        # Delete delisted contracts we're holding
        for k in self.currentHoldings.copy():
            if self.currentHoldings[k].Expiry < self.Time:
                del self.currentHoldings[k]
        
        for chain in slice.FutureChains:
            self.popularContracts = [contract for contract in chain.Value if contract.LastPrice > 0 ]
  
            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 self.Portfolio[self.liquidContract.Symbol].Invested:
                continue
            
            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
                
            elif self.liquidContract.Expiry > self.currentHoldings[underlying].Expiry:
                # Sell the old & Delete asset from currentHoldings (for cleancode)
                #if self.currentHoldings[underlying].Expiry > self.Time:
                self.Liquidate(self.currentHoldings[underlying].Symbol) 
                self.Debug(f"Old contract {self.currentHoldings[underlying].Symbol} liquidated. Expiry: {self.currentHoldings[underlying].Expiry}")

                # 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
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())