Overall Statistics
Total Trades
0
Average Win
0%
Average Loss
0%
Compounding Annual Return
0%
Drawdown
0%
Expectancy
0
Net Profit
0%
Sharpe Ratio
0
Probabilistic Sharpe Ratio
0%
Loss Rate
0%
Win Rate
0%
Profit-Loss Ratio
0
Alpha
0
Beta
0
Annual Standard Deviation
0
Annual Variance
0
Information Ratio
0
Tracking Error
0
Treynor Ratio
0
Total Fees
$0.00
# QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
# Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from clr import AddReference
AddReference("QuantConnect.Common")
AddReference("QuantConnect.Algorithm")
AddReference("QuantConnect.Algorithm.Framework")
AddReference("QuantConnect.Indicators")

from QuantConnect import *
from QuantConnect.Indicators import *
from QuantConnect.Algorithm import *
from QuantConnect.Algorithm.Framework import *
from QuantConnect.Algorithm.Framework.Alphas import *


class MovingAverageCrossAlphaModel(AlphaModel):
    '''Alpha model that uses an EMA cross to create insights'''

    def __init__(self,
                 fastPeriod = 12,
                 slowPeriod = 26,
                 resolution = Resolution.Daily):
        '''Initializes a new instance of the EmaCrossAlphaModel class
        Args:
            fastPeriod: The fast EMA period
            slowPeriod: The slow EMA period'''
        self.fastPeriod = fastPeriod
        self.slowPeriod = slowPeriod
        self.resolution = resolution
        self.predictionInterval = Time.Multiply(Extensions.ToTimeSpan(resolution), fastPeriod)
        self.symbolDataBySymbol = {}

        resolutionString = Extensions.GetEnumString(resolution, Resolution)
        self.Name = '{}({},{},{})'.format(self.__class__.__name__, fastPeriod, slowPeriod, resolutionString)
        

    def Update(self, algorithm, data):
        '''Updates this alpha model with the latest data from the algorithm.
        This is called each time the algorithm receives data for subscribed securities
        Args:
            algorithm: The algorithm instance
            data: The new data available
        Returns:
            The new insights generated'''
        insights = []
        
        for chain in data.FutureChains:
            
            #1. Filter to choose popular contracts with OpenInterest > 1000
            popularContracts = [contract for contract in chain.Value if contract.OpenInterest>1000]
            
            #2. If the length of contracts in this chain is zero, continue to the next chain
            if len(popularContracts) is 0:
                continue
            
            #3. Sort our contracts by open interest in descending order and save to sortedByOIContracts
            sortedByOIContracts = sorted(popularContracts, key=lambda k : k.OpenInterest, reverse=True)
            
            #4. Save the contract with the highest open interest to self.liquidContract
            liquidContract = sortedByOIContracts[0]
            
            symbolData = self.symbolDataBySymbol.get(liquidContract.Symbol)
            
            if symbolData is None:
                # create fast/slow EMAs
                symbolData = SymbolData(liquidContract.Symbol)
                symbolData.Fast = algorithm.EMA(liquidContract.Symbol, self.fastPeriod, self.resolution)
                symbolData.Slow = algorithm.EMA(liquidContract.Symbol, self.slowPeriod, self.resolution)
                symbolData.ATR = alogirthm.ATR(liquidContract.Symbol, self.fastPeriod)
                #self.SetWarmUp(timedelta(self.slowPeriod))
                self.symbolDataBySymbol[liquidContract.Symbol] = symbolData
                
            if symbolData.Fast.IsReady and symbolData.Slow.IsReady:

                if symbolData.FastIsOverSlow:
                    if symbolData.Slow > symbolData.Fast:
                        insights.append(Insight.Price(symbolData.Symbol, self.predictionInterval, InsightDirection.Down))

                elif symbolData.SlowIsOverFast:
                    if symbolData.Fast > symbolData.Slow:
                        insights.append(Insight.Price(symbolData.Symbol, self.predictionInterval, InsightDirection.Up))

            symbolData.FastIsOverSlow = symbolData.Fast > symbolData.Slow

        return insights

    def OnSecuritiesChanged(self, algorithm, changes):
        '''Event fired each time the we add/remove securities from the data feed
        Args:
            algorithm: The algorithm instance that experienced the change in securities
            changes: The security additions and removals from the algorithm'''
        pass


class SymbolData:
    '''Contains data specific to a symbol required by this model'''
    def __init__(self, symbol):
        self.Symbol = symbol
        self.Fast = None
        self.Slow = None
        self.ATR = None

        # True if the fast is above the slow, otherwise false.
        # This is used to prevent emitting the same signal repeatedly
        self.FastIsOverSlow = False

    @property
    def SlowIsOverFast(self):
        return not self.FastIsOverSlow
from Execution.ImmediateExecutionModel import ImmediateExecutionModel
from Portfolio.MeanVarianceOptimizationPortfolioConstructionModel import MeanVarianceOptimizationPortfolioConstructionModel
from Risk.MaximumDrawdownPercentPerSecurity import MaximumDrawdownPercentPerSecurity
from FuturesUniverseSelectionModel import FuturesUniverseSelectionModel
from ATRBasedPositionSizing import ATRBasedPositionSizing
from MovingAverageCrossAlphaModel import MovingAverageCrossAlphaModel

class MovingAverageCrossTrendFollowing(QCAlgorithm):

    def Initialize(self):
        self.SetStartDate(2019, 1, 1)  # Set Start Date
        self.SetStartDate(2019, 3,30)  # Set End Date
        self.SetCash(100000)  # Set Strategy Cash
        self.UniverseSettings.Resolution = Resolution.Daily
        self.AddAlpha(MovingAverageCrossAlphaModel(50, 200, Resolution.Minute))

        self.SetExecution(ImmediateExecutionModel())

        self.SetPortfolioConstruction(EqualWeightingPortfolioConstructionModel())

        self.SetUniverseSelection(FuturesUniverseSelectionModel(self.SelectFuturesSymbols))


    def SelectFuturesSymbols(self, utcTime):
        tickers = [Futures.Indices.SP500EMini,
                  Futures.Grains.BlackSeaCornFinanciallySettledPlatts,
                  Futures.Grains.Wheat,
                  Futures.Grains.Corn,
                  Futures.Grains.Soybeans,
                  Futures.Grains.SoybeanMeal,
                  Futures.Grains.SoybeanOil,
                  Futures.Grains.Oats,
                  Futures.Currencies.USD,
                  Futures.Currencies.GBP,
                  Futures.Currencies.CAD,
                  Futures.Currencies.JPY,
                  Futures.Currencies.CHF,
                  Futures.Currencies.EUR,
                  Futures.Currencies.AUD,
                  Futures.Currencies.NZD,
                  Futures.Currencies.RUB,
                  Futures.Currencies.BRL,
                  Futures.Currencies.MXN,
                  Futures.Currencies.ZAR,
                  Futures.Currencies.AUDCAD,
                  Futures.Currencies.AUDJPY,
                  Futures.Currencies.AUDNZD,
                  Futures.Currencies.BTC,
                  Futures.Currencies.CADJPY,
                  Futures.Currencies.StandardSizeUSDOffshoreRMBCNH,
                  Futures.Currencies.EuroFXEmini,
                  Futures.Currencies.EURAUD,
                  Futures.Currencies.EURCAD,
                  Futures.Currencies.EURSEK,
                  Futures.Currencies.JapaneseYenEmini,
                  Futures.Energies.PropaneNonLDHMontBelvieu,
                  Futures.Energies.ArgusPropaneFarEastIndexBALMO,
                  Futures.Energies.MiniEuropeanThreePointPercentFiveFuelOilBargesPlatts,
                  Futures.Energies.MiniSingaporeFuelOil180CstPlatts,
                  Futures.Energies.GulfCoastULSDPlattsUpDownBALMO,
                  Futures.Energies.GulfCoastJetPlattsUpDownBALMO,
                  Futures.Energies.PropaneNonLDHMontBelvieuOPIS,
                  Futures.Energies.EuropeanPropaneCIFARAArgusBALMO,
                  Futures.Energies.PremiumUnleadedGasoline10ppmFOBMEDPlatts,
                  Futures.Energies.ArgusPropaneFarEastIndex,
                  Futures.Energies.GasolineEurobobOxyNWEBargesArgusCrackSpreadBALMO,
                  Futures.Energies.MontBelvieuNaturalGasolineOPIS,
                  Futures.Energies.MontBelvieuNormalButaneOPISBALMO,
                  Futures.Energies.ConwayPropaneOPIS,
                  Futures.Energies.MontBelvieuLDHPropaneOPISBALMO,
                  Futures.Energies.ArgusPropaneFarEastIndexVsEuropeanPropaneCIFARAArgus,
                  Futures.Energies.ArgusPropaneSaudiAramco,
                  Futures.Energies.GroupThreeULSDPlattsVsNYHarborULSD,
                  Futures.Energies.GroupThreeSuboctaneGasolinePlattsVsRBOB,
                  Futures.Energies.SingaporeFuelOil180cstPlattsBALMO,
                  Futures.Energies.SingaporeFuelOil380cstPlattsBALMO,
                  Futures.Energies.MontBelvieuEthaneOPIS,
                  Futures.Energies.MontBelvieuNormalButaneOPIS,
                  Futures.Energies.BrentCrudeOilVsDubaiCrudeOilPlatts,
                  Futures.Energies.ArgusLLSvsWTIArgusTradeMonth,
                  Futures.Energies.SingaporeGasoilPlattsVsLowSulphurGasoilFutures,
                  Futures.Energies.LosAngelesCARBOBGasolineOPISvsRBOBGasoline,
                  Futures.Energies.LosAngelesJetOPISvsNYHarborULSD,
                  Futures.Energies.LosAngelesCARBDieselOPISvsNYHarborULSD,
                  Futures.Energies.EuropeanNaphthaPlattsBALMO,
                  Futures.Energies.EuropeanPropaneCIFARAArgus,
                  Futures.Energies.MontBelvieuNaturalGasolineOPISBALMO,
                  Futures.Energies.RBOBGasolineCrackSpread,
                  Futures.Energies.GulfCoastHSFOPlattsBALMO,
                  Futures.Energies.MarsArgusVsWTITradeMonth,
                  Futures.Energies.MarsArgusVsWTIFinancial,
                  Futures.Energies.EthanolT2FOBRdamIncludingDutyPlatts,
                  Futures.Energies.MontBelvieuLDHPropaneOPIS,
                  Futures.Energies.GasolineEurobobOxyNWEBargesArgus,
                  Futures.Energies.WTIBrentFinancial,
                  Futures.Energies.ThreePointFivePercentFuelOilBargesFOBRdamPlattsCrackSpread1000mt,
                  Futures.Energies.GasolineEurobobOxyNWEBargesArgusBALMO,
                  Futures.Energies.BrentLastDayFinancial,
                  Futures.Energies.CrudeOilWTI,
                  Futures.Energies.GulfCoastCBOBGasolineA2PlattsVsRBOBGasoline,
                  Futures.Energies.ClearbrookBakkenSweetCrudeOilMonthlyIndexNetEnergy,
                  Futures.Energies.WTIFinancial,
                  Futures.Energies.ChicagoEthanolPlatts,
                  Futures.Energies.SingaporeMogas92UnleadedPlattsBrentCrackSpread,
                  Futures.Energies.DubaiCrudeOilPlattsFinancial,
                  Futures.Energies.JapanCnFNaphthaPlattsBALMO,
                  Futures.Energies.Ethanol,
                  Futures.Energies.EuropeanNaphthaPlattsCrackSpread,
                  Futures.Energies.EuropeanPropaneCIFARAArgusVsNaphthaCargoesCIFNWEPlatts,
                  Futures.Energies.SingaporeFuelOil380cstPlattsVsEuropeanThreePointFivePercentFuelOilBargesFOBRdamPlatts,
                  Futures.Energies.EastWestGasolineSpreadPlattsArgus,
                  Futures.Energies.EastWestNaphthaJapanCFvsCargoesCIFNWESpreadPlatts,
                  Futures.Energies.RBOBGasolineVsEurobobOxyNWEBargesArgusThreeHundredFiftyThousandGallons,
                  Futures.Energies.ThreePointFivePercentFuelOilBargesFOBRdamPlattsCrackSpread,
                  Futures.Energies.FreightRouteTC14Baltic,
                  Futures.Energies.OnePercentFuelOilCargoesFOBNWEPlattsVsThreePointFivePercentFuelOilBargesFOBRdamPlatts,
                  Futures.Energies.GulfCoastHSFOPlattsVsEuropeanThreePointFivePercentFuelOilBargesFOBRdamPlatts,
                  Futures.Energies.WTIHoustonCrudeOil,
                  Futures.Energies.NaturalGasHenryHubLastDayFinancial,
                  Futures.Energies.HeatingOil,
                  Futures.Energies.NaturalGasHenryHubPenultimateFinancial,
                  Futures.Energies.WTIHoustonArgusVsWTITradeMonth,
                  Futures.Energies.Gasoline,
                  Futures.Energies.NaturalGas,
                  Futures.Financials.Y30TreasuryBond,
                  Futures.Financials.Y10TreasuryNote,
                  Futures.Financials.Y5TreasuryNote,
                  Futures.Financials.Y2TreasuryNote,
                  Futures.Financials.EuroDollar,
                  Futures.Financials.FiveYearUSDMACSwap,
                  Futures.Indices.SP500EMini,
                  Futures.Indices.NASDAQ100EMini,
                  Futures.Indices.Dow30EMini,
                  Futures.Indices.VIX,
                  Futures.Indices.Russell2000EMini,
                  Futures.Indices.Nikkei225Dollar,
                  Futures.Indices.BloombergCommodityIndex,
                  Futures.Indices.NASDAQ100BiotechnologyEMini,
                  Futures.Indices.FTSEEmergingEmini,
                  Futures.Indices.SP400MidCapEmini,
                  Futures.Indices.SPGSCICommodity,
                  Futures.Indices.USDDenominatedIbovespa,
                  Futures.Meats.LiveCattle,
                  Futures.Meats.FeederCattle,
                  Futures.Meats.LeanHogs,
                  Futures.Metals.Gold,
                  Futures.Metals.Silver,
                  Futures.Metals.Platinum,
                  Futures.Metals.Palladium,
                  Futures.Metals.AluminumMWUSTransactionPremiumPlatts25MT,
                  Futures.Metals.AluminiumEuropeanPremiumDutyPaidMetalBulletin,
                  Futures.Metals.Copper,
                  Futures.Metals.USMidwestDomesticHotRolledCoilSteelCRUIndex,
                  Futures.Softs.Cotton2,
                  Futures.Softs.OrangeJuice,
                  Futures.Softs.Coffee,
                  Futures.Softs.Sugar11,
                  Futures.Softs.Sugar11CME,
                  Futures.Softs.Cocoa,
                  Futures.Dairy.CashSettledButter,
                  Futures.Dairy.CashSettledCheese,
                  Futures.Dairy.ClassIIIMilk,
                  Futures.Dairy.DryWhey,
                  Futures.Dairy.ClassIVMilk,
                  Futures.Dairy.NonfatDryMilk]
        return [ Symbol.Create(ticker, SecurityType.Future, Market.USA) for ticker in tickers]
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())
from clr import AddReference
AddReference("System")
AddReference("QuantConnect.Algorithm")
AddReference("QuantConnect.Algorithm.Framework")
AddReference("QuantConnect.Common")
AddReference("QuantConnect.Indicators")

from System import *
from QuantConnect import *
from QuantConnect.Indicators import *
from QuantConnect.Algorithm import *
from QuantConnect.Algorithm.Framework import *
from QuantConnect.Algorithm.Framework.Portfolio import *
from Portfolio.MinimumVariancePortfolioOptimizer import MinimumVariancePortfolioOptimizer
from datetime import timedelta
import numpy as np
import pandas as pd

### <summary>
### </summary>
class ATRBasedPositionSizing(PortfolioConstructionModel):
    def __init__(self,
                 riskFactor = 0.2,
                 lookbackPeriod = 100):
        """Initialize the model
        Args:
            lookback(int): Historical return lookback period
            period(int): The time interval of history price to calculate the weight
            resolution: The resolution of the history price
            optimizer(class): Method used to compute the portfolio weights"""
        self.atrs = {}
        self.lookbackPeriod = lookbackPeriod
        self.riskFactor = riskFactor
            
    def CreateTargets(self, algorithm, insights):
        """
        Create portfolio targets from the specified insights
        Args:
            algorithm: The algorithm instance
            insights: The insights to create portfolio targets from
        Returns:
            An enumerable of portfolio targets to be sent to the execution model
        """
        targets = []
        for insight in insights:
            symbol = insight.Symbol
            if symbol in self.atrs.keys():
                direction = 1 if InsightDirection.Up else -1
                price = algorithm.Securities[symbol].Price
                atr = self.atrs[symbol].Current.Value
                numberOfContracts = np.floor((direction * (self.riskFactor*algorithm.Portfolio.TotalPortfolioValue)/(atr*price )))
                target = PortfolioTarget(insight.Symbol, numberOfContracts)
                if target is not None:
                    targets.append(target)
        return targets

    def OnSecuritiesChanged(self, algorithm, changes):
        '''Event fired each time the we add/remove securities from the data feed
        Args:
            algorithm: The algorithm instance that experienced the change in securities
            changes: The security additions and removals from the algorithm'''
        for addedSecurity in changes.AddedSecurities:
            ticker = addedSecurity.Symbol
            if ticker not in self.atrs.keys():
                self.atrs[ticker] = algorithm.ATR(ticker, self.lookbackPeriod)