Overall Statistics
Total Trades
1
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
0
Tracking Error
0
Treynor Ratio
0
Total Fees
$479.15
from math import floor

class BasicTemplateFuturesAlgorithm(QCAlgorithm):

    def Initialize(self):
        self.SetStartDate(2020, 10, 20) 
        self.SetCash(1000000) 
        self.lumber = self.AddFuture(Futures.Forestry.RandomLengthLumber) 
        self.lumber.SetFilter(0, 90)
 
    def OnData(self, slice):
      
        for chain in slice.FutureChains:
            contracts = [c for c in chain.Value]
            contracts = sorted(chain.Value, key=lambda k : k.OpenInterest, reverse=True)
            if len(contracts) == 0:
                return
            contract = contracts[0]
  
            if not self.Portfolio.Invested:
                
                #1. Save the notional value of the futures contract to self.notionalValue  
                notionalValue = contract.AskPrice * self.lumber.SymbolProperties.ContractMultiplier 
                self.Log(f"Ask Price: {contract.AskPrice}; Contract multiplier: {self.lumber.SymbolProperties.ContractMultiplier}; Notional Value: {notionalValue}")
                #self.Plot("Notional Value", "Value", notionalValue)
                
                #2. Save the contract security object to the variable future
                future = self.Securities[contract.Symbol]
                
                #3. Calculate the number of contracts we can afford based on the margin required
                # Divide the margin remaining by the initial margin and save to self.contractsToBuy
                self.contractsToBuy = floor( self.Portfolio.MarginRemaining / future.BuyingPowerModel.InitialOvernightMarginRequirement )
                
                #4. Make a market order for the number of contracts we calculated for that symbol
                self.MarketOrder(contract.Symbol, self.contractsToBuy)