Overall Statistics
Total Trades
231
Average Win
1.81%
Average Loss
-0.69%
Compounding Annual Return
18.354%
Drawdown
28.800%
Expectancy
0.136
Net Profit
12.546%
Sharpe Ratio
0.735
Probabilistic Sharpe Ratio
38.124%
Loss Rate
69%
Win Rate
31%
Profit-Loss Ratio
2.63
Alpha
0.152
Beta
0.393
Annual Standard Deviation
0.244
Annual Variance
0.06
Information Ratio
0.38
Tracking Error
0.29
Treynor Ratio
0.457
Total Fees
$427.35
from clr import AddReference
AddReference("System")
AddReference("QuantConnect.Algorithm")
AddReference("QuantConnect.Common")
from System import *
from QuantConnect import *
from QuantConnect.Algorithm import *
from QuantConnect.Securities import *
from datetime import timedelta
import numpy as np


class FuturesMomentumAlgorithm(QCAlgorithm):
    def Initialize(self):
        self.SetStartDate(2019, 9, 14)
        self.SetEndDate(2020, 5, 26)
        
        #times: 2018, 1, 1       :         2018, 5, 1
        #times: 2018, 9, 1       :         2019, 1, 1
        
        self.SetCash(100000)


        # Adds SPY to be used in our EMA indicators
        equity = self.AddEquity("SPY", Resolution.Minute)
        # 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))
        
        self.Schedule.On(self.DateRules.EveryDay("SPY"), \
                 self.TimeRules.BeforeMarketClose("SPY", 1), \
                 self.EveryDayBeforeMarketClose)
        
        self.hma = self.HMA("SPY", 1200, Resolution.Minute)
        self.HMA("SPY", 1200).Updated += self.hmaUpdated
        self.hmaWindow=RollingWindow[IndicatorDataPoint](5)
        
        self.fasthma = self.HMA("SPY", 10, Resolution.Minute)
        self.HMA("SPY", 10).Updated += self.fasthmaUpdated
        self.fasthmaWindow=RollingWindow[IndicatorDataPoint](5)
        
        self.close = self.SMA("SPY", 1, Resolution.Minute)
        
        self.adx = self.ADX("SPY", 840, Resolution.Minute)
        self.ADX("SPY", 840).Updated += self.adxUpdated
        self.adxWindow=RollingWindow[IndicatorDataPoint](5)
        
        self.rsi = self.RSI("SPY", 3300, Resolution.Hour)
   
        
    def hmaUpdated(self, sender, updated):
        self.hmaWindow.Add(updated) 
        
    def fasthmaUpdated(self, sender, updated):
        self.fasthmaWindow.Add(updated) 
        
    def adxUpdated(self, sender, updated):
        self.adxWindow.Add(updated)
     
        
    def OnData(self, slice):
        
        if not (self.hmaWindow.IsReady and self.fasthmaWindow.IsReady and self.adxWindow.IsReady): return
    
    
        bhma = self.fasthma.Current.Value > self.hma.Current.Value
        shma = self.fasthma.Current.Value < self.hma.Current.Value
        #adx is increasing
        hmadelta = self.hmaWindow[0].Value - self.hmaWindow[1].Value
        prevdelta = self.hmaWindow[1].Value - self.hmaWindow[2].Value
        
        badx = self.adx.Current.Value < self.adxWindow[1].Value
        sadx = self.adx.Current.Value < self.adxWindow[1].Value
        
            #directly below is buy criteria
            
            
        #TO DO:  Increase probability of right    
        if (not self.Portfolio.Invested) and hmadelta > 0 and prevdelta < 0:
            for chain in slice.FuturesChains:
                # find the front contract expiring no earlier than in 90 days
                contracts = list(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)
                self.Notify.Sms("+1 123-456-7890", "Bought E-mini:")

        if self.Portfolio.Invested > 0 and hmadelta < 0:
            self.Liquidate()

            


    def EveryDayBeforeMarketClose(self):
        pass
    #    self.Log("EveryDay.SPY 1 min before close: Fired at: {0}".format(self.Time))
    #    if self.Portfolio.Invested > 0 and (self.Securities["SPY"].Close * 0.95) > self.close.Current.Value or self.Portfolio.Invested < 0 and (self.Securities["SPY"].Close * 1.05) < self.close.Current.Value:
    #        self.Liquidate()
    #    else:
    #        pass

    #def OnEndOfDay(self):
    #    #want to close open positions at end of the day
    #    if self.Portfolio.Invested:
    #        self.Liquidate()


    def OnOrderEvent(self, orderEvent):
        self.Log(str(orderEvent))
from Selection.FutureUniverseSelectionModel import FutureUniverseSelectionModel
from datetime import date, timedelta

class FuturesUniverseSelectionModel(FutureUniverseSelectionModel):
    
    def __init__(self, select_future_chain_symbols):
        super().__init__(timedelta(1), select_future_chain_symbols)

    def Filter(self, filter):
        return (filter.Expiration(timedelta(0), timedelta(90))
                      .OnlyApplyFilterAtMarketOpen())