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

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

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

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

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

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

InBlock.gif                DateTime day;
InBlock.gif                
try
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    day 
= new DateTime(nDate / 10000, (nDate % 10000/ 100, nDate % 100);
ExpandedSubBlockEnd.gif                }

InBlock.gif                
catch(Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    Console.WriteLine(ex.Message);
InBlock.gif                    Console.WriteLine(
"Wrong date:{0}, skip this info", nDate);
InBlock.gif                    nErrorCount
++;
InBlock.gif                    
continue;
ExpandedSubBlockEnd.gif                }
 int nOpenPrice = b_reader.ReadInt32();
InBlock.gif                
int nHighestPrice = b_reader.ReadInt32();
InBlock.gif                
int nLowestPrice = b_reader.ReadInt32();
InBlock.gif                
int nClosePrice = b_reader.ReadInt32();
InBlock.gif                
int nAmount = b_reader.ReadInt32();
InBlock.gif                
int nTransCount = b_reader.ReadInt32();
InBlock.gif                
int nPadding1, nPadding2, nPadding3;
InBlock.gif                nPadding1 
= b_reader.ReadInt32();
InBlock.gif                nPadding2 
= b_reader.ReadInt32();
InBlock.gif                nPadding3 
= b_reader.ReadInt32();
InBlock.gif
InBlock.gif                DayData dd 
= new DayData();
InBlock.gif                dd.m_market 
= p_market;
InBlock.gif                dd.m_nAmount 
= nAmount;
InBlock.gif                dd.m_nClosePrice 
= nClosePrice;
InBlock.gif                dd.m_nCount 
= nTransCount;
InBlock.gif                dd.m_nHighestPrice 
= nHighestPrice;
InBlock.gif                dd.m_nLowestPrice 
= nLowestPrice;
InBlock.gif                dd.m_nOpenPrice 
= nOpenPrice;
InBlock.gif                dd.m_strStockCode 
= p_strStockCode;
InBlock.gif                dd.m_transDate 
= day;
InBlock.gif
InBlock.gif                
if (existingData.ContainsKey(day))
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    DayData t1 
= existingData[day];
InBlock.gif                    Debug.Assert(t1 
== dd);
InBlock.gif                    nExistingCount
++;
InBlock.gif                    
continue;
ExpandedSubBlockEnd.gif                }

InBlock.gif                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    existingData.Add(dd.m_transDate, dd);
ExpandedSubBlockEnd.gif                }

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

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

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

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

None.gif

转载于:https://www.cnblogs.com/sliencer/archive/2007/03/26/688627.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值