Merge branch 'master' into feature-add-smartinsider-demonstration-algorithm

This commit is contained in:
Jared
2019-10-07 13:39:30 -07:00
committed by GitHub
8 changed files with 270 additions and 8 deletions
@@ -0,0 +1,65 @@
# 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("System")
AddReference("QuantConnect.Algorithm")
AddReference("QuantConnect.Common")
from System import *
from QuantConnect import *
from QuantConnect.Algorithm import *
from QuantConnect.Data import *
from QuantConnect.Data.Custom.USTreasury import *
from datetime import datetime, timedelta
class USTreasuryYieldCurveRateAlgorithm(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2000, 3, 1)
self.SetEndDate(2019, 9, 15)
self.SetCash(100000)
self.spy = self.AddEquity("SPY", Resolution.Hour).Symbol
self.yieldCurve = self.AddData(USTreasuryYieldCurveRate, "YIELDCURVE").Symbol
self.lastInversion = datetime(1, 1, 1)
def OnData(self, data):
if not data.ContainsKey(self.yieldCurve):
return
rates = data[self.yieldCurve]
# Check for None before using the values
if rates.TenYear is None or rates.TwoYear is None:
return
# Only advance if a year has gone by
if (self.Time - self.lastInversion) < timedelta(days=365):
return
# if there is a yield curve inversion after not having one for a year, short SPY for two years
if not self.Portfolio.Invested and rates.TwoYear > rates.TenYear:
self.Debug(f"{self.Time} - Yield curve inversion! Shorting the market for two years")
self.SetHoldings(self.spy, -0.5)
self.lastInversion = self.Time
return
# If two years have passed, liquidate our position in SPY
if self.Time - self.lastInversion >= timedelta(days=365 * 2):
self.Liquidate(self.spy)
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="..\packages\Microsoft.CodeAnalysis.FxCopAnalyzers.2.9.3\build\Microsoft.CodeAnalysis.FxCopAnalyzers.props" Condition="Exists('..\packages\Microsoft.CodeAnalysis.FxCopAnalyzers.2.9.3\build\Microsoft.CodeAnalysis.FxCopAnalyzers.props')" />
<Import Project="..\packages\Microsoft.NetFramework.Analyzers.2.9.3\build\Microsoft.NetFramework.Analyzers.props" Condition="Exists('..\packages\Microsoft.NetFramework.Analyzers.2.9.3\build\Microsoft.NetFramework.Analyzers.props')" />
@@ -61,6 +61,7 @@
<Content Include="Alphas\TripleLeverageETFPairVolatilityDecayAlpha.py" />
<Content Include="Alphas\VIXDualThrustAlpha.py" />
<Content Include="AltData\SmartInsiderTransactionAlgorithm.py" />
<Content Include="AltData\USTreasuryYieldCurveRateAlgorithm.py" />
<Content Include="AltData\PsychSignalSentimentAlgorithm.py" />
<Content Include="AltData\TradingEconomicsAlgorithm.py" />
<Content Include="AltData\TiingoNewsAlgorithm.py" />