-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathAPI.py
572 lines (531 loc) · 19.1 KB
/
API.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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
import websocket, json, openpyxl
from datetime import datetime, timedelta
class XTB:
__version__ = "1.0"
def __init__(self, ID, PSW):
self.ID = ID
self.PSW = PSW
self.ws = 0
self.exec_start = self.get_time()
self.connect()
self.login()
################ XTB ####################
def login(self):
login ={
"command": "login",
"arguments": {
"userId": self.ID,
"password": self.PSW
}
}
login_json = json.dumps(login)
#Sending Login Request
result = self.send(login_json)
result = json.loads(result)
status = result["status"]
if str(status)=="True":
#Success
return True
else:
#Error
return False
def logout(self):
logout ={
"command": "logout"
}
logout_json = json.dumps(logout)
#Sending Logout Request
result = self.send(logout_json)
result = json.loads(result)
status = result["status"]
self.disconnect()
if str(status)=="True":
#Success
return True
else:
#Error
return False
def get_AllSymbols(self):
allsymbols ={
"command": "getAllSymbols"
}
allsymbols_json = json.dumps(allsymbols)
result = self.send(allsymbols_json)
result = json.loads(result)
return result
def get_Candles(self, period, symbol, days=0, hours=0, minutes=0, qty_candles=0):
if period=="M1":
minutes+=qty_candles
period=1
elif period=="M5":
minutes+=qty_candles*5
period=5
elif period=="M15":
minutes+=qty_candles*15
period=15
elif period=="M30":
minutes+=qty_candles*30
period=30
elif period=="H1":
minutes+=qty_candles*60
period=60
elif period=="H4":
minutes+=qty_candles*240
period=240
elif period=="D1":
minutes+=qty_candles*1440
period=1440
elif period=="W1":
minutes+=qty_candles*10080
period=10080
elif period=="MN1":
minutes+=qty_candles*43200
period=43200
if qty_candles!=0:
minutes = minutes*2
start = self.get_ServerTime() - self.to_milliseconds(days=days, hours=hours, minutes=minutes)
CHART_LAST_INFO_RECORD ={
"period": period,
"start": start,
"symbol": symbol
}
candles ={
"command": "getChartLastRequest",
"arguments": {
"info": CHART_LAST_INFO_RECORD
}
}
candles_json = json.dumps(candles)
result = self.send(candles_json)
result = json.loads(result)
candles=[]
candle={}
qty=len(result["returnData"]["rateInfos"])
candle["digits"]=result["returnData"]["digits"]
if qty_candles==0:
candle["qty_candles"]=qty
else:
candle["qty_candles"]=qty_candles
candles.append(candle)
if qty_candles==0:
start_qty = 0
else:
start_qty = qty-qty_candles
if qty==0:
start_qty=0
for i in range(start_qty, qty):
candle={}
candle["datetime"]=result["returnData"]["rateInfos"][i]["ctmString"]
candle["open"]=result["returnData"]["rateInfos"][i]["open"]
candle["close"]=result["returnData"]["rateInfos"][i]["close"]
candle["high"]=result["returnData"]["rateInfos"][i]["high"]
candle["low"]=result["returnData"]["rateInfos"][i]["low"]
candles.append(candle)
if len(candles)==1:
return False
return candles
'''
LIMITS:
PERIOD_M1 --- <0-1) month, i.e. one month time
PERIOD_M30 --- <1-7) month, six months time
PERIOD_H4 --- <7-13) month, six months time
PERIOD_D1 --- 13 month, and earlier on
##############################################
PERIOD_M1 1 1 minute
PERIOD_M5 5 5 minutes
PERIOD_M15 15 15 minutes
PERIOD_M30 30 30 minutes
PERIOD_H1 60 60 minutes (1 hour)
PERIOD_H4 240 240 minutes (4 hours)
PERIOD_D1 1440 1440 minutes (1 day)
PERIOD_W1 10080 10080 minutes (1 week)
PERIOD_MN1 43200 43200 minutes (30 days)
##############################################
close Value of close price (shift from open price)
ctm Candle start time in CET / CEST time zone (see Daylight Saving Time)
ctmString String representation of the 'ctm' field
high Highest value in the given period (shift from open price)
low Lowest value in the given period (shift from open price)
open Open price (in base currency * 10 to the power of digits)
vol Volume in lots
'''
def get_CandlesRange(self, period, symbol, start=0, end=0, days=0, qty_candles=0):
if period=="M1":
period=1
elif period=="M5":
period=5
elif period=="M15":
period=15
elif period=="M30":
period=30
elif period=="H1":
period=60
elif period=="H4":
period=240
elif period=="D1":
period=1440
elif period=="W1":
period=10080
elif period=="MN1":
period=43200
if end==0:
end = self.get_time()
end = end.strftime('%m/%d/%Y %H:%M:%S')
if start==0:
if qty_candles==0:
temp = datetime.strptime(end, '%m/%d/%Y %H:%M:%S')
start = temp - timedelta(days=days)
start = start.strftime("%m/%d/%Y %H:%M:%S")
else:
start = datetime.strptime(end, '%m/%d/%Y %H:%M:%S')
minutes = period*qty_candles
start = start - timedelta(minutes=minutes)
start = start.strftime("%m/%d/%Y %H:%M:%S")
start = self.time_conversion(start)
end = self.time_conversion(end)
CHART_RANGE_INFO_RECORD ={
"end": end,
"period": period,
"start": start,
"symbol": symbol,
"ticks": 0
}
candles ={
"command": "getChartRangeRequest",
"arguments": {
"info": CHART_RANGE_INFO_RECORD
}
}
candles_json = json.dumps(candles)
result = self.send(candles_json)
result = json.loads(result)
candles=[]
candle={}
qty=len(result["returnData"]["rateInfos"])
candle["digits"]=result["returnData"]["digits"]
if qty_candles==0:
candle["qty_candles"]=qty
else:
candle["qty_candles"]=qty_candles
candles.append(candle)
if qty_candles==0:
start_qty = 0
else:
start_qty = qty-qty_candles
if qty==0:
start_qty=0
for i in range(start_qty, qty):
candle={}
candle["datetime"]=str(result["returnData"]["rateInfos"][i]["ctmString"])
candle["open"]=result["returnData"]["rateInfos"][i]["open"]
candle["close"]=result["returnData"]["rateInfos"][i]["close"]
candle["high"]=result["returnData"]["rateInfos"][i]["high"]
candle["low"]=result["returnData"]["rateInfos"][i]["low"]
candles.append(candle)
if len(candles)==1:
return False
return candles
'''
LIMITS:
PERIOD_M1 --- <0-1) month, i.e. one month time
PERIOD_M30 --- <1-7) month, six months time
PERIOD_H4 --- <7-13) month, six months time
PERIOD_D1 --- 13 month, and earlier on
##############################################
PERIOD_M1 1 1 minute
PERIOD_M5 5 5 minutes
PERIOD_M15 15 15 minutes
PERIOD_M30 30 30 minutes
PERIOD_H1 60 60 minutes (1 hour)
PERIOD_H4 240 240 minutes (4 hours)
PERIOD_D1 1440 1440 minutes (1 day)
PERIOD_W1 10080 10080 minutes (1 week)
PERIOD_MN1 43200 43200 minutes (30 days)
##############################################
close Value of close price (shift from open price)
ctm Candle start time in CET / CEST time zone (see Daylight Saving Time)
ctmString String representation of the 'ctm' field
high Highest value in the given period (shift from open price)
low Lowest value in the given period (shift from open price)
open Open price (in base currency * 10 to the power of digits)
vol Volume in lots
'''
def get_ServerTime(self):
time ={
"command": "getServerTime"
}
time_json = json.dumps(time)
result = self.send(time_json)
result = json.loads(result)
time = result["returnData"]["time"]
return time
def get_Balance(self):
balance ={
"command": "getMarginLevel"
}
balance_json = json.dumps(balance)
result = self.send(balance_json)
result = json.loads(result)
balance = result["returnData"]["balance"]
return balance
def get_Margin(self, symbol, volume):
margin ={
"command": "getMarginTrade",
"arguments": {
"symbol": symbol,
"volume": volume
}
}
margin_json = json.dumps(margin)
result = self.send(margin_json)
result = json.loads(result)
margin = result["returnData"]["margin"]
return margin
def get_Profit(self, open_price, close_price, transaction_type, symbol, volume):
if transaction_type==1:
#buy
cmd = 0
else:
#sell
cmd = 1
profit ={
"command": "getProfitCalculation",
"arguments": {
"closePrice": close_price,
"cmd": cmd,
"openPrice": open_price,
"symbol": symbol,
"volume": volume
}
}
profit_json = json.dumps(profit)
result = self.send(profit_json)
result = json.loads(result)
profit = result["returnData"]["profit"]
return profit
def get_Symbol(self, symbol):
symbol ={
"command": "getSymbol",
"arguments": {
"symbol": symbol
}
}
symbol_json = json.dumps(symbol)
result = self.send(symbol_json)
result = json.loads(result)
symbol = result["returnData"]
return symbol
def make_Trade(self, symbol, cmd, transaction_type, volume, comment="", order=0, sl=0, tp=0, days=0, hours=0, minutes=0):
price = self.get_Candles("M1",symbol,qty_candles=1)
price = price[1]["open"]+price[1]["close"]
delay = self.to_milliseconds(days=days, hours=hours, minutes=minutes)
if delay==0:
expiration = self.get_ServerTime() + self.to_milliseconds(minutes=1)
else:
expiration = self.get_ServerTime() + delay
TRADE_TRANS_INFO = {
"cmd": cmd,
"customComment": comment,
"expiration": expiration,
"offset": -1,
"order": order,
"price": price,
"sl": sl,
"symbol": symbol,
"tp": tp,
"type": transaction_type,
"volume": volume
}
trade = {
"command": "tradeTransaction",
"arguments": {
"tradeTransInfo": TRADE_TRANS_INFO
}
}
trade_json = json.dumps(trade)
result = self.send(trade_json)
result = json.loads(result)
if result["status"]==True:
#success
return True, result["returnData"]["order"]
else:
#error
return False, 0
"""
format TRADE_TRANS_INFO:
cmd Number Operation code
customComment String The value the customer may provide in order to retrieve it later.
expiration Time Pending order expiration time
offset Number Trailing offset
order Number 0 or position number for closing/modifications
price Floating number Trade price
sl Floating number Stop loss
symbol String Trade symbol
tp Floating number Take profit
type Number Trade transaction type
volume Floating number Trade volume
values cmd:
BUY 0 buy
SELL 1 sell
BUY_LIMIT 2 buy limit
SELL_LIMIT 3 sell limit
BUY_STOP 4 buy stop
SELL_STOP 5 sell stop
BALANCE 6 Read only. Used in getTradesHistory for manager's deposit/withdrawal operations (profit>0 for deposit, profit<0 for withdrawal).
CREDIT 7 Read only
values transaction_type:
OPEN 0 order open, used for opening orders
PENDING 1 order pending, only used in the streaming getTrades command
CLOSE 2 order close
MODIFY 3 order modify, only used in the tradeTransaction command
DELETE 4 order delete, only used in the tradeTransaction command
"""
def check_Trade(self, order):
trade ={
"command": "tradeTransactionStatus",
"arguments": {
"order": order
}
}
trade_json = json.dumps(trade)
result = self.send(trade_json)
result = json.loads(result)
status = result["returnData"]["requestStatus"]
return status
'''
ERROR 0 error
PENDING 1 pending
ACCEPTED 3 The transaction has been executed successfully
REJECTED 4 The transaction has been rejected
'''
def get_History(self, start=0, end=0, days=0, hours=0, minutes=0):
if start!=0:
start = self.time_conversion(start)
if end!=0:
end = self.time_conversion(end)
if days!=0 or hours!=0 or minutes!=0:
if end==0:
end = self.get_ServerTime()
start = end - self.to_milliseconds(days=days, hours=hours, minutes=minutes)
history ={
"command": "getTradesHistory",
"arguments": {
"end": end,
"start": start
}
}
history_json = json.dumps(history)
result = self.send(history_json)
result = json.loads(result)
history = result["returnData"]
return history
def ping(self):
ping ={
"command": "ping"
}
ping_json = json.dumps(ping)
result = self.send(ping_json)
result = json.loads(result)
return result["status"]
################ EXCEL ####################
def candles_to_excel(self, candles, address, name):
self.exec_start = self.get_time()
if candles==False:
print("Error: No Candles!")
#error
return False
try:
wb = openpyxl.Workbook()
wspace = wb.active
wspace.title = "Candles"
for pages in candles:
wspace.append(list(pages.values()))
wb.save(address+name)
#success
return True
except:
#error
return False
def get_candles_from_excel(self, address, name):
temp1=[]
wb = openpyxl.load_workbook(address+name)
wsp = wb.active
for rows in wb.active.iter_rows(min_row=0, max_row=1000000):
temp={}
i=0
for cell in rows:
if i==0 and cell.value==None:
return temp1
elif i==0 and cell.value!=None:
temp["datetime"] = cell.value
elif i==1 and cell.value!=None:
temp["open"] = cell.value
elif i==2 and cell.value!=None:
temp["close"] = cell.value
elif i==3 and cell.value!=None:
temp["high"] = cell.value
elif i==4 and cell.value!=None:
temp["low"] = cell.value
i+=1
temp1.append(temp)
################ TIME/DATE/CONVERSIONS ####################
def get_time(self):
time = datetime.today().strftime('%m/%d/%Y %H:%M:%S%f')
time = datetime.strptime(time, '%m/%d/%Y %H:%M:%S%f')
return time
def to_milliseconds(self, days=0, hours=0, minutes=0):
milliseconds = (days*24*60*60*1000)+(hours*60*60*1000)+(minutes*60*1000)
return milliseconds
def time_conversion(self, date):
start = "01/01/1970 00:00:00"
start = datetime.strptime(start, '%m/%d/%Y %H:%M:%S')
date = datetime.strptime(date, '%m/%d/%Y %H:%M:%S')
final_date = date-start
temp = str(final_date)
temp1, temp2 = temp.split(", ")
hours, minutes, seconds = temp2.split(":")
days = final_date.days
days = int(days)
hours = int(hours)
hours+=2
minutes = int(minutes)
seconds = int(seconds)
time = (days*24*60*60*1000)+(hours*60*60*1000)+(minutes*60*1000)+(seconds*1000)
return time
################ CHECKS ####################
def is_on(self):
temp1 = self.exec_start
temp2 = self.get_time()
temp = temp2 - temp1
temp = temp.total_seconds()
temp = float(temp)
if temp>=8.0:
self.connect()
self.exec_start = self.get_time()
def is_open(self, symbol):
candles = self.get_Candles("M1", symbol, qty_candles=1)
if len(candles)==1:
return False
else:
return True
################ WEBSOCKETS ####################
def connect(self):
try:
self.ws=websocket.create_connection("wss://ws.xtb.com/demo")
#Success
return True
except:
#Error
return False
def disconnect(self):
try:
self.ws.close()
#Success
return True
except:
return False
def send(self, msg):
self.is_on()
self.ws.send(msg)
result = self.ws.recv()
return result+"\n"