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 関数についての私の理解は正しいですか? つまり、正しい入力を入れていますか? または、テキスト ファイルを読み取って解析する方法に何か問題がありますか? これらは、私が間違っていると思われる2つの場所です。どこにあるのかわかりません。

4

1 に答える 1

1

問題は関連していPackDateます - の 32 ビットすべてbv1が時間を格納するために使用され、整数値bv1.Dataを a に直接キャストするulongbv1、int の MSB が記号。

PackDatebv1 にエンコードされるものは次のとおりです。

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 に答える