-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbroker1_api_v2.py
327 lines (217 loc) · 7.13 KB
/
broker1_api_v2.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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
#!/usr/bin/python
from HttpMD5Util import httpGet
import os
import datetime
token = os.environ["BROKER1_TOKEN_V2"]
url = "https://1broker.com"
source = "1Broker"
resource_header = "/api/v2/"
def error_check(result):
if result['warning']:
string = source + " warning: " + str(result['warning_message'])
print string
if result['error']:
string = source + " error[" + str(result['error_code']) + "]: " + str(result['error_message'])
print string
def user_details():
resource = resource_header + "user/details.php"
params = {
"token" : token
}
result = httpGet(url, resource, params, source)
error_check(result)
return result['response']
def user_overview():
resource = resource_header + "user/overview.php"
params = {
"token" : token
}
result = httpGet(url, resource, params, source)
error_check(result)
return result['response']
def user_bitcoin_deposit_address():
resource = resource_header + "user/bitcoin_deposit_address.php"
params = {
"token": token
}
result = httpGet(url, resource, params, source)
error_check(result)
return result['response']
def date_start_string():
return str(datetime.datetime.fromtimestamp(0).isoformat()) + "Z"
def date_end_string():
return str(datetime.datetime.utcnow().isoformat()) + "Z"
def user_transaction_log(offset=0, limit=20, date_start=date_start_string(), date_end=date_end_string()):
print "date start: ", date_start
print "date end: ", date_end
resource = resource_header + "user/transaction_log.php"
params = {
"token": token,
"offset": offset,
"limit": limit,
"date_start": date_start,
"date_end": date_end
}
result = httpGet(url, resource, params, source)
error_check(result)
return result['response']
def user_quota_status():
resource = resource_header + "user/quota_status.php"
params = {
"token": token
}
result = httpGet(url, resource, params, source)
error_check(result)
return result['response']
def order_open():
resource = resource_header + "order/open.php"
params = {
"token": token
}
result = httpGet(url, resource, params, source)
error_check(result)
return result['response']
def order_create(symbol, margin, direction, leverage, order_type, order_type_parameter, stop_loss=None, take_profit=None, shared="false"):
resource = resource_header + "order/create.php"
params = {
"symbol" : symbol,
"margin" : margin,
"direction" : direction,
"leverage" : leverage,
"order_type" : order_type,
"order_type_parameter" : order_type_parameter,
"token" : token,
"referral_id" : 3981,
"stop_loss": stop_loss,
"take_profit": take_profit,
"shared": shared
}
result = httpGet(url, resource, params, source)
error_check(result)
return result['response']
def order_cancel(order_id):
resource = resource_header + "order/cancel.php"
params = {
"order_id" : order_id,
"token" : token
}
result = httpGet(url, resource, params, source)
error_check(result)
return result['response']
def position_open():
resource = resource_header + "position/open.php"
params = {
"token": token
}
result = httpGet(url, resource, params, source)
error_check(result)
return result['response']
def position_history(offset=0, limit=20, date_start=date_start_string(), date_end=date_end_string()):
resource = resource_header + "position/history.php"
params = {
"token": token,
"offset": offset,
"limit": limit,
"date_start": date_start,
"date_end": date_end
}
result = httpGet(url, resource, params, source)
error_check(result)
return result['response']
def position_edit(position_id, stop_loss=None, take_profit=None):
resource = resource_header + "position/edit.php"
params = {
"position_id" : position_id,
"token" : token,
"stop_loss": stop_loss,
"take_profit": take_profit
}
result = httpGet(url, resource, params, source)
error_check(result)
return result['response']
def position_close(position_id):
resource = resource_header + "position/close.php"
params = {
"position_id" : position_id,
"token" : token
}
result = httpGet(url, resource, params, source)
error_check(result)
return result['response']
def position_close_cancel(position_id):
resource = resource_header + "position/close_cancel.php"
params = {
"position_id" : position_id,
"token" : token
}
result = httpGet(url, resource, params, source)
error_check(result)
return result['response']
def market_categories():
resource = resource_header + "market/categories.php"
params = {
"token" : token
}
result = httpGet(url, resource, params, source)
error_check(result)
return result['response']
def market_list(category):
resource = resource_header + "market/list.php"
params = {
"token" : token,
"category" : category
}
result = httpGet(url, resource, params, source)
error_check(result)
return result['response']
def market_details(symbol):
resource = resource_header + "market/details.php"
params = {
"symbol" : symbol,
"token" : token
}
result = httpGet(url, resource, params, source)
error_check(result)
return result['response']
def market_quotes(symbols):
resource = resource_header + "market/quotes.php"
params = {
"symbols" : symbols,
"token" : token
}
result = httpGet(url, resource, params, source)
error_check(result)
return result['response']
def market_bars(symbol, resolution = 86400, date_start=date_start_string(), date_end=date_end_string(), limit = None):
resource = resource_header + "market/bars.php"
params = {
"symbol" : symbol,
"token" : token,
"resolution" : resolution,
"date_start" : date_start,
"date_end" : date_end,
"limit": limit
}
result = httpGet(url, resource, params, source)
error_check(result)
return result['response']
def get_symbols():
symbols = []
for category in market_categories():
for market in market_list(category):
symbols.append(market['symbol'])
return symbols
def get_category_and_symbols():
categories = []
for category in market_categories():
for market in market_list():
if market['category'] not in categories:
categories.append(market['category'])
symbols = []
for category in categories:
temp_symbols = []
for market in market_list():
if category == market['category']:
temp_symbols.append(market['symbol'])
symbols.append((category, temp_symbols))
return symbols