Skip to content

Commit

Permalink
[MT4] Log improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
vdemydiuk committed Oct 22, 2020
1 parent 0cd8edb commit 373bfe4
Showing 1 changed file with 62 additions and 1 deletion.
63 changes: 62 additions & 1 deletion MtApi/MtApiClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public sealed class 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;
Expand All @@ -37,7 +39,12 @@ public sealed class MtApiClient

public MtApiClient()
{
LogConfigurator.Setup(LogProfileName);
#if (DEBUG)
const LogLevel logLevel = LogLevel.Debug;
#else
const LogLevel logLevel = LogLevel.Info;
#endif
LogConfigurator.Setup(LogProfileName, logLevel);
}
#endregion

Expand All @@ -49,6 +56,7 @@ public MtApiClient()
///<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));
}

Expand All @@ -58,6 +66,7 @@ public void BeginConnect(string host, int port)
///<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));
}

Expand All @@ -66,6 +75,7 @@ public void BeginConnect(int port)
///</summary>
public void BeginDisconnect()
{
Log.Info("BeginDisconnect called.");
Task.Factory.StartNew(() => Disconnect(false));
}

Expand Down Expand Up @@ -251,6 +261,8 @@ public double OrderSwap()
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,
Expand All @@ -271,6 +283,8 @@ public double OrderSwap()
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,
Expand All @@ -290,6 +304,8 @@ public double OrderSwap()
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,
Expand All @@ -308,6 +324,8 @@ public double OrderSwap()
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,
Expand All @@ -324,6 +342,8 @@ public double OrderSwap()

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,
Expand All @@ -339,13 +359,17 @@ public int OrderSend(string symbol, TradeOperation cmd, double volume, double pr

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);
}

Expand All @@ -366,6 +390,8 @@ public int OrderSendSell(string symbol, double volume, int slippage, double stop

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,
Expand All @@ -382,6 +408,8 @@ public int OrderSendBuy(string symbol, double volume, int slippage, double stopl

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,
Expand All @@ -398,6 +426,8 @@ public int OrderSendSell(string symbol, double volume, int slippage, double stop

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,
Expand All @@ -411,6 +441,8 @@ public bool OrderClose(int ticket, double lots, double price, int slippage, Colo

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,
Expand All @@ -423,6 +455,8 @@ public bool OrderClose(int ticket, double lots, double price, int slippage)

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,
Expand All @@ -434,6 +468,8 @@ public bool OrderClose(int ticket, double lots, int slippage)

public bool OrderClose(int ticket, int slippage)
{
Log.Debug($"OrderClose: ticket = {ticket}, slippage = {slippage}");

var response = SendRequest<ResponseBase>(new OrderCloseRequest
{
Ticket = ticket,
Expand All @@ -444,6 +480,8 @@ public bool OrderClose(int ticket, int slippage)

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,
Expand All @@ -455,6 +493,8 @@ public bool OrderCloseBy(int ticket, int opposite, Color color)

public bool OrderCloseBy(int ticket, int opposite)
{
Log.Debug($"OrderCloseBy: ticket = {ticket}, opposite = {opposite}");

var response = SendRequest<ResponseBase>(new OrderCloseByRequest
{
Ticket = ticket,
Expand All @@ -465,6 +505,8 @@ public bool OrderCloseBy(int ticket, int opposite)

public bool OrderDelete(int ticket, Color color)
{
Log.Debug($"OrderDelete: ticket = {ticket}, color = {color}");

var response = SendRequest<ResponseBase>(new OrderDeleteRequest
{
Ticket = ticket,
Expand All @@ -475,6 +517,8 @@ public bool OrderDelete(int ticket, Color color)

public bool OrderDelete(int ticket)
{
Log.Debug($"OrderDelete: ticket = {ticket}");

var response = SendRequest<ResponseBase>(new OrderDeleteRequest
{
Ticket = ticket,
Expand All @@ -484,6 +528,8 @@ public bool OrderDelete(int ticket)

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,
Expand All @@ -498,6 +544,8 @@ public bool OrderModify(int ticket, double price, double stoploss, double takepr

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,
Expand All @@ -516,6 +564,8 @@ public void OrderPrint()

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);
}
Expand Down Expand Up @@ -2894,6 +2944,8 @@ private void Connect(MtClient client)
{
client.Dispose();
message = string.IsNullOrEmpty(client.Host) ? $"Failed connection to localhost:{client.Port}. {e.Message}" : $"Failed connection to {client.Host}:{client.Port}. {e.Message}";

Log.Warn(message);
}

if (state == MtConnectionState.Connected)
Expand All @@ -2906,6 +2958,8 @@ private void Connect(MtClient client)
_client.ServerFailed += _client_ServerFailed;
_client.MtEventReceived += _client_MtEventReceived;
message = string.IsNullOrEmpty(client.Host) ? $"Connected to localhost:{client.Port}" : $"Connected to { client.Host}:{client.Port}";

Log.Info(message);
}

_connectionState = state;
Expand Down Expand Up @@ -2973,6 +3027,7 @@ private void Disconnect(bool failed)
_connectionState = state;
}

Log.Info(message);

ConnectionStateChanged?.Invoke(this, new MtConnectionEventArgs(state, message));
}
Expand All @@ -2984,6 +3039,7 @@ private T SendCommand<T>(MtCommandType commandType, ArrayList commandParameters,
var client = Client;
if (client == null)
{
Log.Warn("SendCommand: No connection");
throw new MtConnectionException("No connection");
}

Expand All @@ -2993,16 +3049,19 @@ private T SendCommand<T>(MtCommandType commandType, ArrayList commandParameters,
}
catch (CommunicationException ex)
{
Log.Warn($"SendCommand: {ex.Message}");
throw new MtConnectionException(ex.Message, ex);
}

if (response == null)
{
Log.Warn("SendCommand: Response from MetaTrader is null");
throw new MtExecutionException(MtErrorCode.MtApiCustomError, "Response from MetaTrader is null");
}

if (response.ErrorCode != 0)
{
Log.Warn($"SendCommand: ErrorCode = {response.ErrorCode}. {response}");
throw new MtExecutionException((MtErrorCode)response.ErrorCode, response.ToString());
}

Expand All @@ -3026,12 +3085,14 @@ private T SendRequest<T>(RequestBase request) where T : ResponseBase, new()

if (res == null)
{
Log.Warn("SendRequest: Response from MetaTrader is null");
throw new MtExecutionException(MtErrorCode.MtApiCustomError, "Response from MetaTrader is null");
}

var response = JsonConvert.DeserializeObject<T>(res);
if (response.ErrorCode != 0)
{
Log.Warn($"SendRequest: ErrorCode = {response.ErrorCode}. {response}");
throw new MtExecutionException((MtErrorCode)response.ErrorCode, response.ErrorMessage);
}

Expand Down

0 comments on commit 373bfe4

Please sign in to comment.