-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathPytrader_DLL_API_V1_01.py
1973 lines (1663 loc) · 66.6 KB
/
Pytrader_DLL_API_V1_01.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
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import numpy as np
import pandas as pd
import math
from datetime import datetime
import clr # install pythonnet "pip install pythonnet"
clr.AddReference('C:\Program Files (x86)\BranlyCo\Setup_BT_DLL_V1_01\\IL_BT_DLL_V1_01.dll') # path to the installed dll
from IL_BT_DLL_V1_01 import main_entry # type: ignore
ERROR_DICT = {}
ERROR_DICT['00101'] = 'IP address error'
ERROR_DICT['00102'] = 'Port number error'
ERROR_DICT['00103'] = 'Connection error with license EA'
ERROR_DICT['00104'] = 'Undefined answer from license EA'
ERROR_DICT['00301'] = 'Unknown instrument'
ERROR_DICT['00302'] = 'No instruments defined/configured'
ERROR_DICT['00501'] = 'No instrument defined/configured'
ERROR_DICT['00601'] = 'Data directory name error'
ERROR_DICT['01001'] = 'Error in account settings'
ERROR_DICT['01101'] = 'Instrument already done'
ERROR_DICT['01102'] = 'Error in one of the settings'
ERROR_DICT['01103'] = 'Unknown instrument'
ERROR_DICT['01301'] = 'In demo mode not all instruments can be configured'
ERROR_DICT['01302'] = 'Reset first the back tester'
ERROR_DICT['01303'] = 'Can set instruments only once, first reset the back tester'
ERROR_DICT['01304'] = 'Error in creating data factory'
ERROR_DICT['01305'] = 'Error in reading the M1 bar data from file'
ERROR_DICT['01306'] = 'Data file does not exist'
ERROR_DICT['01307'] = 'Invalid demo instrument'
ERROR_DICT['01501'] = 'Wrong low spread value'
ERROR_DICT['01502'] = 'Wrong high spread value'
ERROR_DICT['01503'] = 'Wrong commissionvalue'
ERROR_DICT['01504'] = 'Instrument not defined'
ERROR_DICT['01601'] = 'Index > 0'
ERROR_DICT['01602'] = 'Index wrong type'
ERROR_DICT['01603'] = 'No instruments defined'
ERROR_DICT['01604'] = 'index > max bars'
ERROR_DICT['01605'] = 'Undefined error'
ERROR_DICT['01901'] = 'Wrong time frame value'
ERROR_DICT['04101'] = 'Unknown instrument'
ERROR_DICT['04201'] = 'History to short for the requested amount of bars'
ERROR_DICT['04202'] = 'No data available, instrument not defined!!!'
ERROR_DICT['04203'] = 'Error in start bar value'
ERROR_DICT['04204'] = 'Error in number of bar value'
ERROR_DICT['04205'] = 'Error in max bar value'
ERROR_DICT['07001'] = 'Instrument not defined'
ERROR_DICT['07002'] = 'Error unknown'
ERROR_DICT['07003'] = 'Wrong lot size'
ERROR_DICT['07004'] = 'Order type unknown'
ERROR_DICT['07005'] = 'Error in one of the parameters'
ERROR_DICT['07006'] = 'Wrong TP value'
ERROR_DICT['07007'] = 'Wrong SL value'
ERROR_DICT['07101'] = 'Position not found'
ERROR_DICT['07301'] = 'Order not found'
ERROR_DICT['07501'] = 'Error in sl or tp value, not real values'
ERROR_DICT['07502'] = 'Position not found'
ERROR_DICT['07506'] = 'Wrong TP value'
ERROR_DICT['07507'] = 'Wrong SL value'
ERROR_DICT['07601'] = 'Error in sl or tp value, not real values'
ERROR_DICT['07602'] = 'Order not found'
ERROR_DICT['07606'] = 'Wrong TP value'
ERROR_DICT['07607'] = 'Wrong SL value'
ERROR_DICT['07701'] = 'Error in sl or tp value, not real values'
ERROR_DICT['07702'] = 'Position not found'
ERROR_DICT['07801'] = 'Error in sl or tp value, not real values'
ERROR_DICT['07802'] = 'Order not found'
ERROR_DICT['50001'] = 'Error in comma/dot function'
ERROR_DICT['50101'] = 'Instrument not defined'
ERROR_DICT['50102'] = 'Undefined error'
class Pytrader_DLL_API:
def __init__(self):
self.socket_error: int = 0
self.socket_error_message: str = ''
self.order_return_message: str = ''
self.order_error: int = 0
self.connected: bool = False
self.timeout: bool = False
self.command_OK: bool = False
self.command_return_error: str = ''
self.debug: bool = False
self.version: str = '1.00'
self.max_bars: int = 25000
self.max_ticks: int = 5000
self.timeout_value: int = 120
self.instrument_conversion_list: dict = {}
self.instrument_name_broker: str = ''
self.instrument_name_universal: str = ''
self.date_from: datetime = '2000/01/01, 00:00:00'
self.date_to: datetime = datetime.now()
self.invert_array: bool = False
self.data_directory: str = None
self.error_dict: dict = {}
self.license: str = 'Demo'
self.dll_interface = main_entry()
def Disconnect(self):
"""
Args:
None
Returns:
bool: True or False
"""
return True
def Connect(self,
server: str = '',
port: int = 8888,
instrument_lookup: dict = []) -> bool:
"""
Connects DLL.
Args:
server: Server IP address, like -> '127.0.0.1', '192.168.5.1' NOT USED
port: port number NOT USED
instrument_lookup: dictionairy with general instrument names and broker intrument names
Returns:
bool: True or False
"""
self.instrument_conversion_list = instrument_lookup
if (len(self.instrument_conversion_list) == 0):
print('Broker Instrument list not available or empty')
self.connected = False
self.command_return_error = 'Broker Instrument list not available or empty'
return False
self.connected = self.Check_connection()
return self.connected
def Check_connection(self) -> bool:
"""
Checks if connection with MT terminal/Ea bot is still active.
Args:
None
Returns:
bool: True or False
"""
self.command = 'F000#0#'
self.command_return_error = ''
ok, dataString = self.send_command(self.command)
if (ok == False):
self.command_OK = False
return False
if self.debug:
print(dataString)
x = dataString.split('#')
if x[2] == 'OK':
self.timeout = True
self.command_OK = True
return True
else:
self.timeout = False
self.command_OK = True
return False
def Set_timeout(self,
timeout_in_seconds: int = 60
):
"""
Only for compatibility
Set time out value for socket communication with MT4 or MT5 EA/Bot.
Args:
timeout_in_seconds: the time out value
Returns:
True
"""
self.timeout_value = timeout_in_seconds
return True
@property
def IsConnected(self) -> bool:
"""
Returns connection status.
Returns:
bool: True or False
"""
return self.connected
def Set_comma_or_dot(self,
mode: str = 'comma') -> bool:
"""
Sets if the DLL must see real values as comma or dot string
This is country/culture/region dependend
Args:
mode: comma or dot
Returns:
bool: True or False
"""
self.command = 'F500#1#' + str(mode) + "#"
self.command_return_error = ''
ok, dataString = self.send_command(self.command)
if not ok:
self.command_OK = False
return False
if self.debug:
print(dataString)
x = dataString.split('#')
if x[0] != 'F500':
self.command_return_error = ERROR_DICT[str(x[3])]
self.command_OK = False
return None
return True
def Check_license(self,
server: str = '127.0.0.1',
port: int = 5555) -> bool:
"""
Check for license.
Args:
server: License Server IP address, like -> '127.0.0.1', '192.168.5.1'
port: port number, default 5555
Returns:
bool: True or False
"""
self.license = 'Demo'
self.command = 'F001#2#' + str(server) + "#" + str(port) + '#'
self.command_return_error = ''
ok, dataString = self.send_command(self.command)
if not ok:
self.command_OK = False
return False
if self.debug:
print(dataString)
x = dataString.split('#')
if x[0] != 'F001':
self.command_return_error = ERROR_DICT[str(x[3])]
self.command_OK = False
return False
self.license = str(x[3])
return True
def Set_data_directory(self,
data_directory: str = None) -> bool:
"""
Set data directory/folder.
Args:
data_directory: data directory where BT can find M1 data files
Returns:
bool: True or False
"""
self.data_directory = data_directory
if (self.data_directory != None):
self.command = 'F006#1#' + str(self.data_directory) + '#'
self.command_return_error = ''
ok, dataString = self.send_command(self.command)
if not ok:
self.command_OK = False
return False
if self.debug:
print(dataString)
x = dataString.split('#')
if x[0] != 'F006':
self.command_return_error = ERROR_DICT[str(x[3])]
self.command_OK = False
return False
else:
return True
def Set_bar_date_asc_dec(self,
asc_dec: bool = False) -> bool:
"""
Sets first row of array as first bar or as last bar
Args:
asc_dec: True = row[0] is oldest bar
False = row[0] is latest bar
Returns:
bool: True or False
"""
self.invert_array = asc_dec
return True
def Get_broker_server_time(self) -> datetime:
"""
Retrieves broker server time.
Args:
None
Returns:
datetime: Boker time, this is the time of the current M1 bar
"""
self.command_return_error = ''
self.command = 'F005#0#'
ok, dataString = self.send_command(self.command)
if not ok:
self.command_OK = False
return None
if self.debug:
print(dataString)
x = dataString.split('#')
if x[0] != 'F005':
self.command_return_error = ERROR_DICT[str(x[3])]
self.command_OK = False
return None
del x[0:2]
x.pop(-1)
y = x[0].split('-')
d = datetime(int(y[0]), int(y[1]), int(y[2]),
int(y[3]), int(y[4]), int(y[5]))
return d
def Get_dynamic_account_info(self) -> dict:
"""
Retrieves dynamic account information.
Returns: Dictionary with:
Account balance,
Account equity,
Account profit
"""
self.command_return_error = ''
ok, dataString = self.send_command('F002#0#')
if (ok == False):
self.command_OK = False
return None
if self.debug:
print(dataString)
x = dataString.split('#')
if x[0] != 'F002':
self.command_return_error = str(x[2])
self.command_OK = False
return None
returnDict = {}
del x[0:2]
x.pop(-1)
returnDict['balance'] = float(x[0])
returnDict['equity'] = float(x[1])
returnDict['profit'] = float(x[2])
self.command_OK = True
return returnDict
def Set_instrument_list(self,
instrument_list: list = ['EURUSD', 'GBPUSD']) -> bool:
"""
Sets the instruments for the BT
Args:
instrument_list: list with instruments
Returns:
bool: True or False
Remark: Now the M1 data file contents are imported in the BT for the specified instruments
"""
self.command_return_error = ''
self.command = 'F013#'
if (len(instrument_list) == 0):
self.command_return_error = 'empty list'
return False
self.command = self.command + str(len(instrument_list)) + '#'
for element in instrument_list:
self.command = self.command + element + '#'
ok, dataString = self.send_command(self.command)
if (ok == False):
self.command_OK = False
return False
if self.debug:
print(dataString)
x = dataString.split('#')
if x[0] != 'F013':
self.command_return_error = ERROR_DICT[str(x[3])]
self.command_OK = False
return False
self.timeout = True
self.command_OK = True
return True
def Set_timeframes(self,
timeframe_list: list = ['M1', 'H1']) -> bool:
"""
Set the timeframes for the BT to be used
Args:
timeframe_list: List with timeframes
Returns:
bool: True or False
"""
self.command_return_error = ''
self.command = 'F019#'
if (len(timeframe_list) == 0):
self.command_return_error = 'empty list'
return False
self.command = self.command + str(len(timeframe_list)) + '#'
for element in timeframe_list:
self.command = self.command + element + '#'
ok, dataString = self.send_command(self.command)
if (ok == False):
self.command_OK = False
return False
if self.debug:
print(dataString)
x = dataString.split('#')
if x[0] != 'F019':
self.command_return_error = ERROR_DICT[str(x[3])]
self.command_OK = False
return False
if x[2] == 'OK':
self.timeout = True
self.command_OK = True
return True
def Set_instrument_parameters(self,
instrument: str = 'EURUSD',
digits: int = 5,
max_lotsize: float = 200.0,
min_lotsize: float = 0.01,
lot_stepsize: float = 0.01,
point: float = 0.00001,
tick_size: float = 0.00001,
tick_value: float = 1.0,
swap_long: float = 0.00,
swap_short: float = 0.00) -> bool:
"""
Set the parameters for an instrument
Args:
server: instrument
digits: number of digits
max_lotsize: maximum lotsize value
min_lotsize: minimum lotsize value
lot_stepsize: minimum lot stepsize increment
point: poit value
tick_size: tick size or point value
tick_value: tick value
swap_long; swap long/buy value
swap_short: swap short/sell value
Returns:
bool: True or False
"""
self.command_return_error = ''
self.command = 'F011#10#' + str(instrument) + '#' + str(digits) + '#' + str(max_lotsize) + '#' + str(min_lotsize) + \
"#" + str(lot_stepsize) + "#" + str(point) + "#" + str(tick_size) + "#" + str(tick_value) + \
'#' + str(swap_long) + '#' + str(swap_short) + '#'
ok, dataString = self.send_command(self.command)
if (ok == False):
self.command_OK = False
return False
if self.debug:
print(dataString)
x = dataString.split('#')
if x[0] != 'F011':
self.command_return_error = ERROR_DICT[str(x[3])]
self.command_OK = False
return False
if x[2] == 'OK':
self.timeout = True
self.command_OK = True
return True
def Get_instrument_info(self,
instrument: str = 'EURUSD') -> dict:
"""
Retrieves instrument information.
Args:
instrument: instrument name
Returns: Dictionary with:
instrument,
digits,
max_lotsize,
min_lotsize,
lot_step,
point,
tick_size,
tick_value
swap_long
swap_short
"""
self.command_return_error = ''
self.instrument_name_universal = instrument.upper()
self.instrument = self.get_broker_instrument_name(self.instrument_name_universal)
if (self.instrument == 'none' or self.instrument == None):
self.command_return_error = 'Instrument not in broker list'
self.command_OK = False
return None
self.command = 'F003#1#' + self.instrument + '#'
ok, dataString = self.send_command(self.command)
if not ok:
self.command_OK = False
return None
if self.debug:
print(dataString)
x = dataString.split('#')
if x[0] != 'F003':
self.command_return_error = ERROR_DICT[str(x[3])]
self.command_OK = False
return None
returnDict = {}
del x[0:2]
x.pop(-1)
returnDict['instrument'] = str(self.instrument_name_universal)
returnDict['digits'] = int(x[0])
returnDict['max_lotsize'] = float(x[1])
returnDict['min_lotsize'] = float(x[2])
returnDict['lot_step'] = float(x[3])
returnDict['point'] = float(x[4])
returnDict['tick_size'] = float(x[5])
returnDict['tick_value'] = float(x[6])
returnDict['swap_long'] = float(x[7])
returnDict['swap_short'] = float(x[8])
self.command_OK = True
return returnDict
def Reset_backtester_pointers(self) -> bool:
"""
All pointers in the DLL will be resetted
But no new instrument list and no new time frame list can be set
"""
self.command = 'F009#0#'
self.command_return_error = ''
ok, dataString = self.send_command(self.command)
if (ok == False):
self.command_OK = False
return False
if self.debug:
print(dataString)
x = dataString.split('#')
if x[2] == 'OK':
self.timeout = True
self.command_OK = True
return True
else:
self.timeout = False
self.command_OK = True
return False
def Reset_backtester(self) -> bool:
"""
Reset of backtester.
After this reset new instrument list can be set
"""
self.command = 'F008#0#'
self.command_return_error = ''
ok, dataString = self.send_command(self.command)
if (ok == False):
self.command_OK = False
return False
if self.debug:
print(dataString)
x = dataString.split('#')
if x[2] == 'OK':
self.timeout = True
self.command_OK = True
return True
else:
self.timeout = False
self.command_OK = True
return False
def Set_account_parameters(self,
balance: float = 10000.0,
max_pendings: int = 50,
margin_call: int = 50) -> bool:
"""
Set account parameters
Args:
balance: Start amount of balance, like initial deposit
max_pendings: maximum number op pending orders
margin_call: to be defined, at the moment not used
Returns:
bool: True or False
"""
self.balance = balance
self.max_pendings = max_pendings
self.margin_call = margin_call
self.command_return_error = ''
self.command = 'F010#3#' + str(self.balance) + '#' + str(self.max_pendings) + '#' + str(self.margin_call) + '#'
ok, dataString = self.send_command(self.command)
if (ok == False):
self.command_OK = False
return False
if self.debug:
print(dataString)
x = dataString.split('#')
if x[0] != 'F010':
self.command_return_error = ERROR_DICT[str(x[3])]
self.command_OK = False
return False
if x[2] == 'OK':
self.timeout = True
self.command_OK = True
return True
def Set_spread_and_commission_in_pips(self,
instrument: str = 'EURUSD',
low_spread_in_pips: float = 1.0,
high_spread_in_pips: float = 1.2,
commission_in_pips: float = 1.10) -> bool:
self.low_spread_in_pips = low_spread_in_pips
self.high_spread_in_pips = high_spread_in_pips
self.commission_in_pips = commission_in_pips
"""
Set spread range and commission
Args:
instrument: instrument name
low_spread_in_pips: minimum spread
high_spread_in_pips: maximum spread
commission_in_pips: commission for opening and closing a trade
Returns:
bool: True or False
Remark:
At opening or closing a trade the spread used will have a value between minimum and maximum spread values (random)
"""
self.command_return_error = ''
self.command = 'F015#4#' + str(instrument) + "#" + str(self.low_spread_in_pips) + '#' + str(self.high_spread_in_pips) + '#' + str(self.commission_in_pips) + '#'
ok, dataString = self.send_command(self.command)
if (ok == False):
self.command_OK = False
return False
if self.debug:
print(dataString)
x = dataString.split('#')
if str(x[0]) != 'F015':
self.command_return_error = ERROR_DICT[str(x[3])]
self.command_OK = False
return False
if x[2] == 'OK':
self.timeout = True
self.command_OK = True
return True
else:
self.timeout = False
self.command_OK = True
return False
def Set_index_for_start(self,
index: int = 0) -> bool:
"""
Set the start index for star using the BT
Args:
index: moves the pointers in the Bt to this value
Returns:
bool: True or False
"""
self.command_return_error = ''
self.command = 'F016#1#' + str(index) + '#'
ok, dataString = self.send_command(self.command)
if (ok == False):
self.command_OK = False
return False
if self.debug:
print(dataString)
x = dataString.split('#')
if x[0] != 'F016':
self.command_return_error = ERROR_DICT[str(x[3])]
self.command_OK = False
return False
if x[2] == 'OK':
self.timeout = True
self.command_OK = True
return True
def Go_x_increments_forwards(self,
increments: int = 0) -> bool:
"""
Moves the index/pointers in the BT forwards
Args:
increments: pointer for M1 data/bars will be incremented by x
all other pointers depends on the time frame
Returns:
bool: True or False
"""
self.command_return_error = ''
self.command = 'F017#1#' + str(increments) + '#'
ok, dataString = self.send_command(self.command)
if (ok == False):
self.command_OK = False
return False
if self.debug:
print(dataString)
x = dataString.split('#')
if x[0] != 'F017':
self.command_return_error = ERROR_DICT[str(x[3])]
self.command_OK = False
return False
if x[2] == 'OK':
self.timeout = True
self.command_OK = True
return True
def Get_actual_bar_info(self,
instrument: str = 'EURUSD',
timeframe: int = 16408) -> dict:
"""
Retrieves instrument last actual data.
Args:
instrument: instrument name
timeframe: time frame like H1, H4
Returns: Dictionary with:
instrument name,
date,
open,
high,
low,
close,
volume
"""
self.command_return_error = ''
self.instrument_name_universal = instrument.upper()
self.command = 'F041#2#' + self.get_broker_instrument_name(
self.instrument_name_universal) + '#' + str(timeframe) + '#'
ok, dataString = self.send_command(self.command)
if not ok:
self.command_OK = False
return None
if self.debug:
print(dataString)
x = dataString.split('#')
if str(x[0]) != 'F041':
self.command_return_error = ERROR_DICT[str(x[3])]
self.command_OK = False
return None
del x[0:2]
x.pop(-1)
returnDict = {}
returnDict['instrument'] = str(self.instrument_name_universal)
returnDict['date'] = int(x[0])
returnDict['open'] = float(x[1])
returnDict['high'] = float(x[2])
returnDict['low'] = float(x[3])
returnDict['close'] = float(x[4])
returnDict['volume'] = float(x[5])
self.command_OK = True
return returnDict
def Get_last_x_bars_from_now(self,
instrument: str = 'EURUSD',
timeframe: int = 16408,
nbrofbars: int = 1000) -> np.array:
"""
Retrieves last x bars .
Args:
instrument: name of instrument like EURUSD
timeframe: timeframe like 'H4'
nbrofbars: Number of bars to retrieve
Returns: numpy array with:
date,
open,
high,
low,
close,
volume
"""
self.command_return_error = ''
self.instrument_name_universal = instrument.upper()
self.numberofbars = nbrofbars
dt = np.dtype([('date', np.int64), ('open', np.float64), ('high', np.float64),
('low', np.float64), ('close', np.float64), ('volume', np.int32)])
rates = np.empty(self.numberofbars, dtype=dt)
if (self.numberofbars > self.max_bars):
iloop = self.numberofbars / self.max_bars
iloop = math.floor(iloop)
itail = int(self.numberofbars - iloop * self.max_bars)
for index in range(0, iloop):
self.command = 'F042#5#' + self.get_broker_instrument_name(self.instrument_name_universal) + '#' + \
str(timeframe) + '#' + str(index * self.max_bars) + '#' + str(self.max_bars) + '#' + str(nbrofbars) + '#'
ok, dataString = self.send_command(self.command)
if not ok:
self.command_OK = False
return None
if self.debug:
print(dataString)
print('')
print(len(dataString))
x = dataString.split('#')
if str(x[0]) != 'F042':
self.command_return_error = ERROR_DICT[str(x[3])]
self.command_OK = False
return None
del x[0:2]
x.pop(-1)
for value in range(0, len(x)):
y = x[value].split('$')
rates[value + index * self.max_bars][0] = int(y[0])
rates[value + index * self.max_bars][1] = float(y[1])
rates[value + index * self.max_bars][2] = float(y[2])
rates[value + index * self.max_bars][3] = float(y[3])
rates[value + index * self.max_bars][4] = float(y[4])
rates[value + index * self.max_bars][5] = float(y[5])
if (len(x) < self.max_bars):
rates = np.sort(rates)
self.command_OK = True
if (self.invert_array == True):
rates = np.sort(rates)
return rates
if (itail == 0):
rates = np.sort(rates)
self.command_OK = True
if (self.invert_array == True):
rates = np.sort(rates)
return rates
if (itail > 0):
self.command = 'F042#5#' + self.get_broker_instrument_name(
self.instrument_name_universal) + '#' + str(timeframe) + '#' + str(
iloop * self.max_bars) + '#' + str(itail) + '#' + str(nbrofbars) + '#'
ok, dataString = self.send_command(self.command)
if not ok:
self.command_OK = False
return None
if self.debug:
print(dataString)
print('')
print(len(dataString))
x = dataString.split('#')
if str(x[0]) != 'F042':
self.command_return_error = ERROR_DICT[str(x[3])]
self.command_OK = False
return None
del x[0:2]
x.pop(-1)
for value in range(0, len(x)):
y = x[value].split('$')
rates[value + iloop * self.max_bars][0] = int(y[0])
rates[value + iloop * self.max_bars][1] = float(y[1])
rates[value + iloop * self.max_bars][2] = float(y[2])
rates[value + iloop * self.max_bars][3] = float(y[3])
rates[value + iloop * self.max_bars][4] = float(y[4])
rates[value + iloop * self.max_bars][5] = float(y[5])
self.command_OK = True
rates = np.sort(rates)
if (self.invert_array == True):
rates = np.sort(rates)
return rates
else:
self.command = 'F042#4#' + str(self.get_broker_instrument_name(self.instrument_name_universal)) + '#' + \
str(timeframe) + '#' + str(0) + '#' + str(self.numberofbars) + '#' + str(nbrofbars) + '#'
#print(self.command)
ok, dataString = self.send_command(self.command)
if not ok:
self.command_OK = False
print('not ok')
return None
if self.debug:
print(dataString)
print('')
print(len(dataString))
x = dataString.split('#')
if str(x[0]) != 'F042':
self.command_return_error = ERROR_DICT[str(x[3])]
self.command_OK = False
return None
del x[0:2]
x.pop(-1)
for value in range(0, len(x)):
y = x[value].split('$')
rates[value][0] = int(y[0])
rates[value][1] = float(y[1])
rates[value][2] = float(y[2])
rates[value][3] = float(y[3])
rates[value][4] = float(y[4])
rates[value][5] = float(y[5])
self.command_OK = True
if (self.invert_array == True):
rates = np.sort(rates)
return rates
def Get_last_x_renko_bars_from_now(self,
instrument: str = 'EURUSD',
nbrofbars: int = 1000) -> np.array:
"""
retrieves last x bars from a MT4 or MT5 EA bot.
Args:
instrument: name of instrument like EURUSD
nbrofbars: Number of bars to retrieve
Returns: numpy array with:
date,
open,
high,
low,
close,
volume
"""
self.command_return_error = ''