» 您尚未登录:请 登录 | 注册 | 标签 | 帮助 | 小黑屋 |


 16 12
发新话题
打印

[电脑] 写了个比特币市场价差监控的python script,请指教

原理:读取中美市场价格,结合汇率,得出当前市场价差,当价差到达一定数值,则进行中美市场同时低买搞卖的对冲操作

汇率的获取因为yahoo服务器的不稳定,有1/3的几率会获取不到,所以改设一个固定汇率

version 1, 仅在终端输出两个市场的价格,标明当前哪个市场属于低位,以及价差的百分比
复制内容到剪贴板
代码:
import os
import json
import urllib2

huobi = json.load(urllib2.urlopen("http://www.btc123.com/e/interfaces/tickers.php?type=huobiTicker"))
hbprice = huobi['ticker']['last']
btce = json.load(urllib2.urlopen("http://www.btc123.com/e/interfaces/tickers.php?type=btceBTCUSDticker"))
btceprice = btce['ticker']['last']
#usdrate = json.load(urllib2.urlopen("http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.xchange%20where%20pair%20in%20(%22USDCNY%22)&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback="))
#rate = usdrate['query']['results']['rate']['Rate']
rate = 6.04
x = float(hbprice)
y = float(btceprice)
z = float(rate)
w = y*z

if (w<x):
small,big=w,x
else:
small,big=x,w
result = (big-small)/big*100

if small==x:
low = "The Lower is HUOBI"
else:
low = "The Lower is BTCE"


print "HUOBI PRICE",x
print "BTCE PRICE",w
print low
print result
version 2  设置了百分比trigger,当市场价差到达trigger值,则在安装了Terminal-Notify的Mac 10.8以上系统弹出系统消息窗口,当前是使用了Automator在后台无限运行
复制内容到剪贴板
代码:
import os
import json
import urllib2



huobi = json.load(urllib2.urlopen("http://www.btc123.com/e/interfaces/tickers.php?type=huobiTicker"))
hbprice = huobi['ticker']['last']
btce = json.load(urllib2.urlopen("http://www.btc123.com/e/interfaces/tickers.php?type=btceBTCUSDticker"))
btceprice = btce['ticker']['last']
#usdrate = json.load(urllib2.urlopen("http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.xchange%20where%20pair%20in%20(%22USDCNY%22)&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback="))
#rate = usdrate['query']['results']['rate']['Rate']
rate = 6.04
x = float(hbprice)
y = float(btceprice)
z = float(rate)
w = y*z

if (w<x):
small,big=w,x
else:
small,big=x,w
result = (big-small)/big*100

if small==x:
low = "The Lower is HUOBI"
else:
low = "The Lower is BTCE"



def notify(title, subtitle, message):
    t = '-title {!r}'.format(title)
    s = '-subtitle {!r}'.format(subtitle)
    m = '-message {!r}'.format(message)
    os.system('terminal-notifier {}'.format(' '.join([m, t, s])))

trigger = 2.5

if result<trigger:
print result
else:
notify(
    title    = low ,
    subtitle = 'Diffrence Percent is',
    message  = result
)
还需要做的

找一个更好更快更稳定的汇率获取点; 实现email发送trigger以后的信息到指定邮箱; 把市场差价的百分比弄到小数点后2或者3位;让这个python script自己运行而不是用外部方法重新执行; 在路由器上运行这个script


TOP

楼主的意思是在中美比特币市场互倒吗?



TOP

出门左转


TOP

posted by wap, platform: GOOGLE (Nexus 4)

美国哪个交易市场?

TOP

现在老外聪明了,都不能马上提现,搬不了砖了

TOP

posted by wap, platform: Android

Python不熟悉,不过Java里一般不用float计算货币。

TOP

搬砖神器?

TOP

posted by wap, platform: Android

原来那个自动换桌面也是LZ做的吗?

TOP

posted by wap, platform: Amazon (Kindle)
引用:
原帖由 @meizizi  于 2014-1-15 13:34 发表
posted by wap, platform: Android

原来那个自动换桌面也是LZ做的吗?
没啥基础,边摸边写的
本帖最近评分记录
  • meizizi 激骚 +1 版务处理 2014-1-15 14:59

TOP

posted by wap, platform: Android

看了看python网站,建议用integer来计算货币。

TOP

简单的建议啊,python现在比较流行的网络库是requests,拥有非常友好的API:https://github.com/kennethreitz/requests,urllib2可以用这个替换了。

代码可以贴 https://gist.github.com/ ,放这缩进没了没法看。

TOP

posted by wap, platform: Android

马克

TOP

if small==x:


我想问python支持浮点数相等比较?精度怎么控制

TOP

引用:
原帖由 alexacc 于 2014-1-16 14:37 发表
if small==x:


我想问python支持浮点数相等比较?精度怎么控制
分析了下返回的json,都是保留2位小数,精度不需要太刻意控制,10位内 不管是float还是decimal都无所谓

TOP

TOP

 16 12
发新话题
     
官方公众号及微博