Overall Statistics Total Trades1Average Win0%Average Loss0%Compounding Annual Return0%Drawdown0%Expectancy0Net Profit0%Sharpe Ratio0Probabilistic Sharpe Ratio0%Loss Rate0%Win Rate0%Profit-Loss Ratio0Alpha0Beta0Annual Standard Deviation0Annual Variance0Information Ratio0Tracking Error0Treynor Ratio0Total Fees\$479.15
from math import floor

class BasicTemplateFuturesAlgorithm(QCAlgorithm):

def Initialize(self):
self.SetStartDate(2020, 10, 20)
self.SetCash(1000000)
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

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)