forked from ejtraderLabs/ejtraderMT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmql.py
executable file
·823 lines (734 loc) · 27.5 KB
/
mql.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
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
from datetime import datetime, timedelta
from pytz import timezone
from tzlocal import get_localzone
from queue import Queue
from threading import Thread
import os
import time
import zmq
import pandas as pd
from ejtraderTH import start
from ejtraderDB import DictSQLite
from influxdb import DataFrameClient
from tqdm import tqdm
import logging
import sys
import warnings
# Configuração do logger
LOGGER = {
"datefmt": "%Y-%m-%d %H:%M:%S",
"format": (
"[%(asctime)s.%(msecs)03d]"
"[%(process)s]"
"[%(funcName)s:%(lineno)d]"
"[%(levelname)s]"
": %(message)s"
),
"level": logging.INFO,
"stream": sys.stdout,
}
class Functions:
def __init__(self, host=None, debug=None, port=15557):
self.HOST = host or "localhost"
self.SYS_PORT = port # REP/REQ port
# ZeroMQ timeout in seconds
sys_timeout = 1000
# initialise ZMQ context
context = zmq.Context()
# connect to server sockets
try:
self.sys_socket = context.socket(zmq.REQ)
# set port timeout
self.sys_socket.RCVTIMEO = sys_timeout
self.sys_socket.connect("tcp://{}:{}".format(self.HOST, self.SYS_PORT))
except zmq.ZMQError:
raise zmq.ZMQBindError("Binding ports ERROR")
except KeyboardInterrupt:
self.sys_socket.close()
self.sys_socket.term()
pass
def _send_request(self, data: dict) -> None:
"""Send request to server via ZeroMQ System socket"""
try:
self.sys_socket.send_json(data)
except AssertionError as err:
raise zmq.NotDone(err)
except zmq.ZMQError:
raise zmq.NotDone("Sending request ERROR")
def _pull_reply(self):
"""Get reply from server via Data socket with timeout"""
try:
msg = self.sys_socket.recv_json()
except zmq.ZMQError:
raise zmq.NotDone("Data socket timeout ERROR")
return msg
def Command(self, **kwargs) -> dict:
"""Construct a request dictionary from default and send it to server"""
# default dictionary
request = {
"action": None,
"actionType": None,
"symbol": None,
"chartTF": None,
"fromDate": None,
"toDate": None,
"id": None,
"magic": None,
"volume": None,
"price": None,
"stoploss": None,
"takeprofit": None,
"expiration": None,
"deviation": None,
"comment": None,
"chartId": None,
"indicatorChartId": None,
"chartIndicatorSubWindow": None,
"style": None,
}
# update dict values if exist
for key, value in kwargs.items():
if key in request:
request[key] = value
else:
raise KeyError("Unknown key in **kwargs ERROR")
# send dict to server
self._send_request(request)
# return server reply
return self._pull_reply()
class Metatrader:
def __init__(
self,
host=None,
real_volume=None,
tz_local=None,
dbtype=None,
dbhost=None,
dbport=None,
dbpass=None,
dbuser=None,
dbname=None,
debug=False,
port=15557
):
if debug:
logging.basicConfig(**LOGGER)
self.__api = Functions(host, debug=debug, port=port)
self.real_volume = real_volume or False
self.__tz_local = tz_local
self.__utc_timezone = timezone("UTC")
self.__my_timezone = get_localzone()
self.__utc_brocker_offset = self.___utc_brocker_offset()
# db settings
self.dbtype = dbtype or "SQLITE" # SQLITE OR INFLUXDB
if self.dbtype == "INFLUXDB":
warnings.warn(
"INFLUXDB will be removed in future versions.", DeprecationWarning
)
# if dbtype is influxdb
self.dbhost = dbhost or "localhost"
self.dbport = dbport or "8086"
self.dbuser = dbuser or "root"
self.dbpass = dbpass or "root"
self.dbname = dbname or "ejtraderMT"
self.protocol = "line"
self.__client = DataFrameClient(
self.dbhost, self.dbport, self.dbuser, self.dbpass, self.dbname
)
self.__client.create_database(self.dbname)
def balance(self):
return self.__api.Command(action="BALANCE")
def symbols(self):
df = None
try:
df = self.__api.Command(action="LISTSYMBOLS")
except Exception as e:
logging.info(f"Error while processing. Error message: {str(e)}")
if df is not None and isinstance(df, dict):
try:
if df["data"]:
df = pd.DataFrame(df["data"])
df.columns = [
"symbol",
"expiration",
"type",
]
except Exception as e:
logging.info(
f"Error while processing Dataframe. Error message: {str(e)}"
)
pass
df = df.drop_duplicates(subset="symbol", keep="first")
return df
def calendar(self, symbol=None, fromDate=None, toDate=None, database=None):
self._symbol = symbol
self._fromDate = fromDate
self._toDate = toDate
self.__calendarQ = Queue()
self.__database = database
try:
start(self._calendar, repeat=1, max_threads=20)
except Exception as e:
logging.info(f"Error: {e}")
return self.__calendarQ.get()
def _calendar(self, data):
symbol = self._symbol
fromDate = self._fromDate
toDate = self._toDate
df = pd.DataFrame()
# count data
if not isinstance(fromDate, int):
start_date = datetime.strptime(fromDate, "%d/%m/%Y")
else:
start_date = self.__brokerTimeDelta(fromDate)
if not toDate:
end_date = self.__brokerTimeDelta(0)
else:
end_date = datetime.strptime(toDate, "%d/%m/%Y")
delta = timedelta(days=1)
delta2 = timedelta(days=1)
diff_days = start_date - end_date
days_count = diff_days.days
pbar = tqdm(total=abs(days_count))
appended_data = []
while start_date <= end_date:
pbar.update(delta.days)
fromDate = start_date.strftime("%d/%m/%Y")
toDate = start_date
toDate += delta2
toDate = toDate.strftime("%d/%m/%Y")
try:
df = self.__api.Command(
action="CALENDAR",
actionType="DATA",
symbol=symbol,
fromDate=self.__date_to_timestamp(fromDate),
toDate=self.__date_to_timestamp(toDate),
)
except Exception as e:
logging.info(
f"Error while processing {symbol}. Error message: {str(e)}"
)
pass
try:
df = pd.DataFrame(df["data"])
df.columns = [
"date",
"currency",
"impact",
"event",
"country",
"actual",
"forecast",
"previous",
]
df["date"] = pd.to_datetime(df["date"], errors="coerce")
df = df.dropna(subset=["date"])
df = df.set_index("date")
df.index = pd.to_datetime(df.index)
except Exception as e:
logging.info(
f"Error while processing {symbol} Dataframe. Error message: {str(e)}"
)
pass
appended_data.append(df)
start_date += delta
pbar.close()
df = pd.concat(appended_data)
if self.__database:
start(self.__save_to_db, data=[df], repeat=1, max_threads=20)
else:
try:
self.__set_utc_or_localtime_tz_df(df)
self.__calendarQ.put(df)
except AttributeError:
pass
def accountInfo(self):
return self.__api.Command(action="ACCOUNT")
def positions(self):
return self.__api.Command(action="POSITIONS")
def orders(self):
return self.__api.Command(action="ORDERS")
def trade(self, symbol, actionType, volume, stoploss, takeprofit, price, deviation):
self.__api.Command(
action="TRADE",
actionType=actionType,
symbol=symbol,
volume=volume,
stoploss=stoploss,
takeprofit=takeprofit,
price=price,
deviation=deviation,
)
def buy(self, symbol, volume, stoploss, takeprofit, deviation=5):
price = 0
self.trade(
symbol, "ORDER_TYPE_BUY", volume, stoploss, takeprofit, price, deviation
)
def sell(self, symbol, volume, stoploss, takeprofit, deviation=5):
price = 0
self.trade(
symbol, "ORDER_TYPE_SELL", volume, stoploss, takeprofit, price, deviation
)
def buyLimit(self, symbol, volume, stoploss, takeprofit, price=0, deviation=5):
self.trade(
symbol,
"ORDER_TYPE_BUY_LIMIT",
volume,
stoploss,
takeprofit,
price,
deviation,
)
def sellLimit(self, symbol, volume, stoploss, takeprofit, price=0, deviation=5):
self.trade(
symbol,
"ORDER_TYPE_SELL_LIMIT",
volume,
stoploss,
takeprofit,
price,
deviation,
)
def buyStop(self, symbol, volume, stoploss, takeprofit, price=0, deviation=5):
self.trade(
symbol,
"ORDER_TYPE_BUY_STOP",
volume,
stoploss,
takeprofit,
price,
deviation,
)
def sellStop(self, symbol, volume, stoploss, takeprofit, price=0, deviation=5):
self.trade(
symbol,
"ORDER_TYPE_SELL_LIMIT",
volume,
stoploss,
takeprofit,
price,
deviation,
)
def cancel_all(self):
orders = self.orders()
if "orders" in orders:
for order in orders["orders"]:
self.CancelById(order["id"])
def close_all(self):
positions = self.positions()
if "positions" in positions:
for position in positions["positions"]:
self.CloseById(position["id"])
def positionModify(self, id, stoploss, takeprofit):
self.__api.Command(
action="TRADE",
actionType="POSITION_MODIFY",
id=id,
stoploss=stoploss,
takeprofit=takeprofit,
)
def ClosePartial(self, id, volume):
self.__api.Command(
action="TRADE", actionType="POSITION_PARTIAL", id=id, volume=volume
)
def CloseById(self, id):
self.__api.Command(action="TRADE", actionType="POSITION_CLOSE_ID", id=id)
def CloseBySymbol(self, symbol):
self.__api.Command(
action="TRADE", actionType="POSITION_CLOSE_SYMBOL", symbol=symbol
)
def orderModify(self, id, stoploss, takeprofit, price):
self.__api.Command(
action="TRADE",
actionType="ORDER_MODIFY",
id=id,
stoploss=stoploss,
takeprofit=takeprofit,
price=price,
)
def CancelById(self, id):
self.__api.Command(action="TRADE", actionType="ORDER_CANCEL", id=id)
def ___utc_brocker_offset(self):
utc = datetime.now(self.__utc_timezone).strftime("%Y-%m-%d %H:%M:%S")
try:
broker = self.accountInfo()
broker = datetime.strptime(broker["time"], "%Y.%m.%d %H:%M:%S")
except KeyError:
raise "Metatrader 5 Server is disconnect"
utc = datetime.strptime(utc, "%Y-%m-%d %H:%M:%S")
duration = broker - utc
duration_in_s = duration.total_seconds()
hour = divmod(duration_in_s, 60)[0]
seconds = int(hour) * 60
return seconds
def _price(self):
connect = self.__api.live_socket()
while True:
price = connect.recv_json()
try:
price = price["data"]
price = pd.DataFrame([price])
price = price.set_index([0])
price.index.name = "date"
if self._allchartTF == "TICK":
price.index = pd.to_datetime(price.index, unit="ms")
price.columns = ["bid", "ask"]
self.__priceQ.put(price)
elif self._allchartTF == "TS":
price.index = pd.to_datetime(price.index, unit="ms")
price.columns = ["type", "bid", "ask", "last", "volume"]
self.__priceQ.put(price)
else:
if self.real_volume:
del price[5]
else:
del price[6]
price.index = pd.to_datetime(price.index, unit="s")
price.columns = ["open", "high", "low", "close", "volume", "spread"]
self.__priceQ.put(price)
except KeyError:
pass
def _start_thread_price(self):
t = Thread(target=self._price, daemon=True)
t.start()
self.__priceQ = Queue()
def _start_thread_event(self):
t = Thread(target=self._event, daemon=True)
t.start()
self.__eventQ = Queue()
def _event(self):
connect = self.__api.streaming_socket()
while True:
event = connect.recv_json()
try:
event = event["request"]
event = pd.DataFrame(event, index=[0])
self.__eventQ.put(event)
except KeyError:
pass
def price(self, symbol, chartTF):
self._allsymbol_ = symbol
self._allchartTF = chartTF
for active in symbol:
self.__api.Command(action="CONFIG", symbol=active, chartTF=chartTF)
self._start_thread_price()
time.sleep(0.5)
return self.__priceQ.get()
def event(self, symbol, chartTF):
self._allsymbol_ = symbol
self._allchartTF = chartTF
for active in symbol:
self.__api.Command(action="CONFIG", symbol=active, chartTF=chartTF)
self._start_thread_event()
time.sleep(0.5)
return self.__eventQ.get()
# convert datestamp to dia/mes/ano
def __date_to_timestamp(self, s):
return time.mktime(datetime.strptime(s, "%d/%m/%Y").timetuple())
# convert datestamp to dia/mes/ano
def datetime_to_timestamp(self, s):
return time.mktime(s.timetuple())
def ___date_to_timestamp_broker(self):
brokertime = time.mktime(
datetime.strptime(
self.accountInfo()["time"], "%Y.%m.%d %H:%M:%S"
).timetuple()
)
return brokertime
def __brokerTimeDelta(self, m):
delta = timedelta(days=m)
broker = datetime.strptime(self.accountInfo()["time"], "%Y.%m.%d %H:%M:%S")
result = broker - delta
return result
def __timeframe_to_sec(self, timeframe):
# Timeframe dictionary
TIMECANDLE = {
"M1": 60,
"M2": 120,
"M3": 180,
"M4": 240,
"M5": 300,
"M15": 900,
"M30": 1800,
"H1": 3600,
"H4": 14400,
"D1": 86400,
"W1": 604800,
"MN": 2629746,
}
return TIMECANDLE[timeframe]
def __set_utc_or_localtime_tz_df(self, df):
try:
df.index = df.index.tz_localize(self.__utc_brocker_offset)
if self.__tz_local:
df.index = df.index.tz_convert(self.__my_timezone)
df.index = df.index.tz_localize(None)
except:
pass
return df
def history(
self,
symbol,
chartTF=None,
fromDate=None,
toDate=None,
database=None,
dataframe=True,
):
self.chartTF = chartTF
self.__database = database
self.fromDate = fromDate
self.toDate = toDate
self.__historyQ = Queue()
self.dataframe = dataframe
if isinstance(symbol, tuple):
for symbols in symbol:
self.__symbol = symbols
print(symbols)
elif isinstance(symbol, list):
self.__symbol = symbol
else:
self.__symbol = [symbol]
if chartTF:
if self.__database:
try:
start(self.__historyThread_save, repeat=1, max_threads=20)
except Exception as e:
logging.info(
f"Error: unable to start History thread Error message: {str(e)}"
)
else:
try:
start(self.__historyThread_save, repeat=1, max_threads=20)
except Exception as e:
logging.info(
f"Error: unable to start History thread Error message: {str(e)}"
)
return self.__historyQ.get()
else:
q = DictSQLite("history")
if isinstance(symbol, list):
try:
if self.dbtype == "SQLITE":
df = q[f"{self.__symbol[0]}"]
else:
df = self.__client.query(f"select * from {self.__symbol[0]}")
df = df[self.__symbol[0]]
df.index.name = "date"
except KeyError:
df = f" {self.__symbol[0]} isn't on database"
pass
else:
try:
if self.dbtype == "SQLITE":
df = q[f"{self.__symbol}"]
else:
df = self.__client.query(f"select * from {self.__symbol}")
df = df[self.__symbol]
df.index.name = "date"
except KeyError:
df = f" {self.__symbol} isn't on database"
pass
return df
def __historyThread_save(self, data):
actives = self.__symbol
chartTF = self.chartTF
fromDate = self.fromDate
toDate = self.toDate
main = pd.DataFrame()
current = pd.DataFrame()
self._count = 0
try:
os.makedirs("DataBase")
except OSError:
pass
# count data
if not isinstance(fromDate, int):
start_date = datetime.strptime(fromDate, "%d/%m/%Y")
else:
start_date = self.__brokerTimeDelta(fromDate)
if not toDate:
end_date = self.__brokerTimeDelta(0)
else:
end_date = datetime.strptime(toDate, "%d/%m/%Y")
delta = timedelta(days=1)
delta2 = timedelta(days=1)
diff_days = start_date - end_date
days_count = diff_days.days
pbar = tqdm(total=abs(days_count))
appended_data = []
active = None
df = None
while start_date <= end_date:
pbar.update(delta.days)
fromDate = start_date.strftime("%d/%m/%Y")
toDate = start_date
toDate += delta2
toDate = toDate.strftime("%d/%m/%Y")
attempts = 0
success = False
# if chartTF == "TICK":
# chartConvert = 60
# else:
# chartConvert = self.__timeframe_to_sec(chartTF)
for active in actives:
self._count += 1
# the first symbol on list is the main and the rest will merge
if active == actives[0]:
self.__active_name = active
while not success and attempts < 5:
try:
data = self.__api.Command(
action="HISTORY",
actionType="DATA",
symbol=active,
chartTF=chartTF,
fromDate=self.__date_to_timestamp(fromDate),
toDate=self.__date_to_timestamp(toDate),
)
success = True
except Exception as e:
logging.info(
f"Error while processing {active} from {fromDate}. Error message: {str(e)}"
)
attempts += 1
if attempts == 5 and not success:
logging.info(f"Check if {active} is avalible from {fromDate}")
pass
if data is not None and isinstance(data, dict):
try:
if data["data"]:
main = pd.DataFrame(data["data"])
main = main.set_index([0])
main.index.name = "date"
# TICK DATA
if chartTF == "TICK":
main.columns = ["bid", "ask"]
main.index = pd.to_datetime(main.index, unit="ms")
else:
main.index = pd.to_datetime(main.index, unit="s")
if self.real_volume:
del main[5]
else:
del main[6]
main.columns = [
"open",
"high",
"low",
"close",
"volume",
"spread",
]
except Exception as e:
logging.info(
f"Error while processing Dataframe {active} from {fromDate}. Error message: {str(e)}"
)
pass
else:
while not success and attempts < 2:
try:
data = self.__api.Command(
action="HISTORY",
actionType="DATA",
symbol=active,
chartTF=chartTF,
fromDate=self.__date_to_timestamp(fromDate),
toDate=self.__date_to_timestamp(toDate),
)
success = True
except Exception as e:
logging.info(
f"Error while processing {active}. Error message: {str(e)}"
)
attempts += 1
if attempts == 2 and not success:
logging.info(f"Check if {active} is avalible from {fromDate}")
pass
if data is not None and isinstance(data, dict):
try:
if data["data"]:
current = pd.DataFrame(data["data"])
current = current.set_index([0])
current.index.name = "date"
active = active.lower()
# TICK DATA
if chartTF == "TICK":
current.index = pd.to_datetime(
current.index, unit="ms"
)
current.columns = [f"{active}_bid", f"{active}_ask"]
else:
current.index = pd.to_datetime(
current.index, unit="s"
)
if self.real_volume:
del current[5]
else:
del current[6]
current.columns = [
f"{active}_open",
f"{active}_high",
f"{active}_low",
f"{active}_close",
f"{active}_volume",
f"{active}_spread",
]
# main = pd.merge(main, current, how='inner',
# left_index=True, right_index=True)
main = pd.merge(main, current, on="date")
except Exception as e:
logging.info(
f"Error while merge Dataframe {active}. Error message: {str(e)}"
)
pass
try:
main = main.loc[~main.index.duplicated(keep="first")]
appended_data.append(main)
except Exception as e:
logging.info(
f"Error while finishing Dataframe for {active}. Error message: {str(e)}"
)
pass
start_date += delta
pbar.close()
if len(appended_data) > 0:
try:
df = pd.concat(appended_data)
except Exception as e:
logging.info(
f"Error while processing {active}. Error message: {str(e)}"
)
pass
if self.__database:
start(self.__save_to_db, data=[df], repeat=1, max_threads=20)
else:
try:
self.__set_utc_or_localtime_tz_df(df)
self.__historyQ.put(df)
except Exception as e:
logging.info(
f"Error while processing {active}. Error message: {str(e)}"
)
pass
def __save_to_db(self, df):
if self.dbtype == "SQLITE":
q = DictSQLite("history", multithreading=True)
try:
self.__set_utc_or_localtime_tz_df(df)
except Exception as e:
logging.info(
f"Error while processing database. Error message: {str(e)}"
)
pass
q[f"{self._symbol}"] = df
else:
try:
self.__set_utc_or_localtime_tz_df(df)
except Exception as e:
logging.info(
f"Error while processing utc or localtime tz. Error message: {str(e)}"
)
pass
if self.dbtype == "INFLUXDB":
self.__client.write_points(df, f"{self._symbol}", protocol=self.protocol)