Overall Statistics
Total Trades
761
Average Win
0.31%
Average Loss
-0.25%
Compounding Annual Return
-83.956%
Drawdown
13.700%
Expectancy
-0.140
Net Profit
-12.660%
Sharpe Ratio
-4.88
Probabilistic Sharpe Ratio
0.088%
Loss Rate
61%
Win Rate
39%
Profit-Loss Ratio
1.21
Alpha
-0.725
Beta
0.125
Annual Standard Deviation
0.157
Annual Variance
0.025
Information Ratio
-1.79
Tracking Error
0.242
Treynor Ratio
-6.124
Total Fees
$1407.85
class FuturesAlgo(QCAlgorithm):

    def Initialize(self):
        self.SetStartDate(2016, 1, 5) 
        self.SetEndDate(2016, 1, 31) 
        self.SetCash(100000)
        self.es = self.AddFuture(Futures.Indices.SP500EMini, Resolution.Minute)
        # Filter contracts to only front month contract
        self.es.SetFilter(lambda x : x.FrontMonth())
        # 5 period SMA
        self.rsi = self.RSI(self.es.Symbol, 14, MovingAverageType.Wilders, Resolution.Minute)
        #self.ema = ExponentialMovingAverage(200)
        # Pointer to track front month contract
        self.frontmonthContract = None
        self.lastrsi = None
        
    def OnData(self, data):
        
        for chain in data.FutureChains:
            # Trades bars for contracts in chain
            tradebars = chain.Value.TradeBars
            
            contracts = [contract for contract in chain.Value]
            
            # If front month contract is updated
            if self.frontmonthContract == None or contracts[0].Symbol != self.frontmonthContract.Symbol:
                # contracts has only 1 element, the front month contract
                self.frontmonthContract = contracts[0]
            
            symbol = self.frontmonthContract.Symbol
            price = 0
            if symbol in tradebars.Keys:   
                tradebar = tradebars[symbol]
                price = tradebar.Close
                self.rsi.Update(tradebar.EndTime, tradebar.Close)
                
            if self.rsi.IsReady:
                if self.lastrsi is None:
                    self.lastrsi = self.rsi.Current.Value
                    return 
           
                if self.lastrsi < 30 and self.rsi.Current.Value > 30:
                    if not self.Portfolio[symbol].Invested:
                        self.OrderTick = self.MarketOrder(symbol, 1)
                        self.Debug(f"LONG, Bid: {self.frontmonthContract.BidPrice}, Ask: {self.frontmonthContract.AskPrice}, Last: {self.frontmonthContract.LastPrice}, FillPrice: {self.OrderTick.AverageFillPrice}, {self.rsi.Current.Value}")
                
                if self.lastrsi > 70 and self.rsi.Current.Value < 70:
                    if not self.Portfolio[symbol].Invested:
                        self.OrderTick = self.MarketOrder(symbol, -1)
                        self.Debug(f"SHORT, Bid: {self.frontmonthContract.BidPrice}, Ask: {self.frontmonthContract.AskPrice}, Last: {self.frontmonthContract.LastPrice}, FillPrice: {self.OrderTick.AverageFillPrice}, {self.rsi.Current.Value}")
                
                if self.Portfolio[symbol].Invested:
                    if self.Portfolio[self.OrderTick.Symbol].UnrealizedProfit > 250:
                        if self.Portfolio[self.OrderTick.Symbol].IsLong:
                            self.MarketOrder(self.OrderTick.Symbol, -1)
                            self.Debug("Out of long for 400")
                        else:
                            self.MarketOrder(self.OrderTick.Symbol, 1)
                            self.Debug("Out of short for 400")
                    
                    elif self.Portfolio[self.OrderTick.Symbol].UnrealizedProfit < -200:
                        if self.Portfolio[self.OrderTick.Symbol].IsLong:
                            OrderSell = self.MarketOrder(self.OrderTick.Symbol, -1)
                            self.Debug(f"LONG EXIT, Ask: {self.frontmonthContract.AskPrice}, FillPrice: {OrderSell.AverageFillPrice}")
                        else:
                             OrderSell = self.MarketOrder(self.OrderTick.Symbol, 1)
                             self.Debug(f"SHORT EXIT, Ask: {self.frontmonthContract.AskPrice}, FillPrice: {OrderSell.AverageFillPrice}")
                
                
                self.lastrsi = self.rsi.Current.Value