从钱龙数据中读取股票权息信息导入到数据库
前面写了如果读股票代码和日线数据,下面是如何读股票的权息信息。
钱龙中权息数据存储在QLDATA\history\shase\weight和QLDATA\history\sznse\weight目录下,每个文件对应一只股票。
与前文一样,只贴核心代码:
钱龙中权息数据存储在QLDATA\history\shase\weight和QLDATA\history\sznse\weight目录下,每个文件对应一只股票。
与前文一样,只贴核心代码:
private static void ReadStockWeights(string strPath, string p_strMarket)
{
string[] parts = strPath.Split('\\');
string strStockCode = null;
for (int i = parts.Length - 1; i >= 0;i-- )
{
string strTemp = parts[i];
if (strTemp.ToUpper().EndsWith(".WGT"))
{
strStockCode = strTemp.Substring(0, strTemp.Length - 4) ;
break;
}
}
Debug.Assert(strStockCode != null);
Console.WriteLine("Read stock weight from file '" + strPath + "'");
FileStream stream = new FileStream(strPath, FileMode.Open, FileAccess.Read);
BinaryReader b_reader = new BinaryReader(stream);
List<StockWeightInfo> weightInfos = new List<StockWeightInfo>();
try
{
while (stream.CanRead && stream.Position < stream.Length)
{
int[] oneRow = new int[9];
for( int i=0;i<9;i++)
{
oneRow[i] = b_reader.ReadInt32();
}//for
if (oneRow[8] != 0)
{
throw new Exception("Last entry is not empty");
}
//read date
int nYear = oneRow[0] >> 20;
int nMon = (int)(((uint)(oneRow[0] << 12))>> 28);
int nDay = (oneRow[0] & 0xffff)>> 11;
DateTime wgtDate;
if (nYear == 0 && nMon == 0 && nDay == 0)
wgtDate = DateTime.MinValue;
else
wgtDate = new DateTime(nYear, nMon, nDay);
StockWeightInfo wgtInfo = new StockWeightInfo();
wgtInfo.m_date = wgtDate;
wgtInfo.m_stockCountAsGift = oneRow[1];///10000.0f;
wgtInfo.m_stockCountForSell = oneRow[2];///10000.0f;
wgtInfo.m_priceForSell = oneRow[3];///1000.0f;
wgtInfo.m_bonus = oneRow[4];///1000.0f;
wgtInfo.m_stockCountOfIncreasement = oneRow[5];///10000.0f;
wgtInfo.m_stockOwnership = (ulong)oneRow[6];
wgtInfo.m_freeStockCount = (ulong)oneRow[7];
if (!weightInfos.Contains(wgtInfo))
{
weightInfos.Add(wgtInfo);
//Console.WriteLine();
//Console.Write(wgtInfo.ToString());
}
}//while
weightInfos.Sort();
}
catch (EndOfStreamException)
{
Console.WriteLine("Unexpected end of stream");
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
throw;
}
finally
{
stream.Close();
}
SqlCommand cmd = new SqlCommand("", m_conn);
cmd.Transaction = m_tran;
cmd.CommandText = "INSERT INTO [StockWeightInfos] ([StockCode] ,[Market] ,[Date] ,[StockCountAsGift] ,[StockCountForSell] ,[PriceForSell] ,[Bonus] ,[StockCountOfIncreasement] ,[StockOwnership] ,[FreeStockCount]) " +
"VALUES (@StockCode, @Market ,@Date ,@StockCountAsGift ,@StockCountForSell ,@PriceForSell ,@Bonus ,@StockCountOfIncreasement ,@StockOwnership ,@FreeStockCount)";
cmd.Parameters.Add("@StockCode", SqlDbType.NVarChar, 50);
cmd.Parameters.Add("@Market", SqlDbType.NVarChar, 50);
cmd.Parameters.Add("@Date", SqlDbType.DateTime);
cmd.Parameters.Add("@StockCountAsGift", SqlDbType.Real);
cmd.Parameters.Add("@StockCountForSell", SqlDbType.Real);
cmd.Parameters.Add("@PriceForSell", SqlDbType.Real);
cmd.Parameters.Add("@Bonus", SqlDbType.Real);
cmd.Parameters.Add("@StockCountOfIncreasement", SqlDbType.Real);
cmd.Parameters.Add("@StockOwnership", SqlDbType.BigInt);
cmd.Parameters.Add("@FreeStockCount", SqlDbType.BigInt);
foreach(StockWeightInfo info in weightInfos)
{
cmd.Parameters["@StockCode"].Value = strStockCode;
cmd.Parameters["@Market"].Value = p_strMarket;
cmd.Parameters["@Bonus"].Value = info.m_bonus / 1000.0;
if (info.m_date != DateTime.MinValue)
cmd.Parameters["@Date"].Value = info.m_date;
else
cmd.Parameters["@Date"].Value = DBNull.Value;
cmd.Parameters["@FreeStockCount"].Value = info.m_freeStockCount;
cmd.Parameters["@PriceForSell"].Value = info.m_priceForSell/1000.0;
cmd.Parameters["@StockCountAsGift"].Value = info.m_stockCountAsGift/10000.0;
cmd.Parameters["@StockCountForSell"].Value = info.m_stockCountForSell/10000.0;
cmd.Parameters["@StockCountOfIncreasement"].Value = info.m_stockCountOfIncreasement/10000.0;
cmd.Parameters["@StockOwnership"].Value = info.m_stockOwnership;
int nCnt=cmd.ExecuteNonQuery();
Debug.Assert(nCnt == 1);
}//foreach
}//ReadStockWeights
{
string[] parts = strPath.Split('\\');
string strStockCode = null;
for (int i = parts.Length - 1; i >= 0;i-- )
{
string strTemp = parts[i];
if (strTemp.ToUpper().EndsWith(".WGT"))
{
strStockCode = strTemp.Substring(0, strTemp.Length - 4) ;
break;
}
}
Debug.Assert(strStockCode != null);
Console.WriteLine("Read stock weight from file '" + strPath + "'");
FileStream stream = new FileStream(strPath, FileMode.Open, FileAccess.Read);
BinaryReader b_reader = new BinaryReader(stream);
List<StockWeightInfo> weightInfos = new List<StockWeightInfo>();
try
{
while (stream.CanRead && stream.Position < stream.Length)
{
int[] oneRow = new int[9];
for( int i=0;i<9;i++)
{
oneRow[i] = b_reader.ReadInt32();
}//for
if (oneRow[8] != 0)
{
throw new Exception("Last entry is not empty");
}
//read date
int nYear = oneRow[0] >> 20;
int nMon = (int)(((uint)(oneRow[0] << 12))>> 28);
int nDay = (oneRow[0] & 0xffff)>> 11;
DateTime wgtDate;
if (nYear == 0 && nMon == 0 && nDay == 0)
wgtDate = DateTime.MinValue;
else
wgtDate = new DateTime(nYear, nMon, nDay);
StockWeightInfo wgtInfo = new StockWeightInfo();
wgtInfo.m_date = wgtDate;
wgtInfo.m_stockCountAsGift = oneRow[1];///10000.0f;
wgtInfo.m_stockCountForSell = oneRow[2];///10000.0f;
wgtInfo.m_priceForSell = oneRow[3];///1000.0f;
wgtInfo.m_bonus = oneRow[4];///1000.0f;
wgtInfo.m_stockCountOfIncreasement = oneRow[5];///10000.0f;
wgtInfo.m_stockOwnership = (ulong)oneRow[6];
wgtInfo.m_freeStockCount = (ulong)oneRow[7];
if (!weightInfos.Contains(wgtInfo))
{
weightInfos.Add(wgtInfo);
//Console.WriteLine();
//Console.Write(wgtInfo.ToString());
}
}//while
weightInfos.Sort();
}
catch (EndOfStreamException)
{
Console.WriteLine("Unexpected end of stream");
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
throw;
}
finally
{
stream.Close();
}
SqlCommand cmd = new SqlCommand("", m_conn);
cmd.Transaction = m_tran;
cmd.CommandText = "INSERT INTO [StockWeightInfos] ([StockCode] ,[Market] ,[Date] ,[StockCountAsGift] ,[StockCountForSell] ,[PriceForSell] ,[Bonus] ,[StockCountOfIncreasement] ,[StockOwnership] ,[FreeStockCount]) " +
"VALUES (@StockCode, @Market ,@Date ,@StockCountAsGift ,@StockCountForSell ,@PriceForSell ,@Bonus ,@StockCountOfIncreasement ,@StockOwnership ,@FreeStockCount)";
cmd.Parameters.Add("@StockCode", SqlDbType.NVarChar, 50);
cmd.Parameters.Add("@Market", SqlDbType.NVarChar, 50);
cmd.Parameters.Add("@Date", SqlDbType.DateTime);
cmd.Parameters.Add("@StockCountAsGift", SqlDbType.Real);
cmd.Parameters.Add("@StockCountForSell", SqlDbType.Real);
cmd.Parameters.Add("@PriceForSell", SqlDbType.Real);
cmd.Parameters.Add("@Bonus", SqlDbType.Real);
cmd.Parameters.Add("@StockCountOfIncreasement", SqlDbType.Real);
cmd.Parameters.Add("@StockOwnership", SqlDbType.BigInt);
cmd.Parameters.Add("@FreeStockCount", SqlDbType.BigInt);
foreach(StockWeightInfo info in weightInfos)
{
cmd.Parameters["@StockCode"].Value = strStockCode;
cmd.Parameters["@Market"].Value = p_strMarket;
cmd.Parameters["@Bonus"].Value = info.m_bonus / 1000.0;
if (info.m_date != DateTime.MinValue)
cmd.Parameters["@Date"].Value = info.m_date;
else
cmd.Parameters["@Date"].Value = DBNull.Value;
cmd.Parameters["@FreeStockCount"].Value = info.m_freeStockCount;
cmd.Parameters["@PriceForSell"].Value = info.m_priceForSell/1000.0;
cmd.Parameters["@StockCountAsGift"].Value = info.m_stockCountAsGift/10000.0;
cmd.Parameters["@StockCountForSell"].Value = info.m_stockCountForSell/10000.0;
cmd.Parameters["@StockCountOfIncreasement"].Value = info.m_stockCountOfIncreasement/10000.0;
cmd.Parameters["@StockOwnership"].Value = info.m_stockOwnership;
int nCnt=cmd.ExecuteNonQuery();
Debug.Assert(nCnt == 1);
}//foreach
}//ReadStockWeights