获取比特币历史数据

Cod*_*ree 121 bitcoin

我想做自己的比特币图表.

您是否知道检索比特币历史价格数据的可靠方法?有没有办法使用REST检索它?我看到Bitfloor,它支持REST,但它没有返回任何有用的值,它有一个"内部服务器错误".

我也看到了Bitcoincharts,但我认为它仅限于2000个数据值.

你会建议我使用任何框架或系统吗?

Lyk*_*nes 147

实际上,您可以通过CSV格式从Bitcoincharts获取整个比特币交易记录:http://api.bitcoincharts.com/v1/csv/

它每天更新两次以进行主动交流,也有一些死交换.

编辑:由于CSV中没有列标题,这里是它们的内容:第1列)交易的时间戳,第2列,价格,第3列)交易量

  • 谨防'bitcoincharts.com`数据的巨大差距.另请注意,没有勾选"买/卖"信息. (3认同)
  • +1是的,它实际上对绘制已建立的交易非常有用.数据也可以通过bitstamp的pusher API实时获取,这正是我现在正在做的事情.将bitstamp索引一天之后,我下载了bitstampUSD.csv并在数据前面添加了完整的图片 (2认同)
  • @Lykegenes 第二列是什么?值在 0.5-33 的范围内,不能是 USD/BTC 的汇率。 (2认同)
  • @theJerm它是UNIX时间戳格式,因此UTC时区自1970年1月1日起的**秒数** (2认同)
  • 我在哪里可以获得Litecoin,以太坊或其他重要硬币的数据? (2认同)

Sea*_*ean 31

您可以在此处找到大量历史数据:https://www.quandl.com/data/BCHARTS-Bitcoin-Charts-Exchange-Rate-Data

  • SO 不鼓励仅链接答案。此外,他正在寻找一种检索数据的方法,而不仅仅是数据本身。 (2认同)
  • 你说得对,我的回答应该更全面。但是它确实回答了他的请求,因为用于检索数据的 API 调用列在页面右侧。 (2认同)
  • @GuillaumeChevalier我发现https://www.quandl.com/data/BCHARTS-Bitcoin-Charts-Exchange-Rate (2认同)

met*_*ttw 15

在这种情况下,您希望在更长的时间段内以更高的分辨率从他们的websocket收集bitstamp交易数据,您可以使用下面的脚本log_bitstamp_trades.py.

该脚本使用python websocket-client和pusher_client_python库,因此请安装它们.

#!/usr/bin/python

import pusherclient
import time
import logging
import sys
import datetime
import signal
import os

logging.basicConfig()
log_file_fd = None

def sigint_and_sigterm_handler(signal, frame):
    global log_file_fd
    log_file_fd.close()
    sys.exit(0)


class BitstampLogger:

    def __init__(self, log_file_path, log_file_reload_path, pusher_key, channel, event):
        self.channel = channel
        self.event = event
        self.log_file_fd = open(log_file_path, "a")
        self.log_file_reload_path = log_file_reload_path
        self.pusher = pusherclient.Pusher(pusher_key)
        self.pusher.connection.logger.setLevel(logging.WARNING)
        self.pusher.connection.bind('pusher:connection_established', self.connect_handler)
        self.pusher.connect()

    def callback(self, data):
        utc_timestamp = time.mktime(datetime.datetime.utcnow().timetuple())
        line = str(utc_timestamp) + " " + data + "\n"
        if os.path.exists(self.log_file_reload_path):
            os.remove(self.log_file_reload_path)
            self.log_file_fd.close()
            self.log_file_fd = open(log_file_path, "a")
        self.log_file_fd.write(line)

    def connect_handler(self, data):
        channel = self.pusher.subscribe(self.channel)
        channel.bind(self.event, self.callback)


def main(log_file_path, log_file_reload_path):
    global log_file_fd
    bitstamp_logger = BitstampLogger(
        log_file_path,
        log_file_reload_path,
        "de504dc5763aeef9ff52",
        "live_trades",
        "trade")
    log_file_fd = bitstamp_logger.log_file_fd
    signal.signal(signal.SIGINT, sigint_and_sigterm_handler)
    signal.signal(signal.SIGTERM, sigint_and_sigterm_handler)
    while True:
        time.sleep(1)


if __name__ == '__main__':
    log_file_path = sys.argv[1]
    log_file_reload_path = sys.argv[2]
    main(log_file_path, log_file_reload_path
Run Code Online (Sandbox Code Playgroud)

和logrotate文件配置

/mnt/data/bitstamp_logs/bitstamp-trade.log
{
    rotate 10000000000
    minsize 10M
    copytruncate
    missingok
    compress
    postrotate
        touch /mnt/data/bitstamp_logs/reload_log > /dev/null
    endscript
}
Run Code Online (Sandbox Code Playgroud)

然后你可以在后台运行它

nohup ./log_bitstamp_trades.py /mnt/data/bitstamp_logs/bitstamp-trade.log /mnt/data/bitstamp_logs/reload_log &
Run Code Online (Sandbox Code Playgroud)


use*_*123 7

Bitstamp具有JSON此链接上公开可用的实时比特币数据. 不要试图在十分钟内访问它超过600次,否则他们会阻止你的IP(另外,这是不必要的; 在这里阅读更多).以下是C#获取实时数据的方法:

using (var WebClient = new System.Net.WebClient())
{
     var json = WebClient.DownloadString("https://www.bitstamp.net/api/ticker/");
     string value = Convert.ToString(json);
     // Parse/use from here
}
Run Code Online (Sandbox Code Playgroud)

从这里,您可以解析JSON并将其存储在数据库中(或MongoDB直接插入),然后访问它.

对于历史数据(取决于数据库 - 如果您接近它的方式),从平面文件执行插入,大多数数据库允许您使用(例如,SQL Server您可以BULK INSERTCSV文件中执行).


dom*_*omi 5

我为这种情况编写了一个java示例:

使用 json.org 库检索 JSONObjects 和 JSONArrays。下面的例子使用了blockchain.info 的数据,它可以作为JSONObject 获取。

    public class main 
    {
        public static void main(String[] args) throws MalformedURLException, IOException
        {
            JSONObject data = getJSONfromURL("https://blockchain.info/charts/market-price?format=json");
            JSONArray data_array = data.getJSONArray("values");

            for (int i = 0; i < data_array.length(); i++)
            {
                JSONObject price_point = data_array.getJSONObject(i);

                //  Unix time
                int x = price_point.getInt("x");

                //  Bitcoin price at that time
                double y = price_point.getDouble("y");

                //  Do something with x and y.
            }

        }

        public static JSONObject getJSONfromURL(String URL)
        {
            try
            {
                URLConnection uc;
                URL url = new URL(URL);
                uc = url.openConnection();
                uc.setConnectTimeout(10000);
                uc.addRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)");
                uc.connect();

                BufferedReader rd = new BufferedReader(
                        new InputStreamReader(uc.getInputStream(), 
                        Charset.forName("UTF-8")));

                StringBuilder sb = new StringBuilder();
                int cp;
                while ((cp = rd.read()) != -1)
                {
                    sb.append((char)cp);
                }

                String jsonText = (sb.toString());            

                return new JSONObject(jsonText.toString());
            } catch (IOException ex)
            {
                return null;
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)


lei*_*man 5

Coinbase 有一个REST API,可让您从其网站访问历史价格。数据似乎每十分钟显示一次 Coinbase 现货价格(以美元计)。

结果以 CSV 格式返回。您必须通过 API 查询您想要的页码。每页有 1000 个结果(或价格点)。这大约是每页 7 天的数据。


f1l*_*t3r 5

使用 Node.js 将其抓取为 JSON 会很有趣:)

https://github.com/f1lt3r/bitcoin-scraper

在此处输入图片说明

[
  [
    1419033600,  // Timestamp (1 for each minute of entire history)
    318.58,      // Open
    318.58,      // High
    318.58,      // Low
    318.58,      // Close
    0.01719605,  // Volume (BTC)
    5.478317609, // Volume (Currency)
    318.58       // Weighted Price (USD)
  ]
]
Run Code Online (Sandbox Code Playgroud)