Back to Community
Live trading not tracking backtesting

I have two algos running in live Quantopian paper trading mode. Unfortunately, neither are tracking the current backtest results. Both of these algos were "borrowed" from other users.

I've attached one example. In live mode, 277 shares of XIV was purchased on 8/25. The backtest shows the current position should be half AGG and half XIV.

I'm at a loss as to how to troubleshoot this and fix it.

I've got another algo also not tracking the backtest -- also a volatility-based system for what that may mean.

Clone Algorithm
0
Loading...
Backtest from to with initial capital
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
import pandas as pd
import numpy as np
import math
from quantopian.pipeline import Pipeline
from quantopian.algorithm import attach_pipeline, pipeline_output
from quantopian.pipeline.data.builtin import USEquityPricing
from quantopian.pipeline.factors import CustomFactor
from quantopian.pipeline.data.quandl import yahoo_index_vix

NDays = 5

class ImpliedVolatility(CustomFactor):
    inputs = [yahoo_index_vix.close]
    def compute(self, today, assets, out, vix):
        out[:] = np.mean(vix, axis=0) # NB one identical column for every asset
        #out[:] = np.mean(vix) # Does same as above, but more correct for ANTICIPATE CHANGE: https://www.quantopian.com/posts/upcoming-changes-to-quandl-datasets-in-pipeline-vix-vxv-etc-dot

        
def initialize(context):
    
    context.vxx = sid(38054)
    context.xiv = sid(40516)
    context.uvxy = sid(41969)
    context.spy = sid(8554)
    context.bond = sid(25485)
  
    
    set_benchmark(context.xiv)
    
    pipe = Pipeline()
    attach_pipeline(pipe, 'example')
    
    iv = ImpliedVolatility(window_length=NDays)
    pipe.add(iv, 'iv')
    
    schedule_function(func=allocate, 
                      time_rule=time_rules.market_open(),
                      half_days=True)

    
def before_trading_start(context, data):

    output = pipeline_output('example')
    output = output.dropna() #makes a copy
    iv = output["iv"].iloc[0]
    hv = calculate_hv(context, data, NDays)  

    context.vrp = iv-hv

    record(ImpliedVolatility=iv)
    record(HistoricVolatility=hv)
    #record(VRP=iv-hv)
    

def calculate_hv(context, data, days):    
    close = data.history(context.spy, ["price"], days+1, "1d")
    close["ret"] = (np.log(close.price) - np.log(close.price).shift(1))
    return close.ret.std()*math.sqrt(252)*100

 
def allocate(context, data):
    if context.vrp > 0:
        order_target_percent(context.vxx, 0)
        order_target_percent(context.uvxy, 0)
        order_target_percent(context.xiv, .5)
        order_target_percent(context.bond, .5)
    else:
        order_target_percent(context.vxx, .5)
        order_target_percent(context.xiv, 0)
        order_target_percent(context.uvxy, 0)
        order_target_percent(context.bond, .5)
      
        
There was a runtime error.