Lyk*_*nes 147
实际上,您可以通过CSV格式从Bitcoincharts获取整个比特币交易记录:http://api.bitcoincharts.com/v1/csv/
它每天更新两次以进行主动交流,也有一些死交换.
编辑:由于CSV中没有列标题,这里是它们的内容:第1列)交易的时间戳,第2列,价格,第3列)交易量
Sea*_*ean 31
您可以在此处找到大量历史数据:https://www.quandl.com/data/BCHARTS-Bitcoin-Charts-Exchange-Rate-Data
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)
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 INSERT从CSV文件中执行).
我为这种情况编写了一个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)
使用 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)
| 归档时间: |
|
| 查看次数: |
117822 次 |
| 最近记录: |