Overall Statistics
Total Trades
16
Average Win
2.92%
Average Loss
-2.45%
Compounding Annual Return
-13.667%
Drawdown
17.000%
Expectancy
-0.453
Net Profit
-8.880%
Sharpe Ratio
-1.167
Loss Rate
75%
Win Rate
25%
Profit-Loss Ratio
1.19
Alpha
-0.131
Beta
2.377
Annual Standard Deviation
0.084
Annual Variance
0.007
Information Ratio
-1.331
Tracking Error
0.084
Treynor Ratio
-0.041
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))
        
        # Set time zone
        self.SetTimeZone(TimeZones.NewYork)
        
        # Set exchange
        self.exchange = self.Securities["SPY"].Exchange;

    def OnData(self, slice):
        if not self._psar.IsReady: return
        # Only trade during regular market hours
        if not self.exchange.ExchangeOpen: 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.Log("self.Time.hour: "+str(self.Time.hour) + " - self.Time.minute  "+ str(self.Time.minute))
                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))