通过区块链交易框架ccxt连接okex的api方法介绍与ccxt常用函数用法

本文主要介绍ccxt这一框架的基本使用,以连接okex为例进行说明。
首先需要在okex上用自己的账号申请一个api,如下图所示:
通过区块链交易框架ccxt连接okex的api方法介绍与ccxt常用函数用法_第1张图片
权限那一栏勾选上‘交易’,但是为了资金的安全,建议不要勾‘提币’,这样即使不慎泄露apiKey也不会被不法之人盗取与转移属于我们账号里的币。

用‘pip install ccxt’这一命令就能直接安装最新版的ccxt。
 

连接okex账户

import ccxt

apikey = "这里根据申请api时的内容填写"
secretkey = "这里根据申请api时的内容填写"
password="这里根据申请api时的内容填写"

okex=ccxt.okex({
    'apiKey':apikey,
    'secret':secretkey,
    'password':password
})

#由于现在直接访问okex会被墙,需要通过代理的方式访问,若是国外服务器则不需要下面的这几行代码
okex.proxies={
    'http': 'socks5://127.0.0.1:10808',
    'https': 'socks5://127.0.0.1:10808',
}

 

查询账户余额:

balance=okex.fetch_balance()
print(balance)

注意这里是币币账户的余额,需先把其它账户转到币币账户才能查看到。
返回结果是个字典,如下面的格式:

{'ETH': {'free': 0.059924, 'total': 3.881099, 'used': 3.821175},
 'USDT': {'free': 562.49938611, 'total': 648.80668611, 'used': 86.3073},
 'free': {'ETH': 0.059924, 'USDT': 562.49938611},
 'info': [{'available': '0.059924',
           'balance': '3.881099',
           'currency': 'ETH',
           'frozen': '3.821175',
           'hold': '3.821175',
           'holds': '3.821175',
           'id': ''},
          {'available': '562.49938611',
           'balance': '648.80668611',
           'currency': 'USDT',
           'frozen': '86.3073',
           'hold': '86.3073',
           'holds': '86.3073',
           'id': ''}],
 'total': {'ETH': 3.881099, 'USDT': 648.80668611},
 'used': {'ETH': 3.821175, 'USDT': 86.3073}}

之后写策略时可以根据当前可用资金balance[‘free’]的金额大小决定买入数量。
 

查看数字货币当前价格:

order_symbol='ETH/USDT'
ETH_info=okex.fetch_ticker(order_symbol)
print(ETH_info)

这里以ETH为例,返回的结果如下:

{'ask': 338.89,
 'askVolume': 10.800432,
 'average': None,
 'baseVolume': 377594.879742,
 'bid': 338.88,
 'bidVolume': 18.341471,
 'change': None,
 'close': 339.0,
 'datetime': '2020-09-21T16:02:22.750Z',
 'high': 376.9,
 'info': {'ask': '338.89',
          'base_volume_24h': '377594.879742',
          'best_ask': '338.89',
          'best_ask_size': '10.800432',
          'best_bid': '338.88',
          'best_bid_size': '18.341471',
          'bid': '338.88',
          'high_24h': '376.9',
          'instrument_id': 'ETH-USDT',
          'last': '339',
          'last_qty': '1.00336',
          'low_24h': '331.07',
          'open_24h': '369.65',
          'product_id': 'ETH-USDT',
          'quote_volume_24h': '134000006.1',
          'timestamp': '2020-09-21T16:02:22.750Z'},
 'last': 339.0,
 'low': 331.07,
 'open': 369.65,
 'percentage': None,
 'previousClose': None,
 'quoteVolume': 134000006.1,
 'symbol': 'ETH/USDT',
 'timestamp': 1600704142750,
 'vwap': None}

最新的价格就是ETH_info[‘last’],现在是339.0。
 

下单:

这里为了演示,下一个300的限价买单。(order_side的值控制是买单还是卖单,'buy’为买,'sell’为卖)

order_symbol='ETH/USDT'
order_type='limit'
order_side='buy'
order_amount=0.01
order_price=300
take_order=okex.create_order(order_symbol,order_type,order_side,order_amount,order_price)
print(take_order)

结果如下:

{'amount': None,
 'average': None,
 'clientOrderId': None,
 'cost': None,
 'datetime': None,
 'fee': None,
 'filled': None,
 'id': '5634087522287616',
 'info': {'client_oid': '',
          'code': '0',
          'error_code': '0',
          'error_message': '',
          'message': '',
          'order_id': '5634087522287616',
          'result': True},
 'lastTradeTimestamp': None,
 'price': None,
 'remaining': None,
 'side': 'buy',
 'status': None,
 'symbol': 'ETH/USDT',
 'timestamp': None,
 'trades': None,
 'type': 'limit'}

然后可以通过take_order[‘id’]取得订单id,这里即’5634087522287616’。
 

查看订单状态:

由于下的是限价单,一般不会立马成交,所以要根据订单id查询该笔订单状态,若成交的话则采取下一步策略。

takeorder_id='5634087522287616'
order_symbol='ETH/USDT'
order_info=okex.fetch_order(takeorder_id,order_symbol)
print(order_info)

结果如下:

{'amount': 0.01,
 'average': 0.0,
 'clientOrderId': None,
 'cost': 0.0,
 'datetime': '2020-09-21T16:22:33.063Z',
 'fee': None,
 'filled': 0.0,
 'id': '5634087522287616',
 'info': {'client_oid': '',
          'created_at': '2020-09-21T16:22:33.063Z',
          'fee': '',
          'fee_currency': '',
          'filled_notional': '0',
          'filled_size': '0',
          'funds': '',
          'instrument_id': 'ETH-USDT',
          'notional': '',
          'order_id': '5634087522287616',
          'order_type': '0',
          'price': '300',
          'price_avg': '0',
          'product_id': 'ETH-USDT',
          'rebate': '',
          'rebate_currency': '',
          'side': 'buy',
          'size': '0.01',
          'state': '0',
          'status': 'open',
          'timestamp': '2020-09-21T16:22:33.063Z',
          'type': 'limit'},
 'lastTradeTimestamp': None,
 'price': 300.0,
 'remaining': 0.01,
 'side': 'buy',
 'status': 'open',
 'symbol': 'ETH/USDT',
 'timestamp': 1600705353063,
 'trades': None,
 'type': 'limit'}

可以看出现在的order_info[‘status’]的值是‘open’的状态,说明还未成交,若成交的话该值会变为‘close’。
 

撤销订单:

注意撤销订单的前提条件是该笔订单还未成交。

takeorder_id='5634087522287616'
order_symbol='ETH/USDT'
cancel_res=okex.cancel_order(takeorder_id, order_symbol)
print(cancel_res)

结果如下:

{'amount': None,
 'average': None,
 'clientOrderId': None,
 'cost': None,
 'datetime': None,
 'fee': None,
 'filled': None,
 'id': '5634087522287616',
 'info': {'client_oid': '',
          'code': '0',
          'error_code': '0',
          'error_message': '',
          'message': '',
          'order_id': '5634087522287616',
          'result': True},
 'lastTradeTimestamp': None,
 'price': None,
 'remaining': None,
 'side': None,
 'status': None,
 'symbol': 'ETH/USDT',
 'timestamp': None,
 'trades': None,
 'type': None}

若返回结果cancel_res[‘info’][‘result’]为True则撤单成功。
 

总结:

以上就是ccxt这个交易框架基本的使用方法,可以看出ccxt对api的封装已经使得我们实际调用起来实现自己的策略时十分方便了。掌握了上述几个常用函数就几乎可以实现任何图灵完备的策略。

你可能感兴趣的:(量化交易,区块链,python,数字货币,以太坊,经验分享)