Example #1
0
 def get_funding_rate(self):
     """获取最新资金费率"""
     try:
         data = requests.get(
             "https://api.hbdm.com/swap-api/v1/swap_funding_rate?contract_code=%s"
             % self.__instrument_id).json()
         instrument_id = data['data']['contract_code']
         funding_time = data['data']['funding_time']
         funding_rate = data['data']['funding_rate']
         result = {
             "instrument_id": instrument_id,
             "funding_time": funding_time,
             "funding_rate": funding_rate
         }
         return result
     except:
         data = requests.get(
             "https://api.hbdm.com/linear-swap-api/v1/swap_funding_rate?contract_code=%s"
             % self.__instrument_id).json()
         instrument_id = data['data']['contract_code']
         funding_time = data['data']['funding_time']
         funding_rate = data['data']['funding_rate']
         result = {
             "instrument_id": instrument_id,
             "funding_time": funding_time,
             "funding_rate": funding_rate
         }
         return result
Example #2
0
 def info(self):
     if "USDT" in instrument_id or "usdt" in instrument_id:
         result = requests.get(
             "https://api.hbdm.com/linear-swap-api/v1/swap_contract_info"
         ).json()
     else:
         result = requests.get(
             "https://api.hbdm.com/swap-api/v1/swap_contract_info").json()
     return result
Example #3
0
 def _get_timestamp(self):
     url = c.API_URL + c.SERVER_TIMESTAMP_URL
     response = requests.get(url)
     if response.status_code == 200:
         return response.json()['iso']
     else:
         return ""
Example #4
0
    def _request(self, method, request_path, params, cursor=False):
        if method == c.GET:
            request_path = request_path + utils.parse_params_to_str(params)
        # url
        url = c.API_URL + request_path

        # 获取本地时间
        timestamp = utils.get_timestamp()

        # sign & header
        if self.use_server_time:
            # 获取服务器时间
            timestamp = self._get_timestamp()

        body = json.dumps(params) if method == c.POST else ""
        sign = utils.sign(
            utils.pre_hash(timestamp, method, request_path, str(body)),
            self.API_SECRET_KEY)
        header = utils.get_header(self.API_KEY, sign, timestamp,
                                  self.PASSPHRASE)

        if self.test:
            header['x-simulated-trading'] = '1'
        if self.first:
            print("url:", url)
            self.first = False

        # print("url:", url)
        # print("headers:", header)
        # print("body:", body)

        # send request
        response = None
        if method == c.GET:
            response = requests.get(url, headers=header)
        elif method == c.POST:
            response = requests.post(url, data=body, headers=header)
        elif method == c.DELETE:
            response = requests.delete(url, headers=header)

        # exception handle
        if not str(response.status_code).startswith('2'):
            raise exceptions.OkexAPIException(response)
        try:
            res_header = response.headers
            if cursor:
                r = dict()
                try:
                    r['before'] = res_header['OK-BEFORE']
                    r['after'] = res_header['OK-AFTER']
                except:
                    pass
                return response.json(), r
            else:
                return response.json()

        except ValueError:
            raise exceptions.OkexRequestException('Invalid Response: %s' %
                                                  response.text)
Example #5
0
 def get_funding_rate(self):
     """获取最新资金费率"""
     data = requests.get("https://www.okex.com/api/swap/v3/instruments/%s/funding_time" % self.__instrument_id).json()
     instrument_id = data['instrument_id']
     funding_time = data['funding_time']
     funding_rate = data['funding_rate']
     result = {
         "instrument_id": instrument_id,
         "funding_time": funding_time,
         "funding_rate": funding_rate
     }
     return result
Example #6
0
 def get_funding_rate(self):
     """获取最新资金费率"""
     data = requests.get(
         "https://dapi.binance.com/dapi/v1/premiumIndex?symbol=%s" %
         self.__instrument_id).json()
     instrument_id = data['symbol']
     funding_time = data['time']
     funding_rate = data['lastFundingRate']
     result = {
         "instrument_id": instrument_id,
         "funding_time": funding_time,
         "funding_rate": funding_rate
     }
     return result
Example #7
0
def http_get_request(url, params, add_to_headers=None):
    headers = {
        "Content-type": "application/x-www-form-urlencoded",
        'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:53.0) Gecko/20100101 Firefox/53.0'
    }
    if add_to_headers:
        headers.update(add_to_headers)
    postdata = urllib.parse.urlencode(params)
    try:
        response = requests.get(url, postdata, headers=headers, timeout=TIMEOUT)
        if response.status_code == 200:
            return response.json()
        else:
            return {"status":"fail"}
    except Exception as e:
        print("httpGet failed, detail is:%s" %e)
        return {"status":"fail","msg": "%s"%e}
Example #8
0
def signedRequest(method, path, params):
    if "apiKey" not in options or "secret" not in options:
        raise ValueError("Api key and secret must be set")
    timestamp = requests.get("https://api.binance.com/api/v3/time").json()['serverTime']
    query = urlencode(sorted(params.items()))
    query += "&timestamp={}".format(timestamp)
    secret = bytes(options["secret"].encode("utf-8"))
    signature = hmac.new(secret, query.encode("utf-8"),
                         hashlib.sha256).hexdigest()
    query += "&signature={}".format(signature)
    resp = requests.request(method,
                            ENDPOINT + path + "?" + query,
                            headers={"X-MBX-APIKEY": options["apiKey"]})
    data = resp.json()
    # if "msg" in data:
    #     logging.error(data['msg'])
    return data
Example #9
0
    def http_get_request(self, url, params, add_to_headers=None):
        headers = {
            "Content-type":
            "application/x-www-form-urlencoded",
            'User-Agent':
            'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.71 Safari/537.36',
        }
        if add_to_headers:
            headers.update(add_to_headers)
        postdata = urllib.parse.urlencode(params)
        response = requests.get(url, postdata, headers=headers, timeout=5)
        try:

            if response.status_code == 200:
                return response.json()
            else:
                return
        except BaseException as e:
            print("httpGet failed, detail is:%s,%s" % (response.text, e))
            return
Example #10
0
 def info(self):
     result = requests.get(
         "https://www.okex.com/api/spot/v3/instruments").json()
     return result
Example #11
0
 def info(self):
     result = requests.get(
         "https://dapi.binance.com/dapi/v1/exchangeInfo").json()
     return result
Example #12
0
 def info(self):
     result = requests.get("https://api.huobi.pro/v1/common/symbols").json()
     return result
Example #13
0
 def info(self):
     result = requests.get(
         "https://api.hbdm.com/api/v1/contract_contract_info").json()
     return result