# 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]