@
liushuaikobe @
binux @
Crossin 源码贴出,跪谢各位大神。
#coding=utf-8
import time, re, requests, md5, urllib, urllib2, json ,sys ,os
#sys.path.append(os.path.dirname(__file__).replace('\\','/'))
from huobi import HuoBi
from bitfinex import Bitfinex
from configs import config,keys
class getprice(object):
def __init__(self):
#获取动态配置
self.huobi = HuoBi(keys['huobi']['key'],keys['huobi']['secretKey'])
self.bitf = Bitfinex(keys['bitf']['key'],keys['bitf']['secretKey'])
def buy_seller(self):
p1 = self.get_huobi()
pr1 = p1['sell']
p2 = self.get_bitf()
pr2 = p2['sell'] * config['rate']
if pr1 > pr2:
pre = (pr1 - pr2) / pr1 * 100
if pre >= config['agio']:
self.huobi_sell(p1['buy'])
self.bitf_sell('buy',p2['sell'])
else:
pre = (pr2 - pr1) / pr2 * 100
if pre >= config['agio']:
self.huobi_buy(p1['sell'])
self.bitf_sell('sell',p2['buy'])
print("huobi : %s %s finex : %s ") % (pr1,pre,pr2)
def get_huobi_seller(self):
try:
r = requests.get('
http://market.huobi.com/staticmarket/depth_btc_json.js')
return r
except:
time.sleep(18)
self.get_huobi_seller()
def get_bitf_seller(self):
try:
r = requests.get('
https://api.bitfinex.com/v1/book/btcusd')
return r
except:
time.sleep(18)
self.get_bitf_seller()
def get_huobi(self):
r1 = self.get_huobi_seller()
t = r1.json()
p1 = t['asks'][-5]
p2 = t['bids'][4]
pr1 = float(p1[0])
pr2 = float(p2[0])
return {'buy':pr2,'sell':pr1}
def get_bitf(self):
r2 = self.get_bitf_seller()
t = r2.json()
p1 = t['asks'][4]
p2 = t['bids'][4]
pr1 = float(p1['price'])
pr2 = float(p2['price'])
return {'buy':pr2,'sell':pr1}
def huobi_sell(self,price=''):
#getattr(huobi,method)
price = self.get_huobi()
price = price['buy']
info = self.huobi.sell(price,config['amount']) #{u'result': u'success', u'id': 18161354}
print(info)
def huobi_buy(self,price):
info = self.huobi.buy(price,config['amount'])
print(info)
def bitf_sell(self,side='sell',price=''):
"""
symbol (string): The name of the symbol (see `/symbols`).
amount (decimal): Order size: how much to buy or sell.
price (price): Price to buy or sell at. May omit if a market order.
exchange (string): "bitfinex".
side (string): Either "buy" or "sell".
type (string): Either "market" / "limit" / "stop" / "trailing-stop" / "fill-or-kill" / "exchange market" / "exchange limit" / "exchange stop" / "exchange trailing-stop" / "exchange fill-or-kill". (type starting by "exchange " are exchange orders, others are margin trading orders)
is_hidden (bool) true if the order should be hidden. Default is false.
"""
price = self.get_bitf()
price = price['buy']
params = {
"symbol":"btcusd",
"amount" : str(config['amount']),
"price" : str(price),
"exchange" : "bitfinex",
"side" : side,
"type" : "exchange market",
}
info = self.bitf.order_new(params)
print(info)
x = getprice()
while config['start']:
x.buy_seller()
time.sleep(18)