| Overall Statistics |
|
Total Trades 9 Average Win 16.18% Average Loss -25.88% Compounding Annual Return 16.253% Drawdown 34.600% Expectancy 0.219 Net Profit 16.301% Sharpe Ratio 0.624 Probabilistic Sharpe Ratio 31.770% Loss Rate 25% Win Rate 75% Profit-Loss Ratio 0.63 Alpha 0.03 Beta 0.732 Annual Standard Deviation 0.272 Annual Variance 0.074 Information Ratio -0.118 Tracking Error 0.181 Treynor Ratio 0.231 Total Fees $99.90 |
from math import floor
class BasicTemplateFuturesAlgorithm(QCAlgorithm):
def Initialize(self):
self.SetBrokerageModel(BrokerageName.InteractiveBrokersBrokerage, AccountType.Cash)
self.SetStartDate(2020, 1, 1)
self.SetEndDate(2020, 12, 31)
self.SetCash(1000000)
self.ticker = "ES"
self.sp500 = self.AddFuture(self.ticker, 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]
# self.future = self.Securities[self.liquidContract.Symbol]
# self.contractsToBuy = floor( self.Portfolio.MarginRemaining / ((self.liquidContract.AskPrice*self.future.SymbolProperties.ContractMultiplier)*self.cashRatio) )
# self.MarketOrder(self.liquidContract.Symbol, self.contractsToBuy)
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