-
Notifications
You must be signed in to change notification settings - Fork 130
/
Copy pathrun.py
84 lines (73 loc) · 3.8 KB
/
run.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# -*- coding: utf-8 -*-
from app.BinanceAPI import BinanceAPI
from app.authorization import api_key,api_secret
from data.runBetData import RunBetData
from app.dingding import Message
from data.calcIndex import CalcIndex
import time
binan = BinanceAPI(api_key,api_secret)
runbet = RunBetData()
msg = Message()
index = CalcIndex()
class Run_Main():
def __init__(self):
self.coinList = runbet.get_coinList()
pass
def pre_data(self,cointype):
'''获取交易对的data.json基础信息
cointype:交易对
'''
grid_buy_price = runbet.get_buy_price(cointype) # 当前网格买入价格
grid_sell_price = runbet.get_sell_price(cointype) # 当前网格卖出价格
quantity = runbet.get_quantity(cointype) # 买入量
step = runbet.get_step(cointype) # 当前步数
cur_market_price = binan.get_ticker_price(cointype) # 当前交易对市价
right_size = len(str(cur_market_price).split(".")[1])
return [grid_buy_price,grid_sell_price,quantity,step,cur_market_price,right_size]
def loop_run(self):
while True:
for coinType in self.coinList:
[grid_buy_price,grid_sell_price,quantity,step,cur_market_price,right_size] = self.pre_data(coinType)
if grid_buy_price >= cur_market_price:#and index.calcAngle(coinType,"5m",False,right_size): # 是否满足买入价
res = msg.buy_market_msg(coinType, quantity)
if 'orderId' in res: # 挂单成功
success_price = float(res['fills'][0]['price'])
runbet.set_ratio(coinType)
time.sleep(1)
runbet.set_record_price(coinType,success_price)
time.sleep(1)
runbet.modify_price(coinType,cur_market_price, step+1,cur_market_price) #修改data.json中价格、当前步数
time.sleep(60*2) # 挂单后,停止运行1分钟
else:
break
elif grid_sell_price < cur_market_price :#and index.calcAngle(coinType,"5m",True,right_size): # 是否满足卖出价
if step==0: # setp=0 防止踏空,跟随价格上涨
runbet.modify_price(coinType,grid_sell_price,step,cur_market_price)
else:
last_price = runbet.get_record_price(coinType)
sell_amount = runbet.get_quantity(coinType,False)
porfit_usdt = (cur_market_price - last_price) * sell_amount
res = msg.sell_market_msg(coinType, runbet.get_quantity(coinType,False),porfit_usdt)
if 'orderId' in res: #True 代表下单成功
runbet.set_ratio(coinType) #启动动态改变比率
time.sleep(1)
runbet.modify_price(coinType,runbet.get_record_price(coinType), step - 1,cur_market_price)
time.sleep(1)
runbet.remove_record_price(coinType)
time.sleep(60*1) # 挂单后,停止运行1分钟
else:
break
else:
print("币种:{coin}当前市价:{market_price}。未能满足交易,继续运行".format(market_price = cur_market_price,coin=coinType))
time.sleep(1)
#if __name__ == "__main__":
# instance = Run_Main()
# try:
# instance.loop_run()
# except Exception as e:
# error_info = "报警:做多网格,服务停止"
# msg.dingding_warn(error_info)
# 调试看报错运行下面,正式运行用上面
if __name__ == "__main__":
instance = Run_Main()
instance.loop_run()