火币otc法币报价python监控脚本

96
卢衍泓
0.1 2018.03.04 22:43 字数 307

看了这篇文章《如何利用USDT无风险套利日赚1000?

文章提到利用USDT场外交易进行套利,原理不多说了,自己去看文章。

我作了个火币上法币交易的报价监控脚本。跑的时间不多,偶尔能发现价差,这里交流下思路和源码,能不能赚到钱就再说吧,还要再优化。

火币的法币交易网站
https://otc.huobi.pro/#/trade/list?coin=1&type=1
打开chrome的develop tool 工具可以找到具体的api

从preview中可以看到报价信息就藏在这里


api长这个样子
https://api-otc.huobi.pro/v1/otc/trade/list/public?coinId=3&tradeType=1&currentPage=1&payWay=&country=&merchant=1&online=1&range=0

可以将这个api复制到浏览器打开 ,第一个price字段,就是我们要提取的数据


image.png

直接先看log信息:
这里btc,eth,usdt买卖价都监控了。每次查询执行约5-10秒。跑了约半小时共发现5次价差。当然这里并没保留具体价差数据,还要完善。

22:36:53 132  #查询多少次
          btc      eth  usdt
buy   72099.0  5522.88  6.45
sell  71911.0  5505.00  6.44
差价:  #买价-卖价
btc     188.00
eth      17.88
usdt      0.01
dtype: float64
第 5 次发现价差

源码

import json
from urllib.request import Request, urlopen
import time
import pandas as pd

# coinID
btc = '1'
eth = '3'
usdt = '2'
# tradeType
buy = '1'
sell = '0'


def getPrice(coinID, tradeType):
    huobiapi = "https://api-otc.huobi.pro/v1/otc/trade/list/public"
    api_url = huobiapi + "?coinId=" + coinID + "&tradeType=" + tradeType + "&currentPage=1&payWay=&country=&merchant=1&online=1&range=0"
    firefox_headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:23.0) Gecko/20100101 Firefox/23.0'}
    request = Request( api_url, headers=firefox_headers )
    html = urlopen( request )
    data = html.read().decode( 'utf-8' )
    dataJson = json.loads( data )
    price = dataJson['data'][0]['price']
    # print(price)
    return price


def getAllPrice(showprice=0):
    btc_buy = getPrice( coinID=btc, tradeType=buy )
    btc_sell = getPrice( coinID=btc, tradeType=sell )

    eth_buy = getPrice( coinID=eth, tradeType=buy )
    eth_sell = getPrice( coinID=eth, tradeType=sell )

    usdt_buy = getPrice( coinID=usdt, tradeType=buy )
    usdt_sell = getPrice( coinID=usdt, tradeType=sell )

    prices = {
        "btc": {
            "buy": btc_buy,
            "sell": btc_sell
        },
        "eth": {
            "buy": eth_buy,
            "sell": eth_sell
        },
        "usdt": {
            "buy": usdt_buy,
            "sell": usdt_sell
        }
    }
    prices_df = pd.DataFrame( prices )
    if showprice:
        print( prices_df )
    return prices_df

def getChajia(prices,showChajia = 0):
    cj = prices.iloc[0]-prices.iloc[1]
    if showChajia:
        print('差价:')
        print(cj)
    return cj


num = 0
r = 0

while(1):
    t = time.strftime( '%H:%M:%S', time.localtime( time.time() ) )
    print( t,r )

    try:
        prices = getAllPrice( showprice=1 )
        cj = getChajia( prices=prices, showChajia=1 )

        if (cj['btc'] < 0 or cj['eth'] < 0 or cj['usdt'] < 0):
            num = num + 1
        print( u"第",num,u"次发现价差")

    except:
        pass
    r = r+1

    time.sleep( 3 )

开发笔记
Web note ad 1