Overall Statistics |
Total Trades 16 Average Win 2.18% Average Loss -3.08% Compounding Annual Return 10.908% Drawdown 9.900% Expectancy 0.282 Net Profit 6.770% Sharpe Ratio 0.876 Loss Rate 25% Win Rate 75% Profit-Loss Ratio 0.71 Alpha -0.06 Beta 9.757 Annual Standard Deviation 0.086 Annual Variance 0.007 Information Ratio 0.715 Tracking Error 0.086 Treynor Ratio 0.008 Total Fees $29.60 |
from datetime import timedelta import decimal as d import numpy as np ### PSAR with SP500 E-mini futures ### In this example, we demostrate how to trade futures contracts using ### a equity to generate the trading signals ### It also shows how you can prefilter contracts easily based on expirations. ### It also shows how you can inspect the futures chain to pick a specific contract to trade. class futuresPSAR(QCAlgorithm): def Initialize(self): self.SetStartDate(2016, 1, 1) self.SetEndDate(2016, 8, 18) self.SetCash(100000) self.IsUpTrend = False self.IsDownTrend = False # Adds SPY to be used in PSAR self.symbol = "SPY" equity = self.AddEquity(self.symbol, Resolution.Daily) self._psar = self.PSAR(equity.Symbol, 0.02, 0.02, 0.2, Resolution.Daily) # Adds the future that will be traded and # set our expiry filter for this futures chain future = self.AddFuture(Futures.Indices.SP500EMini) future.SetFilter(timedelta(0), timedelta(182)) def OnData(self, slice): if not self._psar.IsReady: return self.IsUpTrend = self._psar.Current.Value > self.Portfolio[self.symbol].Price self.IsDownTrend = self._psar.Current.Value < self.Portfolio[self.symbol].Price if (not self.Portfolio.Invested) and self.IsUpTrend: for chain in slice.FuturesChains: contracts = None # find the front contract expiring no earlier than in 90 days contracts = filter(lambda x: x.Expiry > self.Time + timedelta(90), chain.Value) # if there is any contract, trade the front contract #if len(contracts) == 0: continue contract = sorted(contracts, key = lambda x: x.Expiry, reverse=True)[0] self.MarketOrder(contract.Symbol , 1) if self.Portfolio.Invested and self.IsDownTrend: self.Liquidate() def OnEndOfDay(self): if self.IsUpTrend: self.Plot("Indicator Signal", "EOD",1) elif self.IsDownTrend: self.Plot("Indicator Signal", "EOD",-1) elif self._psar.IsReady: self.Plot("Indicator Signal", "EOD",0) def OnOrderEvent(self, orderEvent): self.Log(str(orderEvent))