Overall Statistics |
Total Orders 170 Average Win 1.92% Average Loss -1.88% Compounding Annual Return 0.843% Drawdown 14.200% Expectancy 0.059 Start Equity 1000000 End Equity 1068748.82 Net Profit 6.875% Sharpe Ratio -0.282 Sortino Ratio -0.281 Probabilistic Sharpe Ratio 0.266% Loss Rate 48% Win Rate 52% Profit-Loss Ratio 1.02 Alpha -0.008 Beta 0.172 Annual Standard Deviation 0.036 Annual Variance 0.001 Information Ratio 0.028 Tracking Error 0.057 Treynor Ratio -0.06 Total Fees $716.30 Estimated Strategy Capacity $4000000.00 Lowest Capacity Asset GC Y9O6T2ED3VRX Portfolio Turnover 2.65% |
from AlgorithmImports import * class PredictionOnFuturesContango(QCAlgorithm): def Initialize(self): self.SetStartDate(2015, 8, 1) self.SetEndDate(2023, 7, 1) self.SetCash(1000000) # Subscribe and set our expiry filter for the futures chain self.futureGold = self.AddFuture( Futures.Metals.Gold, resolution = Resolution.Minute, dataNormalizationMode = DataNormalizationMode.BackwardsRatio, dataMappingMode = DataMappingMode.OpenInterest, contractDepthOffset = 0 ) # expiry between 0 and 90 days to avoid naked position stays for too long to tie up fund self.futureGold.SetFilter(0, 90) # 20-day SMA on return as the basis mean-reversion predictor self.roc = self.ROC(self.futureGold.Symbol, 1, Resolution.Daily) self.sma = IndicatorExtensions.Of(SimpleMovingAverage(20), self.roc) self.SetWarmUp(21, Resolution.Daily) ief = self.AddEquity("IEF").Symbol self.SetBenchmark(ief) def OnData(self, slice): if not self.Portfolio.Invested and not self.IsWarmingUp: # We only trade during last-day return is lower than average return if not self.roc.IsReady or not self.sma.IsReady or self.sma.Current.Value < self.roc.Current.Value: return spreads = {} for chain in slice.FutureChains: contracts = list(chain.Value) # if there is less than or equal 1 contracts, we cannot compare the spot price if len(contracts) < 2: continue # sort the contracts by expiry sorted_contracts = sorted(contracts, key=lambda x: x.Expiry) # compare the spot price for i, contract in enumerate(sorted_contracts): if i == 0: continue # compare the ask price for each contract having nearer term for j in range(i): near_contract = sorted_contracts[j] # get the spread and total cost (price of contracts and commission fee $1 x 2) horizontal_spread = contract.BidPrice - near_contract.AskPrice total_price = contract.BidPrice + near_contract.AskPrice + 2 spreads[(contract.Symbol, near_contract.Symbol)] = (horizontal_spread, total_price) # Select the pair with the lowest spread to trade for maximum potential contango if spreads: min_spread_pair = sorted(spreads.items(), key=lambda x: x[1][0])[0] far_contract, near_contract = min_spread_pair[0] # subscribe to the contracts to avoid removing from the universe self.AddFutureContract(far_contract, Resolution.Minute) self.AddFutureContract(near_contract, Resolution.Minute) num_of_contract = max((self.Portfolio.TotalPortfolioValue / min_spread_pair[1][1]) // self.futureGold.SymbolProperties.ContractMultiplier, 1) self.MarketOrder(far_contract, num_of_contract) self.MarketOrder(near_contract, -num_of_contract)