[Python quantitative trading] - 1, Package Exchange API
In the past week, bloggers have been crunching Python quantitative trading content. In the time of this writing has been achieved using python function grid method of automated trading, followed by successfully deploy scripts to the server to run automatically, and with another flask modules complete WebAPI package, to achieve a transaction status of real-time monitoring . The next few articles where I will introduce one by one how I continue to climb out of the pit, and the pit arduous journey, little friends do not miss interest -
personal blog address: ht / tps: //www.asyu17.cn/
Portal
[Python quantitative trading] - 1, Package Exchange API
[Python] --2 quantitative trading, the use of python grid method to achieve back-tested trading strategies and
[Python] --3 quantitative trading, storage module and package WebAPI
[Python] --4 quantitative trading, the main function
[Python] --5 quantitative trading, deployed to the linux server
table of Contents
- Accesskey acquisition and secretkey
- Packaging Market Module
- Trade Module Package
- Epilogue
- Micro-channel public number
Accesskey acquisition and secretkey
I was registered in net currency trading account, so I will take the currency network as an example. To avoid suspicion ad I hold the portal. . .
After you log in, there is an option in the picture below API set, click to enter.
Enter the necessary information, add a api, and then record the acquired accesskey and secretkey (two numbers to remember that behind all transaction module requires two parameters)
Packaging Market Module
Click on the market , these interfaces we all need to be encapsulated. First establish a Market class to be able to guarantee the result acquisition request, the package we get module , is completed, the remaining number of requests we package (all request parameters on which data) call sample in the following :
Market.py
import requests
import time
# 市场部分
class market(object):
# 官方请求函数
def get(self, url):
# 返回字典类型
while True:
try:
r = requests.get(url)
except Exception:
time.sleep(0.5)
continue
if r.status_code != 200:
time.sleep(0.5)
continue
r_info = r.json()
r.close()
return r_info
# 获取所有币的当前数据
def GetAllCoinCurrentData(self):
url = 'http://api.zb.live/data/v1/allTicker'
data = self.get(url=url)
return data
# 获取一个币的当前数据
def GetOneCoinCurrentData(self, market):
url = 'http://api.zb.live/data/v1/ticker?market=' + market
data = self.get(url=url)
return data
# 获取市场深度
def GetMarketDepth(self, market, size):
# 市场深度是指在不影响当前价格的前提下可以完成的最大交易量
# 应该是获取市场上有多少买单和多少卖单
url = 'http://api.zb.live/data/v1/depth?market=' + market + '&size=' + size
data = self.get(url=url)
return data
# 获取指定市场最新五十条成交数据
def GetHistoryTradeData(self, market):
url = 'http://api.zb.live/data/v1/trades?market=' + market
data = self.get(url=url)
return data
# 获取K线数据
def GetKlineData(self, market, type='1day', size='100'):
# 最多获取最新的1000条
# type String 1min/3min/5min/15min/30min/1day/3day/1week/1hour/2hour/4hour/6hour/12hour
# since 为获取这个时间戳之后的数据
url = 'http://api.zb.live/data/v1/kline?market=' + market + '&type=' + type + '&size=' + size
data = self.get(url=url)
return data
Trade Module Package
Into the API documentation , click on the trading circle of five is necessary to package, we have to package these, for example, others are exactly the same. In the trading module, parameters more complex, we are in the package when all the data into the dictionary , then to resolve with urllib.parse function and placed in the url.
Special note:
In Encryption When encryption function, data dictionary to not add sign and reqTime parameters, encryption is completed after obtaining the sign, and then further data which add these two parameters! ! ! Call:
data = {
'accesskey': 你的accesskey,
'method': 'getAccountInfo',
}
sign = zbTrade.Encryption(data=data, secretKey=secretKey)
data = {
'accesskey': accessKey,
'method': 'getAccountInfo',
'sign': sign,
'reqTime': int(time.time() * 1000)
}
res = zbTrade.GetAccountInfo(data=data)
print(res)
Trade.py
import requests
import time
import hmac
import hashlib
from urllib import parse
# 交易
class trade(object):
# 官方请求函数
def get(self, url):
# 返回字典类型
while True:
try:
r = requests.get(url)
except Exception:
time.sleep(0.5)
continue
if r.status_code != 200:
time.sleep(0.5)
continue
r_info = r.json()
r.close()
return r_info
# 委托下单
def Order(self, data):
# accesskey String accesskey
# market String 交易币种计价币种
# tradeParams String 交易参数,数组格式[[价格,数量],[价格,数量]],最多20个
# tradeType int 交易类型1/0[buy/sell]
# sign String 请求加密签名串
# reqTime long 当前时间毫秒数
# acctType int 杠杆 1/0[杠杆/现货](可选参数,默认为: 0 现货)
url = 'https://trade.zb.live/api/order?' + parse.urlencode(data)
data = self.get(url=url)
return data
# 取消委托下单
def CancelOrder(self, data):
# accesskey String accesskey
# currency String 交易币种计价币种
# id long 委托挂单号
# sign String 请求加密签名串
# reqTime long 当前时间毫秒数
# customerOrderId String 自定义ID(4-36位字符,数字或字母组成,customerOrderId和id只能选一个填写)
url = 'https://trade.zb.live/api/cancelOrder?' + parse.urlencode(data)
data = self.get(url=url)
return data
# 获取当前市场上的委托单
def GetOrder(self, data):
# accesskey String accesskey
# tradeType int 交易类型1/0[buy/sell]
# currency String 交易币种计价币种
# pageIndex int 当前页数
# sign String 请求加密签名串
# reqTime long 当前时间毫秒数
url = 'https://trade.zb.live/api/getOrder?' + parse.urlencode(data)
data = self.get(url=url)
return data
# 获取未成交或部份成交挂单
def GetUnfinishedOrders(self,data):
# method String 直接赋值getUnfinishedOrdersIgnoreTradeType
# accesskey String accesskey
# currency String 交易币种计价币种
# pageIndex int 当前页数
# pageSize int 每页数量
# sign String 请求加密签名串
# reqTime long 当前时间毫秒数
url = 'https://trade.zb.live/api/getUnfinishedOrdersIgnoreTradeType?' + parse.urlencode(data)
data = self.get(url=url)
return data
# 获取充值地址
def GetRechargeAddress(self, data):
# accesskey String accesskey
# currency String 币种
# sign String 请求加密签名串
# reqTime long 当前时间毫秒数
url = 'https://trade.zb.live/api/getUserAddress?' + parse.urlencode(data)
data = self.get(url=url)
return data
# 获取提现地址
def GetWithdrawalAddress(self, data):
# method String 直接赋值getWithdrawAddress
# accesskey String accesskey
# currency String 币种
# sign String 请求加密签名串
# reqTime long 当前时间毫秒数
url = 'https://trade.zb.live/api/getWithdrawAddress?' + parse.urlencode(data)
data = self.get(url=url)
return data
# 提现
def Withdrawal(self, data):
# accesskey String accesskey
# amount float 提现金额
# currency String 币种
# fees float 提现矿工费
# itransfer int 是否同意bitbank系内部转账(0不同意,1同意,默认不同意)
# method String 直接赋值withdraw
# receiveAddr String 接收地址(必须是认证了的地址,bts的话,以"账户_备注"这样的格式)
# safePwd String 资金安全密码
# sign String 请求加密签名串
# reqTime long 当前时间毫秒数
url = 'https://trade.zb.live/api/getChargeRecord?' + parse.urlencode(data)
data = self.get(url=url)
return data
def GetAccountInfo(self, data):
# method String 直接赋值getAccountInfo
# accesskey String accesskey
# sign String 请求加密签名串
# reqTime long 当前时间毫秒数
url = 'https://trade.zb.live/api/getAccountInfo?' + parse.urlencode(data)
data = self.get(url=url)
return data
# 完成sign加密
def Encryption(self, data,
secretKey):
# data最后需要转换的格式如:accesskey=359f9eaa-53ec-491d-b04b-dc8462698123&method=getAccountInfo
accessKey = parse.urlencode(data)
secretKey = hashlib.sha1(secretKey.encode('utf-8')).hexdigest()
sign = hmac.new(key=bytes(secretKey.encode('utf-8')), msg=accessKey.encode('utf-8'),
digestmod=hashlib.md5).hexdigest()
return sign
Epilogue
This completes the simplest packaging market API. In the next article we will achieve grid method trading strategies using python, and the use of encapsulated API in this article were back-tested trading strategies.