2012-08-07 28 views
1

我正在使用来自http://code.google.com/p/amibroker的非官方C#sdk为Amibroker开发数据插件。什么是读取文本文件,将内容解析为amibroker的数据库格式,以便将数据显示为图形。下面是输入数据的一个示例:将编码日期设为ulong以便与AmiBroker一起使用

2012.07.09 01:35:27,12763,1
2012.07.09 01:35:50,12762,1
2012.07.09 1点36分43秒, 12761,1
2012.07.09 01:37:10,12760,1
2012.07.09 01:37:44,12761,1

格式为日期/时间,关闭,音量。

输出的样品是(以上5条线是怎么解析):

2012/07/09 1点35 12763 1
2083年8月22日12762 1
2083年8月22日12761 1
2012/07/09 01:37 12760 1
2083年8月22日12761 1

正如你所看到的,有些日期是走出正确的,其他人都出来为2083年8月22日。这是怎么发生的,因为所有的输入日期都是相同的格式?

在C#DLL,我使用了进口的代码是:

unsafe public static int GetQuotesEx(string ticker, Periodicity periodicity, int lastValid, int size, Quotation* quotes, GQEContext* context) 
    { 

     string fileName = "C:\\" + ticker + ".txt"; 

     System.IO.StreamReader objReader; 
     objReader = new System.IO.StreamReader(fileName); 
     int i = 0; 
     string line = ""; 

     while((line = objReader.ReadLine()) != null && i < size) 
     { 
      string[] splitLine = line.Split(','); 
      DateTime dt = Convert.ToDateTime(splitLine[0]); 
      float bidprice = float.Parse(splitLine[1]); 
      float volume = float.Parse(splitLine[2]); 

      quotes[i].DateTime = PackDate(dt); 
      quotes[i].Price = bidprice; 
      quotes[i].Open = bidprice; 
      quotes[i].High = bidprice; 
      quotes[i].Low = bidprice; 
      quotes[i].Volume = volume; 
      i++; 
     } 

     return i; 
    } 

而且PackDate功能代码(我没有写这一点,它在DLL):

/// <summary> 
    /// Pack AmiBroker DateTime object into UInt64 
    /// </summary> 
    static ulong PackDate(DateTime date) 
    { 
     return PackDate(date, false); 
    } 

    /// <summary> 
    /// Pack AmiBroker DateTime object into UInt64 
    /// </summary> 
    static ulong PackDate(DateTime date, bool isFeaturePad) 
    { 
     var isEOD = date.Hour == 0 && date.Minute == 0 && date.Second == 0; 

     // lower 32 bits 
     var ft = BitVector32.CreateSection(1); 
     var rs = BitVector32.CreateSection(23, ft); 
     var ms = BitVector32.CreateSection(999, rs); 
     var ml = BitVector32.CreateSection(999, ms); 
     var sc = BitVector32.CreateSection(59, ml); 

     var bv1 = new BitVector32(0); 
     bv1[ft] = isFeaturePad ? 1 : 0;   // bit marking "future data" 
     bv1[rs] = 0;       // reserved set to zero 
     bv1[ms] = 0;       // microseconds 0..999 
     bv1[ml] = date.Millisecond;    // milliseconds 0..999 
     bv1[sc] = date.Second;     // 0..59 

     // higher 32 bits 
     var mi = BitVector32.CreateSection(59); 
     var hr = BitVector32.CreateSection(23, mi); 
     var dy = BitVector32.CreateSection(31, hr); 
     var mn = BitVector32.CreateSection(12, dy); 
     var yr = BitVector32.CreateSection(4095, mn); 

     var bv2 = new BitVector32(0); 
     bv2[mi] = isEOD ? 63 : date.Minute;  // 0..59  63 is reserved as EOD marker 
     bv2[hr] = isEOD ? 31 : date.Hour;  // 0..23  31 is reserved as EOD marker 
     bv2[dy] = date.Day;      // 1..31 
     bv2[mn] = date.Month;     // 1..12 
     bv2[yr] = date.Year;     // 0..4095 

     return ((ulong)bv2.Data << 32)^(ulong)bv1.Data; 
    } 

我想知道的是,我对PackDate函数的理解是否正确?那就是,我是否将正确的输入放入其中?或者,我正在阅读和解析文本文件的方式有问题吗?那些是我怀疑我会犯错的两个地方。我只是不确定在哪里。

回答

1

的问题是关系到PackDate - 的bv1所有32位被用来存储时间,和铸造在bv1.Data直到ulong整数值引起的溢流填充的bv1第一比特时,由于实际上一个int的MSB用于符号。

这里是PackDate编码为BV1:

Name | Max size (decimal) | Bits required 
ft | 1    | 1 
rs | 23    | 5 
ms | 999    | 10 
ml | 999    | 10 
sc | 59    | 6 
==================================== 
TOTAL      32 

自“秒”领域占据着最显著位,在溢出为其中的秒数超过31

任何日期为防止溢出,最后一行PackDate需要更改为:

return ((ulong)(uint)bv2.Data << 32)^(uint)bv1.Data; 
+0

谢谢你这么这么好!你已经帮了我很多了!有效。我试图标记它,显然我没有足够的声望去做+1000。 – Sethmo011 2012-08-16 06:49:39

+0

不用担心,@ user1581495。这是一个很好的问题!即使你没有享有盛誉的声望,你仍然可以将我的答案标记为[接受的答案](http://cdn.sstatic.net/img/faq/faq-accept-answer.png)... – 2012-08-16 21:19:00

相关问题