forked from vdemydiuk/mtapi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMtApiClient.cs
executable file
·3187 lines (2791 loc) · 142 KB
/
MtApiClient.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using MTApiService;
using System.Drawing;
using System.Collections;
using System.ServiceModel;
using MtApi.Requests;
using MtApi.Responses;
using Newtonsoft.Json;
using System.Threading.Tasks;
using MtApi.Events;
namespace MtApi
{
public delegate void MtApiQuoteHandler(object sender, string symbol, double bid, double ask);
public sealed class MtApiClient
{
#region MetaTrader Constants
//Special constant
public const int NULL = 0;
public const int EMPTY = -1;
private const string LogProfileName = "MtApiClient";
#endregion
#region Private Fields
private static readonly MtLog Log = LogConfigurator.GetLogger(typeof(MtApiClient));
private MtClient _client;
private readonly object _locker = new object();
private MtConnectionState _connectionState = MtConnectionState.Disconnected;
private volatile bool _isBacktestingMode;
private int _executorHandle;
#endregion
#region ctor
public MtApiClient()
{
#if (DEBUG)
const LogLevel logLevel = LogLevel.Debug;
#else
const LogLevel logLevel = LogLevel.Info;
#endif
LogConfigurator.Setup(LogProfileName, logLevel);
}
#endregion
#region Public Methods
///<summary>
///Connect with MetaTrader API. Async method.
///</summary>
///<param name="host">Address of MetaTrader host (ex. 192.168.1.2)</param>
///<param name="port">Port of host connection (default 8222) </param>
public void BeginConnect(string host, int port)
{
Log.Info($"BeginConnect: host = {host}, port = {port}");
Task.Factory.StartNew(() => Connect(host, port));
}
///<summary>
///Connect with MetaTrader API. Async method.
///</summary>
///<param name="port">Port of host connection (default 8222) </param>
public void BeginConnect(int port)
{
Log.Info($"BeginConnect: port = {port}");
Task.Factory.StartNew(() => Connect(port));
}
///<summary>
///Disconnect from MetaTrader API. Async method.
///</summary>
public void BeginDisconnect()
{
Log.Info("BeginDisconnect called.");
Task.Factory.StartNew(() => Disconnect(false));
}
///<summary>
///Load quotes connected into MetaTrader API.
///</summary>
public List<MtQuote> GetQuotes()
{
var client = Client;
var quotes = client != null ? client.GetQuotes() : null;
return quotes?.Select(q => new MtQuote(q)).ToList();
}
#endregion
#region Properties
///<summary>
///Connection status of MetaTrader API.
///</summary>
public MtConnectionState ConnectionState
{
get
{
lock (_locker)
{
return _connectionState;
}
}
}
///<summary>
///Handle of expert used to execute commands
///</summary>
public int ExecutorHandle
{
get
{
lock (_locker)
{
return _executorHandle;
}
}
set
{
lock (_locker)
{
_executorHandle = value;
}
}
}
#endregion
#region Deprecated Methods
[Obsolete("OrderCloseByCurrentPrice is deprecated, please use OrderClose instead.")]
public bool OrderCloseByCurrentPrice(int ticket, int slippage)
{
return OrderClose(ticket, slippage);
}
[Obsolete("OrderClosePrice is deprecated, please use GetOrder instead.")]
public double OrderClosePrice()
{
return SendCommand<double>(MtCommandType.OrderClosePrice, null);
}
[Obsolete("OrderClosePrice is deprecated, please use GetOrder instead.")]
public double OrderClosePrice(int ticket)
{
var commandParameters = new ArrayList { ticket };
double retVal = SendCommand<double>(MtCommandType.OrderClosePriceByTicket, commandParameters);
return retVal;
}
[Obsolete("OrderCloseTime is deprecated, please use GetOrder instead.")]
public DateTime OrderCloseTime()
{
var commandResponse = SendCommand<int>(MtCommandType.OrderCloseTime, null);
return MtApiTimeConverter.ConvertFromMtTime(commandResponse);
}
[Obsolete("OrderComment is deprecated, please use GetOrder instead.")]
public string OrderComment()
{
return SendCommand<string>(MtCommandType.OrderComment, null);
}
[Obsolete("OrderCommission is deprecated, please use GetOrder instead.")]
public double OrderCommission()
{
return SendCommand<double>(MtCommandType.OrderCommission, null);
}
[Obsolete("OrderExpiration is deprecated, please use GetOrder instead.")]
public DateTime OrderExpiration()
{
var commandResponse = SendCommand<int>(MtCommandType.OrderExpiration, null);
return MtApiTimeConverter.ConvertFromMtTime(commandResponse);
}
[Obsolete("OrderLots is deprecated, please use GetOrder instead.")]
public double OrderLots()
{
return SendCommand<double>(MtCommandType.OrderLots, null);
}
[Obsolete("OrderMagicNumber is deprecated, please use GetOrder instead.")]
public int OrderMagicNumber()
{
return SendCommand<int>(MtCommandType.OrderMagicNumber, null);
}
[Obsolete("OrderOpenPrice is deprecated, please use GetOrder instead.")]
public double OrderOpenPrice()
{
return SendCommand<double>(MtCommandType.OrderOpenPrice, null);
}
[Obsolete("OrderOpenPrice is deprecated, please use GetOrder instead.")]
public double OrderOpenPrice(int ticket)
{
var commandParameters = new ArrayList { ticket };
var retVal = SendCommand<double>(MtCommandType.OrderOpenPriceByTicket, commandParameters);
return retVal;
}
[Obsolete("OrderOpenTime is deprecated, please use GetOrder instead.")]
public DateTime OrderOpenTime()
{
var commandResponse = SendCommand<int>(MtCommandType.OrderOpenTime, null);
return MtApiTimeConverter.ConvertFromMtTime(commandResponse);
}
[Obsolete("OrderProfit is deprecated, please use GetOrder instead.")]
public double OrderProfit()
{
return SendCommand<double>(MtCommandType.OrderProfit, null);
}
[Obsolete("OrderStopLoss is deprecated, please use GetOrder instead.")]
public double OrderStopLoss()
{
return SendCommand<double>(MtCommandType.OrderStopLoss, null);
}
[Obsolete("OrderSymbol is deprecated, please use GetOrder instead.")]
public string OrderSymbol()
{
return SendCommand<string>(MtCommandType.OrderSymbol, null);
}
[Obsolete("OrderTakeProfit is deprecated, please use GetOrder instead.")]
public double OrderTakeProfit()
{
return SendCommand<double>(MtCommandType.OrderTakeProfit, null);
}
[Obsolete("OrderTicket is deprecated, please use GetOrder instead.")]
public int OrderTicket()
{
return SendCommand<int>(MtCommandType.OrderTicket, null);
}
[Obsolete("OrderType is deprecated, please use GetOrder instead.")]
public TradeOperation OrderType()
{
var retVal = SendCommand<int>(MtCommandType.OrderType, null);
return (TradeOperation)retVal;
}
[Obsolete("OrderSwap is deprecated, please use GetOrder instead.")]
public double OrderSwap()
{
return SendCommand<double>(MtCommandType.OrderSwap, null);
}
#endregion
#region Trading functions
public int OrderSend(string symbol, TradeOperation cmd, double volume, double price, int slippage, double stoploss, double takeprofit
, string comment, int magic, DateTime expiration, Color arrowColor)
{
Log.Debug($"OrderSend: symbol = {symbol}, cmd = {cmd}, volume = {volume}, price = {price}, slippage = {slippage}, stoploss = {stoploss}, takeprofit = {takeprofit}, comment = {comment}, magic = {magic}, expiration = {expiration}, arrowColor = {arrowColor}");
var response = SendRequest<OrderSendResponse>(new OrderSendRequest
{
Symbol = symbol,
Cmd = (int)cmd,
Volume = volume,
Price = price,
Slippage = slippage,
Stoploss = stoploss,
Takeprofit = takeprofit,
Comment = comment,
Magic = magic,
Expiration = MtApiTimeConverter.ConvertToMtTime(expiration),
ArrowColor = MtApiColorConverter.ConvertToMtColor(arrowColor)
});
return response?.Ticket ?? -1;
}
public int OrderSend(string symbol, TradeOperation cmd, double volume, double price, int slippage, double stoploss, double takeprofit
, string comment, int magic, DateTime expiration)
{
Log.Debug($"OrderSend: symbol = {symbol}, cmd = {cmd}, volume = {volume}, price = {price}, slippage = {slippage}, stoploss = {stoploss}, takeprofit = {takeprofit}, comment = {comment}, magic = {magic}, expiration = {expiration}");
var response = SendRequest<OrderSendResponse>(new OrderSendRequest
{
Symbol = symbol,
Cmd = (int)cmd,
Volume = volume,
Price = price,
Slippage = slippage,
Stoploss = stoploss,
Takeprofit = takeprofit,
Comment = comment,
Magic = magic,
Expiration = MtApiTimeConverter.ConvertToMtTime(expiration)
});
return response?.Ticket ?? -1;
}
public int OrderSend(string symbol, TradeOperation cmd, double volume, double price, int slippage, double stoploss, double takeprofit
, string comment, int magic)
{
Log.Debug($"OrderSend: symbol = {symbol}, cmd = {cmd}, volume = {volume}, price = {price}, slippage = {slippage}, stoploss = {stoploss}, takeprofit = {takeprofit}, comment = {comment}, magic = {magic}");
var response = SendRequest<OrderSendResponse>(new OrderSendRequest
{
Symbol = symbol,
Cmd = (int)cmd,
Volume = volume,
Price = price,
Slippage = slippage,
Stoploss = stoploss,
Takeprofit = takeprofit,
Comment = comment,
Magic = magic
});
return response?.Ticket ?? -1;
}
public int OrderSend(string symbol, TradeOperation cmd, double volume, double price, int slippage, double stoploss, double takeprofit
, string comment)
{
Log.Debug($"OrderSend: symbol = {symbol}, cmd = {cmd}, volume = {volume}, price = {price}, slippage = {slippage}, stoploss = {stoploss}, takeprofit = {takeprofit}, comment = {comment}");
var response = SendRequest<OrderSendResponse>(new OrderSendRequest
{
Symbol = symbol,
Cmd = (int)cmd,
Volume = volume,
Price = price,
Slippage = slippage,
Stoploss = stoploss,
Takeprofit = takeprofit,
Comment = comment
});
return response?.Ticket ?? -1;
}
public int OrderSend(string symbol, TradeOperation cmd, double volume, double price, int slippage, double stoploss, double takeprofit)
{
Log.Debug($"OrderSend: symbol = {symbol}, cmd = {cmd}, volume = {volume}, price = {price}, slippage = {slippage}, stoploss = {stoploss}, takeprofit = {takeprofit}");
var response = SendRequest<OrderSendResponse>(new OrderSendRequest
{
Symbol = symbol,
Cmd = (int)cmd,
Volume = volume,
Price = price,
Slippage = slippage,
Stoploss = stoploss,
Takeprofit = takeprofit,
});
return response?.Ticket ?? -1;
}
public int OrderSend(string symbol, TradeOperation cmd, double volume, string price, int slippage, double stoploss, double takeprofit)
{
Log.Debug($"OrderSend: symbol = {symbol}, cmd = {cmd}, volume = {volume}, price = {price}, slippage = {slippage}, stoploss = {stoploss}, takeprofit = {takeprofit}");
double dPrice;
return double.TryParse(price, out dPrice) ?
OrderSend(symbol, cmd, volume, dPrice, slippage, stoploss, takeprofit) : 0;
}
public int OrderSendBuy(string symbol, double volume, int slippage)
{
Log.Debug($"OrderSendBuy: symbol = {symbol}, volume = {volume}, slippage = {slippage}");
return OrderSendBuy(symbol, volume, slippage, 0, 0, null, 0);
}
public int OrderSendSell(string symbol, double volume, int slippage)
{
return OrderSendSell(symbol, volume, slippage, 0, 0, null, 0);
}
public int OrderSendBuy(string symbol, double volume, int slippage, double stoploss, double takeprofit)
{
return OrderSendBuy(symbol, volume, slippage, stoploss, takeprofit, null, 0);
}
public int OrderSendSell(string symbol, double volume, int slippage, double stoploss, double takeprofit)
{
return OrderSendSell(symbol, volume, slippage, stoploss, takeprofit, null, 0);
}
public int OrderSendBuy(string symbol, double volume, int slippage, double stoploss, double takeprofit, string comment, int magic)
{
Log.Debug($"OrderSendBuy: symbol = {symbol}, volume = {volume}, slippage = {slippage}, stoploss = {stoploss}, takeprofit = {takeprofit}, comment = {comment}, magic = {magic}");
var response = SendRequest<OrderSendResponse>(new OrderSendRequest
{
Symbol = symbol,
Cmd = (int)TradeOperation.OP_BUY,
Volume = volume,
Slippage = slippage,
Stoploss = stoploss,
Takeprofit = takeprofit,
Comment = comment,
Magic = magic,
});
return response?.Ticket ?? -1;
}
public int OrderSendSell(string symbol, double volume, int slippage, double stoploss, double takeprofit, string comment, int magic)
{
Log.Debug($"OrderSendSell: symbol = {symbol}, volume = {volume}, slippage = {slippage}, stoploss = {stoploss}, takeprofit = {takeprofit}, comment = {comment}, magic = {magic}");
var response = SendRequest<OrderSendResponse>(new OrderSendRequest
{
Symbol = symbol,
Cmd = (int)TradeOperation.OP_SELL,
Volume = volume,
Slippage = slippage,
Stoploss = stoploss,
Takeprofit = takeprofit,
Comment = comment,
Magic = magic,
});
return response?.Ticket ?? -1;
}
public bool OrderClose(int ticket, double lots, double price, int slippage, Color color)
{
Log.Debug($"OrderClose: ticket = {ticket}, lots = {lots}, price = {price}, slippage = {slippage}, color = {color}");
var response = SendRequest<ResponseBase>(new OrderCloseRequest
{
Ticket = ticket,
Lots = lots,
Price = price,
Slippage = slippage,
ArrowColor = MtApiColorConverter.ConvertToMtColor(color)
});
return response != null;
}
public bool OrderClose(int ticket, double lots, double price, int slippage)
{
Log.Debug($"OrderClose: ticket = {ticket}, lots = {lots}, price = {price}, slippage = {slippage}");
var response = SendRequest<ResponseBase>(new OrderCloseRequest
{
Ticket = ticket,
Lots = lots,
Price = price,
Slippage = slippage,
});
return response != null;
}
public bool OrderClose(int ticket, double lots, int slippage)
{
Log.Debug($"OrderClose: ticket = {ticket}, lots = {lots}, slippage = {slippage}");
var response = SendRequest<ResponseBase>(new OrderCloseRequest
{
Ticket = ticket,
Lots = lots,
Slippage = slippage,
});
return response != null;
}
public bool OrderClose(int ticket, int slippage)
{
Log.Debug($"OrderClose: ticket = {ticket}, slippage = {slippage}");
var response = SendRequest<ResponseBase>(new OrderCloseRequest
{
Ticket = ticket,
Slippage = slippage,
});
return response != null;
}
public bool OrderCloseBy(int ticket, int opposite, Color color)
{
Log.Debug($"OrderCloseBy: ticket = {ticket}, opposite = {opposite}, color = {color}");
var response = SendRequest<ResponseBase>(new OrderCloseByRequest
{
Ticket = ticket,
Opposite = opposite,
ArrowColor = MtApiColorConverter.ConvertToMtColor(color)
});
return response != null;
}
public bool OrderCloseBy(int ticket, int opposite)
{
Log.Debug($"OrderCloseBy: ticket = {ticket}, opposite = {opposite}");
var response = SendRequest<ResponseBase>(new OrderCloseByRequest
{
Ticket = ticket,
Opposite = opposite,
});
return response != null;
}
public bool OrderDelete(int ticket, Color color)
{
Log.Debug($"OrderDelete: ticket = {ticket}, color = {color}");
var response = SendRequest<ResponseBase>(new OrderDeleteRequest
{
Ticket = ticket,
ArrowColor = MtApiColorConverter.ConvertToMtColor(color),
});
return response != null;
}
public bool OrderDelete(int ticket)
{
Log.Debug($"OrderDelete: ticket = {ticket}");
var response = SendRequest<ResponseBase>(new OrderDeleteRequest
{
Ticket = ticket,
});
return response != null;
}
public bool OrderModify(int ticket, double price, double stoploss, double takeprofit, DateTime expiration, Color arrowColor)
{
Log.Debug($"OrderModify: ticket = {ticket}, price = {price}, stoploss = {stoploss}, takeprofit = {takeprofit}, expiration = {expiration}, arrowColor = {arrowColor}");
var response = SendRequest<ResponseBase>(new OrderModifyRequest
{
Ticket = ticket,
Price = price,
Stoploss = stoploss,
Takeprofit = takeprofit,
Expiration = MtApiTimeConverter.ConvertToMtTime(expiration),
ArrowColor = MtApiColorConverter.ConvertToMtColor(arrowColor)
});
return response != null;
}
public bool OrderModify(int ticket, double price, double stoploss, double takeprofit, DateTime expiration)
{
Log.Debug($"OrderModify: ticket = {ticket}, price = {price}, stoploss = {stoploss}, takeprofit = {takeprofit}, expiration = {expiration}");
var response = SendRequest<ResponseBase>(new OrderModifyRequest
{
Ticket = ticket,
Price = price,
Stoploss = stoploss,
Takeprofit = takeprofit,
Expiration = MtApiTimeConverter.ConvertToMtTime(expiration),
});
return response != null;
}
public void OrderPrint()
{
SendCommand<object>(MtCommandType.OrderPrint, null);
}
public bool OrderSelect(int index, OrderSelectMode select, OrderSelectSource pool)
{
Log.Debug($"OrderSelect: index = {index}, select = {select}, pool = {pool}");
var commandParameters = new ArrayList { index, (int)select, (int)pool };
return SendCommand<bool>(MtCommandType.OrderSelect, commandParameters);
}
public bool OrderSelect(int index, OrderSelectMode select)
{
return OrderSelect(index, select, OrderSelectSource.MODE_TRADES);
}
public int OrdersHistoryTotal()
{
return SendCommand<int>(MtCommandType.OrdersHistoryTotal, null);
}
public int OrdersTotal()
{
return SendCommand<int>(MtCommandType.OrdersTotal, null);
}
public bool OrderCloseAll()
{
return SendCommand<bool>(MtCommandType.OrderCloseAll, null);
}
public MtOrder GetOrder(int index, OrderSelectMode select, OrderSelectSource pool)
{
var response = SendRequest<GetOrderResponse>(new GetOrderRequest { Index = index, Select = (int) select, Pool = (int) pool});
return response?.Order;
}
public List<MtOrder> GetOrders(OrderSelectSource pool)
{
var response = SendRequest<GetOrdersResponse>(new GetOrdersRequest { Pool = (int)pool });
return response != null ? response.Orders : new List<MtOrder>();
}
#endregion
#region Checkup
///<summary>
///Returns the contents of the system variable _LastError.
///After the function call, the contents of _LastError are reset.
///</summary>
///<returns>
///Returns the value of the last error that occurred during the execution of an mql4 program.
///</returns>
public int GetLastError()
{
return SendCommand<int>(MtCommandType.GetLastError, null);
}
///<summary>
///Checks connection between client terminal and server.
///</summary>
///<returns>
///It returns true if connection to the server was successfully established, otherwise, it returns false.
///</returns>
public bool IsConnected()
{
return SendCommand<bool>(MtCommandType.IsConnected, null);
}
///<summary>
///Checks if the Expert Advisor runs on a demo account.
///</summary>
///<returns>
///Returns true if the Expert Advisor runs on a demo account, otherwise returns false.
///</returns>
public bool IsDemo()
{
return SendCommand<bool>(MtCommandType.IsDemo, null);
}
///<summary>
///Checks if the DLL function call is allowed for the Expert Advisor.
///</summary>
///<returns>
///Returns true if the DLL function call is allowed for the Expert Advisor, otherwise returns false.
///</returns>
public bool IsDllsAllowed()
{
return SendCommand<bool>(MtCommandType.IsDllsAllowed, null);
}
///<summary>
///Checks if Expert Advisors are enabled for running.
///</summary>
///<returns>
///Returns true if Expert Advisors are enabled for running, otherwise returns false.
///</returns>
public bool IsExpertEnabled()
{
return SendCommand<bool>(MtCommandType.IsExpertEnabled, null);
}
///<summary>
///Checks if the Expert Advisor can call library function.
///</summary>
///<returns>
///Returns true if the Expert Advisor can call library function, otherwise returns false.
///</returns>
public bool IsLibrariesAllowed()
{
return SendCommand<bool>(MtCommandType.IsLibrariesAllowed, null);
}
///<summary>
///Checks if Expert Advisor runs in the Strategy Tester optimization mode.
///</summary>
///<returns>
///Returns true if Expert Advisor runs in the Strategy Tester optimization mode, otherwise returns false.
///</returns>
public bool IsOptimization()
{
return SendCommand<bool>(MtCommandType.IsOptimization, null);
}
///<summary>
///Checks the forced shutdown of an mql4 program.
///</summary>
///<returns>
///Returns true, if the _StopFlag system variable contains a value other than 0.
///A nonzero value is written into _StopFlag, if a mql4 program has been commanded to complete its operation.
///In this case, you must immediately terminate the program, otherwise the program will be completed
///forcibly from the outside after 3 seconds.
///</returns>
public bool IsStopped()
{
return SendCommand<bool>(MtCommandType.IsStopped, null);
}
///<summary>
///Checks if the Expert Advisor runs in the testing mode.
///</summary>
///<returns>
///Returns true if the Expert Advisor runs in the testing mode, otherwise returns false.
///</returns>
public bool IsTesting()
{
return SendCommand<bool>(MtCommandType.IsTesting, null);
}
///<summary>
///Checks if the Expert Advisor is allowed to trade and trading context is not busy.
///</summary>
///<returns>
///Returns true if the Expert Advisor is allowed to trade and trading context is not busy, otherwise returns false.
///</returns>
public bool IsTradeAllowed()
{
return SendCommand<bool>(MtCommandType.IsTradeAllowed, null);
}
///<summary>
///Returns the information about trade context.
///</summary>
///<returns>
///Returns true if a thread for trading is occupied by another Expert Advisor, otherwise returns false.
///</returns>
public bool IsTradeContextBusy()
{
return SendCommand<bool>(MtCommandType.IsTradeContextBusy, null);
}
///<summary>
///Checks if the Expert Advisor is tested in visual mode.
///</summary>
///<returns>
///Returns true if the Expert Advisor is tested with checked "Visual Mode" button, otherwise returns false.
///</returns>
public bool IsVisualMode()
{
return SendCommand<bool>(MtCommandType.IsVisualMode, null);
}
///<summary>
///Returns the code of a reason for deinitialization.
///</summary>
///<returns>
///Returns the value of _UninitReason which is formed before OnDeinit() is called.
///Value depends on the reasons that led to deinitialization.
///</returns>
public int UninitializeReason()
{
return SendCommand<int>(MtCommandType.UninitializeReason, null);
}
///<summary>
///Print the error description.
///</summary>
public string ErrorDescription(int errorCode)
{
var commandParameters = new ArrayList { errorCode };
return SendCommand<string>(MtCommandType.ErrorDescription, commandParameters);
}
///<summary>
///Returns the value of a corresponding property of the mql4 program environment.
///</summary>
///<param name="propertyId">Identifier of a property. Can be one of the values of the ENUM_TERMINAL_INFO_STRING enumeration.</param>
///<returns>
///Value of string type.
///</returns>
public string TerminalInfoString(ENUM_TERMINAL_INFO_STRING propertyId)
{
var commandParameters = new ArrayList { (int)propertyId };
return SendCommand<string>(MtCommandType.TerminalInfoString, commandParameters);
}
///<summary>
///Returns the value of a corresponding property of the mql4 program environment.
///</summary>
///<param name="propertyId">Identifier of a property. Can be one of the values of the ENUM_TERMINAL_INFO_INTEGER enumeration.</param>
///<returns>
///Value of int type.
///</returns>
public int TerminalInfoInteger(EnumTerminalInfoInteger propertyId)
{
var commandParameters = new ArrayList { (int)propertyId };
return SendCommand<int>(MtCommandType.TerminalInfoInteger, commandParameters);
}
///<summary>
///Returns the value of a corresponding property of the mql4 program environment.
///</summary>
///<param name="propertyId">Identifier of a property. Can be one of the values of the ENUM_TERMINAL_INFO_DOUBLE enumeration.</param>
///<returns>
///Value of double type.
///</returns>
public double TerminalInfoDouble(EnumTerminalInfoDouble propertyId)
{
var commandParameters = new ArrayList { (int)propertyId };
return SendCommand<double>(MtCommandType.TerminalInfoDouble, commandParameters);
}
///<summary>
///Returns the name of company owning the client terminal.
///</summary>
///<returns>
///The name of company owning the client terminal.
///</returns>
public string TerminalCompany()
{
return SendCommand<string>(MtCommandType.TerminalCompany, null);
}
///<summary>
///Returns client terminal name.
///</summary>
///<returns>
///Client terminal name.
///</returns>
public string TerminalName()
{
return SendCommand<string>(MtCommandType.TerminalName, null);
}
///<summary>
///Returns the directory, from which the client terminal was launched.
///</summary>
///<returns>
///The directory, from which the client terminal was launched.
///</returns>
public string TerminalPath()
{
return SendCommand<string>(MtCommandType.TerminalPath, null);
}
#endregion
#region Account functions
public double AccountBalance()
{
return SendCommand<double>(MtCommandType.AccountBalance, null);
}
public double AccountCredit()
{
return SendCommand<double>(MtCommandType.AccountCredit, null);
}
public string AccountCompany()
{
return SendCommand<string>(MtCommandType.AccountCompany, null);
}
public string AccountCurrency()
{
return SendCommand<string>(MtCommandType.AccountCurrency, null);
}
public double AccountEquity()
{
return SendCommand<double>(MtCommandType.AccountEquity, null);
}
public double AccountFreeMargin()
{
return SendCommand<double>(MtCommandType.AccountFreeMargin, null);
}
public double AccountFreeMarginCheck(string symbol, TradeOperation cmd, double volume)
{
var commandParameters = new ArrayList { symbol, (int)cmd, volume };
return SendCommand<double>(MtCommandType.AccountFreeMarginCheck, commandParameters);
}
public double AccountFreeMarginMode()
{
return SendCommand<double>(MtCommandType.AccountFreeMarginMode, null);
}
public int AccountLeverage()
{
return SendCommand<int>(MtCommandType.AccountLeverage, null);
}
public double AccountMargin()
{
return SendCommand<double>(MtCommandType.AccountMargin, null);
}
public string AccountName()
{
return SendCommand<string>(MtCommandType.AccountName, null);
}
public int AccountNumber()
{
return SendCommand<int>(MtCommandType.AccountNumber, null);
}
public double AccountProfit()
{
return SendCommand<double>(MtCommandType.AccountProfit, null);
}
public string AccountServer()
{
return SendCommand<string>(MtCommandType.AccountServer, null);
}
public int AccountStopoutLevel()
{
return SendCommand<int>(MtCommandType.AccountStopoutLevel, null);
}
public int AccountStopoutMode()
{
return SendCommand<int>(MtCommandType.AccountStopoutMode, null);
}
public bool ChangeAccount(string login, string password, string host)
{
var commandParameters = new ArrayList { login, password, host};
return SendCommand<bool>(MtCommandType.ChangeAccount, commandParameters);
}
#endregion
#region Common Function
public void Alert(string msg)
{
var commandParameters = new ArrayList { msg };
SendCommand<object>(MtCommandType.Alert, commandParameters);
}
public void Comment(string msg)
{
var commandParameters = new ArrayList { msg };
SendCommand<object>(MtCommandType.Comment, commandParameters);
}
public int GetTickCount()
{
return SendCommand<int>(MtCommandType.GetTickCount, null);
}
public int MessageBox(string text, string caption, int flag)
{
var commandParameters = new ArrayList { text, caption, flag };
return SendCommand<int>(MtCommandType.MessageBoxA, commandParameters);
}
public int MessageBox(string text, string caption)
{
return MessageBox(text, caption, EMPTY);
}
public int MessageBox(string text)
{
var commandParameters = new ArrayList { text };
return SendCommand<int>(MtCommandType.MessageBox, commandParameters);
}
public bool PlaySound(string filename)
{
var commandParameters = new ArrayList { filename };
return SendCommand<bool>(MtCommandType.PlaySound, commandParameters);
}
public void Print(string msg)
{
var commandParameters = new ArrayList { msg };
SendCommand<object>(MtCommandType.Print, commandParameters);
}
public bool SendFTP(string filename)
{
var commandParameters = new ArrayList { filename };
return SendCommand<bool>(MtCommandType.SendFTP, commandParameters);
}
public bool SendFTP(string filename, string ftpPath)
{
var commandParameters = new ArrayList { filename, ftpPath };
return SendCommand<bool>(MtCommandType.SendFTPA, commandParameters);
}
public bool SendMail(string subject, string someText)
{
var commandParameters = new ArrayList { subject, someText };
return SendCommand<bool>(MtCommandType.SendMail, commandParameters);
}
public void Sleep(int milliseconds)
{
var commandParameters = new ArrayList { milliseconds };
SendCommand<object>(MtCommandType.Sleep, commandParameters);