Overall Statistics
Total Trades
81
Average Win
2.77%
Average Loss
-1.43%
Compounding Annual Return
3.822%
Drawdown
12.200%
Expectancy
0.248
Net Profit
18.321%
Sharpe Ratio
0.482
Loss Rate
58%
Win Rate
42%
Profit-Loss Ratio
1.94
Alpha
-0.014
Beta
0.409
Annual Standard Deviation
0.071
Annual Variance
0.005
Information Ratio
-0.967
Tracking Error
0.085
Treynor Ratio
0.083
Total Fees
$208.90
import numpy as np
from QuantConnect.Python import PythonQuandl # quandl data not CLOSE
from QuantConnect.Python import PythonData # custom data
from QuantConnect.Data import SubscriptionDataSource
from datetime import datetime, timedelta
import decimal

class FearAlgorithm(QCAlgorithm):

    def Initialize(self):
        self.SetCash(100000)
        self.SetStartDate(2014, 1, 1)
        self.SetEndDate(2017, 10, 10)
        
        # Add a benchmark asset to synchronize the algorithm
        self.AddEquity("SPY", Resolution.Daily).Symbol
        
        # Add VIX as our fear index.
        self.AddEquity("VXX", Resolution.Daily).Symbol
        
        # Get the 30-day EMA of the VIX:
        self.ema = self.EMA("VXX", 30)
        
        # Set warm up so we're instantly trading:
        self.SetWarmUp(180)
        
    def OnData(self, slice):
        if self.LiveMode: self.Debug("Running algorithm!!")
        
        # Make sure all the data we need is in place
        if self.IsWarmingUp: return 
        if not slice.ContainsKey("VXX"): return
        if not slice.ContainsKey("SPY"): return
        
        if self.LiveMode: self.Debug("Warm Up Complete Deciding..")
        
        # Make some nice plots 
        self.Plot("FEAR", "VXX", self.Securities["VXX"].Price)
        self.Plot("FEAR", "EMA", self.ema.Current.Value)
        
        # Decide when to invest
        if self.Securities["VXX"].Price < self.ema.Current.Value:
            if self.Portfolio.Invested: return
            if self.LiveMode: self.Debug("Investing in SPY...")
            self.SetHoldings("SPY", 1);
        else:
            self.Liquidate()
            if self.LiveMode: self.Debug("FEAR too great, liquidating...")