从Dukascopy刻度二进制文件读取数据 [英] Reading data from Dukascopy tick binary file

查看:170
本文介绍了从Dukascopy刻度二进制文件读取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经下载了Dukascopy tick数据,我已经解压缩它与easylzma库。原始压缩二进制文件是EURUSD / 2010/00/08 / 12h_ticks.bi5(EURUSD / 2010 / ian / 8 / 12h)解压缩后,我们得到以下格式:

I have downloaded the Dukascopy tick data and I have decompressing it with easylzma library. The original compressed binary file is EURUSD/2010/00/08/12h_ticks.bi5 (EURUSD/2010/ian/8/12h) After decompressing we get the following format:

+-------------------------+--------+-------+
|           time          |  Bid   |   Ask |
+-------------------------+--------+-------+
000003CA 00022EC0 00022EB6 40CCCCCD 41180000
000004F5 00022EB6 00022EB1 4099999A 404CCCCD

压缩文件从:
EURUSD / 2010/00/08 / 12h_ticks.bi5
使用lzma解压缩后,我们得到以下文件:
12h_ticks

(You can download original compressed file from: EURUSD/2010/00/08/12h_ticks.bi5. After decompressing it with lzma we get the file: 12h_ticks)

读取二进制文件:

int ii1;
int ii2;
int ii3;
float ff1;
float ff2;
ifstream in("12h_ticks",ofstream::binary);
in.read((char*)(&ii1), sizeof(int));
in.read((char*)(&ii2), sizeof(int));
in.read((char*)(&ii3), sizeof(int));
in.read((char*)(&ff1), sizeof(float));
in.read((char*)(&ff2), sizeof(float));
std::cout << " ii1=" << ii1 << std::endl;
std::cout << " ii2=" << ii2 << std::endl;
std::cout << " ii3=" << ii3 << std::endl;
std::cout << " ff1=" << ff1 << std::endl;
std::cout << " ff2=" << ff2 << std::endl;
in.close();

我得到以下结果:

ii1=-905773056
ii2=-1070726656
ii3=-1238498816
ff1=-4.29492e+08
ff2=8.70066e-42

有什么问题?我无法从二进制文件读取数据。请帮助我。

What is wrong? I can't read data from binary file. Please help me.

推荐答案

数据似乎以大尾数格式存储在文件中。

The data appears to be stored in big endian format in the file. You'll need to convert it to little endian when you load it.

#include <iostream>
#include <fstream>
#include <algorithm>

template<typename T>
void ByteSwap(T* p)
{
    for (int i = 0;  i < sizeof(T)/2;  ++i)
        std::swap( ((char *)p)[i], ((char *)p)[sizeof(T)-1-i] );
}

int main()
{
    int ii1;
    int ii2;
    int ii3;
    float ff1;
    float ff2;
    std::ifstream in("12h_ticks",std::ofstream::binary);
    in.read((char*)(&ii1), sizeof(int));
    in.read((char*)(&ii2), sizeof(int));
    in.read((char*)(&ii3), sizeof(int));
    in.read((char*)(&ff1), sizeof(float));
    in.read((char*)(&ff2), sizeof(float));

    ByteSwap(&ii1);
    ByteSwap(&ii2);
    ByteSwap(&ii3);
    ByteSwap(&ff1);
    ByteSwap(&ff2);

    std::cout << " ii1=" << ii1 << std::endl;
    std::cout << " ii2=" << ii2 << std::endl;
    std::cout << " ii3=" << ii3 << std::endl;
    std::cout << " ff1=" << ff1 << std::endl;
    std::cout << " ff2=" << ff2 << std::endl;
    in.close();
    return 0;
}

这会得到结果:

ii1=970
ii2=143040
ii3=143030
ff1=6.4
ff2=9.5

如果你想更多地了解这个主题,我从这里抓取了ByteSwap函数。 如何在大-endian和little-endian值在C ++?

I grabbed the ByteSwap function from here if you want to read more about that subject. How do I convert between big-endian and little-endian values in C++?

这篇关于从Dukascopy刻度二进制文件读取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆