Loading ...

各交易所交易对波动率行情

Author: kilo, Date: 2021-02-07 16:54:09
Tags:


import numpy as np
import requests
def public_request(method, api_url, **payload):
        """request public url"""
        r_url = api_url
        try:
            r = requests.request(method, r_url, params=payload)
            r.raise_for_status()
            if r.status_code == 200:
                return r.json()
            else:
                return {'error': 'E10000', 'data': r.status_code}
        except requests.exceptions.HTTPError as err:
            return {'error': 'E10001', 'data': r.text}
        except Exception as err:
            return {'error': 'E10002', 'data': err}

table1 = {"type": "table", "title": "okex_swap", "cols": ["交易对", "最后成交价", "ATR", "ATR比", "交易量"], "rows": []}
table2 = {"type": "table", "title": "huobi_linear", "cols": ["交易对", "最后成交价", "ATR", "ATR比", "交易量"], "rows": []}

def main():
    
    table1["rows"].clear()
    okex_exchangeinfo=public_request('GET', 'https://www.okex.com/api/swap/v3/instruments')
    for e in okex_exchangeinfo:
        data=public_request('GET', 'https://www.okex.com/api/swap/v3/instruments/{instrument_id}/candles'.format(instrument_id=e['instrument_id']),granularity='60')  
        datac=[]
        closeprice=float(data[0][4])
        for i in range(0, 14):
            h,l,o,c=float(data[i][2]),float(data[i][3]),float(data[i][1]),float(data[i][4])
            a =  max(max(h-l,abs(h-o)),abs(c-l))
            datac.append(a)
        atr1m=np.mean(datac)
        table1["rows"].append([e['instrument_id'],str(closeprice), str(atr1m),str(round(atr1m*100/closeprice,2))+"%",data[0][6]])
        
    table2["rows"].clear()
    houbi_exchangeinfo=public_request('GET', 'https://api.hbdm.com/linear-swap-api/v1/swap_contract_info')
    for e in houbi_exchangeinfo['data']:
        data=public_request('GET', 'https://api.hbdm.com/index/market/history/linear_swap_mark_price_kline',contract_code=e['contract_code'],period='1min',size=50)
        datac=[]
        closeprice=float(data['data'][0]['close'])
        for i in range(0, 14):
            h,l,o,c=float(data['data'][i]['high']),float(data['data'][i]['low']),float(data['data'][i]['open']),float(data['data'][i]['close'])
            a =  max(max(h-l,abs(h-o)),abs(c-l))
            datac.append(a)
        atr1m=np.mean(datac)
        table2["rows"].append([e['contract_code'],str(closeprice), str(atr1m),str(round(atr1m*100/closeprice,2))+"%",data['data'][0]['trade_turnover']])
    LogStatus("`" + json.dumps([table1, table2]) + "`")


More

千年老松鼠 26行报错

kilo 我自己运行正常,服务器要境外的