# Percent Contango -- not sure if my math is correct

R

#### reknirt

##### New member
Hello,

I'm trying to write an indicator plotting percent contango between spot VIX -- last closing VIX price -- and the near month /VX future. I got the idea from an indicator Jeff Augen did here:

In theory the indicator should follow the S&P very closely -- at least until backwardation occurs and the near month contract becomes more expensive than spot VIX.

However, my indicator seems to be very noisy, whereas Augen's follows the S&P extremely closely. My formula for calculating the difference is:

contangoPercent = (NearContract - SpotVIX) / SpotVIX;

I corrected some of the noise by filtering out NaNs and replacing them with the previous bar. But I'm still not sure if my calculation is correct. Here is my code:

Code:
``````def month = GetMonth();
def daysTillExp = Next3rdFriday(2) - 30;
def whichMonth = if daysTillExp > 4 then month
else if daysTillExp < 5 and month == 12
then 1
else if daysTillExp < 5
then month + 1
else double.nan;

def Year = GetYear();
def Year_Ext = Year - 2000;
def agg = AggregationPeriod.day;

def SpotVIX = close("VIX");
def NearContract = if whichMonth == 1
then Close(symbol = "/VXF" + Year_Ext, period = agg)
else if whichMonth == 2
then Close(symbol = "/VXG" + Year_Ext, period = agg)
else if whichMonth == 3
then Close(symbol = "/VXH" + Year_Ext, period = agg)
else if whichMonth == 4
then Close(symbol = "/VXJ" + Year_Ext, period = agg)
else if whichMonth == 5
then Close(symbol = "/VXK" + Year_Ext, period = agg)
else if whichMonth == 6
then Close(symbol = "/VXM" + Year_Ext, period = agg)
else if whichMonth == 7
then Close(symbol = "/VXN" + Year_Ext, period = agg)
else if whichMonth == 8
then Close(symbol = "/VXQ" + Year_Ext, period = agg)
else if whichMonth == 9
then Close(symbol = "/VXU" + Year_Ext, period = agg)
else if whichMonth == 10
then Close(symbol = "/VXV" + Year_Ext, period = agg)
else if whichMonth == 11
then Close(symbol = "/VXX" + Year_Ext, period = agg)
else if whichMonth == 12
then Close(symbol = "/VXZ" + Year_Ext, period = agg)
else double.nan;

# if the calculations on any of the bar return NaN, use the previous bar.

def spotFiltered = if IsNaN(spotVIX) then  spotFiltered[1]  else SpotVIX;
def nearFiltered = if IsNaN(NearContract) then  nearFiltered[1]  else NearContract;

plot contangoPercent = simpleMovingAvg((nearFiltered - spotFiltered) / spotFiltered, 5, 0);
#plot contangoPercent = (NearContract - SpotVIX) / SpotVIX;

plot zeroLine = 0;

addlabel(Close(symbol = "VIX") <= NearContract,"Contango", color.green);
addlabel(Close(symbol = "VIX") > NearContract,"Backwardation", color.red);

# End Code VIX Contango/Normal Backwardation``````

Here is what it currently looks like on an /ES chart

I appreciate any help!