skka3134

skka3134

email
telegram

加密货币量化cctx

1. 安装 anaconda 和 pycharm,anaconda 是一个 python 环境,pycharm 是 python 的编辑器
https://anaconda.org/anaconda/conda
https://www.jetbrains.com/pycharm/
2. 打开终端,查看都有哪些环境

conda env list

image
3. 删除环境 py1

conda env remove -n py1

4. 打开 pycharm,创建一个新的项目,python 版本选择 3.8
image
5. 安装包 ccxt,ccxt 封装了全世界绝大多数的交易所 API

pip install ccxt

6. 创建一个新的.py 文件,获取所有交易所

import ccxt
print(ccxt.exchanges)

7. 实例化一个 binance 交易所,

binance_exchange = ccxt.binance({
})
print('交易所ID',binance_exchange.id)
print('交易所名称',binance_exchange.name)
print('交易所是否支持公有API',binance_exchange.has['publicAPI'])
print('交易所是否支持私有API',binance_exchange.has['privateAPI'])
print('交易所支持的时间频率',binance_exchange.timeframes)
print('交易所最长等待时间s',binance_exchange.timeout/1000)
print('交易所访问频率s',binance_exchange.rateLimit/1000)
print('交易所当前时间',binance_exchange.iso8601(ccxt.binance.milliseconds()))

如果使用 okex 交易所,那就是 ccxt.okex (),
apiKey 和 secret 就是我门在交易所申请的 api 的 api key,
使用 okex 交易所交易所还需要一个 password,其他交易所不需要

binance_exchange = ccxt.binance({
    'apiKey': '',
    'secret': '',
    'timeout': 15000,
    'enableRateLimit': True,
})

8. 获取市场信息

binance_markets=binance_exchange.load_markets()
print(binance_exchange.id,binance_markets) 

9. 获取盘口信息

symbol='BTC/USDT'
orderbook=exchange.fetch_order_book(symbol)
print('orderbook',orderbook)
print('bids',orderbook['bids'])
print('asks',orderbook['asks'])

10. 获取 ticket

if (exchange.has['fetchTicker']):
    print(exchange.fetch_ticker(symbol))

11. 获取 k 线

kline_data=exchange.fetch_ohlcv(symbol,'1d')
print('kline_data',kline_data)

12. 获取公共交易

public_trade=exchange.fetch_trades(symbol)
print('public_trade',public_trade)

13. 获取余额

balance=exchange.fetch_balance()
print(balance)
print(balance['USDT'])
print(balance['USDT']['free'])

14. 获取交易

all_orders=exchange.fetch_orders(symbol=symbol)
print(all_orders)
open_orders=exchange.fetch_open_orders(symbol=symbol)
print(open_orders)

15. 获取指定订单交易

order_info=exchange.fetch_order('545646','BTC/USDT')
print(order_info) 

16. 下单,第二个参数是数量,第三个参数是价格

exchange.create_limit_buy_order('BTC/USDT',1,2)
exchange.cancel_order('54646554')
加载中...
此文章数据所有权由区块链加密技术和智能合约保障仅归创作者所有。