Overall Statistics
Total Trades
1
Average Win
0%
Average Loss
0%
Compounding Annual Return
6.619%
Drawdown
54.800%
Expectancy
0
Net Profit
132.653%
Sharpe Ratio
0.393
Probabilistic Sharpe Ratio
0.665%
Loss Rate
0%
Win Rate
0%
Profit-Loss Ratio
0
Alpha
0.081
Beta
-0.127
Annual Standard Deviation
0.183
Annual Variance
0.034
Information Ratio
-0.001
Tracking Error
0.276
Treynor Ratio
-0.568
Total Fees
$1.00
import numpy as np
import pandas as pd

from QuantConnect.Data.Custom import Quandl
from QuantConnect.Python import PythonQuandl
from QuantConnect.Data.Custom.USTreasury import *

# ref 
# https://www.quantconnect.com/forum/discussion/2445/using-quandl-data-w-python/p1
# https://github.com/QuantConnect/Lean/blob/master/Algorithm.Python/AltData/USTreasuryYieldCurveRateAlgorithm.py
# https://www.quandl.com/data/USTREASURY/YIELD-Treasury-Yield-Curve-Rates

class QuandlYield2yr(PythonQuandl):
    def __init__(self):
        self.ValueColumnName = "2 yr"
class QuandlYield10yer(PythonQuandl):
    def __init__(self):
        self.ValueColumnName = "10 yr"
        
class QuandlAlgo(QCAlgorithm):

    def Initialize(self):
        
        self.SetStartDate(2007, 2, 1)

        self.SetCash(10000)
        self.SetBrokerageModel(AlphaStreamsBrokerageModel())
        
        self.spy = self.AddEquity('SPY', Resolution.Daily).Symbol
        self.vix = self.AddData(Quandl,"CHRIS/CBOE_VX1", Resolution.Daily).Symbol
        self.yieldCurveTwo = self.AddData(QuandlYield2yr,"USTREASURY/YIELD", Resolution.Daily).Symbol
        self.yieldCurveTen = self.AddData(QuandlYield10yer,"USTREASURY/YIELD", Resolution.Daily).Symbol
        self.yieldCurve = self.AddData(USTreasuryYieldCurveRate, "USTYCR", Resolution.Daily).Symbol
        
        self.History(USTreasuryYieldCurveRate, self.yieldCurve, 1, Resolution.Daily)
        self.History(self.yieldCurveTwo, 1, Resolution.Daily)
        self.History(self.yieldCurveTen, 1, Resolution.Daily)
        
        self.twoyear_ref = 0
        self.tenyear_ref = 0
        
        self.twoyear = 0
        self.tenyear = 0
        
        self.Schedule.On(self.DateRules.EveryDay(self.spy), self.TimeRules.AfterMarketOpen(self.spy, 1), self.MyBalance)
        self.Schedule.On(self.DateRules.EveryDay(self.spy), self.TimeRules.BeforeMarketClose(self.spy, 1), self.MyPlot)
        
        # Create custom charts
        myplot = Chart('vix')
        myplot.AddSeries(Series('vix', SeriesType.Line, 0))
        myplot.AddSeries(Series('ma', SeriesType.Line, 0))
        
        myplot = Chart('yield')
        myplot.AddSeries(Series('2yr_ref', SeriesType.Line, 0))
        myplot.AddSeries(Series('10yr_ref', SeriesType.Line, 0))
        myplot.AddSeries(Series('2yr', SeriesType.Line, 0))
        myplot.AddSeries(Series('10yr', SeriesType.Line, 0))
        
    def MyBalance(self):
        self.SetHoldings(self.spy,1.0)
        
    def OnData(self, data):
        
        if data.ContainsKey(self.yieldCurveTwo):
            self.twoyear = data[self.yieldCurveTwo].Value
            
        if data.ContainsKey(self.yieldCurveTen):
            self.tenyear = data[self.yieldCurveTen].Value
            
        if data.ContainsKey(self.yieldCurve):

            rates = data[self.yieldCurve]
            
            # Check for None before using the values
            if rates.TenYear is None or rates.TwoYear is None:
                pass
            else:
                self.tenyear_ref = rates.TenYear
                self.twoyear_ref = rates.TwoYear
            
    def MyPlot(self):
        
        self.Plot('yield', '2yr_ref', self.twoyear_ref)
        self.Plot('yield', '10yr_ref', self.tenyear_ref)
        
        self.Plot('yield', '2yr', self.twoyear)
        self.Plot('yield', '10yr', self.tenyear)
        
        data = self.History(self.vix,timedelta(days = 20),Resolution.Daily)
        if len(data) > 10:
            values = data['close'].values
            self.Plot('vix', 'vix', values[-1])
            self.Plot('vix', 'ma', np.nanmean(values))