forked from workkkfor2012/BitmexEasy-Martingale
-
Notifications
You must be signed in to change notification settings - Fork 0
/
runMartingale.py
252 lines (224 loc) · 8.66 KB
/
runMartingale.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
import math
import datetime
from threading import Timer
from bitmex_websocket import Instrument
import asyncio
import websocket
import time
from bitmexClient import bitmexclient
def printlog(message):
timestr = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
with open('log.txt', 'a') as f:
s = timestr + ":" + message + "\n"
# s = str(s.encode("GBK"))
print(s)
f.write(s)
class BitmexWS:
def handleSupportAndPressurePrice(self, lastprice):
if lastprice < self.lowcontrolPriceline:
if self.bc.pos > 0 and self.cengshu >= 4:
# 关键点位上,持仓逆向,且层数过高,平仓避避风头
printlog(
"关键点位上,持仓逆向,且层数过高,平仓避避风头 self.bc.pos = " + str(self.bc.pos) + " self.cengshu = " + str(
self.cengshu))
self.orderClose()
return True
elif lastprice > self.highcontrolPriceline:
if self.bc.pos < 0 and self.cengshu >= 4:
printlog(
"关键点位上,持仓逆向,且层数过高,平仓避避风头 self.bc.pos = " + str(self.bc.pos) + " self.cengshu = " + str(
self.cengshu))
self.orderClose()
return True
def handlehaveLong(self, gap):
# 大于止盈点数,平仓
if gap > self.targetProfit:
printlog("持有多仓,超过盈利点数,平仓:" + str(gap))
self.orderClose()
# 处理多单亏损
else:
# 如果亏损超过初始设定,则加多仓
if gap < -self.init_jiacanggap:
printlog("持有多仓,亏损超过设定点数,加仓: " + str(gap))
self.order()
else:
pass
# print("持有多仓,不触发平仓和加仓 gap = "+str(gap))
def handlehaveShort(self, gap):
if gap < -self.targetProfit:
printlog("持有空仓,超过盈利点数,平仓:" + str(gap))
self.orderClose()
# 处理空单亏损
else:
# 价格上升到空仓开仓价格超过初始设定,则加空仓
if gap > self.init_jiacanggap:
printlog("持有空仓,亏损超过设定点数,加仓" + str(gap))
self.order()
else:
pass
# print("持有空仓,不触发平仓和加仓 gap = " + str(gap))
def onMessage(self, message):
a1 = message["data"]
b = 'lastPrice' in a1[0]
c = 'timestamp' in a1[0]
# 过滤 websocket 信息,只需要最新价格
if b and c:
lastprice = float(a1[0]['lastPrice'])
timestamp = a1[0]['timestamp']
# 同步状态
# sendToAll({
# "lastprice": lastprice
# })
# 如果存在仓位,gap就是当前仓位的盈利或者亏损的点数
gap = lastprice - self.bc.avgPrice
# 每十次websocket返回信息,就打印一次当前的状态信息
if self.n % 10 == 0:
printlog("lastprice = " + str(lastprice) + "self.bc.pos:" + str(self.prepos) + " gap = " + str(
gap) + " self.init_zhiying = " + str(self.targetProfit) + " self.cengshu = " + str(self.cengshu))
self.n = self.n+1
# 如果仓位变化了,那么一直请求获得最新的仓位,如果仓位跟本地的对比,有变化了,才继续执行
isshouldgo = self.isAfterOrderPosChange()
if isshouldgo == False:
return
# 处理价格到了设定好的压力位,支撑位,平仓
if self.handleSupportAndPressurePrice(lastprice) == True:
return
if self.prepos == 0:
printlog("无仓位立刻开仓")
self.order()
else:
# 为了处理没有仓位,gap会等于当前价格的时候
if gap > 1000:
return
# 当前持有多仓
if self.prepos > 0:
self.handlehaveLong(gap)
# 当前持有空仓
elif self.prepos < 0:
self.handlehaveShort(gap)
# 平仓
def orderClose(self):
self.isInOrder = True
self.bc.orderClose()
self.cengshu = 0
self.mypos = 0
self.init_jiacanggap = 10
self.isPosChange = True
# 下单
def order(self):
self.isInOrder = True
printlog("self.cengshu = " + str(self.cengshu))
if self.prepos == 0:
self.bc.orderauto(1)
else:
self.bc.orderauto(abs(self.prepos) * 2)
self.cengshu = self.cengshu + 1
self.isPosChange = True
def isAfterOrderPosChange(self):
# printlog(" isAfterOrderPosChange 仓位改变,等待"+str(self.isPosChange)+"self.prepos = "+str(self.prepos))
if self.isPosChange == True:
p = self.bc.getpos()
if self.prepos == p:
self.retryposchangetimes = self.retryposchangetimes + 1
if self.retryposchangetimes >= 10:
self.retryposchangetimes = 0
self.isPosChange = False
return True
printlog(" 仓位改变,等待")
return False
else:
printlog(" 仓位改变完毕")
self.prepos = p
self.retryposchangetimes = 0
self.isPosChange = False
return True
else:
return True
def __init__(self):
self.isRun = False
def stopRun(sef):
print('...')
def startRun(self, settingidc):
if(self.isRun):
return
self.isRun = True
print('开始运行', settingidc)
# 下限价格
self.lowcontrolPriceline = float(settingidc["low"])
print("self.lowcontrolPriceline", self.lowcontrolPriceline)
# 上限价格
self.highcontrolPriceline = float(settingidc["high"])
print("self.highcontrolPriceline", self.highcontrolPriceline)
# 赚了多少点就卖
self.targetProfit = float(settingidc["targetProfit"])
print("self.targetProfit", self.targetProfit)
# 每次加仓的价格间隔
self.init_jiacanggap = float(settingidc["priceGap"])
print("self.init_jiacanggap", self.init_jiacanggap)
# 初始仓位
self.initorderPos = float(settingidc["initPos"])
print("self.initorderPos", self.initorderPos)
API_KEY = settingidc["API_KEY"]
print("API_KEY", API_KEY)
API_SECRET = settingidc["API_SECRET"]
print("API_SECRET", API_SECRET)
print("1")
self.n = 0
self.retryposchangetimes = 0
self.isInOrder = False
self.isPosChange = False
self.cengshu = 0
websocket.enableTrace(True)
print("2")
self.XBTH17 = Instrument(symbol='XBTUSD',
# subscribes to all channels by default, here we
# limit to just these two
channels=['margin', 'instrument'],
# you must set your environment variables to authenticate
# see .env.example
shouldAuth=True)
print("3")
self.bc = bitmexclient(API_KEY, API_SECRET)
print("4")
pos = self.bc.getpos()
print("pos = ", pos)
self.prepos = pos
orderBook10 = self.XBTH17.get_table('instrument')
self.XBTH17.on('action', self.onMessage)
# 静态文件服务器
import http.server
import threading
httpd = http.server.HTTPServer(
('localhost', 8000),
http.server.SimpleHTTPRequestHandler
)
threading.Thread(target=httpd.serve_forever).start()
# http://localhost:8000/web/app.html
# websocket
import asyncio
import websockets
import json
stringify = json.JSONEncoder().encode
parse = json.JSONDecoder().decode
clients = []
bws = BitmexWS()
def sendToAll(obj):
str = stringify(obj)
for ws in clients:
asyncio.get_event_loop().create_task(ws.send(str))
async def hello(ws, path):
print('join')
clients.append(ws)
while True:
try:
str = await ws.recv()
print('recv')
bws.startRun(parse(str))
except:
print('exit')
clients.remove(ws)
break
asyncio.get_event_loop().run_until_complete(
websockets.serve(hello, 'localhost', 3000)
)
asyncio.get_event_loop().run_forever()