| Overall Statistics |
|
Total Trades 81 Average Win 5.42% Average Loss -8.23% Compounding Annual Return 12.273% Drawdown 34.500% Expectancy 0.368 Net Profit 218.645% Sharpe Ratio 0.732 Probabilistic Sharpe Ratio 14.148% Loss Rate 18% Win Rate 82% Profit-Loss Ratio 0.66 Alpha 0.006 Beta 0.846 Annual Standard Deviation 0.148 Annual Variance 0.022 Information Ratio -0.172 Tracking Error 0.076 Treynor Ratio 0.128 Total Fees $2484.55 |
from math import floor
class BasicTemplateFuturesAlgorithm(QCAlgorithm):
def Initialize(self):
self.SetBrokerageModel(BrokerageName.InteractiveBrokersBrokerage, AccountType.Cash)
self.SetStartDate(2010, 12, 3)
self.SetEndDate(2020, 12, 3)
self.SetCash(1000000)
self.sp500 = self.AddFuture(Futures.Indices.SP500EMini, Resolution.Minute)
self.sp500.SetFilter(0, 180)
self.SetBenchmark("SPY")
self.cashRatio = 1
#1. Widen the free portfolio percentage to avoid margin calls for futures
# self.Settings.FreePortfolioValuePercentage = 0.99
# self.Settings
def OnMarginCallWarning(self):
self.Error("You received a margin call warning..")
def OnMarginCall(self, requests):
self.Error("YOU MESSED UP, MARGIN CALLED.")
def OnData(self, slice):
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]
if not self.Portfolio.Invested:
# #2. Delete the old code for manually calculating the position size (lines 30-33)
# self.notionalValue = self.liquidContract.AskPrice*self.gold.SymbolProperties.ContractMultiplier
self.future = self.Securities[self.liquidContract.Symbol]
# future.Leverage
self.contractsToBuy = floor( self.Portfolio.MarginRemaining / ((self.liquidContract.AskPrice*self.future.SymbolProperties.ContractMultiplier)*self.cashRatio) )
# self.contractsToBuy = floor( self.Portfolio.MarginRemaining / self.future.BuyingPowerModel.InitialOvernightMarginRequirement )
# self.SetHoldings(self.liquidContract.Symbol, 1)
self.MarketOrder(self.liquidContract.Symbol, self.contractsToBuy)
#3. Replace the manually calculated math above with
# the SetHoldings function to automatically calculate your position size