有了前幾天的數據准備,那讓我們看一下7月份的股市是牛市還是熊市呢?
牛市和熊市主要就看股票的漲跌分布情況,雖然我們看大盤數據,但是不過中石油占的A股市值太大了,並且中石油95%的股票都是大股東控制的相當於非流通的股票。
我們這里看一下每天的股票漲跌數量占總的可交易股票數量的百分比。我們按照每漲跌1%為一個區間進行划分。
首先我們寫一個存儲過程,計算每天的股票漲跌情況。

CREATE PROCEDURE [dbo].[GetDistribution] -- Add the parameters for the stored procedure here @date smalldatetime AS BEGIN -- SET NOCOUNT ON added to prevent extra result sets from -- interfering with SELECT statements. SET NOCOUNT ON; declare @days int set @days=(select COUNT(*) from DayData Where 日期=@date) INSERT INTO dbo.Distribution select @date AS 日期,* from (select COUNT(*)*1.0/@days 幅度_9 from dbo.DayData where 日期=@date AND 漲跌幅<-9) a, (select COUNT(*)*1.0/@days 幅度_9_8 from dbo.DayData where 日期=@date AND 漲跌幅>-9 AND 漲跌幅<-8) b, (select COUNT(*)*1.0/@days 幅度_8_7 from dbo.DayData where 日期=@date AND 漲跌幅>-8 AND 漲跌幅<-7) c, (select COUNT(*)*1.0/@days 幅度_7_6 from dbo.DayData where 日期=@date AND 漲跌幅>-7 AND 漲跌幅<-6) d, (select COUNT(*)*1.0/@days 幅度_6_5 from dbo.DayData where 日期=@date AND 漲跌幅>-6 AND 漲跌幅<-5) e, (select COUNT(*)*1.0/@days 幅度_5_4 from dbo.DayData where 日期=@date AND 漲跌幅>-5 AND 漲跌幅<-4) f, (select COUNT(*)*1.0/@days 幅度_4_3 from dbo.DayData where 日期=@date AND 漲跌幅>-4 AND 漲跌幅<-3)g, (select COUNT(*)*1.0/@days 幅度_3_2 from dbo.DayData where 日期=@date AND 漲跌幅>-3 AND 漲跌幅<-2)h, (select COUNT(*)*1.0/@days 幅度_2_1 from dbo.DayData where 日期=@date AND 漲跌幅>-2 AND 漲跌幅<-1)i, (select COUNT(*)*1.0/@days 幅度_10 from dbo.DayData where 日期=@date AND 漲跌幅>-1 AND 漲跌幅<0)j, (select COUNT(*)*1.0/@days 幅度01 from dbo.DayData where 日期=@date AND 漲跌幅>0 AND 漲跌幅<1)k, (select COUNT(*)*1.0/@days 幅度12 from dbo.DayData where 日期=@date AND 漲跌幅>1 AND 漲跌幅<2)l, (select COUNT(*)*1.0/@days 幅度23 from dbo.DayData where 日期=@date AND 漲跌幅>2 AND 漲跌幅<3)m, (select COUNT(*)*1.0/@days 幅度34 from dbo.DayData where 日期=@date AND 漲跌幅>3 AND 漲跌幅<4)n, (select COUNT(*)*1.0/@days 幅度45 from dbo.DayData where 日期=@date AND 漲跌幅>4 AND 漲跌幅<5)o, (select COUNT(*)*1.0/@days 幅度56 from dbo.DayData where 日期=@date AND 漲跌幅>5 AND 漲跌幅<6)p, (select COUNT(*)*1.0/@days 幅度67 from dbo.DayData where 日期=@date AND 漲跌幅>6 AND 漲跌幅<7)q, (select COUNT(*)*1.0/@days 幅度78 from dbo.DayData where 日期=@date AND 漲跌幅>7 AND 漲跌幅<8)r, (select COUNT(*)*1.0/@days 幅度89 from dbo.DayData where 日期=@date AND 漲跌幅>8 AND 漲跌幅<9)s, (select COUNT(*)*1.0/@days 幅度9 from dbo.DayData where 日期=@date AND 漲跌幅>9)t END
然后我們統計一下今年的股票的分布情況,寫入一個表中,代碼如下:
declare cur Cursor scroll for SELECT distinct 日期 FROM [dbo].[DayData] open cur; declare @date smalldatetime; while @@FETCH_STATUS=0 begin fetch next from cur into @date exec dbo.GetDistribution @date end close cur deallocate cur
接下來我們就要編寫一個繪制柱狀圖的小工具啦,這里我們直接用.Net 的Chart控件,在VisualStudio 數據控件組中就有。
var xName = new String[] { "-9", "-8", "-7", "-6", "-5", "-4", "-3", "-2", "-1", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" }; var yValue=new Double[20]; string connString = "Data Source=.;Initial Catalog=Stock;Integrated Security=True"; var list = new List<String>(); using (SqlConnection connection = new SqlConnection(connString)) { connection.Open(); String SQL = "select distinct CONVERT(varchar(10),日期,23) from dbo.DayData"; SqlCommand CMD = new SqlCommand(SQL, connection); SqlDataReader reader = CMD.ExecuteReader(); while (reader.Read()) { list.Add(reader[0].ToString().Trim()); } } foreach (var date in list) { using (SqlConnection connection = new SqlConnection(connString)) { connection.Open(); String SQL = "select * from dbo.Distribution where 日期='"+date+"'"; SqlCommand CMD = new SqlCommand(SQL, connection); SqlDataReader reader = CMD.ExecuteReader(); while (reader.Read()) { for (int i = 0; i < 20;i++ ) { yValue[i] = Convert.ToDouble(reader[i+1]); } } } Chart1.Series["Series1"].Points.DataBindXY(xName, yValue); Chart1.SaveImage(@"C:\Users\20060737\Documents\Visual Studio 2013\WebSites\Distribution\"+date+".png"); }
這樣我們就得到了每天的股票分布圖,大家來看一下7月份的股票漲跌分布吧。
最左邊代表的是下跌超9%的股票數占A股可交易股票數的比例,接下來是-8%,-7%直至上漲超9%的股票數量。
大家看了這張圖,肯定和我一樣不由得感嘆,A股真是太刺激了!