MT4 Files

Check-in [1db6efd34a]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Merged with exec_order branch
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1:1db6efd34a714bf73ef9ad11442495d733c6e4cd
User & Date: onagano 2011-04-19 09:06:08
Context
2011-04-20
08:53
Consolidated check-in: c006007ae5 user: onagano tags: trunk
2011-04-19
09:06
Merged with exec_order branch check-in: 1db6efd34a user: onagano tags: trunk
08:49
Added the DLL file Closed-Leaf check-in: 0870b55101 user: onagano tags: exec_order
2011-04-09
04:42
Updated MegaDroid from v1.30 to v1.35 check-in: 0314994c4f user: onagano tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Added experts/MOB_executeOrder.mq4.

































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
//+------------------------------------------------------------------+
//|                                             MOB_executeOrder.mq4 |
//|                                                     Osamu Nagano |
//|                      https://profiles.google.com/onagano.g/about |
//+------------------------------------------------------------------+
#property copyright "Osamu Nagano"
#property link      "https://profiles.google.com/onagano.g/about"

#import "mt4odbcbridge.ex4"
void     mob_create();
int      mob_open(string dsn, string username, string password);
int      mob_close();
int      mob_isDead();
int		mob_commit();
int		mob_rollback();
int		mob_getAutoCommit();
int		mob_setAutoCommit(int autoCommit);
int      mob_execute(string sql);
int      mob_getLastErrorNo();
string   mob_getLastErrorMesg();
int      mob_registerStatement(string sql);
int      mob_unregisterStatement(int stmtId);
int		mob_bindIntParameter(int stmtId, int slot, int intp[]);
int		mob_bindDoubleParameter(int stmtId, int slot, double dblp[]);
int		mob_bindStringParameter(int stmtId, int slot, string strp);
int		mob_bindDatetimeParameter(int stmtId, int slot, datetime dtp[]);
int		mob_bindIntColumn(int stmtId, int slot, int intp[]);
int		mob_bindDoubleColumn(int stmtId, int slot, double dblp[]);
int		mob_bindStringColumn(int stmtId, int slot, string strp);
int		mob_bindDatetimeColumn(int stmtId, int slot, datetime dtp[]);
int      mob_executeStatement(int stmtId);
int      mob_selectToFetch(int stmtId);
int      mob_fetch(int stmtId);
int      mob_fetchedAll(int stmtId);
int      mob_insertTick(int stmtId, datetime dt, int fraction, double vals[]);
int      mob_insertBar(int stmtId, datetime dt, double vals[]);
int      mob_copyRates(int stmtId, double rates[][6], int size, int start, int end);
double   mob_selectDouble(string sql, double defaultVal);
int      mob_selectInt(string sql, int defaultVal);
datetime mob_selectDatetime(string sql, datetime defaultVal);
int      checkMOBError(int rc);
datetime mob_time();
int      mob_strcpy(string dest, int size, string src);
#import "stdlib.ex4"
string   ErrorDescription(int error_code);
#import

#define OPE_SEND           1
#define OPE_CLOSE          2
#define OPE_CLOSEBY        3
#define OPE_DELETE         4
#define OPE_MODIFY         5
#define OPE_DUMP_TRADES    6
#define OPE_DUMP_HISTORY   7
#define OPE_GET_ACCOUNT    8
#define OPE_GET_STATUS     9

#define ORD_DEFAULT_SLIPPAGE 3

//--- input parameters
extern string    ExtDataSourceName = "testpostgresql";
extern string    ExtUsername       = "";
extern string    ExtPassword       = "";
extern string    ExtTablePrefix    = "MOB_";

//--- global variables
string orderTable       = "ORDER";
int    selectOrderStmt  = 0;
int    updateOrderStmt  = 0;

int      ordId[1];
int      ordOperation[1];
int      ordTicket[1];
int      ordOppositeTicket[1];
string   ordSymbol = "A234567890";
int      ordSymbolSize;
int      ordType[1];
double   ordLots[1];
double   ordOpenPrice[1];
double   ordClosePrice[1];
int      ordSlippage[1];
double   ordStopLoss[1];
double   ordTakeProfit[1];
string   ordComment = "B2345678901234567890123456789012345678901234567890123456789012345678901234567890";
int      ordCommentSize;
int      ordMagicNumber[1];
datetime ordExpiration[1];
color    ordArrowColor[1];
int      ordErrorNumber[1];
string   ordErrorMessage = "C2345678901234567890123456789012345678901234567890123456789012345678901234567890";
int      ordErrorMessageSize;

string tradeTable          = "TRADE";
int    truncateTradeStmt   = 0;
int    insertTradeStmt     = 0;
string historyTable        = "HISTORY";
int    truncateHistoryStmt = 0;
int    insertHistoryStmt   = 0;
string statusTable         = "STATUS";
int    deleteStatusStmt    = 0;
int    insertStatusStmt    = 0;

int      trdTicket[1];
datetime trdOpenTime[1];
int      trdType[1];
double   trdLots[1];
string   trdSymbol = "D234567890";
int      trdSymbolSize;
double   trdOpenPrice[1];
double   trdStopLoss[1];
double   trdTakeProfit[1];
datetime trdCloseTime[1];
double   trdClosePrice[1];
double   trdCommission[1];
double   trdSwap[1];
double   trdProfit[1];
datetime trdExpiration[1];
int      trdMagicNumber[1];
string   trdComment = "E2345678901234567890123456789012345678901234567890123456789012345678901234567890";
int      trdCommentSize;

string accountTable     = "ACCOUNT";
int insertAccountStmt   = 0;

datetime accServerTime[1];
double   accBalance[1];
double   accEquity[1];
double   accMargin[1];
double   accFreeMargin[1];
double   accProfit[1];
int      accOpenTrades[1];
int      accClosedTrades[1];

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
   if (!IsDllsAllowed()) {
      Alert("ERROR: [Allow DLL imports] NOT Checked.");
      return (-1);
   }
//----
   mob_create();
   int rc;
   
   rc = checkMOBError(mob_open(ExtDataSourceName, ExtUsername, ExtPassword));
   if (rc < 0) return (rc);

   rc = checkMOBError(mob_setAutoCommit(true));
   if (rc < 0) return (rc);
   
   // Preparing order table
   
   orderTable = ExtTablePrefix + orderTable;
   rc = checkMOBError(createTableIfNotExists(orderTable, createOrderTableSQL(orderTable)));
   if (rc < 0) return (rc);

   // Regstering select order statement
   
   rc = checkMOBError(mob_registerStatement(selectOrderSQL(orderTable)));
   if (rc < 0) return (rc);
   selectOrderStmt = rc;

   int i = 1;
   mob_bindIntColumn       (selectOrderStmt, i, ordId);              i++;
   mob_bindIntColumn       (selectOrderStmt, i, ordOperation);       i++;
   mob_bindIntColumn       (selectOrderStmt, i, ordTicket);          i++;
   mob_bindIntColumn       (selectOrderStmt, i, ordOppositeTicket);  i++;
   mob_bindStringColumn    (selectOrderStmt, i, ordSymbol);          i++;
   mob_bindIntColumn       (selectOrderStmt, i, ordType);            i++;
   mob_bindDoubleColumn    (selectOrderStmt, i, ordLots);            i++;
   mob_bindDoubleColumn    (selectOrderStmt, i, ordOpenPrice);       i++;
   mob_bindDoubleColumn    (selectOrderStmt, i, ordClosePrice);      i++;
   mob_bindIntColumn       (selectOrderStmt, i, ordSlippage);        i++;
   mob_bindDoubleColumn    (selectOrderStmt, i, ordStopLoss);        i++;
   mob_bindDoubleColumn    (selectOrderStmt, i, ordTakeProfit);      i++;
   mob_bindStringColumn    (selectOrderStmt, i, ordComment);         i++;
   mob_bindIntColumn       (selectOrderStmt, i, ordMagicNumber);     i++;
   mob_bindDatetimeColumn  (selectOrderStmt, i, ordExpiration);      i++;
   mob_bindIntColumn       (selectOrderStmt, i, ordArrowColor);      i++;
   mob_bindIntColumn       (selectOrderStmt, i, ordErrorNumber);     i++;
   mob_bindStringColumn    (selectOrderStmt, i, ordErrorMessage);    i++;

   // Registering update order statement
   
   rc = checkMOBError(mob_registerStatement(updateOrderSQL(orderTable)));
   if (rc < 0) return (rc);
   updateOrderStmt = rc;

   i = 1;
   mob_bindIntParameter       (updateOrderStmt, i, ordOperation);       i++;
   mob_bindIntParameter       (updateOrderStmt, i, ordTicket);          i++;
   mob_bindIntParameter       (updateOrderStmt, i, ordOppositeTicket);  i++;
   mob_bindStringParameter    (updateOrderStmt, i, ordSymbol);          i++;
   mob_bindIntParameter       (updateOrderStmt, i, ordType);            i++;
   mob_bindDoubleParameter    (updateOrderStmt, i, ordLots);            i++;
   mob_bindDoubleParameter    (updateOrderStmt, i, ordOpenPrice);       i++;
   mob_bindDoubleParameter    (updateOrderStmt, i, ordClosePrice);      i++;
   mob_bindIntParameter       (updateOrderStmt, i, ordSlippage);        i++;
   mob_bindDoubleParameter    (updateOrderStmt, i, ordStopLoss);        i++;
   mob_bindDoubleParameter    (updateOrderStmt, i, ordTakeProfit);      i++;
   mob_bindStringParameter    (updateOrderStmt, i, ordComment);         i++;
   mob_bindIntParameter       (updateOrderStmt, i, ordMagicNumber);     i++;
   mob_bindDatetimeParameter  (updateOrderStmt, i, ordExpiration);      i++;
   mob_bindIntParameter       (updateOrderStmt, i, ordArrowColor);      i++;
   mob_bindIntParameter       (updateOrderStmt, i, ordErrorNumber);     i++;
   mob_bindStringParameter    (updateOrderStmt, i, ordErrorMessage);    i++;
   mob_bindIntParameter       (updateOrderStmt, i, ordId);              i++;
   
   ordSymbolSize = StringLen(ordSymbol);
   ordCommentSize = StringLen(ordComment);
   ordErrorMessageSize = StringLen(ordErrorMessage);

   // Preparing trade table
   
   tradeTable = ExtTablePrefix + tradeTable;
   rc = checkMOBError(createTableIfNotExists(tradeTable, createTradeTableSQL(tradeTable)));
   if (rc < 0) return (rc);

   // Registering truncate trade statement
   
   rc = checkMOBError(mob_registerStatement(truncateTradeSQL(tradeTable)));
   if (rc < 0) return (rc);
   truncateTradeStmt = rc;

   // Registering insert trade statement
   
   rc = checkMOBError(mob_registerStatement(insertTradeSQL(tradeTable)));
   if (rc < 0) return (rc);
   insertTradeStmt = rc;

   i = 1;
   mob_bindIntParameter       (insertTradeStmt, i, trdTicket);       i++;
   mob_bindDatetimeParameter  (insertTradeStmt, i, trdOpenTime);     i++;
   mob_bindIntParameter       (insertTradeStmt, i, trdType);         i++;
   mob_bindDoubleParameter    (insertTradeStmt, i, trdLots);         i++;
   mob_bindStringParameter    (insertTradeStmt, i, trdSymbol);       i++;
   mob_bindDoubleParameter    (insertTradeStmt, i, trdOpenPrice);    i++;
   mob_bindDoubleParameter    (insertTradeStmt, i, trdStopLoss);     i++;
   mob_bindDoubleParameter    (insertTradeStmt, i, trdTakeProfit);   i++;
   mob_bindDatetimeParameter  (insertTradeStmt, i, trdCloseTime);    i++;
   mob_bindDoubleParameter    (insertTradeStmt, i, trdClosePrice);   i++;
   mob_bindDoubleParameter    (insertTradeStmt, i, trdCommission);   i++;
   mob_bindDoubleParameter    (insertTradeStmt, i, trdSwap);         i++;
   mob_bindDoubleParameter    (insertTradeStmt, i, trdProfit);       i++;
   mob_bindDatetimeParameter  (insertTradeStmt, i, trdExpiration);   i++;
   mob_bindIntParameter       (insertTradeStmt, i, trdMagicNumber);  i++;
   mob_bindStringParameter    (insertTradeStmt, i, trdComment);      i++;

   // Preparing history table
   
   historyTable = ExtTablePrefix + historyTable;
   rc = checkMOBError(createTableIfNotExists(historyTable, createTradeTableSQL(historyTable)));
   if (rc < 0) return (rc);

   // Registering truncate history statement
   
   rc = checkMOBError(mob_registerStatement(truncateTradeSQL(historyTable)));
   if (rc < 0) return (rc);
   truncateHistoryStmt = rc;

   // Registering insert history statement
   
   rc = checkMOBError(mob_registerStatement(insertTradeSQL(historyTable)));
   if (rc < 0) return (rc);
   insertHistoryStmt = rc;

   i = 1;
   mob_bindIntParameter       (insertHistoryStmt, i, trdTicket);       i++;
   mob_bindDatetimeParameter  (insertHistoryStmt, i, trdOpenTime);     i++;
   mob_bindIntParameter       (insertHistoryStmt, i, trdType);         i++;
   mob_bindDoubleParameter    (insertHistoryStmt, i, trdLots);         i++;
   mob_bindStringParameter    (insertHistoryStmt, i, trdSymbol);       i++;
   mob_bindDoubleParameter    (insertHistoryStmt, i, trdOpenPrice);    i++;
   mob_bindDoubleParameter    (insertHistoryStmt, i, trdStopLoss);     i++;
   mob_bindDoubleParameter    (insertHistoryStmt, i, trdTakeProfit);   i++;
   mob_bindDatetimeParameter  (insertHistoryStmt, i, trdCloseTime);    i++;
   mob_bindDoubleParameter    (insertHistoryStmt, i, trdClosePrice);   i++;
   mob_bindDoubleParameter    (insertHistoryStmt, i, trdCommission);   i++;
   mob_bindDoubleParameter    (insertHistoryStmt, i, trdSwap);         i++;
   mob_bindDoubleParameter    (insertHistoryStmt, i, trdProfit);       i++;
   mob_bindDatetimeParameter  (insertHistoryStmt, i, trdExpiration);   i++;
   mob_bindIntParameter       (insertHistoryStmt, i, trdMagicNumber);  i++;
   mob_bindStringParameter    (insertHistoryStmt, i, trdComment);      i++;

   // Preparing status table

   statusTable = ExtTablePrefix + statusTable;
   rc = checkMOBError(createTableIfNotExists(statusTable, createTradeTableSQL(statusTable)));
   if (rc < 0) return (rc);

   // Registering delete status statement
   
   rc = checkMOBError(mob_registerStatement(deleteTradeSQL(statusTable)));
   if (rc < 0) return (rc);
   deleteStatusStmt = rc;

   i = 1;
   mob_bindIntParameter       (deleteStatusStmt, i, ordTicket);       i++;

   // Registering insert status statement
   
   rc = checkMOBError(mob_registerStatement(insertTradeSQL(statusTable)));
   if (rc < 0) return (rc);
   insertStatusStmt = rc;

   i = 1;
   mob_bindIntParameter       (insertStatusStmt, i, trdTicket);       i++;
   mob_bindDatetimeParameter  (insertStatusStmt, i, trdOpenTime);     i++;
   mob_bindIntParameter       (insertStatusStmt, i, trdType);         i++;
   mob_bindDoubleParameter    (insertStatusStmt, i, trdLots);         i++;
   mob_bindStringParameter    (insertStatusStmt, i, trdSymbol);       i++;
   mob_bindDoubleParameter    (insertStatusStmt, i, trdOpenPrice);    i++;
   mob_bindDoubleParameter    (insertStatusStmt, i, trdStopLoss);     i++;
   mob_bindDoubleParameter    (insertStatusStmt, i, trdTakeProfit);   i++;
   mob_bindDatetimeParameter  (insertStatusStmt, i, trdCloseTime);    i++;
   mob_bindDoubleParameter    (insertStatusStmt, i, trdClosePrice);   i++;
   mob_bindDoubleParameter    (insertStatusStmt, i, trdCommission);   i++;
   mob_bindDoubleParameter    (insertStatusStmt, i, trdSwap);         i++;
   mob_bindDoubleParameter    (insertStatusStmt, i, trdProfit);       i++;
   mob_bindDatetimeParameter  (insertStatusStmt, i, trdExpiration);   i++;
   mob_bindIntParameter       (insertStatusStmt, i, trdMagicNumber);  i++;
   mob_bindStringParameter    (insertStatusStmt, i, trdComment);      i++;

   trdSymbolSize = StringLen(trdSymbol);
   trdCommentSize = StringLen(trdComment);

   // Preparing account table
   
   accountTable = ExtTablePrefix + accountTable;
   rc = checkMOBError(createTableIfNotExists(accountTable, createAccountTableSQL(accountTable)));
   if (rc < 0) return (rc);

   // Registering insert account statement
   
   rc = checkMOBError(mob_registerStatement(insertAccountSQL(accountTable)));
   if (rc < 0) return (rc);
   insertAccountStmt = rc;

   i = 1;
   mob_bindDatetimeParameter  (insertAccountStmt, i, accServerTime);    i++;
   mob_bindDoubleParameter    (insertAccountStmt, i, accBalance);       i++;
   mob_bindDoubleParameter    (insertAccountStmt, i, accEquity);        i++;
   mob_bindDoubleParameter    (insertAccountStmt, i, accMargin);        i++;
   mob_bindDoubleParameter    (insertAccountStmt, i, accFreeMargin);    i++;
   mob_bindDoubleParameter    (insertAccountStmt, i, accProfit);        i++;
   mob_bindIntParameter       (insertAccountStmt, i, accOpenTrades);    i++;
   mob_bindIntParameter       (insertAccountStmt, i, accClosedTrades);  i++;
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   int rc = checkMOBError(mob_close());
   if (rc < 0) return (rc);
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
//----
   int rc = selectOrders();
   if (rc < 0) return (rc);
//----
   return(0);
  }
//+------------------------------------------------------------------+

int createTableIfNotExists(string tableName, string sql, bool drop = false) {
   int rc = 0;

   // MySQL has 'CREATE TABLE IF NOT EXISTS ...'
   // while PostgreSQL has 'DROP TABLE IF EXISTS ...'
 
   if (drop) rc = mob_execute("drop table " + tableName);

   rc = mob_selectInt("select count(*) from " + tableName, -1);
   bool exist = 0 <= rc;
   
   if (!exist) {
      rc = mob_execute(sql);
   }
   
   return (rc);
}

string createOrderTableSQL(string tableName) {
   // MySQL has AUTO_INCREMENT option at column definition,
   // but PostgreSQL needs SEQUENCE object...
   string seqName = tableName + "_ID_SEQ";
   string opeIndName = tableName + "_OPEIND";
   string sql = ""
      + "drop sequence if exists " + seqName + ";"
      + "create sequence " + seqName + ";"
      + "create table " + tableName + " ("
      + "  id integer default nextval('" + seqName + "') primary key"
      + ", operation integer default 0"
      + ", ticket integer default -1"
      + ", opposite_ticket integer default -1"
      + ", symbol varchar(10) default ''"
      + ", type integer default -1"
      + ", lots double precision default 0.0"
      + ", open_price double precision default 0.0"
      + ", close_price double precision default 0.0"
      + ", slippage integer default " + ORD_DEFAULT_SLIPPAGE
      + ", stop_loss double precision default 0.0"
      + ", take_profit double precision default 0.0"
      + ", comment varchar(140) default ''"
      + ", magic_number integer default 0"
      + ", expiration timestamp with time zone default '1970-01-01 00:00:00-00'"
      + ", arrow_color integer default " + CLR_NONE
      + ", error_number integer default 0"
      + ", error_message varchar(140) default ''"
      + ");"
      + "drop index if exists " + opeIndName + ";"
      + "create index " + opeIndName + " on " + tableName + " (operation)";
   return (sql);
}

string selectOrderSQL(string tableName) {
   string sql = "select"
      + "  id"
      + ", operation"
      + ", ticket"
      + ", opposite_ticket"
      + ", symbol"
      + ", type"
      + ", lots"
      + ", open_price"
      + ", close_price"
      + ", slippage"
      + ", stop_loss"
      + ", take_profit"
      + ", comment"
      + ", magic_number"
      + ", expiration"
      + ", arrow_color"
      + ", error_number"
      + ", error_message"
      + " from " + tableName + " where "
      + "operation > 0 order by id";
   return (sql);
}

int selectOrders() {
   int rc = 0;

   rc = checkMOBError(mob_selectToFetch(selectOrderStmt));
   while (checkMOBError(mob_fetch(selectOrderStmt)) != 0) {
   
      switch (ordOperation[0]) {
      case OPE_SEND:
         orderSend();
         break;
      case OPE_CLOSE:
         orderClose();
         break;
      case OPE_CLOSEBY:
         orderCloseBy();
         break;
      case OPE_DELETE:
         orderDelete();
         break;
      case OPE_MODIFY:
         orderModify();
         break;
      case OPE_DUMP_TRADES:
         dumpTradesInTransaction(false);
         break;
      case OPE_DUMP_HISTORY:
         dumpTradesInTransaction(true);
         break;
      case OPE_GET_ACCOUNT:
         insertAccount();
         break;
      case OPE_GET_STATUS:
         getStatusInTransaction();
         break;
      default:
         string mesg = "Unknown operation: " + ordOperation[0];
         Print(mesg);
         ordErrorNumber[0] = -1;
         mob_strcpy(ordErrorMessage, ordErrorMessageSize, mesg);
      }
      
      ordOperation[0] = - ordOperation[0];
      rc = checkMOBError(mob_executeStatement(updateOrderStmt));
   }
   checkMOBError(mob_fetchedAll(selectOrderStmt));
   
   return (rc);
}

string updateOrderSQL(string tableName) {
   string sql = "update " + tableName + " set"
      + "  operation = ?"
      + ", ticket = ?"
      + ", opposite_ticket = ?"
      + ", symbol = ?"
      + ", type = ?"
      + ", lots = ?"
      + ", open_price = ?"
      + ", close_price = ?"
      + ", slippage = ?"
      + ", stop_loss = ?"
      + ", take_profit = ?"
      + ", comment = ?"
      + ", magic_number = ?"
      + ", expiration = ?"
      + ", arrow_color = ?"
      + ", error_number = ?"
      + ", error_message = ?"
      + " where id = ?";
   return (sql);
}

void getServerResponse() {
   int err = GetLastError();
   string errMesg = ErrorDescription(err);
   ordErrorNumber[0] = err;
   mob_strcpy(ordErrorMessage, ordErrorMessageSize, errMesg);
   Print("#", err, ":", ordErrorMessage, "(", StringLen(ordErrorMessage), ")");
}

void orderSend() {
   double margin;
   double thePip    = getPipSize(ordSymbol);
   int    theDigits = MarketInfo(ordSymbol, MODE_DIGITS);
   
   // open at market price
   if (ordOpenPrice[0] == 0.0) {
      switch (ordType[0]) {
      case OP_BUY:
         ordOpenPrice[0] = MarketInfo(ordSymbol, MODE_ASK);
         break;
      case OP_SELL:
         ordOpenPrice[0] = MarketInfo(ordSymbol, MODE_BID);
         break;
      }
   }
   else if (ordOpenPrice[0] < 0.0) {
      // relative limit/stop open price
      margin = MathAbs(ordOpenPrice[0]) * thePip;
      switch (ordType[0]) {
      case OP_BUYLIMIT:
         ordOpenPrice[0] = NormalizeDouble(MarketInfo(ordSymbol, MODE_ASK) - margin, theDigits);
         break;
      case OP_SELLLIMIT:
         ordOpenPrice[0] = NormalizeDouble(MarketInfo(ordSymbol, MODE_BID) + margin, theDigits);
         break;
      case OP_BUYSTOP:
         ordOpenPrice[0] = NormalizeDouble(MarketInfo(ordSymbol, MODE_ASK) + margin, theDigits);
         break;
      case OP_SELLSTOP:
         ordOpenPrice[0] = NormalizeDouble(MarketInfo(ordSymbol, MODE_BID) - margin, theDigits);
         break;
      }
   }
   
   if (ordStopLoss[0] < 0.0) {
      // relative stop loss
      margin = MathAbs(ordStopLoss[0]) * thePip;
      switch (ordType[0]) {
      case OP_BUY:
      case OP_BUYLIMIT:
      case OP_BUYSTOP:
         ordStopLoss[0] = NormalizeDouble(ordOpenPrice[0] - margin, theDigits);
         break;
      case OP_SELL:
      case OP_SELLLIMIT:
      case OP_SELLSTOP:
         ordStopLoss[0] = NormalizeDouble(ordOpenPrice[0] + margin, theDigits);
         break;
      }
   }
   
   if (ordTakeProfit[0] < 0.0) {
      // relative take profit
      margin = MathAbs(ordTakeProfit[0]) * thePip;
      switch (ordType[0]) {
      case OP_BUY:
      case OP_BUYLIMIT:
      case OP_BUYSTOP:
         ordTakeProfit[0] = NormalizeDouble(ordOpenPrice[0] + margin, theDigits);
         break;
      case OP_SELL:
      case OP_SELLLIMIT:
      case OP_SELLSTOP:
         ordTakeProfit[0] = NormalizeDouble(ordOpenPrice[0] - margin, theDigits);
         break;
      }
   }
   
   Print("Sending ",
      ordSymbol, ", ",
      ordType[0], ", ",
      ordLots[0], ", ",
      ordOpenPrice[0], ", ",
      ordSlippage[0], ", ",
      ordStopLoss[0], ", ",
      ordTakeProfit[0], ", ",
      ordComment, ", ",
      ordMagicNumber[0], ", ",
      TimeToStr(ordExpiration[0]), ", ",
      ordArrowColor[0]
   );
   
   // ordering
   int ticket = OrderSend(
      ordSymbol,
      ordType[0],
      ordLots[0],
      ordOpenPrice[0],
      ordSlippage[0],
      ordStopLoss[0],
      ordTakeProfit[0],
      ordComment,
      ordMagicNumber[0],
      ordExpiration[0],
      ordArrowColor[0]
   );
   
   if (ticket == -1) {
      getServerResponse();
   } else {
      ordTicket[0] = ticket;
   }
}

void orderClose() {
   if (OrderSelect(ordTicket[0], SELECT_BY_TICKET)) {
      // close at market price
      if (ordClosePrice[0] == 0.0) {
         switch (OrderType()) {
         case OP_BUY:
            ordClosePrice[0] = MarketInfo(OrderSymbol(), MODE_BID);
            break;
         case OP_SELL:
            ordClosePrice[0] = MarketInfo(OrderSymbol(), MODE_ASK);
            break;
         }
      }
      // close all position
      if (ordLots[0] == 0.0) {
         ordLots[0] = OrderLots();
      }
      // closing
      if (OrderClose(
         ordTicket[0],
         ordLots[0],
         ordClosePrice[0],
         ordSlippage[0],
         ordArrowColor[0])) {
      } else {
         getServerResponse();
      }
   } else { // cannot select order
      getServerResponse();
   }
}

void orderCloseBy() {
   if (OrderCloseBy(
      ordTicket[0],
      ordOppositeTicket[0],
      ordArrowColor[0])) {
   } else {
      getServerResponse();
   }
}

void orderDelete() {
   if (OrderDelete(
      ordTicket[0],
      ordArrowColor[0])) {
   } else {
      getServerResponse();
   }
}

void orderModify() {
   if (OrderSelect(ordTicket[0], SELECT_BY_TICKET)) {
      double margin;
      double thePip    = getPipSize(OrderSymbol());
      int    theDigits = MarketInfo(OrderSymbol(), MODE_DIGITS);
      
      if (ordOpenPrice[0] == 0.0) {
         ordOpenPrice[0] = OrderOpenPrice();
      }
      else if (ordOpenPrice[0] < 0.0) {
         // relative open price
         margin = MathAbs(ordOpenPrice[0]) * thePip;
         switch (OrderType()) {
         case OP_BUYLIMIT:
            ordOpenPrice[0] = NormalizeDouble(MarketInfo(OrderSymbol(), MODE_ASK) - margin, theDigits);
            break;
         case OP_SELLLIMIT:
            ordOpenPrice[0] = NormalizeDouble(MarketInfo(OrderSymbol(), MODE_BID) + margin, theDigits);
            break;
         case OP_BUYSTOP:
            ordOpenPrice[0] = NormalizeDouble(MarketInfo(OrderSymbol(), MODE_ASK) + margin, theDigits);
            break;
         case OP_SELLSTOP:
            ordOpenPrice[0] = NormalizeDouble(MarketInfo(OrderSymbol(), MODE_BID) - margin, theDigits);
            break;
         default:
            ordOpenPrice[0] = OrderOpenPrice();
         }
      }
      
      if (ordStopLoss[0] == 0.0) {
         ordStopLoss[0] = OrderStopLoss();
      }
      else if (ordStopLoss[0] < 0.0) {
         // relative stop loss
         margin = MathAbs(ordStopLoss[0]) * thePip;
         switch (OrderType()) {
         case OP_BUY:
            ordStopLoss[0] = NormalizeDouble(MarketInfo(OrderSymbol(), MODE_BID) - margin, theDigits);
            break;
         case OP_BUYLIMIT:
         case OP_BUYSTOP:
            ordStopLoss[0] = NormalizeDouble(ordOpenPrice[0] - margin, theDigits);
            break;
         case OP_SELL:
            ordStopLoss[0] = NormalizeDouble(MarketInfo(OrderSymbol(), MODE_ASK) + margin, theDigits);
            break;
         case OP_SELLLIMIT:
         case OP_SELLSTOP:
            ordStopLoss[0] = NormalizeDouble(ordOpenPrice[0] + margin, theDigits);
            break;
         }
      }
      
      if (ordTakeProfit[0] == 0.0) {
         ordTakeProfit[0] = OrderTakeProfit();
      }
      else if (ordTakeProfit[0] < 0.0) {
         // relative take profit
         margin = MathAbs(ordTakeProfit[0]) * thePip;
         switch (OrderType()) {
         case OP_BUY:
            ordTakeProfit[0] = NormalizeDouble(MarketInfo(OrderSymbol(), MODE_BID) + margin, theDigits);
            break;
         case OP_BUYLIMIT:
         case OP_BUYSTOP:
            ordTakeProfit[0] = NormalizeDouble(ordOpenPrice[0] + margin, theDigits);
            break;
         case OP_SELL:
            ordTakeProfit[0] = NormalizeDouble(MarketInfo(OrderSymbol(), MODE_ASK) - margin, theDigits);
            break;
         case OP_SELLLIMIT:
         case OP_SELLSTOP:
            ordTakeProfit[0] = NormalizeDouble(ordOpenPrice[0] - margin, theDigits);
            break;
         }
      }
      
      if (ordExpiration[0] == 0) {
         ordExpiration[0] = OrderExpiration();
      }
      
      Print("Modifying ",
         ordTicket[0], ", ",
         ordOpenPrice[0], ", ",
         ordStopLoss[0], ", ",
         ordTakeProfit[0], ", ",
         TimeToStr(ordExpiration[0]), ", ",
         ordArrowColor[0]
      );
      
      // modifying
      if (OrderModify(
         ordTicket[0],
         ordOpenPrice[0],
         ordStopLoss[0],
         ordTakeProfit[0],
         ordExpiration[0],
         ordArrowColor[0])) {
      } else {
         getServerResponse();
      }
   } else { // cannot select order
      getServerResponse();
   }
}

string createTradeTableSQL(string tableName) {
   string sql = "create table " + tableName + " ("
      + "  ticket integer primary key"
      + ", open_time timestamp with time zone default '1970-01-01 00:00:00-00'"
      + ", type integer default -1"
      + ", lots double precision default 0.0"
      + ", symbol varchar(10) default ''"
      + ", open_price double precision default 0.0"
      + ", stop_loss double precision default 0.0"
      + ", take_profit double precision default 0.0"
      + ", close_time timestamp with time zone default '1970-01-01 00:00:00-00'"
      + ", close_price double precision default 0.0"
      + ", commission double precision default 0.0"
      + ", swap double precision default 0.0"
      + ", profit double precision default 0.0"
      + ", expiration timestamp with time zone default '1970-01-01 00:00:00-00'"
      + ", magic_number integer default 0"
      + ", comment varchar(140) default ''"
      + ")";
   return (sql);
}

string truncateTradeSQL(string tableName) {
   string sql = "truncate table " + tableName;
   return (sql);
}

string deleteTradeSQL(string tableName) {
   string sql = "delete from " + tableName + " where ticket = ?";
   return (sql);
}

string insertTradeSQL(string tableName) {
   string sql = "insert into " + tableName + " ("
      + "  ticket"
      + ", open_time"
      + ", type"
      + ", lots"
      + ", symbol"
      + ", open_price"
      + ", stop_loss"
      + ", take_profit"
      + ", close_time"
      + ", close_price"
      + ", commission"
      + ", swap"
      + ", profit"
      + ", expiration"
      + ", magic_number"
      + ", comment"
      + ") values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
   return (sql);
}

void bindTradeParameters() {
   trdTicket[0]      = OrderTicket();
   trdOpenTime[0]    = OrderOpenTime();
   trdType[0]        = OrderType();
   trdLots[0]        = OrderLots();
   mob_strcpy(trdSymbol, trdSymbolSize, OrderSymbol());
   trdOpenPrice[0]   = OrderOpenPrice();
   trdStopLoss[0]    = OrderStopLoss();
   trdTakeProfit[0]  = OrderTakeProfit();
   trdCloseTime[0]   = OrderCloseTime();
   trdClosePrice[0]  = OrderClosePrice();
   trdCommission[0]  = OrderCommission();
   trdSwap[0]        = OrderSwap();
   trdProfit[0]      = OrderProfit();
   trdExpiration[0]  = OrderExpiration();
   trdMagicNumber[0] = OrderMagicNumber();
   mob_strcpy(trdComment, trdCommentSize, OrderComment());
}

int dumpTrades(bool history) {
   int rc = 0;
   int truncateStmt;
   int insertStmt;
   int n;
   int pool;

   if (history) {
      truncateStmt = truncateHistoryStmt;
      insertStmt = insertHistoryStmt;
      n = OrdersHistoryTotal();
      pool = MODE_HISTORY;
   } else {
      truncateStmt = truncateTradeStmt;
      insertStmt = insertTradeStmt;
      n = OrdersTotal();
      pool = MODE_TRADES;
   }

   rc = checkMOBError(mob_executeStatement(truncateStmt));
   if (rc < 0) {
      getDatabaseResponse();
      return (rc);
   }
   for (int i = 0; i < n; i++) {
      if (OrderSelect(i, SELECT_BY_POS, pool)) {
         bindTradeParameters();
         rc = checkMOBError(mob_executeStatement(insertStmt));
         if (rc < 0) {
            getDatabaseResponse();
            return (rc);
         }
      } else {
         getServerResponse();
      }
   }
   
   return (rc);
}

void getDatabaseResponse() {
   int err = mob_getLastErrorNo();
   string errMesg = mob_getLastErrorMesg();
   ordErrorNumber[0] = err;
   mob_strcpy(ordErrorMessage, ordErrorMessageSize, errMesg);
   //Print("#", err, ":", ordErrorMessage, "(", StringLen(ordErrorMessage), ")");
}

void dumpTradesInTransaction(bool history) {
   mob_setAutoCommit(false);
   int rc = dumpTrades(history);
   if (rc < 0) {
      mob_rollback();
   } else {
      mob_commit();
   }
   mob_setAutoCommit(true);
}

void getStatusInTransaction() {
   mob_setAutoCommit(false);
   int rc = getStatus();
   if (rc < 0) {
      mob_rollback();
   } else {
      mob_commit();
   }
   mob_setAutoCommit(true);
}

int getStatus() {
   int rc = 0;
   rc = checkMOBError(mob_executeStatement(deleteStatusStmt));
   if (rc < 0) {
      getDatabaseResponse();
      return (rc);
   }
   if (OrderSelect(ordTicket[0], SELECT_BY_TICKET)) {
      bindTradeParameters();
      rc = checkMOBError(mob_executeStatement(insertStatusStmt));
      if (rc < 0) {
         getDatabaseResponse();
         return (rc);
      }
   } else {
      getServerResponse();
   }
   return (rc);
}

string createAccountTableSQL(string tableName) {
   string seqName = tableName + "_ID_SEQ";
   string sql = ""
      + "drop sequence if exists " + seqName + ";"
      + "create sequence " + seqName + ";"
      + "create table " + tableName + " ("
      + "  id integer default nextval('" + seqName + "') primary key"
      + ", server_time timestamp with time zone default '1970-01-01 00:00:00-00'"
      + ", balance double precision"
      + ", equity double precision"
      + ", margin double precision"
      + ", free_margin double precision"
      + ", profit double precision"
      + ", open_trades integer"
      + ", closed_trades integer"
      + ")";
   return (sql);
}

string insertAccountSQL(string tableName) {
   string sql = "insert into " + tableName + " ("
      + "  server_time"
      + ", balance"
      + ", equity"
      + ", margin"
      + ", free_margin"
      + ", profit"
      + ", open_trades"
      + ", closed_trades"
      + ") values (?, ?, ?, ?, ?, ?, ?, ?)";
   return (sql);
}

void insertAccount() {
   accServerTime[0]     = TimeCurrent();
   accBalance[0]        = AccountBalance();
   accEquity[0]         = AccountEquity();
   accMargin[0]         = AccountMargin();
   accFreeMargin[0]     = AccountFreeMargin();
   accProfit[0]         = AccountProfit();
   accOpenTrades[0]     = OrdersTotal();
   accClosedTrades[0]   = OrdersHistoryTotal();

   int rc = checkMOBError(mob_executeStatement(insertAccountStmt));
   if (rc < 0) {
      getDatabaseResponse();
   }
}

double getPipSize(string symbol) {
   double pip    = 0.0;
   double point  = MarketInfo(symbol, MODE_POINT);
   int    digits = MarketInfo(symbol, MODE_DIGITS);
   switch (digits) {
      case 5:
      case 3:
         pip = NormalizeDouble(point * 10, digits);
         break;
      case 4:
      case 2:
         pip = NormalizeDouble(point, digits);
         break;
   }
   return (pip);
}

Changes to experts/MOB_insertData.mq4.

31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
string   getPeriodSymbol(int period_XX);
int      createTableIfNotExists(string tableName, bool isBar = true);
string   insertSQL(string tableName, bool isBar = true);
datetime mob_time();
#import

//---- input parameters
extern string    ExtDataSourceName = "fxddx";
extern string    ExtUsername       = "";
extern string    ExtPassword       = "";
extern string    ExtTablePrefix    = "MOB_";

int      insertTick;
int      insertBar;
datetime lastTickTime;







|







31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
string   getPeriodSymbol(int period_XX);
int      createTableIfNotExists(string tableName, bool isBar = true);
string   insertSQL(string tableName, bool isBar = true);
datetime mob_time();
#import

//---- input parameters
extern string    ExtDataSourceName = "testpostgresql";
extern string    ExtUsername       = "";
extern string    ExtPassword       = "";
extern string    ExtTablePrefix    = "MOB_";

int      insertTick;
int      insertBar;
datetime lastTickTime;

Changes to experts/libraries/MT4ODBCBridge.dll.

cannot compute difference between binary files

Changes to experts/libraries/mt4odbcbridge.mq4.

27
28
29
30
31
32
33












34
35
36
37
38
39
40


41
42
43
44
45
46
47
...
103
104
105
106
107
108
109
















































110
111
112
113
114
115
116
...
131
132
133
134
135
136
137









138
139
140
141
142
143
144
int		MOB_getAutoCommit(int conId);
int		MOB_setAutoCommit(int conId, int autoCommit);
int      MOB_execute(int conId, string sql);
int      MOB_getLastErrorNo(int conId);
string   MOB_getLastErrorMesg(int conId);
int      MOB_registerStatement(int conId, string sql);
int      MOB_unregisterStatement(int conId, int stmtId);












int      MOB_insertTick(int conId, int stmtId, datetime dt, int fraction, double vals[], int size);
int      MOB_insertBar(int conId, int stmtId, datetime dt, double vals[], int size);
int      MOB_copyRates(int conId, int stmtId, double rates[][6], int size, int start, int end);
double   MOB_selectDouble(int conId, string sql, double defaultVal);
int      MOB_selectInt(int conId, string sql, int defaultVal);
datetime MOB_selectDatetime(int conId, string sql, datetime defaultVal);
datetime MOB_time();


#import

//+------------------------------------------------------------------+
//| EX4 imports                                                      |
//+------------------------------------------------------------------+
#import "stdlib.ex4"
   string ErrorDescription(int error_code);
................................................................................
int mob_registerStatement(string sql) {
   return (MOB_registerStatement(mob_conId, sql));
}

int mob_unregisterStatement(int stmtId) {
   return (MOB_unregisterStatement(mob_conId, stmtId));
}

















































int mob_insertTick(int stmtId, datetime dt, int fraction, double vals[]) {
   return (MOB_insertTick(mob_conId, stmtId, dt, fraction, vals, ArraySize(vals)));
}

int mob_insertBar(int stmtId, datetime dt, double vals[]) {
   return (MOB_insertBar(mob_conId, stmtId, dt, vals, ArraySize(vals)));
................................................................................
datetime mob_selectDatetime(string sql, datetime defaultVal) {
   return (MOB_selectDatetime(mob_conId, sql, defaultVal));
}

datetime mob_time() {
   return (MOB_time());
}










//+------------------------------------------------------------------+
//| Stateful helper functions                                        |
//+------------------------------------------------------------------+
int checkMOBError(int rc, bool rollback = false, bool disconnect = false) {
   if (rc < 0) {
      int eno = mob_getLastErrorNo();







>
>
>
>
>
>
>
>
>
>
>
>







>
>







 







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







 







>
>
>
>
>
>
>
>
>







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
...
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
...
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
int		MOB_getAutoCommit(int conId);
int		MOB_setAutoCommit(int conId, int autoCommit);
int      MOB_execute(int conId, string sql);
int      MOB_getLastErrorNo(int conId);
string   MOB_getLastErrorMesg(int conId);
int      MOB_registerStatement(int conId, string sql);
int      MOB_unregisterStatement(int conId, int stmtId);
int		MOB_bindIntParameter(int conId, int stmtId, int slot, int intp[]);
int		MOB_bindDoubleParameter(int conId, int stmtId, int slot, double dblp[]);
int		MOB_bindStringParameter(int conId, int stmtId, int slot, string strp);
int		MOB_bindDatetimeParameter(int conId, int stmtId, int slot, datetime dtp[]);
int		MOB_bindIntColumn(int conId, int stmtId, int slot, int intp[]);
int		MOB_bindDoubleColumn(int conId, int stmtId, int slot, double dblp[]);
int		MOB_bindStringColumn(int conId, int stmtId, int slot, string strp);
int		MOB_bindDatetimeColumn(int conId, int stmtId, int slot, datetime dtp[]);
int      MOB_executeStatement(int conId, int stmtId);
int      MOB_selectToFetch(int conId, int stmtId);
int      MOB_fetch(int conId, int stmtId);
int      MOB_fetchedAll(int conId, int stmtId);
int      MOB_insertTick(int conId, int stmtId, datetime dt, int fraction, double vals[], int size);
int      MOB_insertBar(int conId, int stmtId, datetime dt, double vals[], int size);
int      MOB_copyRates(int conId, int stmtId, double rates[][6], int size, int start, int end);
double   MOB_selectDouble(int conId, string sql, double defaultVal);
int      MOB_selectInt(int conId, string sql, int defaultVal);
datetime MOB_selectDatetime(int conId, string sql, datetime defaultVal);
datetime MOB_time();
datetime MOB_localtime();
int      MOB_strcpy(string dest, int size, string src);
#import

//+------------------------------------------------------------------+
//| EX4 imports                                                      |
//+------------------------------------------------------------------+
#import "stdlib.ex4"
   string ErrorDescription(int error_code);
................................................................................
int mob_registerStatement(string sql) {
   return (MOB_registerStatement(mob_conId, sql));
}

int mob_unregisterStatement(int stmtId) {
   return (MOB_unregisterStatement(mob_conId, stmtId));
}

int mob_bindIntParameter(int stmtId, int stmtId, int slot, int intp[]) {
   return (MOB_bindIntParameter(mob_conId, stmtId, slot, intp));
}

int mob_bindDoubleParameter(int stmtId, int stmtId, int slot, double dblp[]) {
   return (MOB_bindDoubleParameter(mob_conId, stmtId, slot, dblp));
}

int mob_bindStringParameter(int stmtId, int stmtId, int slot, string strp) {
   return (MOB_bindStringParameter(mob_conId, stmtId, slot, strp));
}

int mob_bindDatetimeParameter(int stmtId, int stmtId, int slot, datetime dtp[]) {
   return (MOB_bindDatetimeParameter(mob_conId, stmtId, slot, dtp));
}

int mob_bindIntColumn(int stmtId, int stmtId, int slot, int intp[]) {
   return (MOB_bindIntColumn(mob_conId, stmtId, slot, intp));
}

int mob_bindDoubleColumn(int stmtId, int stmtId, int slot, double dblp[]) {
   return (MOB_bindDoubleColumn(mob_conId, stmtId, slot, dblp));
}

int mob_bindStringColumn(int stmtId, int stmtId, int slot, string strp) {
   return (MOB_bindStringColumn(mob_conId, stmtId, slot, strp));
}

int mob_bindDatetimeColumn(int stmtId, int stmtId, int slot, datetime dtp[]) {
   return (MOB_bindDatetimeColumn(mob_conId, stmtId, slot, dtp));
}

int mob_executeStatement(int stmtId) {
   return (MOB_executeStatement(mob_conId, stmtId));
}

int mob_selectToFetch(int stmtId) {
   return (MOB_selectToFetch(mob_conId, stmtId));
}

int mob_fetch(int stmtId) {
   return (MOB_fetch(mob_conId, stmtId));
}

int mob_fetchedAll(int stmtId) {
   return (MOB_fetchedAll(mob_conId, stmtId));
}

int mob_insertTick(int stmtId, datetime dt, int fraction, double vals[]) {
   return (MOB_insertTick(mob_conId, stmtId, dt, fraction, vals, ArraySize(vals)));
}

int mob_insertBar(int stmtId, datetime dt, double vals[]) {
   return (MOB_insertBar(mob_conId, stmtId, dt, vals, ArraySize(vals)));
................................................................................
datetime mob_selectDatetime(string sql, datetime defaultVal) {
   return (MOB_selectDatetime(mob_conId, sql, defaultVal));
}

datetime mob_time() {
   return (MOB_time());
}

datetime mob_localtime() {
   return (MOB_localtime());
}

int mob_strcpy(string dest, int size, string src) {
   return (MOB_strcpy(dest, size, src));
}


//+------------------------------------------------------------------+
//| Stateful helper functions                                        |
//+------------------------------------------------------------------+
int checkMOBError(int rc, bool rollback = false, bool disconnect = false) {
   if (rc < 0) {
      int eno = mob_getLastErrorNo();

Changes to experts/scripts/MOB_copyRates.mq4.

20
21
22
23
24
25
26
27
28
29
30
31
32
      Alert("ERROR: [Allow DLL imports] NOT Checked.");
      return (-1);
   }
//----
   int rc = 0;
   string tableName = getDefaultTableName();
   
   rc = copyRates("fxddx", NULL, NULL, tableName);
//----
   return(rc);
  }
//+------------------------------------------------------------------+








|





20
21
22
23
24
25
26
27
28
29
30
31
32
      Alert("ERROR: [Allow DLL imports] NOT Checked.");
      return (-1);
   }
//----
   int rc = 0;
   string tableName = getDefaultTableName();
   
   rc = copyRates("testpostgresql", NULL, NULL, tableName);
//----
   return(rc);
  }
//+------------------------------------------------------------------+

Added experts/scripts/testAccounts.mq4.

























































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
//+------------------------------------------------------------------+
//|                                                 testAccounts.mq4 |
//|                                                     Osamu Nagano |
//|                      https://profiles.google.com/onagano.g/about |
//+------------------------------------------------------------------+
#property copyright "Osamu Nagano"
#property link      "https://profiles.google.com/onagano.g/about"

//+------------------------------------------------------------------+
//| script program start function                                    |
//+------------------------------------------------------------------+
int start()
  {
//----
   string sym = Symbol();
   
   Print("=Account=");
   Print("Balance: ", AccountBalance());
   Print("Credit: ", AccountCredit());
   Print("Company: ", AccountCompany());
   Print("Currency: ", AccountCurrency());
   Print("Equity: ", AccountEquity());
   Print("FreeMargin: ", AccountFreeMargin());
   Print("FreeMarginCheck: ", AccountFreeMarginCheck(sym, OP_BUY, 1.0));
   Print("FreeMarginMode: ", AccountFreeMarginMode());
   Print("Leverage: ", AccountLeverage());
   Print("Margin: ", AccountMargin());
   Print("Name: ", AccountName());
   Print("Number: ", AccountNumber());
   Print("Profit: ", AccountProfit());
   Print("Server: ", AccountServer());
   Print("StopoutLevel: ", AccountStopoutLevel());
   Print("StopoutMode: ", AccountStopoutMode());
   
   Print("=Terminal=");
   Print("Company: ", TerminalCompany());
   Print("Name: ", TerminalName());
   Print("Path: ", TerminalPath());
   
   Print("=Trading=");
   Print("HistoryTotal: ", OrdersHistoryTotal());
   Print("Total: ", OrdersTotal());
   
   Print("=MarketInfo=");
   Print("MODE_LOW: ", MarketInfo(sym, MODE_LOW));
   Print("MODE_HIGH: ", MarketInfo(sym, MODE_HIGH));
   Print("MODE_TIME: ", MarketInfo(sym, MODE_TIME));
   Print("MODE_BID: ", MarketInfo(sym, MODE_BID));
   Print("MODE_ASK: ", MarketInfo(sym, MODE_ASK));
   Print("MODE_POINT: ", MarketInfo(sym, MODE_POINT));
   Print("MODE_DIGITS: ", MarketInfo(sym, MODE_DIGITS));
   Print("MODE_SPREAD: ", MarketInfo(sym, MODE_SPREAD));
   Print("MODE_STOPLEVEL: ", MarketInfo(sym, MODE_STOPLEVEL));
   Print("MODE_LOTSIZE: ", MarketInfo(sym, MODE_LOTSIZE));
   Print("MODE_TICKVALUE: ", MarketInfo(sym, MODE_TICKVALUE));
   Print("MODE_TICKSIZE: ", MarketInfo(sym, MODE_TICKSIZE));
   Print("MODE_SWAPLONG: ", MarketInfo(sym, MODE_SWAPLONG));
   Print("MODE_SWAPSHORT: ", MarketInfo(sym, MODE_SWAPSHORT));
   Print("MODE_STARTING: ", MarketInfo(sym, MODE_STARTING));
   Print("MODE_EXPIRATION: ", MarketInfo(sym, MODE_EXPIRATION));
   Print("MODE_TRADEALLOWED: ", MarketInfo(sym, MODE_TRADEALLOWED));
   Print("MODE_MINLOT: ", MarketInfo(sym, MODE_MINLOT));
   Print("MODE_LOTSTEP: ", MarketInfo(sym, MODE_LOTSTEP));
   Print("MODE_MAXLOT: ", MarketInfo(sym, MODE_MAXLOT));
   Print("MODE_SWAPTYPE: ", MarketInfo(sym, MODE_SWAPTYPE));
   Print("MODE_PROFITCALCMODE: ", MarketInfo(sym, MODE_PROFITCALCMODE));
   Print("MODE_MARGINCALCMODE: ", MarketInfo(sym, MODE_MARGINCALCMODE));
   Print("MODE_MARGININIT: ", MarketInfo(sym, MODE_MARGININIT));
   Print("MODE_MARGINMAINTENANCE: ", MarketInfo(sym, MODE_MARGINMAINTENANCE));
   Print("MODE_MARGINHEDGED: ", MarketInfo(sym, MODE_MARGINHEDGED));
   Print("MODE_MARGINREQUIRED: ", MarketInfo(sym, MODE_MARGINREQUIRED));
   Print("MODE_FREEZELEVEL: ", MarketInfo(sym, MODE_FREEZELEVEL));
//----
   return(0);
  }
//+------------------------------------------------------------------+

Changes to experts/scripts/testMT4ODBCBridge.mq4.

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
string   insertTickSQL(string tableName);
int      checkMOBError(int rc);
int      createTableIfNotExists(string tableName);
int      timeDifference(string dsn, string username, string password);
string   getDefaultTableName();
string   getPeriodSymbol(int period_XX);
int      copyRates(string dsn, string username, string password, string tableName);



#import

//+------------------------------------------------------------------+
//| script program start function                                    |
//+------------------------------------------------------------------+
int start()
  {
   int rc = 0;

   string tableName = getDefaultTableName();
   
//   rc = copyRates("testpostgresql", "testuser", "password", tableName);
   rc = copyRates("testpostgresql", NULL, NULL, tableName);
   


   






   int timeDiff = timeDifference("testpostgresql", "testuser", "password");
   Print("Time diff is ", timeDiff);
   
   mob_create();
   mob_open("testpostgresql", "testuser", "password");
   datetime dtMax = mob_selectDatetime("select max(time) from " + tableName, -1);
   datetime dtMin = mob_selectDatetime("select min(time) from " + tableName, -1);
   mob_close();

   Print("Max date selected ", TimeToStr(dtMax));
   Print("Min date selected ", TimeToStr(dtMin));
   







>
>
>








>


<
|

>
>

>
>
>
>
>
>
|



|







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
string   insertTickSQL(string tableName);
int      checkMOBError(int rc);
int      createTableIfNotExists(string tableName);
int      timeDifference(string dsn, string username, string password);
string   getDefaultTableName();
string   getPeriodSymbol(int period_XX);
int      copyRates(string dsn, string username, string password, string tableName);
datetime mob_time();
datetime mob_localtime();
int      mob_strcpy(string dest, int size, string src);
#import

//+------------------------------------------------------------------+
//| script program start function                                    |
//+------------------------------------------------------------------+
int start()
  {
   int rc = 0;
   string dsName = "testpostgresql";
   string tableName = getDefaultTableName();
   

   rc = copyRates(dsName, NULL, NULL, tableName);
   
   datetime serverTime = TimeCurrent();
   Print("Server time is ", TimeToStr(serverTime));
   
   datetime nativeTime = mob_time();
   Print("Native time is ", TimeToStr(nativeTime));
   
   datetime localTime = mob_localtime();
   Print("Local time is ", TimeToStr(localTime));

   int timeDiff = timeDifference(dsName, NULL, NULL);
   Print("Time diff is ", timeDiff);
   
   mob_create();
   mob_open(dsName, NULL, NULL);
   datetime dtMax = mob_selectDatetime("select max(time) from " + tableName, -1);
   datetime dtMin = mob_selectDatetime("select min(time) from " + tableName, -1);
   mob_close();

   Print("Max date selected ", TimeToStr(dtMax));
   Print("Min date selected ", TimeToStr(dtMin));