@@ -26,6 +26,8 @@ public sealed class MtApiClient
26
26
#endregion
27
27
28
28
#region Private Fields
29
+ private static readonly MtLog Log = LogConfigurator . GetLogger ( typeof ( MtApiClient ) ) ;
30
+
29
31
private MtClient _client ;
30
32
private readonly object _locker = new object ( ) ;
31
33
private MtConnectionState _connectionState = MtConnectionState . Disconnected ;
@@ -37,7 +39,12 @@ public sealed class MtApiClient
37
39
38
40
public MtApiClient ( )
39
41
{
40
- LogConfigurator . Setup ( LogProfileName ) ;
42
+ #if ( DEBUG )
43
+ const LogLevel logLevel = LogLevel . Debug ;
44
+ #else
45
+ const LogLevel logLevel = LogLevel . Info ;
46
+ #endif
47
+ LogConfigurator . Setup ( LogProfileName , logLevel ) ;
41
48
}
42
49
#endregion
43
50
@@ -49,6 +56,7 @@ public MtApiClient()
49
56
///<param name="port">Port of host connection (default 8222) </param>
50
57
public void BeginConnect ( string host , int port )
51
58
{
59
+ Log . Info ( $ "BeginConnect: host = { host } , port = { port } ") ;
52
60
Task . Factory . StartNew ( ( ) => Connect ( host , port ) ) ;
53
61
}
54
62
@@ -58,6 +66,7 @@ public void BeginConnect(string host, int port)
58
66
///<param name="port">Port of host connection (default 8222) </param>
59
67
public void BeginConnect ( int port )
60
68
{
69
+ Log . Info ( $ "BeginConnect: port = { port } ") ;
61
70
Task . Factory . StartNew ( ( ) => Connect ( port ) ) ;
62
71
}
63
72
@@ -66,6 +75,7 @@ public void BeginConnect(int port)
66
75
///</summary>
67
76
public void BeginDisconnect ( )
68
77
{
78
+ Log . Info ( "BeginDisconnect called." ) ;
69
79
Task . Factory . StartNew ( ( ) => Disconnect ( false ) ) ;
70
80
}
71
81
@@ -251,6 +261,8 @@ public double OrderSwap()
251
261
public int OrderSend ( string symbol , TradeOperation cmd , double volume , double price , int slippage , double stoploss , double takeprofit
252
262
, string comment , int magic , DateTime expiration , Color arrowColor )
253
263
{
264
+ 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 } ") ;
265
+
254
266
var response = SendRequest < OrderSendResponse > ( new OrderSendRequest
255
267
{
256
268
Symbol = symbol ,
@@ -271,6 +283,8 @@ public int OrderSend(string symbol, TradeOperation cmd, double volume, double pr
271
283
public int OrderSend ( string symbol , TradeOperation cmd , double volume , double price , int slippage , double stoploss , double takeprofit
272
284
, string comment , int magic , DateTime expiration )
273
285
{
286
+ Log . Debug ( $ "OrderSend: symbol = { symbol } , cmd = { cmd } , volume = { volume } , price = { price } , slippage = { slippage } , stoploss = { stoploss } , takeprofit = { takeprofit } , comment = { comment } , magic = { magic } , expiration = { expiration } ") ;
287
+
274
288
var response = SendRequest < OrderSendResponse > ( new OrderSendRequest
275
289
{
276
290
Symbol = symbol ,
@@ -290,6 +304,8 @@ public int OrderSend(string symbol, TradeOperation cmd, double volume, double pr
290
304
public int OrderSend ( string symbol , TradeOperation cmd , double volume , double price , int slippage , double stoploss , double takeprofit
291
305
, string comment , int magic )
292
306
{
307
+ Log . Debug ( $ "OrderSend: symbol = { symbol } , cmd = { cmd } , volume = { volume } , price = { price } , slippage = { slippage } , stoploss = { stoploss } , takeprofit = { takeprofit } , comment = { comment } , magic = { magic } ") ;
308
+
293
309
var response = SendRequest < OrderSendResponse > ( new OrderSendRequest
294
310
{
295
311
Symbol = symbol ,
@@ -308,6 +324,8 @@ public int OrderSend(string symbol, TradeOperation cmd, double volume, double pr
308
324
public int OrderSend ( string symbol , TradeOperation cmd , double volume , double price , int slippage , double stoploss , double takeprofit
309
325
, string comment )
310
326
{
327
+ Log . Debug ( $ "OrderSend: symbol = { symbol } , cmd = { cmd } , volume = { volume } , price = { price } , slippage = { slippage } , stoploss = { stoploss } , takeprofit = { takeprofit } , comment = { comment } ") ;
328
+
311
329
var response = SendRequest < OrderSendResponse > ( new OrderSendRequest
312
330
{
313
331
Symbol = symbol ,
@@ -324,6 +342,8 @@ public int OrderSend(string symbol, TradeOperation cmd, double volume, double pr
324
342
325
343
public int OrderSend ( string symbol , TradeOperation cmd , double volume , double price , int slippage , double stoploss , double takeprofit )
326
344
{
345
+ Log . Debug ( $ "OrderSend: symbol = { symbol } , cmd = { cmd } , volume = { volume } , price = { price } , slippage = { slippage } , stoploss = { stoploss } , takeprofit = { takeprofit } ") ;
346
+
327
347
var response = SendRequest < OrderSendResponse > ( new OrderSendRequest
328
348
{
329
349
Symbol = symbol ,
@@ -339,13 +359,17 @@ public int OrderSend(string symbol, TradeOperation cmd, double volume, double pr
339
359
340
360
public int OrderSend ( string symbol , TradeOperation cmd , double volume , string price , int slippage , double stoploss , double takeprofit )
341
361
{
362
+ Log . Debug ( $ "OrderSend: symbol = { symbol } , cmd = { cmd } , volume = { volume } , price = { price } , slippage = { slippage } , stoploss = { stoploss } , takeprofit = { takeprofit } ") ;
363
+
342
364
double dPrice ;
343
365
return double . TryParse ( price , out dPrice ) ?
344
366
OrderSend ( symbol , cmd , volume , dPrice , slippage , stoploss , takeprofit ) : 0 ;
345
367
}
346
368
347
369
public int OrderSendBuy ( string symbol , double volume , int slippage )
348
370
{
371
+ Log . Debug ( $ "OrderSendBuy: symbol = { symbol } , volume = { volume } , slippage = { slippage } ") ;
372
+
349
373
return OrderSendBuy ( symbol , volume , slippage , 0 , 0 , null , 0 ) ;
350
374
}
351
375
@@ -366,6 +390,8 @@ public int OrderSendSell(string symbol, double volume, int slippage, double stop
366
390
367
391
public int OrderSendBuy ( string symbol , double volume , int slippage , double stoploss , double takeprofit , string comment , int magic )
368
392
{
393
+ Log . Debug ( $ "OrderSendBuy: symbol = { symbol } , volume = { volume } , slippage = { slippage } , stoploss = { stoploss } , takeprofit = { takeprofit } , comment = { comment } , magic = { magic } ") ;
394
+
369
395
var response = SendRequest < OrderSendResponse > ( new OrderSendRequest
370
396
{
371
397
Symbol = symbol ,
@@ -382,6 +408,8 @@ public int OrderSendBuy(string symbol, double volume, int slippage, double stopl
382
408
383
409
public int OrderSendSell ( string symbol , double volume , int slippage , double stoploss , double takeprofit , string comment , int magic )
384
410
{
411
+ Log . Debug ( $ "OrderSendSell: symbol = { symbol } , volume = { volume } , slippage = { slippage } , stoploss = { stoploss } , takeprofit = { takeprofit } , comment = { comment } , magic = { magic } ") ;
412
+
385
413
var response = SendRequest < OrderSendResponse > ( new OrderSendRequest
386
414
{
387
415
Symbol = symbol ,
@@ -398,6 +426,8 @@ public int OrderSendSell(string symbol, double volume, int slippage, double stop
398
426
399
427
public bool OrderClose ( int ticket , double lots , double price , int slippage , Color color )
400
428
{
429
+ Log . Debug ( $ "OrderClose: ticket = { ticket } , lots = { lots } , price = { price } , slippage = { slippage } , color = { color } ") ;
430
+
401
431
var response = SendRequest < ResponseBase > ( new OrderCloseRequest
402
432
{
403
433
Ticket = ticket ,
@@ -411,6 +441,8 @@ public bool OrderClose(int ticket, double lots, double price, int slippage, Colo
411
441
412
442
public bool OrderClose ( int ticket , double lots , double price , int slippage )
413
443
{
444
+ Log . Debug ( $ "OrderClose: ticket = { ticket } , lots = { lots } , price = { price } , slippage = { slippage } ") ;
445
+
414
446
var response = SendRequest < ResponseBase > ( new OrderCloseRequest
415
447
{
416
448
Ticket = ticket ,
@@ -423,6 +455,8 @@ public bool OrderClose(int ticket, double lots, double price, int slippage)
423
455
424
456
public bool OrderClose ( int ticket , double lots , int slippage )
425
457
{
458
+ Log . Debug ( $ "OrderClose: ticket = { ticket } , lots = { lots } , slippage = { slippage } ") ;
459
+
426
460
var response = SendRequest < ResponseBase > ( new OrderCloseRequest
427
461
{
428
462
Ticket = ticket ,
@@ -434,6 +468,8 @@ public bool OrderClose(int ticket, double lots, int slippage)
434
468
435
469
public bool OrderClose ( int ticket , int slippage )
436
470
{
471
+ Log . Debug ( $ "OrderClose: ticket = { ticket } , slippage = { slippage } ") ;
472
+
437
473
var response = SendRequest < ResponseBase > ( new OrderCloseRequest
438
474
{
439
475
Ticket = ticket ,
@@ -444,6 +480,8 @@ public bool OrderClose(int ticket, int slippage)
444
480
445
481
public bool OrderCloseBy ( int ticket , int opposite , Color color )
446
482
{
483
+ Log . Debug ( $ "OrderCloseBy: ticket = { ticket } , opposite = { opposite } , color = { color } ") ;
484
+
447
485
var response = SendRequest < ResponseBase > ( new OrderCloseByRequest
448
486
{
449
487
Ticket = ticket ,
@@ -455,6 +493,8 @@ public bool OrderCloseBy(int ticket, int opposite, Color color)
455
493
456
494
public bool OrderCloseBy ( int ticket , int opposite )
457
495
{
496
+ Log . Debug ( $ "OrderCloseBy: ticket = { ticket } , opposite = { opposite } ") ;
497
+
458
498
var response = SendRequest < ResponseBase > ( new OrderCloseByRequest
459
499
{
460
500
Ticket = ticket ,
@@ -465,6 +505,8 @@ public bool OrderCloseBy(int ticket, int opposite)
465
505
466
506
public bool OrderDelete ( int ticket , Color color )
467
507
{
508
+ Log . Debug ( $ "OrderDelete: ticket = { ticket } , color = { color } ") ;
509
+
468
510
var response = SendRequest < ResponseBase > ( new OrderDeleteRequest
469
511
{
470
512
Ticket = ticket ,
@@ -475,6 +517,8 @@ public bool OrderDelete(int ticket, Color color)
475
517
476
518
public bool OrderDelete ( int ticket )
477
519
{
520
+ Log . Debug ( $ "OrderDelete: ticket = { ticket } ") ;
521
+
478
522
var response = SendRequest < ResponseBase > ( new OrderDeleteRequest
479
523
{
480
524
Ticket = ticket ,
@@ -484,6 +528,8 @@ public bool OrderDelete(int ticket)
484
528
485
529
public bool OrderModify ( int ticket , double price , double stoploss , double takeprofit , DateTime expiration , Color arrowColor )
486
530
{
531
+ Log . Debug ( $ "OrderModify: ticket = { ticket } , price = { price } , stoploss = { stoploss } , takeprofit = { takeprofit } , expiration = { expiration } , arrowColor = { arrowColor } ") ;
532
+
487
533
var response = SendRequest < ResponseBase > ( new OrderModifyRequest
488
534
{
489
535
Ticket = ticket ,
@@ -498,6 +544,8 @@ public bool OrderModify(int ticket, double price, double stoploss, double takepr
498
544
499
545
public bool OrderModify ( int ticket , double price , double stoploss , double takeprofit , DateTime expiration )
500
546
{
547
+ Log . Debug ( $ "OrderModify: ticket = { ticket } , price = { price } , stoploss = { stoploss } , takeprofit = { takeprofit } , expiration = { expiration } ") ;
548
+
501
549
var response = SendRequest < ResponseBase > ( new OrderModifyRequest
502
550
{
503
551
Ticket = ticket ,
@@ -516,6 +564,8 @@ public void OrderPrint()
516
564
517
565
public bool OrderSelect ( int index , OrderSelectMode select , OrderSelectSource pool )
518
566
{
567
+ Log . Debug ( $ "OrderSelect: index = { index } , select = { select } , pool = { pool } ") ;
568
+
519
569
var commandParameters = new ArrayList { index , ( int ) select , ( int ) pool } ;
520
570
return SendCommand < bool > ( MtCommandType . OrderSelect , commandParameters ) ;
521
571
}
@@ -2894,6 +2944,8 @@ private void Connect(MtClient client)
2894
2944
{
2895
2945
client . Dispose ( ) ;
2896
2946
message = string . IsNullOrEmpty ( client . Host ) ? $ "Failed connection to localhost:{ client . Port } . { e . Message } " : $ "Failed connection to { client . Host } :{ client . Port } . { e . Message } ";
2947
+
2948
+ Log . Warn ( message ) ;
2897
2949
}
2898
2950
2899
2951
if ( state == MtConnectionState . Connected )
@@ -2906,6 +2958,8 @@ private void Connect(MtClient client)
2906
2958
_client . ServerFailed += _client_ServerFailed ;
2907
2959
_client . MtEventReceived += _client_MtEventReceived ;
2908
2960
message = string . IsNullOrEmpty ( client . Host ) ? $ "Connected to localhost:{ client . Port } " : $ "Connected to { client . Host } :{ client . Port } ";
2961
+
2962
+ Log . Info ( message ) ;
2909
2963
}
2910
2964
2911
2965
_connectionState = state ;
@@ -2973,6 +3027,7 @@ private void Disconnect(bool failed)
2973
3027
_connectionState = state ;
2974
3028
}
2975
3029
3030
+ Log . Info ( message ) ;
2976
3031
2977
3032
ConnectionStateChanged ? . Invoke ( this , new MtConnectionEventArgs ( state , message ) ) ;
2978
3033
}
@@ -2984,6 +3039,7 @@ private T SendCommand<T>(MtCommandType commandType, ArrayList commandParameters,
2984
3039
var client = Client ;
2985
3040
if ( client == null )
2986
3041
{
3042
+ Log . Warn ( "SendCommand: No connection" ) ;
2987
3043
throw new MtConnectionException ( "No connection" ) ;
2988
3044
}
2989
3045
@@ -2993,16 +3049,19 @@ private T SendCommand<T>(MtCommandType commandType, ArrayList commandParameters,
2993
3049
}
2994
3050
catch ( CommunicationException ex )
2995
3051
{
3052
+ Log . Warn ( $ "SendCommand: { ex . Message } ") ;
2996
3053
throw new MtConnectionException ( ex . Message , ex ) ;
2997
3054
}
2998
3055
2999
3056
if ( response == null )
3000
3057
{
3058
+ Log . Warn ( "SendCommand: Response from MetaTrader is null" ) ;
3001
3059
throw new MtExecutionException ( MtErrorCode . MtApiCustomError , "Response from MetaTrader is null" ) ;
3002
3060
}
3003
3061
3004
3062
if ( response . ErrorCode != 0 )
3005
3063
{
3064
+ Log . Warn ( $ "SendCommand: ErrorCode = { response . ErrorCode } . { response } ") ;
3006
3065
throw new MtExecutionException ( ( MtErrorCode ) response . ErrorCode , response . ToString ( ) ) ;
3007
3066
}
3008
3067
@@ -3026,12 +3085,14 @@ private T SendCommand<T>(MtCommandType commandType, ArrayList commandParameters,
3026
3085
3027
3086
if ( res == null )
3028
3087
{
3088
+ Log . Warn ( "SendRequest: Response from MetaTrader is null" ) ;
3029
3089
throw new MtExecutionException ( MtErrorCode . MtApiCustomError , "Response from MetaTrader is null" ) ;
3030
3090
}
3031
3091
3032
3092
var response = JsonConvert . DeserializeObject < T > ( res ) ;
3033
3093
if ( response . ErrorCode != 0 )
3034
3094
{
3095
+ Log . Warn ( $ "SendRequest: ErrorCode = { response . ErrorCode } . { response } ") ;
3035
3096
throw new MtExecutionException ( ( MtErrorCode ) response . ErrorCode , response . ErrorMessage ) ;
3036
3097
}
3037
3098
0 commit comments