100天自学大数据

100天自学大数据:Python获取比特币交易所行情实战!

2018-08-03  本文已影响83人  金哥数据分析

适合人群

注意事项

参考资料

原始链接

https://blog.csdn.net/weixin_37272286/article/details/78032131

交易所概念

http://baijiahao.baidu.com/s?id=1579175614310713604&wfr=spider&for=pc
值得注意的是,比特币在国内外各家交易所的价格都不相同,这就诞生了一门新的生意——“搬砖”。所谓比特币搬砖,就是把比特币在不同交易所之间腾挪,赚取差价套利
比特币交易所停运对于海外的比特币衍生品也是利好。注册在塞舌尔群岛,美国运营的一家比特币商品交易所BitMEX相关人士对澎湃新闻表示,每次中国央行加强监管,他们平台的交易量就会暴涨,究其原因,是因为中国有些比特币持有者不愿意抛掉手中的币,选择观望,但是也不甘心贬值,所以会选择与美股挂钩的衍生品工具来对冲风险

下一步计划

做自动交易(量化交易基础)

怎么读取交易所行情

# Request 
GET https://www.okcoin.cn/api/v1/ticker.do?symbol=ltc_cny
# Response
{
    "date":"1410431279",
    "ticker":{ 
        "buy":"33.15",
        "high":"34.15",
        "last":"33.15",
        "low":"32.05",
        "sell":"33.16",
        "vol":"10532696.39199642"
    }
}

请自己用浏览器,Json.cn, 或shell用curl命令体验一下

以下是代码实现

import json
import time
import pandas as pd
import math
import matplotlib.pyplot as plt

def ticker(bit) : 
    try:
        ret = requests.get("https://www.okcoin.cn/api/v1/ticker.do?symbol=" + bit)
        ret.text

        d = json.loads(ret.text)
        print(d)
    except :
        print('error')

for i in range(10):
    ticker('btc_cny')
    time.sleep(1)

{'date': '1533278866', 'ticker': {'high': '40303.0', 'vol': '1.626', 'last': '40303.0', 'low': '40303.0', 'buy': '40102.0', 'sell': '40303.0'}}
{'date': '1533278868', 'ticker': {'high': '40303.0', 'vol': '1.626', 'last': '40303.0', 'low': '40303.0', 'buy': '40102.0', 'sell': '40303.0'}}
{'date': '1533278869', 'ticker': {'high': '40303.0', 'vol': '1.626', 'last': '40303.0', 'low': '40303.0', 'buy': '40102.0', 'sell': '40303.0'}}
{'date': '1533278871', 'ticker': {'high': '40303.0', 'vol': '1.626', 'last': '40303.0', 'low': '40303.0', 'buy': '40102.0', 'sell': '40303.0'}}
{'date': '1533278872', 'ticker': {'high': '40303.0', 'vol': '1.626', 'last': '40303.0', 'low': '40303.0', 'buy': '40102.0', 'sell': '40303.0'}}
{'date': '1533278873', 'ticker': {'high': '40303.0', 'vol': '1.626', 'last': '40303.0', 'low': '40303.0', 'buy': '40102.0', 'sell': '40303.0'}}
{'date': '1533278875', 'ticker': {'high': '40303.0', 'vol': '1.626', 'last': '40303.0', 'low': '40303.0', 'buy': '40102.0', 'sell': '40303.0'}}
{'date': '1533278877', 'ticker': {'high': '40303.0', 'vol': '1.626', 'last': '40303.0', 'low': '40303.0', 'buy': '40102.0', 'sell': '40303.0'}}
{'date': '1533278878', 'ticker': {'high': '40303.0', 'vol': '1.626', 'last': '40303.0', 'low': '40303.0', 'buy': '40102.0', 'sell': '40303.0'}}

#找了半天,找了这个api.hpx.com 接口获取k线数据

ret = requests.get("https://api.hpx.com/data/v2/kline?currency=btc_cnyt")
d = json.loads(ret.text)
kline = d['data']['data']

kline = pd.DataFrame(kline)
kline[0] = kline[0] // 1000
kline[0]

kline[0] = pd.to_datetime(kline[0],unit='s')

x = kline[0]
y = kline[1]

#x,y
plt.plot(x, y)
plt.show()
image.png

遇到的坑总结

需要源代码请联系 "api1024"

自学交流群:QQ 829163554 纯自学

上一篇 下一篇

猜你喜欢

热点阅读