-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
1,301 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,181 @@ | ||
import REST.REST_API as CONST | ||
from REST.REST_API import REST_API | ||
import configparser | ||
from Logging import Logger | ||
import math | ||
import schedule | ||
import time | ||
|
||
|
||
conf = configparser.ConfigParser() | ||
conf.read("conf.ini") | ||
api = REST_API() | ||
# 写日志 | ||
log = Logger('all.log',level='debug') | ||
key = conf.get("section", "key") | ||
secret = conf.get("section", "secret") | ||
#参数 | ||
mount = int(conf.get("section", 'mount')) | ||
symbol = conf.get("section", 'symbol') | ||
order_sleep = int(conf.get("section", "order_sleep")) | ||
trading_strategy = int(conf.get("section", "trading_strategy")) #1为差价2为平价 | ||
|
||
api = REST_API() | ||
api.auth(key, secret)#授权 | ||
|
||
# 程序停止时间设置 | ||
def sleep(t): | ||
time.sleep(t) | ||
#撤销订单 | ||
def ordercancle(index,order_id): | ||
log.logger.info(str(index) + "号:订单号" +str(order_id)) | ||
cmd = api.cancel_order_cmd(index,order_id) | ||
success, r = api.multi_sign_cmd(cmd) | ||
if success: | ||
if index == 2: | ||
log.logger.info("撤销卖单") | ||
elif index == 1: | ||
log.logger.info("撤销买单") | ||
log.logger.info("撤销缓冲...") | ||
sleep(5) | ||
else: | ||
log.logger.info(r) | ||
#获取订单状态 | ||
def orderstatus(order_id): | ||
success, r = api.get_order(order_id) | ||
#log.logger.info(r) | ||
if success: | ||
status = int(r['result'][0]['result']['status']) | ||
else: | ||
log.logger.info(r) | ||
return 0 | ||
#log.logger.info(status) | ||
if status == 3: | ||
return 3 | ||
elif status == 2: | ||
return 2 | ||
else: | ||
return 0 | ||
#循环判断订单完成 | ||
def orderjust(order_id): | ||
flag=0 | ||
while True: | ||
r = orderstatus(order_id) | ||
if r == 3: | ||
return True | ||
elif r == 2: | ||
log.logger.info("订单部分已成交...") | ||
else: | ||
log.logger.info("订单正在等待撮合...") | ||
flag+=1 | ||
sleep(1) | ||
if flag == order_sleep: | ||
return False | ||
# 精度控制,直接抹除多余位数,非四舍五入 | ||
def digits(num, digit): | ||
site = pow(10, digit) | ||
tmp = num * site | ||
tmp = math.floor(tmp) / site | ||
return tmp | ||
def pricedecimal(num): | ||
num = digits(num, 4) | ||
#h获得买一卖一进行价差运算,得到price | ||
def getprice(): | ||
success, ticker = api.get_ticker(symbol) | ||
if success: | ||
buy = float(ticker['result']['buy']) | ||
sell = float(ticker['result']['sell']) | ||
return buy,sell | ||
else: | ||
print("获得买卖价失败!") | ||
log.logger.info(ticker) | ||
return 1,0 | ||
#获取USDT余额 | ||
def getusdt(): | ||
success, usdt = api.get_balance(1) | ||
if success: | ||
r = usdt['result'][0]['result']['assets_list'][3]['balance'] | ||
return r | ||
else: | ||
log.logger.info(usdt) | ||
return 0 | ||
def handler(): | ||
buyprice, sellprice = getprice()#获取买一卖一价 | ||
if trading_strategy == 2:#平价(有千一价差) | ||
r = (buyprice+sellprice)/2.0 | ||
r = digits(r,4) | ||
#print("买一",buyprice,"卖一",sellprice,"平价",r) | ||
log.logger.info("买一"+str(buyprice)+"卖一"+str(sellprice)+"平价"+str(r)) | ||
buyprice=r | ||
sellprice=r+0.0001 | ||
elif trading_strategy ==1:#买一卖一 | ||
#print("买一", buyprice, "卖一", sellprice) | ||
log.logger.info("买一"+str(buyprice)+"卖一"+str(sellprice)) | ||
margin = buyprice - sellprice | ||
if margin <= 0: | ||
buymount = digits(mount/buyprice,4) | ||
sellmount = digits(mount/sellprice,4) | ||
cm1 = api.create_order_cmd(1, symbol, CONST.SIDE_BUY, CONST.TYPE_LIMIT_PRICE, buyprice, buymount, 0) | ||
cm2 = api.create_order_cmd(2, symbol, CONST.SIDE_SELL, CONST.TYPE_LIMIT_PRICE, sellprice, sellmount, 0) | ||
success, r = api.multi_sign_cmd([cm1, cm2]) | ||
log.logger.info("调试信息:") | ||
log.logger.info(r) | ||
if success: | ||
try: | ||
ordersellid = r['result'][0]['result'] | ||
ordersellindex = r['result'][0]['index'] | ||
orderbuyid = r['result'][1]['result'] | ||
orderbuyindex = r['result'][1]['index'] | ||
#调试 | ||
#print("sell_price", sellprice, "sell_mount", sellmount,"sell_orderid",ordersellid) | ||
#log.logger.info("sell_price"+str(sellprice)+"sell_mount"+str(sellmount)+"sell_orderid"+str(ordersellid)) | ||
#print("buy_price", buyprice, "buy_mount", buymount,"buy_orderid",orderbuyid) | ||
#log.logger.info("buy_price"+str(buyprice) + "buy_mount"+str(buymount)+"buy_orderid"+str(orderbuyid)) | ||
|
||
except KeyError: | ||
pass | ||
try: | ||
#判断订单是否完成 | ||
if orderjust(orderbuyid) == False: | ||
print(orderbuyid) | ||
ordercancle(orderbuyindex,orderbuyid) | ||
else: | ||
log.logger.info("买单已成交") | ||
if orderjust(ordersellid) == False: | ||
print(ordersellid) | ||
ordercancle(ordersellindex,ordersellid) | ||
else: | ||
log.logger.info("卖单已成交") | ||
log.logger.info("结尾日志:") | ||
log.logger.info(r) | ||
except UnboundLocalError: | ||
pass | ||
else: | ||
log.logger.info(r) | ||
schedule.every(1).seconds.do(handler) | ||
log.logger.info("使用交易对:") | ||
log.logger.info(symbol) | ||
log.logger.info("对冲USDT消耗数量:") | ||
log.logger.info(mount) | ||
while True: | ||
#log.logger.info("USDT余额:"+getusdt()) | ||
schedule.run_pending() | ||
time.sleep(2) | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# -*- mode: python -*- | ||
|
||
block_cipher = None | ||
|
||
|
||
a = Analysis(['CPbot.py'], | ||
pathex=['D:\\Pycharm\\coinpark-master'], | ||
binaries=[], | ||
datas=[], | ||
hiddenimports=[], | ||
hookspath=[], | ||
runtime_hooks=[], | ||
excludes=[], | ||
win_no_prefer_redirects=False, | ||
win_private_assemblies=False, | ||
cipher=block_cipher, | ||
noarchive=False) | ||
pyz = PYZ(a.pure, a.zipped_data, | ||
cipher=block_cipher) | ||
exe = EXE(pyz, | ||
a.scripts, | ||
a.binaries, | ||
a.zipfiles, | ||
a.datas, | ||
[], | ||
name='CPbot', | ||
debug=False, | ||
strip=False, | ||
upx=True, | ||
runtime_tmpdir=None, | ||
console=True ) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import logging | ||
from logging import handlers | ||
|
||
class Logger(object): | ||
level_relations = { | ||
'debug':logging.DEBUG, | ||
'info':logging.INFO, | ||
'warning':logging.WARNING, | ||
'error':logging.ERROR, | ||
'crit':logging.CRITICAL | ||
}#日志级别关系映射 | ||
|
||
def __init__(self, filename, level='info', when='D', backCount=3, fmt='%(levelname)s: %(message)s'): | ||
self.logger = logging.getLogger(filename) | ||
format_str = logging.Formatter(fmt)#设置日志格式 | ||
self.logger.setLevel(self.level_relations.get(level))#设置日志级别 | ||
sh = logging.StreamHandler()#往屏幕上输出 | ||
sh.setFormatter(format_str) #设置屏幕上显示的格式 | ||
th = handlers.TimedRotatingFileHandler(filename=filename, when=when, backupCount=backCount, encoding='utf-8')#往文件里写入#指定间隔时间自动生成文件的处理器 | ||
#实例化TimedRotatingFileHandler | ||
#interval是时间间隔,backupCount是备份文件的个数,如果超过这个个数,就会自动删除,when是间隔的时间单位,单位有以下几种: | ||
# S 秒 | ||
# M 分 | ||
# H 小时、 | ||
# D 天、 | ||
# W 每星期(interval==0时代表星期一) | ||
# midnight 每天凌晨 | ||
th.setFormatter(format_str)#设置文件里写入的格式 | ||
self.logger.addHandler(sh) #把对象加到logger里 | ||
self.logger.addHandler(th) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,37 @@ | ||
# coinpark | ||
coinpark挖矿代码,包含可执行exe文件 | ||
|
||
Key:coinpark交易平台上你自己申请的API的key值 | ||
secret:API的secret值 | ||
mount USDT单次交易数量 | ||
symbol 交易对,仅提供与usdt的交易对,例如:symbol=ETH_USDT | ||
order_sleep为订单交易等待时间 | ||
trading_Strategy 交易策略 | ||
|
||
交易策略为两种 | ||
等于2代表平价策略(买一卖一取中间值,有千一价差,特点是最容易成交快速买卖) | ||
等于1代表差价策略买一卖一(低买高卖) | ||
建议使用平价 | ||
|
||
代码有问题可以加微信说,互相交朋友:jianbingguozi1995 | ||
|
||
代码提供源码,可以自行检查是否有后门,如果老哥们用的好的话给点饭钱就行,在这里给各位老铁抱拳了 | ||
|
||
USDT地址(非ERC20):1PSybg27pQ7y9FSo1uKjNmWRrCzgew7tHe | ||
|
||
后续会慢慢做量化交易,希望能从小白的道路上走向财务自由 | ||
|
||
作者:煎饼果子+小矿工 | ||
|
||
开源倡导者 | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
Oops, something went wrong.