示例#1
0
        public string GetStrVIX()
        {
            //Downloading live data from vixcentral.com.
            string?webpageLive = Utils.DownloadStringWithRetryAsync("http://vixcentral.com", 3, TimeSpan.FromSeconds(2), true).TurnAsyncToSyncTask();

            if (webpageLive == null)
            {
                return("Error in live data");
            }

            //Selecting data from live data string.

            string liveFuturesDataDT   = string.Empty;
            string liveFuturesDataDate = string.Empty;
            string liveFuturesDataTime = string.Empty;
            string liveFuturesData     = string.Empty;
            string prevFuturesData     = string.Empty;
            string liveFuturesNextExp  = string.Empty;
            string spotVixData         = string.Empty;
            string titleVIX            = "VIX Futures Term Structure";
            string dataSourceVIX       = "http://vixcentral.com";

            int startPosLiveDate = webpageLive.IndexOf("var time_data_var=['") + "var time_data_var=['".Length;
            int startPosLive     = webpageLive.IndexOf("var last_data_var=[", startPosLiveDate) + "var last_data_var=[".Length;
            int endPosLive       = webpageLive.IndexOf("];last_data_var=clean_array(last_data_var);", startPosLive);
            int startPosPrev     = webpageLive.IndexOf("];var previous_close_var=[", endPosLive) + "];var previous_close_var=[".Length;
            int endPosPrev       = webpageLive.IndexOf("];var contango_graph_exists=", startPosPrev);
            int nextExpLiveMonth = webpageLive.IndexOf("var mx=['", endPosPrev) + "var mx=['".Length;
            int startSpotVix     = webpageLive.IndexOf("{id:'VIX_Index',name:'VIX Index',legendIndex:9,lineWidth:2,color:'green',dashStyle:'LongDash',marker:{enabled:false},dataLabels:{enabled:true,align:'left',x:5,y:4,formatter:function(){if(this.point.x==this.series.data.length-1){return Highcharts.numberFormat(this.y,2);}else{return null;}}},data:[", nextExpLiveMonth) + "{id:'VIX_Index',name:'VIX Index',legendIndex:9,lineWidth:2,color:'green',dashStyle:'LongDash',marker:{enabled:false},dataLabels:{enabled:true,align:'left',x:5,y:4,formatter:function(){if(this.point.x==this.series.data.length-1){return Highcharts.numberFormat(this.y,2);}else{return null;}}},data:[".Length;
            int endSpotVix       = webpageLive.IndexOf("]},{id:'VXV_Index',name:'VXV Index',legendIndex:10,lineWidth:2", startSpotVix);

            // liveFuturesDataDT = webpageLive.Substring(startPosLiveDate, 20);
            liveFuturesDataDT  = webpageLive.Substring(startPosLiveDate, 8);
            liveFuturesNextExp = webpageLive.Substring(nextExpLiveMonth, 3);
            liveFuturesData    = webpageLive.Substring(startPosLive, endPosLive - startPosLive);
            prevFuturesData    = webpageLive.Substring(startPosPrev, endPosPrev - startPosPrev);
            spotVixData        = webpageLive.Substring(startSpotVix, endSpotVix - startSpotVix);

            // liveFuturesDataDate = liveFuturesDataDT.Substring(0,10);
            // liveFuturesDataTime = liveFuturesDataDT.Substring(12, 8) + " EST";

            string[] liveFuturesPrices       = liveFuturesData.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
            int      lengthLiveFuturesPrices = liveFuturesPrices.Length;

            string[] prevFuturesPrices = prevFuturesData.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
            string[] spotVixPrices     = spotVixData.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
            double   spotVixValue      = Double.Parse(spotVixPrices[0]);

            string[] monthsNumList = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
            int      monthsNum     = Array.IndexOf(monthsNumList, liveFuturesNextExp) + 1;


            // DateTime liveDateTime;
            DateTime timeNowETVIX = Utils.ConvertTimeFromUtcToEt(DateTime.UtcNow);
            int      dayOfWeekVIX;

            dayOfWeekVIX = Convert.ToInt32(timeNowETVIX.DayOfWeek);
            if (dayOfWeekVIX == 0)
            {
                timeNowETVIX = timeNowETVIX.AddDays(-2);
            }
            else if (dayOfWeekVIX == 6)
            {
                timeNowETVIX = timeNowETVIX.AddDays(-1);
            }
            string liveDate = string.Empty;

            // liveDateTime = DateTime.Parse(liveFuturesDataDate);
            liveDate = timeNowETVIX.ToString("yyyy-MM-dd");

            liveFuturesDataTime = liveFuturesDataDT.Substring(0, 8) + " EST";

            //Sorting historical data.
            VixCentralRec2[] vixCentralRec = new VixCentralRec2[2];

            vixCentralRec[0].Date       = DateTime.Parse(liveDate);
            vixCentralRec[0].FirstMonth = monthsNum;
            vixCentralRec[0].F1         = Double.Parse(liveFuturesPrices[0]);
            vixCentralRec[0].F2         = Double.Parse(liveFuturesPrices[1]);
            vixCentralRec[0].F3         = Double.Parse(liveFuturesPrices[2]);
            vixCentralRec[0].F4         = Double.Parse(liveFuturesPrices[3]);
            vixCentralRec[0].F5         = Double.Parse(liveFuturesPrices[4]);
            vixCentralRec[0].F6         = Double.Parse(liveFuturesPrices[5]);
            vixCentralRec[0].F7         = Double.Parse(liveFuturesPrices[6]);
            vixCentralRec[0].F8         = (lengthLiveFuturesPrices == 8) ? Double.Parse(liveFuturesPrices[7]) : 0;
            vixCentralRec[0].STCont     = vixCentralRec[0].F2 / vixCentralRec[0].F1 - 1;
            vixCentralRec[0].LTCont     = vixCentralRec[0].F7 / vixCentralRec[0].F4 - 1;

            vixCentralRec[1].Date       = DateTime.Parse(liveDate);
            vixCentralRec[1].FirstMonth = monthsNum;
            vixCentralRec[1].F1         = Double.Parse(prevFuturesPrices[0]);
            vixCentralRec[1].F2         = Double.Parse(prevFuturesPrices[1]);
            vixCentralRec[1].F3         = Double.Parse(prevFuturesPrices[2]);
            vixCentralRec[1].F4         = Double.Parse(prevFuturesPrices[3]);
            vixCentralRec[1].F5         = Double.Parse(prevFuturesPrices[4]);
            vixCentralRec[1].F6         = Double.Parse(prevFuturesPrices[5]);
            vixCentralRec[1].F7         = Double.Parse(prevFuturesPrices[6]);
            vixCentralRec[1].F8         = (lengthLiveFuturesPrices == 8) ? Double.Parse(prevFuturesPrices[7]) : 0;
            vixCentralRec[1].STCont     = vixCentralRec[1].F2 / vixCentralRec[1].F1 - 1;
            vixCentralRec[1].LTCont     = vixCentralRec[1].F7 / vixCentralRec[1].F4 - 1;

            //Calculating futures expiration dates.

            var    lastDataDay  = vixCentralRec[0].Date;
            int    lastDataYear = lastDataDay.Year;
            string lastData     = lastDataDay.ToString("yyyy-MM-dd");

            var lengthExps = /*(lastDataYear - firstDataYear + 2)*/ 2 * 12;

            int[,] expDatesDat = new int[lengthExps, 2];

            expDatesDat[0, 0] = lastDataYear + 1;
            expDatesDat[0, 1] = 12;

            for (int iRows = 1; iRows < expDatesDat.GetLength(0); iRows++)
            {
                decimal f = iRows / 12;
                expDatesDat[iRows, 0] = lastDataYear - Decimal.ToInt32(Math.Floor(f)) + 1;
                expDatesDat[iRows, 1] = 12 - iRows % 12;
            }

            DateTime[] expDates = new DateTime[expDatesDat.GetLength(0)];
            for (int iRows = 0; iRows < expDates.Length; iRows++)
            {
                DateTime thirdFriday = new DateTime(expDatesDat[iRows, 0], expDatesDat[iRows, 1], 15);
                while (thirdFriday.DayOfWeek != DayOfWeek.Friday)
                {
                    thirdFriday = thirdFriday.AddDays(1);
                }
                expDates[iRows] = thirdFriday.AddDays(-30);
                if (expDates[iRows] == DateTime.Parse("2014-03-19"))
                {
                    expDates[iRows] = DateTime.Parse("2014-03-18");
                }
            }

            //Calculating number of calendar days until expirations.
            for (int iRec = 0; iRec < vixCentralRec.Length; iRec++)
            {
                int index1 = Array.FindIndex(expDates, item => item <= vixCentralRec[iRec].Date);
                vixCentralRec[iRec].NextExpiryDate = expDates[index1 - 1];
                vixCentralRec[iRec].F1expDays      = (expDates[index1 - 1] - vixCentralRec[iRec].Date).Days;
                vixCentralRec[iRec].F2expDays      = (expDates[index1 - 2] - vixCentralRec[iRec].Date).Days;
                vixCentralRec[iRec].F3expDays      = (expDates[index1 - 3] - vixCentralRec[iRec].Date).Days;
                vixCentralRec[iRec].F4expDays      = (expDates[index1 - 4] - vixCentralRec[iRec].Date).Days;
                vixCentralRec[iRec].F5expDays      = (expDates[index1 - 5] - vixCentralRec[iRec].Date).Days;
                vixCentralRec[iRec].F6expDays      = (expDates[index1 - 6] - vixCentralRec[iRec].Date).Days;
                vixCentralRec[iRec].F7expDays      = (expDates[index1 - 7] - vixCentralRec[iRec].Date).Days;
                vixCentralRec[iRec].F8expDays      = (vixCentralRec[0].F8 > 0) ? (expDates[index1 - 8] - vixCentralRec[iRec].Date).Days:0;
            }

            string ret = Processing(vixCentralRec, expDates, liveDate, liveFuturesDataTime, spotVixValue, titleVIX, dataSourceVIX);

            return(ret);
        }
示例#2
0
        public string GetStrGAS()
        {
            //Downloading live data from cmegroup.com.
            string?webpageLive = Utils.DownloadStringWithRetryAsync("https://www.cmegroup.com/CmeWS/mvc/Quotes/Future/444/G", 3, TimeSpan.FromSeconds(2), true).TurnAsyncToSyncTask();

            //bool isOkLive = Utils.DownloadStringWithRetry("http://www.cmegroup.com/trading/energy/natural-gas/natural-gas.html", out webpageLive, 3, TimeSpan.FromSeconds(2), true);
            if (webpageLive == null)
            {
                return("Error in live data");
            }

            //Selecting data from live data string.
            string[] liveFuturesDataVec    = new string[8];
            int[]    liveFuturesDataVecInd = new int[8];
            int      startPosLiveB0        = webpageLive.IndexOf("\"last\":\"", 0) + "\"last\":\"".Length;
            int      endPosLiveB0          = webpageLive.IndexOf("\",\"change\":", startPosLiveB0);
            string   liveFuturesDataB0     = webpageLive.Substring(startPosLiveB0, endPosLiveB0 - startPosLiveB0);

            liveFuturesDataVec[0]    = liveFuturesDataB0;
            liveFuturesDataVecInd[0] = endPosLiveB0;

            for (int iRows = 1; iRows < 8; iRows++)
            {
                int    startPosLiveB    = webpageLive.IndexOf("\"last\":\"", liveFuturesDataVecInd[iRows - 1]) + "\"last\":\"".Length;
                int    endPosLiveB      = webpageLive.IndexOf("\",\"change\":", startPosLiveB);
                string liveFuturesDataB = webpageLive.Substring(startPosLiveB, endPosLiveB - startPosLiveB);

                liveFuturesDataVec[iRows]    = liveFuturesDataB;
                liveFuturesDataVecInd[iRows] = endPosLiveB;
            }

            string[] prevFuturesDataVec = new string[8];
            int      startPosPrevB0     = webpageLive.IndexOf("\"priorSettle\":\"", liveFuturesDataVecInd[0]) + "\"priorSettle\":\"".Length;
            int      endPosPrevB0       = webpageLive.IndexOf("\",\"open\":\"", startPosPrevB0);
            string   prevFuturesDataB0  = webpageLive.Substring(startPosPrevB0, endPosPrevB0 - startPosPrevB0);

            prevFuturesDataVec[0] = prevFuturesDataB0;

            for (int iRows = 1; iRows < 8; iRows++)
            {
                int    startPosPrevB    = webpageLive.IndexOf("\"priorSettle\":\"", liveFuturesDataVecInd[iRows]) + "\"priorSettle\":\"".Length;
                int    endPosPrevB      = webpageLive.IndexOf("\",\"open\":\"", startPosPrevB);
                string prevFuturesDataB = webpageLive.Substring(startPosPrevB, endPosPrevB - startPosPrevB);

                prevFuturesDataVec[iRows] = prevFuturesDataB;
            }

            string liveFuturesDataDT   = string.Empty;
            string liveFuturesDataDate = string.Empty;
            string liveFuturesDataTime = string.Empty;
            string liveFuturesNextExp  = string.Empty;
            string futCodeNext         = string.Empty;
            string spotVixData         = string.Empty;
            string titleGAS            = "GAS Futures Term Structure";
            string dataSourceGAS       = "https://www.cmegroup.com/trading/energy/natural-gas/natural-gas.html";

            int startPosLiveDate = webpageLive.IndexOf("\"updated\":\"", liveFuturesDataVecInd[0]) + "\"updated\":\"".Length;

            liveFuturesDataDT   = webpageLive.Substring(startPosLiveDate, 29);
            liveFuturesDataDate = liveFuturesDataDT.Substring(18, 11);
            liveFuturesDataTime = liveFuturesDataDT.Substring(0, 8) + " CT";

            int nextExpLiveMonth = webpageLive.IndexOf("\"expirationMonth\":\"", 0) + "\"expirationMonth\":\"".Length;

            liveFuturesNextExp = webpageLive.Substring(nextExpLiveMonth, 3);

            int futCodeInd = webpageLive.IndexOf("\"escapedQuoteCode\":\"", endPosPrevB0) + "\"escapedQuoteCode\":\"".Length;

            futCodeNext = webpageLive.Substring(futCodeInd, 3);

            //Downloading expiration dates from cmegroup.com.
            string?webpageLiveExp = Utils.DownloadStringWithRetryAsync("https://www.cmegroup.com/CmeWS/mvc/ProductCalendar/Future/444", 3, TimeSpan.FromSeconds(2), true).TurnAsyncToSyncTask();

            if (webpageLiveExp == null)
            {
                return("Error in live data");
            }

            string[] liveFuturesDataExpVec    = new string[8];
            int[]    liveFuturesDataExpVecInd = new int[8];
            int      startPosLiveExpB0Ass     = webpageLiveExp.IndexOf(futCodeNext, 0);
            int      startPosLiveExpB0        = webpageLiveExp.IndexOf("\"lastTrade\":\"", startPosLiveExpB0Ass) + "\"lastTrade\":\"".Length;
            int      endPosLiveExpB0          = webpageLiveExp.IndexOf(",\"settlement", startPosLiveExpB0);
            string   liveFuturesDataExpB0     = webpageLiveExp.Substring(startPosLiveExpB0, endPosLiveExpB0 - startPosLiveExpB0 - 1);

            liveFuturesDataExpVec[0]    = liveFuturesDataExpB0;
            liveFuturesDataExpVecInd[0] = endPosLiveExpB0;

            for (int iRows = 1; iRows < 8; iRows++)
            {
                int    startPosLiveExpBAss = webpageLiveExp.IndexOf("NG", liveFuturesDataExpVecInd[iRows - 1]);
                int    startPosLiveExpB    = webpageLiveExp.IndexOf("\"lastTrade\":\"", startPosLiveExpBAss) + "\"lastTrade\":\"".Length;
                int    endPosLiveExpB      = webpageLiveExp.IndexOf(",\"settlement", startPosLiveExpB);
                string liveFuturesDataExpB = webpageLiveExp.Substring(startPosLiveExpB, endPosLiveExpB - startPosLiveExpB - 1);

                liveFuturesDataExpVec[iRows]    = liveFuturesDataExpB;
                liveFuturesDataExpVecInd[iRows] = endPosLiveExpB;
            }

            string[] monthsNumList = { "JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC" };
            int      monthsNum     = Array.IndexOf(monthsNumList, liveFuturesNextExp) + 1;

            DateTime[] expDates = new DateTime[8];
            for (int iRows = 0; iRows < expDates.Length; iRows++)
            {
                expDates[iRows] = DateTime.Parse(liveFuturesDataExpVec[iRows]);
            }

            string[] liveFuturesPrices       = liveFuturesDataVec;
            int      lengthLiveFuturesPrices = liveFuturesPrices.Length;

            string[] prevFuturesPrices = prevFuturesDataVec;

            double spotVixValue = 0;/*Double.Parse(spotVixPrices[0]);*/

            for (int iRows = 0; iRows < 8; iRows++)
            {
                if (String.Equals(liveFuturesPrices[iRows], "-"))
                {
                    liveFuturesPrices[iRows] = prevFuturesPrices[iRows];
                }
            }


            DateTime liveDateTime;
            string   liveDate = string.Empty;

            liveDateTime = DateTime.Parse(liveFuturesDataDate);
            liveDate     = liveDateTime.ToString("yyyy-MM-dd");

            //Sorting historical data.
            VixCentralRec2[] vixCentralRec = new VixCentralRec2[2];

            vixCentralRec[0].Date       = DateTime.Parse(liveDate);
            vixCentralRec[0].FirstMonth = monthsNum;
            vixCentralRec[0].F1         = Double.Parse(liveFuturesPrices[0]);
            vixCentralRec[0].F2         = Double.Parse(liveFuturesPrices[1]);
            vixCentralRec[0].F3         = Double.Parse(liveFuturesPrices[2]);
            vixCentralRec[0].F4         = Double.Parse(liveFuturesPrices[3]);
            vixCentralRec[0].F5         = Double.Parse(liveFuturesPrices[4]);
            vixCentralRec[0].F6         = Double.Parse(liveFuturesPrices[5]);
            vixCentralRec[0].F7         = Double.Parse(liveFuturesPrices[6]);
            vixCentralRec[0].F8         = (lengthLiveFuturesPrices == 8) ? Double.Parse(liveFuturesPrices[7]) : 0;
            vixCentralRec[0].STCont     = vixCentralRec[0].F2 / vixCentralRec[0].F1 - 1;
            vixCentralRec[0].LTCont     = vixCentralRec[0].F7 / vixCentralRec[0].F4 - 1;

            vixCentralRec[1].Date       = DateTime.Parse(liveDate);
            vixCentralRec[1].FirstMonth = monthsNum;
            vixCentralRec[1].F1         = Double.Parse(prevFuturesPrices[0]);
            vixCentralRec[1].F2         = Double.Parse(prevFuturesPrices[1]);
            vixCentralRec[1].F3         = Double.Parse(prevFuturesPrices[2]);
            vixCentralRec[1].F4         = Double.Parse(prevFuturesPrices[3]);
            vixCentralRec[1].F5         = Double.Parse(prevFuturesPrices[4]);
            vixCentralRec[1].F6         = Double.Parse(prevFuturesPrices[5]);
            vixCentralRec[1].F7         = Double.Parse(prevFuturesPrices[6]);
            vixCentralRec[1].F8         = (lengthLiveFuturesPrices == 8) ? Double.Parse(prevFuturesPrices[7]) : 0;
            vixCentralRec[1].STCont     = vixCentralRec[1].F2 / vixCentralRec[1].F1 - 1;
            vixCentralRec[1].LTCont     = vixCentralRec[1].F7 / vixCentralRec[1].F4 - 1;


            //Calculating number of calendar days until expirations.
            for (int iRec = 0; iRec < vixCentralRec.Length; iRec++)
            {
                vixCentralRec[iRec].NextExpiryDate = expDates[0];
                vixCentralRec[iRec].F1expDays      = (expDates[0] - vixCentralRec[iRec].Date).Days + 1;
                vixCentralRec[iRec].F2expDays      = (expDates[1] - vixCentralRec[iRec].Date).Days + 1;
                vixCentralRec[iRec].F3expDays      = (expDates[2] - vixCentralRec[iRec].Date).Days + 1;
                vixCentralRec[iRec].F4expDays      = (expDates[3] - vixCentralRec[iRec].Date).Days + 1;
                vixCentralRec[iRec].F5expDays      = (expDates[4] - vixCentralRec[iRec].Date).Days + 1;
                vixCentralRec[iRec].F6expDays      = (expDates[5] - vixCentralRec[iRec].Date).Days + 1;
                vixCentralRec[iRec].F7expDays      = (expDates[6] - vixCentralRec[iRec].Date).Days + 1;
                vixCentralRec[iRec].F8expDays      = (vixCentralRec[0].F8 > 0) ? (expDates[7] - vixCentralRec[iRec].Date).Days + 1 : 0;
            }

            string ret = Processing(vixCentralRec, expDates, liveDate, liveFuturesDataTime, spotVixValue, titleGAS, dataSourceGAS);

            return(ret);
        }