从钱龙数据中读取股票交易数据(日线)导入到数据库

前面写了如果读股票代码,下面是如何读日线数据的代码。

钱龙中日线数据存储在QLDATA\history\shase\day和QLDATA\history\sznse\day目录下,每个文件对应一只股票。

与前文一样,只贴核心代码:

        public void ParseAndSave(string p_strFileName, string p_strStockCode, Market p_market)
        
{
            Console.WriteLine(
"Parsing stock {0} using file '{1}'", p_strStockCode, p_strFileName);
            Debug.Assert(p_strStockCode.Trim().Length 
== 6);
            FileStream stream 
= new FileStream(p_strFileName, FileMode.Open, FileAccess.Read);
            BinaryReader b_reader 
= new BinaryReader(stream);
            SqlCommand cmd 
= m_conn.CreateCommand();
            cmd.Transaction 
= m_tran;
            
string strInsertCmd = Properties.Resources.InsertCommand;
            strInsertCmd 
= strInsertCmd.Replace("@StockData_DayInfo", Properties.Settings.Default.TableName);
            cmd.CommandText 
= strInsertCmd;
            cmd.Parameters.Add(
"@StockCode", SqlDbType.NChar, 6);
            cmd.Parameters.Add(
"@TransactionDate", SqlDbType.DateTime);
            cmd.Parameters.Add(
"@OpenPrice", SqlDbType.BigInt);
            cmd.Parameters.Add(
"@HighestPrice", SqlDbType.BigInt);
            cmd.Parameters.Add(
"@LowestPrice", SqlDbType.BigInt);
            cmd.Parameters.Add(
"@ClosePrice", SqlDbType.BigInt);
            cmd.Parameters.Add(
"@TotalTransactionAmount", SqlDbType.BigInt);
            cmd.Parameters.Add(
"@TotalTransactionCount", SqlDbType.BigInt);
            cmd.Parameters.Add(
"@Market", SqlDbType.NVarChar, 10);
            
int nInsertCount = 0;
            
int nExistingCount = 0;
            
int nErrorCount = 0;
            
            
while (stream.CanRead && stream.Position < stream.Length)
            
{
                
int nDate = 0;
                
try
                
{
                    nDate 
= b_reader.ReadInt32();
                }

                
catch(Exception ex)
                
{
                    nErrorCount
++;
                    Debug.Assert(
!p_strStockCode.StartsWith("600"));
                    Console.WriteLine(
"Failed to read stock code {0} from market {1}, error = {2}", p_strStockCode, p_market, ex.Message);
                    
break;
                }

                DateTime day;
                
try
                
{
                    day 
= new DateTime(nDate / 10000, (nDate % 10000/ 100, nDate % 100);
                }

                
catch(Exception ex)
                
{
                    Console.WriteLine(ex.Message);
                    Console.WriteLine(
"Wrong date:{0}, skip this info", nDate);
                    nErrorCount
++;
                    
continue;
                }
 int nOpenPrice = b_reader.ReadInt32();
                
int nHighestPrice = b_reader.ReadInt32();
                
int nLowestPrice = b_reader.ReadInt32();
                
int nClosePrice = b_reader.ReadInt32();
                
int nAmount = b_reader.ReadInt32();
                
int nTransCount = b_reader.ReadInt32();
                
int nPadding1, nPadding2, nPadding3;
                nPadding1 
= b_reader.ReadInt32();
                nPadding2 
= b_reader.ReadInt32();
                nPadding3 
= b_reader.ReadInt32();

                DayData dd 
= new DayData();
                dd.m_market 
= p_market;
                dd.m_nAmount 
= nAmount;
                dd.m_nClosePrice 
= nClosePrice;
                dd.m_nCount 
= nTransCount;
                dd.m_nHighestPrice 
= nHighestPrice;
                dd.m_nLowestPrice 
= nLowestPrice;
                dd.m_nOpenPrice 
= nOpenPrice;
                dd.m_strStockCode 
= p_strStockCode;
                dd.m_transDate 
= day;

                
if (existingData.ContainsKey(day))
                
{
                    DayData t1 
= existingData[day];
                    Debug.Assert(t1 
== dd);
                    nExistingCount
++;
                    
continue;
                }

                
else
                
{
                    existingData.Add(dd.m_transDate, dd);
                }



                cmd.Parameters[
0].Value = p_strStockCode;
                cmd.Parameters[
1].Value = day;
                cmd.Parameters[
2].Value = nOpenPrice;
                cmd.Parameters[
3].Value = nHighestPrice;
                cmd.Parameters[
4].Value = nLowestPrice;
                cmd.Parameters[
5].Value = nClosePrice;
                cmd.Parameters[
6].Value = nAmount;
                cmd.Parameters[
7].Value = nTransCount;
                cmd.Parameters[
8].Value = p_market.ToString();
                
int nResultCnt;
                
try
                
{
                    nResultCnt 
= cmd.ExecuteNonQuery();
                    Debug.Assert(nResultCnt 
== 1);
                    nInsertCount 
+= nResultCnt;
                }

                
catch (Exception ex)
                
{
                    Console.WriteLine(ex.Message);
                    Console.WriteLine(
"{0}\t{1}\t{2}\t{3}\t{4}\t{5}\t{6} - \t{7}\t{8}\t{9}",
                        day.ToShortDateString(), nOpenPrice, nHighestPrice, nLowestPrice, nClosePrice, nAmount, nTransCount,
                        nPadding1, nPadding2, nPadding3);
                    
throw;
                }
 Debug.Assert(nResultCnt == 1);
            }

            b_reader.Close();
            Console.WriteLine(
"{0} recorded have been inserted into Table{1}", nInsertCount, Properties.Settings.Default.TableName);
            
if (nErrorCount > 0)
            
{
                Console.WriteLine(
"{0} recorded has error", nErrorCount);
            }

            Console.WriteLine(
"{0} existing", nExistingCount);
        }


posted on 2007-03-26 16:11  learner  阅读(4190)  评论(0编辑  收藏  举报

导航