1

我正在使用来自http://code.google.com/p/amibroker的非官方 C# sdk 为 Amibroker 开发数据插件。所做的是读取文本文件,将内容解析为 amibroker 的数据库格式,以便将数据显示为图形。这是输入数据的示例:

2012.07.09 01:35:27,12763,1
2012.07.09 01:35:50,12762,1
2012.07.09 01:36:43,12761,1
2012.07.09 01:37:10,12760,1 2012.07
。 09 01:37:44,12761,1

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

输出示例是(如何解析上述 5 行):

2012/07/09 01:35 12763 1
2083/08/22 12762 1
2083/08/22 12761 1
2012/07/09 01:37 12760 1
2083/08/22 12761 1

如您所见,有些日期是正确的,有些日期是 2083/08/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 函数的理解是否正确?也就是说,我是否投入了正确的输入?还是我读取和解析文本文件的方式有问题?这是我怀疑我出错的两个地方。我只是不确定在哪里。

4

1 回答 1

1

问题与PackDate- 所有 32 位bv1都用于存储时间,并且将整数值bv1.Data直接转换为 aulong会导致在填充的第一位时溢出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;
于 2012-08-08T04:45:34.690 回答