-
Notifications
You must be signed in to change notification settings - Fork 0
/
book.py
243 lines (158 loc) · 5.8 KB
/
book.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
from config import *
from position import Position, Long, Short, Scalp
import asyncio
import numpy as np
class PositionBook(object):
def __init__(self, pair):
self.pair = pair
self.symbol = binance_coins[pair]
self.book = {WAIT_OPEN: {},
OPEN: {},
CLOSED: {},
CANCELED: {}}
def fetch_position(self, pos_id):
position = None
for key, booklet in self.book.items():
try:
position = booklet[pos_id]
except:
pass
return position
def sync(self):
"""
goal is -> position is in category <=> position is in the exchange with corresponding credentials
:return: void
"""
new_book = {}
update_list = [self.book[WAIT_OPEN], self.book[OPEN]]
for status, booklet in self.book.items():
new_book[status] = {}
for status, booklet in self.book.items():
for pos_id, position in booklet.items():
position.update()
new_status = position.status
if status == new_status:
new_book[status][pos_id] = position
else:
new_book[new_status][pos_id] = position
self.book = new_book
def open(self, params={}):
"""
:param params: MUST contain:
1. type (one of the Positions' instances) -> str
2. instruction (limit, market, etc) -> str
:return: 0 on success
-1 on failure
"""
if 'position' not in params:
raise KeyError("please specify position kind")
pos_type = params['position'].lower()
if type(pos_type) is not str or pos_type not in position_types:
return -1
new_pos = None
if pos_type == 'long':
new_pos = Long(pair=self.pair, params=params)
if pos_type == 'short':
# TODO - add after finishing short
pass
if pos_type == 'scalp':
# TODO - add after finishing scalp
pass
if new_pos is None or new_pos.open() != 0:
return -1
return self._enter(position=new_pos)
def close(self, pos_id, **kwargs):
position = self.fetch_position(pos_id=pos_id)
if position is None:
return -1
res = position.close(kwargs)
if res != 0:
return -1
return 0
def close_cond(self, cond, **kwargs):
"""
closing open positions and canceling wait_open positions
:param cond: gets position and returns boolean
:return: 0 on success -# on failure # is the number of unclosed positions with this cond
"""
limit = kwargs.get("limit", None)
status = kwargs.get("status", None)
if type(limit) not in [int, float] or limit < 0:
raise ValueError('closing price has to be a non negative number')
if status not in [OPEN, WAIT_OPEN, None]:
raise KeyError("no status {} exists in book".format(status))
pos_list = []
if status == OPEN:
pos_list = self.get_cond_positions(cond=cond, status=OPEN)
elif status == WAIT_OPEN:
pos_list = self.get_cond_positions(cond=cond, status=WAIT_OPEN)
elif status is None:
open_pos_list = self.get_cond_positions(cond=cond, status=OPEN)
wait_open_pos_list = self.get_cond_positions(cond=cond, status=WAIT_OPEN)
pos_list = open_pos_list + wait_open_pos_list
return self._close_list(pos_list=pos_list, limit=limit)
def _enter(self, position):
if not isinstance(position, Position):
return -1
if position.pair != self.pair:
return -1
position.update()
if self.fetch_position(pos_id=position.id) is not None:
return -1
self.book[position.status][position.id] = position
return position.id
def _close_list(self, pos_list, limit):
if type(pos_list) is not list:
return 0
not_closed = []
# timeout control
iteration = 0
initial_size = len(pos_list)
while len(pos_list) != 0 and iteration < 100:
for position in pos_list:
if not isinstance(position, Position):
continue
if position.close(limit=limit) != 0:
not_closed.append(position)
pos_list = not_closed
not_closed = []
iteration += 1
self.sync()
# always non-positive number
return len(not_closed) - len(pos_list)
def get_size(self):
sum = 0
for key, booklet in self.book.items():
sum += len(booklet)
return sum
def get_cond_positions(self, cond, status=None):
position_base = {}
cond_positions = []
try:
position_base = self.book[status]
except:
for status, booklet in self.book.items():
position_base = {**position_base, **booklet}
for pos_id, position in position_base.items():
try:
if cond(position):
cond_positions.append(position)
except:
pass
return cond_positions
def __getitem__(self, status):
try:
return self.book[status]
except:
return self.book[OPEN]
@staticmethod
def move_to(key, src, dest):
if type(src) != dict or type(dest) != dict:
return -1
try:
tmp = src[key]
del src[key]
dest[key] = tmp
except Exception as e:
print(e)
return 0