diff --git a/MtApi/MtApi.csproj b/MtApi/MtApi.csproj index ee9e53aa..af7bae5e 100755 --- a/MtApi/MtApi.csproj +++ b/MtApi/MtApi.csproj @@ -78,6 +78,7 @@ + diff --git a/MtApi/MtApiClient.cs b/MtApi/MtApiClient.cs index 13896b09..ac7ab6ed 100755 --- a/MtApi/MtApiClient.cs +++ b/MtApi/MtApiClient.cs @@ -17,7 +17,7 @@ namespace MtApi public sealed class MtApiClient { - private const int DOUBLE_ARRAY_LIMIT = 64800; + private const int DoubleArrayLimit = 64800; #region MetaTrader Constants @@ -96,26 +96,6 @@ namespace MtApi #region Trading functions - private int InternalOrderSend(string symbol, TradeOperation cmd, double volume, double? price, int? slippage, double? stoploss, double? takeprofit - , string comment, int? magic, DateTime? expiration, Color? arrowColor) - { - var response = SendRequest(new OrderSendRequest - { - Symbol = symbol, - Cmd = (int)cmd, - Volume = volume, - Price = price, - Slippage = slippage, - Stoploss = stoploss, - Takeprofit = takeprofit, - Comment = comment, - Magic = magic, - Expiration = expiration.HasValue ? MtApiTimeConverter.ConvertToMtTime(expiration.Value) : default(int?), - ArrowColor = arrowColor.HasValue ? MtApiColorConverter.ConvertToMtColor(arrowColor.Value) : default(int?) - }); - return response != null ? 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, Color arrowColor) { @@ -186,19 +166,34 @@ namespace MtApi public bool OrderClose(int ticket, double lots, double price, int slippage, Color color) { - var commandParameters = new ArrayList { ticket, lots, price, slippage, MtApiColorConverter.ConvertToMtColor(color) }; - return sendCommand(MtCommandType.OrderClose, commandParameters); + return InternalOrderClose(ticket, lots, price, slippage, color); } public bool OrderClose(int ticket, double lots, double price, int slippage) { - return OrderClose(ticket, lots, price, slippage, Color.Empty); + return InternalOrderClose(ticket, lots, price, slippage, null); + } + + public bool OrderClose(int ticket, double lots, int slippage) + { + return InternalOrderClose(ticket, lots, null, slippage, null); + } + + public bool OrderClose(int ticket, int slippage) + { + return InternalOrderClose(ticket, null, null, slippage, null); + } + + [Obsolete("OrderCloseByCurrentPrice is deprecated, please use OrderClose instead.")] + public bool OrderCloseByCurrentPrice(int ticket, int slippage) + { + return InternalOrderClose(ticket, null, null, slippage, null); } public bool OrderCloseBy(int ticket, int opposite, Color color) { var commandParameters = new ArrayList { ticket, opposite, MtApiColorConverter.ConvertToMtColor(color) }; - return sendCommand(MtCommandType.OrderCloseBy, commandParameters); + return SendCommand(MtCommandType.OrderCloseBy, commandParameters); } public bool OrderCloseBy(int ticket, int opposite) @@ -206,45 +201,39 @@ namespace MtApi return OrderCloseBy(ticket, opposite, Color.Empty); } - public bool OrderCloseByCurrentPrice(int ticket, int slippage) - { - var commandParameters = new ArrayList { ticket, slippage }; - return sendCommand(MtCommandType.OrderCloseByCurrentPrice, commandParameters); - } - public double OrderClosePrice() { - return sendCommand(MtCommandType.OrderClosePrice, null); + return SendCommand(MtCommandType.OrderClosePrice, null); } public double OrderClosePrice(int ticket) { var commandParameters = new ArrayList { ticket }; - double retVal = sendCommand(MtCommandType.OrderClosePriceByTicket, commandParameters); + double retVal = SendCommand(MtCommandType.OrderClosePriceByTicket, commandParameters); return retVal; } public DateTime OrderCloseTime() { - var commandResponse = sendCommand(MtCommandType.OrderCloseTime, null); + var commandResponse = SendCommand(MtCommandType.OrderCloseTime, null); return MtApiTimeConverter.ConvertFromMtTime(commandResponse); } public string OrderComment() { - return sendCommand(MtCommandType.OrderComment, null); + return SendCommand(MtCommandType.OrderComment, null); } public double OrderCommission() { - return sendCommand(MtCommandType.OrderCommission, null); + return SendCommand(MtCommandType.OrderCommission, null); } public bool OrderDelete(int ticket, Color color) { var commandParameters = new ArrayList { ticket, MtApiColorConverter.ConvertToMtColor(color) }; - return sendCommand(MtCommandType.OrderDelete, commandParameters); + return SendCommand(MtCommandType.OrderDelete, commandParameters); } public bool OrderDelete(int ticket) @@ -254,24 +243,24 @@ namespace MtApi public DateTime OrderExpiration() { - var commandResponse = sendCommand(MtCommandType.OrderExpiration, null); + var commandResponse = SendCommand(MtCommandType.OrderExpiration, null); return MtApiTimeConverter.ConvertFromMtTime(commandResponse); } public double OrderLots() { - return sendCommand(MtCommandType.OrderLots, null); + return SendCommand(MtCommandType.OrderLots, null); } public int OrderMagicNumber() { - return sendCommand(MtCommandType.OrderMagicNumber, null); + return SendCommand(MtCommandType.OrderMagicNumber, null); } public bool OrderModify(int ticket, double price, double stoploss, double takeprofit, DateTime expiration, Color arrow_color) { var commandParameters = new ArrayList { ticket, price, stoploss, takeprofit, MtApiTimeConverter.ConvertToMtTime(expiration), MtApiColorConverter.ConvertToMtColor(arrow_color) }; - return sendCommand(MtCommandType.OrderModify, commandParameters); + return SendCommand(MtCommandType.OrderModify, commandParameters); } public bool OrderModify(int ticket, double price, double stoploss, double takeprofit, DateTime expiration) @@ -281,37 +270,37 @@ namespace MtApi public double OrderOpenPrice() { - return sendCommand(MtCommandType.OrderOpenPrice, null); + return SendCommand(MtCommandType.OrderOpenPrice, null); } public double OrderOpenPrice(int ticket) { var commandParameters = new ArrayList { ticket }; - double retVal = sendCommand(MtCommandType.OrderOpenPriceByTicket, commandParameters); + double retVal = SendCommand(MtCommandType.OrderOpenPriceByTicket, commandParameters); return retVal; } public DateTime OrderOpenTime() { - var commandResponse = sendCommand(MtCommandType.OrderOpenTime, null); + var commandResponse = SendCommand(MtCommandType.OrderOpenTime, null); return MtApiTimeConverter.ConvertFromMtTime(commandResponse); } public void OrderPrint() { - sendCommand(MtCommandType.OrderPrint, null); + SendCommand(MtCommandType.OrderPrint, null); } public double OrderProfit() { - return sendCommand(MtCommandType.OrderProfit, null); + return SendCommand(MtCommandType.OrderProfit, null); } public bool OrderSelect(int index, OrderSelectMode select, OrderSelectSource pool) { var commandParameters = new ArrayList { index, (int)select, (int)pool }; - return sendCommand(MtCommandType.OrderSelect, commandParameters); + return SendCommand(MtCommandType.OrderSelect, commandParameters); } public bool OrderSelect(int index, OrderSelectMode select) @@ -321,49 +310,49 @@ namespace MtApi public int OrdersHistoryTotal() { - return sendCommand(MtCommandType.OrdersHistoryTotal, null); + return SendCommand(MtCommandType.OrdersHistoryTotal, null); } public double OrderStopLoss() { - return sendCommand(MtCommandType.OrderStopLoss, null); + return SendCommand(MtCommandType.OrderStopLoss, null); } public int OrdersTotal() { - return sendCommand(MtCommandType.OrdersTotal, null); + return SendCommand(MtCommandType.OrdersTotal, null); } public double OrderSwap() { - return sendCommand(MtCommandType.OrderSwap, null); + return SendCommand(MtCommandType.OrderSwap, null); } public string OrderSymbol() { - return sendCommand(MtCommandType.OrderSymbol, null); + return SendCommand(MtCommandType.OrderSymbol, null); } public double OrderTakeProfit() { - return sendCommand(MtCommandType.OrderTakeProfit, null); + return SendCommand(MtCommandType.OrderTakeProfit, null); } public int OrderTicket() { - return sendCommand(MtCommandType.OrderTicket, null); + return SendCommand(MtCommandType.OrderTicket, null); } public TradeOperation OrderType() { - int retVal = sendCommand(MtCommandType.OrderType, null); + int retVal = SendCommand(MtCommandType.OrderType, null); return (TradeOperation) retVal; } public bool OrderCloseAll() { - return sendCommand(MtCommandType.OrderCloseAll, null); + return SendCommand(MtCommandType.OrderCloseAll, null); } public MtOrder GetOrder(int index, OrderSelectMode select, OrderSelectSource pool) @@ -383,73 +372,73 @@ namespace MtApi public int GetLastError() { - return sendCommand(MtCommandType.GetLastError, null); + return SendCommand(MtCommandType.GetLastError, null); } public bool IsConnected() { - return sendCommand(MtCommandType.IsConnected, null); + return SendCommand(MtCommandType.IsConnected, null); } public bool IsDemo() { - return sendCommand(MtCommandType.IsDemo, null); + return SendCommand(MtCommandType.IsDemo, null); } public bool IsDllsAllowed() { - return sendCommand(MtCommandType.IsDllsAllowed, null); + return SendCommand(MtCommandType.IsDllsAllowed, null); } public bool IsExpertEnabled() { - return sendCommand(MtCommandType.IsExpertEnabled, null); + return SendCommand(MtCommandType.IsExpertEnabled, null); } public bool IsLibrariesAllowed() { - return sendCommand(MtCommandType.IsLibrariesAllowed, null); + return SendCommand(MtCommandType.IsLibrariesAllowed, null); } public bool IsOptimization() { - return sendCommand(MtCommandType.IsOptimization, null); + return SendCommand(MtCommandType.IsOptimization, null); } public bool IsStopped() { - return sendCommand(MtCommandType.IsStopped, null); + return SendCommand(MtCommandType.IsStopped, null); } public bool IsTesting() { - return sendCommand(MtCommandType.IsTesting, null); + return SendCommand(MtCommandType.IsTesting, null); } public bool IsTradeAllowed() { - return sendCommand(MtCommandType.IsTradeAllowed, null); + return SendCommand(MtCommandType.IsTradeAllowed, null); } public bool IsTradeContextBusy() { - return sendCommand(MtCommandType.IsTradeContextBusy, null); + return SendCommand(MtCommandType.IsTradeContextBusy, null); } public bool IsVisualMode() { - return sendCommand(MtCommandType.IsVisualMode, null); + return SendCommand(MtCommandType.IsVisualMode, null); } public int UninitializeReason() { - return sendCommand(MtCommandType.UninitializeReason, null); + return SendCommand(MtCommandType.UninitializeReason, null); } public string ErrorDescription(int errorCode) { var commandParameters = new ArrayList { errorCode }; - return sendCommand(MtCommandType.ErrorDescription, commandParameters); + return SendCommand(MtCommandType.ErrorDescription, commandParameters); } #endregion @@ -458,83 +447,83 @@ namespace MtApi public double AccountBalance() { - return sendCommand(MtCommandType.AccountBalance, null); + return SendCommand(MtCommandType.AccountBalance, null); } public double AccountCredit() { - return sendCommand(MtCommandType.AccountCredit, null); + return SendCommand(MtCommandType.AccountCredit, null); } public string AccountCompany() { - return sendCommand(MtCommandType.AccountCompany, null); + return SendCommand(MtCommandType.AccountCompany, null); } public string AccountCurrency() { - return sendCommand(MtCommandType.AccountCurrency, null); + return SendCommand(MtCommandType.AccountCurrency, null); } public double AccountEquity() { - return sendCommand(MtCommandType.AccountEquity, null); + return SendCommand(MtCommandType.AccountEquity, null); } public double AccountFreeMargin() { - return sendCommand(MtCommandType.AccountFreeMargin, null); + return SendCommand(MtCommandType.AccountFreeMargin, null); } public double AccountFreeMarginCheck(string symbol, TradeOperation cmd, double volume) { var commandParameters = new ArrayList { symbol, (int)cmd, volume }; - return sendCommand(MtCommandType.AccountFreeMarginCheck, commandParameters); + return SendCommand(MtCommandType.AccountFreeMarginCheck, commandParameters); } public double AccountFreeMarginMode() { - return sendCommand(MtCommandType.AccountFreeMarginMode, null); + return SendCommand(MtCommandType.AccountFreeMarginMode, null); } public int AccountLeverage() { - return sendCommand(MtCommandType.AccountLeverage, null); + return SendCommand(MtCommandType.AccountLeverage, null); } public double AccountMargin() { - return sendCommand(MtCommandType.AccountMargin, null); + return SendCommand(MtCommandType.AccountMargin, null); } public string AccountName() { - return sendCommand(MtCommandType.AccountName, null); + return SendCommand(MtCommandType.AccountName, null); } public int AccountNumber() { - return sendCommand(MtCommandType.AccountNumber, null); + return SendCommand(MtCommandType.AccountNumber, null); } public double AccountProfit() { - return sendCommand(MtCommandType.AccountProfit, null); + return SendCommand(MtCommandType.AccountProfit, null); } public string AccountServer() { - return sendCommand(MtCommandType.AccountServer, null); + return SendCommand(MtCommandType.AccountServer, null); } public int AccountStopoutLevel() { - return sendCommand(MtCommandType.AccountStopoutLevel, null); + return SendCommand(MtCommandType.AccountStopoutLevel, null); } public int AccountStopoutMode() { - return sendCommand(MtCommandType.AccountStopoutMode, null); + return SendCommand(MtCommandType.AccountStopoutMode, null); } #endregion @@ -544,30 +533,30 @@ namespace MtApi public void Alert(string msg) { var commandParameters = new ArrayList { msg }; - sendCommand(MtCommandType.Alert, commandParameters); + SendCommand(MtCommandType.Alert, commandParameters); } public void Comment(string msg) { var commandParameters = new ArrayList { msg }; - sendCommand(MtCommandType.Comment, commandParameters); + SendCommand(MtCommandType.Comment, commandParameters); } public int GetTickCount() { - return sendCommand(MtCommandType.GetTickCount, null); + return SendCommand(MtCommandType.GetTickCount, null); } public double MarketInfo(string symbol, MarketInfoModeType type) { var commandParameters = new ArrayList { symbol, (int)type }; - return sendCommand(MtCommandType.MarketInfo, commandParameters); + return SendCommand(MtCommandType.MarketInfo, commandParameters); } public int MessageBox(string text, string caption, int flag) { var commandParameters = new ArrayList { text, caption, flag }; - return sendCommand(MtCommandType.MessageBoxA, commandParameters); + return SendCommand(MtCommandType.MessageBoxA, commandParameters); } public int MessageBox(string text, string caption) @@ -578,43 +567,43 @@ namespace MtApi public int MessageBox(string text) { var commandParameters = new ArrayList { text }; - return sendCommand(MtCommandType.MessageBox, commandParameters); + return SendCommand(MtCommandType.MessageBox, commandParameters); } public void PlaySound(string filename) { var commandParameters = new ArrayList { filename }; - sendCommand(MtCommandType.PlaySound, commandParameters); + SendCommand(MtCommandType.PlaySound, commandParameters); } public void Print(string msg) { var commandParameters = new ArrayList { msg }; - sendCommand(MtCommandType.Print, commandParameters); + SendCommand(MtCommandType.Print, commandParameters); } public bool SendFTP(string filename) { var commandParameters = new ArrayList { filename }; - return sendCommand(MtCommandType.SendFTP, commandParameters); + return SendCommand(MtCommandType.SendFTP, commandParameters); } public bool SendFTP(string filename, string ftp_path) { var commandParameters = new ArrayList { filename, ftp_path }; - return sendCommand(MtCommandType.SendFTPA, commandParameters); + return SendCommand(MtCommandType.SendFTPA, commandParameters); } public void SendMail(string subject, string some_text) { var commandParameters = new ArrayList { subject, some_text }; - sendCommand(MtCommandType.SendMail, commandParameters); + SendCommand(MtCommandType.SendMail, commandParameters); } public void Sleep(int milliseconds) { var commandParameters = new ArrayList { milliseconds }; - sendCommand(MtCommandType.Sleep, commandParameters); + SendCommand(MtCommandType.Sleep, commandParameters); } #endregion @@ -623,17 +612,17 @@ namespace MtApi public string TerminalCompany() { - return sendCommand(MtCommandType.TerminalCompany, null); + return SendCommand(MtCommandType.TerminalCompany, null); } public string TerminalName() { - return sendCommand(MtCommandType.TerminalName, null); + return SendCommand(MtCommandType.TerminalName, null); } public string TerminalPath() { - return sendCommand(MtCommandType.TerminalPath, null); + return SendCommand(MtCommandType.TerminalPath, null); } #endregion @@ -642,103 +631,103 @@ namespace MtApi public int Day() { - return sendCommand(MtCommandType.Day, null); + return SendCommand(MtCommandType.Day, null); } public int DayOfWeek() { - return sendCommand(MtCommandType.DayOfWeek, null); + return SendCommand(MtCommandType.DayOfWeek, null); } public int DayOfYear() { - return sendCommand(MtCommandType.DayOfYear, null); + return SendCommand(MtCommandType.DayOfYear, null); } public int Hour() { - return sendCommand(MtCommandType.Hour, null); + return SendCommand(MtCommandType.Hour, null); } public int Minute() { - return sendCommand(MtCommandType.Minute, null); + return SendCommand(MtCommandType.Minute, null); } public int Month() { - return sendCommand(MtCommandType.Month, null); + return SendCommand(MtCommandType.Month, null); } public int Seconds() { - return sendCommand(MtCommandType.Seconds, null); + return SendCommand(MtCommandType.Seconds, null); } public DateTime TimeCurrent() { - var commandResponse = sendCommand(MtCommandType.TimeCurrent, null); + var commandResponse = SendCommand(MtCommandType.TimeCurrent, null); return MtApiTimeConverter.ConvertFromMtTime(commandResponse); } public int TimeDay(DateTime date) { var commandParameters = new ArrayList { MtApiTimeConverter.ConvertToMtTime(date) }; - return sendCommand(MtCommandType.TimeDay, commandParameters); + return SendCommand(MtCommandType.TimeDay, commandParameters); } public int TimeDayOfWeek(DateTime date) { var commandParameters = new ArrayList { MtApiTimeConverter.ConvertToMtTime(date) }; - return sendCommand(MtCommandType.TimeDayOfWeek, commandParameters); + return SendCommand(MtCommandType.TimeDayOfWeek, commandParameters); } public int TimeDayOfYear(DateTime date) { var commandParameters = new ArrayList { MtApiTimeConverter.ConvertToMtTime(date) }; - return sendCommand(MtCommandType.TimeDayOfYear, commandParameters); + return SendCommand(MtCommandType.TimeDayOfYear, commandParameters); } public int TimeHour(DateTime time) { var commandParameters = new ArrayList { MtApiTimeConverter.ConvertToMtTime(time) }; - return sendCommand(MtCommandType.TimeHour, commandParameters); + return SendCommand(MtCommandType.TimeHour, commandParameters); } public DateTime TimeLocal() { - var commandResponse = sendCommand(MtCommandType.TimeLocal, null); + var commandResponse = SendCommand(MtCommandType.TimeLocal, null); return MtApiTimeConverter.ConvertFromMtTime(commandResponse); } public int TimeMinute(DateTime time) { var commandParameters = new ArrayList { MtApiTimeConverter.ConvertToMtTime(time) }; - return sendCommand(MtCommandType.TimeMinute, commandParameters); + return SendCommand(MtCommandType.TimeMinute, commandParameters); } public int TimeMonth(DateTime time) { var commandParameters = new ArrayList { MtApiTimeConverter.ConvertToMtTime(time) }; - return sendCommand(MtCommandType.TimeMonth, commandParameters); + return SendCommand(MtCommandType.TimeMonth, commandParameters); } public int TimeSeconds(DateTime time) { var commandParameters = new ArrayList { MtApiTimeConverter.ConvertToMtTime(time) }; - return sendCommand(MtCommandType.TimeSeconds, commandParameters); + return SendCommand(MtCommandType.TimeSeconds, commandParameters); } public int TimeYear(DateTime time) { var commandParameters = new ArrayList { MtApiTimeConverter.ConvertToMtTime(time) }; - return sendCommand(MtCommandType.TimeYear, commandParameters); + return SendCommand(MtCommandType.TimeYear, commandParameters); } public int Year(DateTime time) { var commandParameters = new ArrayList { MtApiTimeConverter.ConvertToMtTime(time) }; - return sendCommand(MtCommandType.Year, commandParameters); + return SendCommand(MtCommandType.Year, commandParameters); } #endregion @@ -747,49 +736,49 @@ namespace MtApi public bool GlobalVariableCheck(string name) { var commandParameters = new ArrayList { name }; - return sendCommand(MtCommandType.GlobalVariableCheck, commandParameters); + return SendCommand(MtCommandType.GlobalVariableCheck, commandParameters); } public bool GlobalVariableDel(string name) { var commandParameters = new ArrayList { name }; - return sendCommand(MtCommandType.GlobalVariableDel, commandParameters); + return SendCommand(MtCommandType.GlobalVariableDel, commandParameters); } public double GlobalVariableGet(string name) { var commandParameters = new ArrayList { name }; - return sendCommand(MtCommandType.GlobalVariableGet, commandParameters); + return SendCommand(MtCommandType.GlobalVariableGet, commandParameters); } public string GlobalVariableName(int index) { var commandParameters = new ArrayList { index }; - return sendCommand(MtCommandType.GlobalVariableName, commandParameters); + return SendCommand(MtCommandType.GlobalVariableName, commandParameters); } public DateTime GlobalVariableSet(string name, double value) { var commandParameters = new ArrayList { name, value }; - var commandResponse = sendCommand(MtCommandType.GlobalVariableSet, commandParameters); + var commandResponse = SendCommand(MtCommandType.GlobalVariableSet, commandParameters); return MtApiTimeConverter.ConvertFromMtTime(commandResponse); } public bool GlobalVariableSetOnCondition(string name, double value, double check_value) { var commandParameters = new ArrayList { name, value, check_value }; - return sendCommand(MtCommandType.GlobalVariableSetOnCondition, commandParameters); + return SendCommand(MtCommandType.GlobalVariableSetOnCondition, commandParameters); } public int GlobalVariablesDeleteAll(string prefix_name) { var commandParameters = new ArrayList { prefix_name }; - return sendCommand(MtCommandType.GlobalVariableSetOnCondition, commandParameters); + return SendCommand(MtCommandType.GlobalVariableSetOnCondition, commandParameters); } public int GlobalVariablesTotal() { - return sendCommand(MtCommandType.GlobalVariablesTotal, null); + return SendCommand(MtCommandType.GlobalVariablesTotal, null); } #endregion @@ -798,49 +787,49 @@ namespace MtApi public double iAC(string symbol, ChartPeriod timeframe, int shift) { var commandParameters = new ArrayList { symbol, (int)timeframe, shift }; - return sendCommand(MtCommandType.iAC, commandParameters); + return SendCommand(MtCommandType.iAC, commandParameters); } public double iAD(string symbol, int timeframe, int shift) { var commandParameters = new ArrayList { symbol, timeframe, shift }; - return sendCommand(MtCommandType.iAD, commandParameters); + return SendCommand(MtCommandType.iAD, commandParameters); } public double iAlligator(string symbol, int timeframe, int jaw_period, int jaw_shift, int teeth_period, int teeth_shift, int lips_period, int lips_shift, int ma_method, int applied_price, int mode, int shift) { var commandParameters = new ArrayList { symbol, timeframe, jaw_period, jaw_shift, teeth_period, teeth_shift, lips_period, lips_shift, ma_method, applied_price, mode, shift }; - return sendCommand(MtCommandType.iAlligator, commandParameters); + return SendCommand(MtCommandType.iAlligator, commandParameters); } public double iADX(string symbol, int timeframe, int period, int applied_price, int mode, int shift) { var commandParameters = new ArrayList { symbol, timeframe, period, applied_price, mode, shift }; - return sendCommand(MtCommandType.iADX, commandParameters); + return SendCommand(MtCommandType.iADX, commandParameters); } public double iATR(string symbol, int timeframe, int period, int shift) { var commandParameters = new ArrayList { symbol, timeframe, period, shift }; - return sendCommand(MtCommandType.iATR, commandParameters); + return SendCommand(MtCommandType.iATR, commandParameters); } public double iAO(string symbol, int timeframe, int shift) { var commandParameters = new ArrayList { symbol, timeframe, shift }; - return sendCommand(MtCommandType.iAO, commandParameters); + return SendCommand(MtCommandType.iAO, commandParameters); } public double iBearsPower(string symbol, int timeframe, int period, int applied_price, int shift) { var commandParameters = new ArrayList { symbol, timeframe, period, applied_price, shift }; - return sendCommand(MtCommandType.iBearsPower, commandParameters); + return SendCommand(MtCommandType.iBearsPower, commandParameters); } public double iBands(string symbol, int timeframe, int period, int deviation, int bands_shift, int applied_price, int mode, int shift) { var commandParameters = new ArrayList { symbol, timeframe, period, deviation, bands_shift, applied_price, mode, shift }; - return sendCommand(MtCommandType.iBands, commandParameters); + return SendCommand(MtCommandType.iBands, commandParameters); } public double iBandsOnArray(double[] array, int total, int period, int deviation, int bands_shift, int mode, int shift) @@ -855,19 +844,19 @@ namespace MtApi commandParameters.Add(mode); commandParameters.Add(shift); - return sendCommand(MtCommandType.iBandsOnArray, commandParameters); + return SendCommand(MtCommandType.iBandsOnArray, commandParameters); } public double iBullsPower(string symbol, int timeframe, int period, int applied_price, int shift) { var commandParameters = new ArrayList { symbol, timeframe, period, applied_price, shift }; - return sendCommand(MtCommandType.iBullsPower, commandParameters); + return SendCommand(MtCommandType.iBullsPower, commandParameters); } public double iCCI(string symbol, int timeframe, int period, int applied_price, int shift) { var commandParameters = new ArrayList { symbol, timeframe, period, applied_price, shift }; - return sendCommand(MtCommandType.iCCI, commandParameters); + return SendCommand(MtCommandType.iCCI, commandParameters); } public double iCCIOnArray(double[] array, int total, int period, int shift) @@ -879,7 +868,7 @@ namespace MtApi commandParameters.Add(period); commandParameters.Add(shift); - return sendCommand(MtCommandType.iCCIOnArray, commandParameters); + return SendCommand(MtCommandType.iCCIOnArray, commandParameters); } public double iCustom(string symbol, int timeframe, string name, int[] parameters, int mode, int shift) @@ -894,7 +883,7 @@ namespace MtApi commandParameters.Add(mode); commandParameters.Add(shift); - return sendCommand(MtCommandType.iCustom, commandParameters); + return SendCommand(MtCommandType.iCustom, commandParameters); } public double iCustom(string symbol, int timeframe, string name, double[] parameters, int mode, int shift) @@ -906,19 +895,19 @@ namespace MtApi commandParameters.Add(mode); commandParameters.Add(shift); - return sendCommand(MtCommandType.iCustom_d, commandParameters); + return SendCommand(MtCommandType.iCustom_d, commandParameters); } public double iDeMarker(string symbol, int timeframe, int period, int shift) { var commandParameters = new ArrayList { symbol, timeframe, period, shift }; - return sendCommand(MtCommandType.iDeMarker, commandParameters); + return SendCommand(MtCommandType.iDeMarker, commandParameters); } public double iEnvelopes(string symbol, int timeframe, int ma_period, int ma_method, int ma_shift, int applied_price, double deviation, int mode, int shift) { var commandParameters = new ArrayList { symbol, timeframe, ma_period, ma_method, ma_shift, applied_price, deviation, mode, shift }; - return sendCommand(MtCommandType.iEnvelopes, commandParameters); + return SendCommand(MtCommandType.iEnvelopes, commandParameters); } public double iEnvelopesOnArray(double[] array, int total, int ma_period, int ma_method, int ma_shift, double deviation, int mode, int shift) @@ -934,43 +923,43 @@ namespace MtApi commandParameters.Add(mode); commandParameters.Add(shift); - return sendCommand(MtCommandType.iEnvelopesOnArray, commandParameters); + return SendCommand(MtCommandType.iEnvelopesOnArray, commandParameters); } public double iForce(string symbol, int timeframe, int period, int ma_method, int applied_price, int shift) { var commandParameters = new ArrayList { symbol, timeframe, period, ma_method, applied_price, shift }; - return sendCommand(MtCommandType.iForce, commandParameters); + return SendCommand(MtCommandType.iForce, commandParameters); } public double iFractals(string symbol, int timeframe, int mode, int shift) { var commandParameters = new ArrayList { symbol, timeframe, mode, shift }; - return sendCommand(MtCommandType.iFractals, commandParameters); + return SendCommand(MtCommandType.iFractals, commandParameters); } public double iGator(string symbol, int timeframe, int jaw_period, int jaw_shift, int teeth_period, int teeth_shift, int lips_period, int lips_shift, int ma_method, int applied_price, int mode, int shift) { var commandParameters = new ArrayList { symbol, timeframe, jaw_period, jaw_shift, teeth_period, teeth_shift, lips_period, lips_shift, ma_method, applied_price, mode, shift }; - return sendCommand(MtCommandType.iGator, commandParameters); + return SendCommand(MtCommandType.iGator, commandParameters); } public double iIchimoku(string symbol, int timeframe, int tenkan_sen, int kijun_sen, int senkou_span_b, int mode, int shift) { var commandParameters = new ArrayList { symbol, timeframe, tenkan_sen, kijun_sen, senkou_span_b, mode, shift }; - return sendCommand(MtCommandType.iIchimoku, commandParameters); + return SendCommand(MtCommandType.iIchimoku, commandParameters); } public double iBWMFI(string symbol, int timeframe, int shift) { var commandParameters = new ArrayList { symbol, timeframe, shift }; - return sendCommand(MtCommandType.iBWMFI, commandParameters); + return SendCommand(MtCommandType.iBWMFI, commandParameters); } public double iMomentum(string symbol, int timeframe, int period, int applied_price, int shift) { var commandParameters = new ArrayList { symbol, timeframe, period, applied_price, shift }; - return sendCommand(MtCommandType.iMomentum, commandParameters); + return SendCommand(MtCommandType.iMomentum, commandParameters); } public double iMomentumOnArray(double[] array, int total, int period, int shift) @@ -982,19 +971,19 @@ namespace MtApi commandParameters.Add(period); commandParameters.Add(shift); - return sendCommand(MtCommandType.iMomentumOnArray, commandParameters); + return SendCommand(MtCommandType.iMomentumOnArray, commandParameters); } public double iMFI(string symbol, int timeframe, int period, int shift) { var commandParameters = new ArrayList { symbol, timeframe, period, shift }; - return sendCommand(MtCommandType.iMFI, commandParameters); + return SendCommand(MtCommandType.iMFI, commandParameters); } public double iMA(string symbol, int timeframe, int period, int ma_shift, int ma_method, int applied_price, int shift) { var commandParameters = new ArrayList { symbol, timeframe, period, ma_shift, ma_method, applied_price, shift }; - return sendCommand(MtCommandType.iMA, commandParameters); + return SendCommand(MtCommandType.iMA, commandParameters); } double iMAOnArray(double[] array, int total, int period, int ma_shift, int ma_method, int shift) @@ -1008,37 +997,37 @@ namespace MtApi commandParameters.Add(ma_method); commandParameters.Add(shift); - return sendCommand(MtCommandType.iMAOnArray, commandParameters); + return SendCommand(MtCommandType.iMAOnArray, commandParameters); } public double iOsMA(string symbol, int timeframe, int fast_ema_period, int slow_ema_period, int signal_period, int applied_price, int shift) { var commandParameters = new ArrayList { symbol, timeframe, fast_ema_period, slow_ema_period, signal_period, applied_price, shift }; - return sendCommand(MtCommandType.iOsMA, commandParameters); + return SendCommand(MtCommandType.iOsMA, commandParameters); } public double iMACD(string symbol, int timeframe, int fast_ema_period, int slow_ema_period, int signal_period, int applied_price, int mode, int shift) { var commandParameters = new ArrayList { symbol, timeframe, fast_ema_period, slow_ema_period, signal_period, applied_price, mode, shift }; - return sendCommand(MtCommandType.iMACD, commandParameters); + return SendCommand(MtCommandType.iMACD, commandParameters); } public double iOBV(string symbol, int timeframe, int applied_price, int shift) { var commandParameters = new ArrayList { symbol, timeframe, applied_price, shift }; - return sendCommand(MtCommandType.iOBV, commandParameters); + return SendCommand(MtCommandType.iOBV, commandParameters); } public double iSAR(string symbol, int timeframe, double step, double maximum, int shift) { var commandParameters = new ArrayList { symbol, timeframe, step, maximum, shift }; - return sendCommand(MtCommandType.iSAR, commandParameters); + return SendCommand(MtCommandType.iSAR, commandParameters); } public double iRSI( string symbol, int timeframe, int period, int applied_price, int shift) { var commandParameters = new ArrayList { symbol, timeframe, period, applied_price, shift }; - return sendCommand(MtCommandType.iRSI, commandParameters); + return SendCommand(MtCommandType.iRSI, commandParameters); } public double iRSIOnArray(double[] array, int total, int period, int shift) @@ -1050,19 +1039,19 @@ namespace MtApi commandParameters.Add(period); commandParameters.Add(shift); - return sendCommand(MtCommandType.iMomentumOnArray, commandParameters); + return SendCommand(MtCommandType.iMomentumOnArray, commandParameters); } public double iRVI(string symbol, int timeframe, int period, int mode, int shift) { var commandParameters = new ArrayList { symbol, timeframe, period, mode, shift }; - return sendCommand(MtCommandType.iRVI, commandParameters); + return SendCommand(MtCommandType.iRVI, commandParameters); } public double iStdDev(string symbol, int timeframe, int ma_period, int ma_shift, int ma_method, int applied_price, int shift) { var commandParameters = new ArrayList { symbol, timeframe, ma_period, ma_shift, ma_method, applied_price, shift }; - return sendCommand(MtCommandType.iStdDev, commandParameters); + return SendCommand(MtCommandType.iStdDev, commandParameters); } public double iStdDevOnArray(double[] array, int total, int ma_period, int ma_shift, int ma_method, int shift) @@ -1076,19 +1065,19 @@ namespace MtApi commandParameters.Add(ma_method); commandParameters.Add(shift); - return sendCommand(MtCommandType.iStdDevOnArray, commandParameters); + return SendCommand(MtCommandType.iStdDevOnArray, commandParameters); } public double iStochastic(string symbol, int timeframe, int pKperiod, int pDperiod, int slowing, int method, int price_field, int mode, int shift) { var commandParameters = new ArrayList { symbol, timeframe, pKperiod, pDperiod, slowing, method, price_field, mode, shift }; - return sendCommand(MtCommandType.iStochastic, commandParameters); + return SendCommand(MtCommandType.iStochastic, commandParameters); } public double iWPR(string symbol, int timeframe, int period, int shift) { var commandParameters = new ArrayList { symbol, timeframe, period, shift }; - return sendCommand(MtCommandType.iWPR, commandParameters); + return SendCommand(MtCommandType.iWPR, commandParameters); } #endregion @@ -1096,67 +1085,67 @@ namespace MtApi public int iBars(string symbol, ChartPeriod timeframe) { var commandParameters = new ArrayList { symbol, (int)timeframe }; - return sendCommand(MtCommandType.iBars, commandParameters); + return SendCommand(MtCommandType.iBars, commandParameters); } public int iBarShift(string symbol, ChartPeriod timeframe, DateTime time, bool exact) { var commandParameters = new ArrayList { symbol, (int)timeframe, MtApiTimeConverter.ConvertToMtTime(time), exact }; - return sendCommand(MtCommandType.iBarShift, commandParameters); + return SendCommand(MtCommandType.iBarShift, commandParameters); } public double iClose(string symbol, ChartPeriod timeframe, int shift) { var commandParameters = new ArrayList { symbol, (int)timeframe, shift }; - return sendCommand(MtCommandType.iClose, commandParameters); + return SendCommand(MtCommandType.iClose, commandParameters); } public double iHigh(string symbol, ChartPeriod timeframe, int shift) { var commandParameters = new ArrayList { symbol, (int)timeframe, shift }; - return sendCommand(MtCommandType.iHigh, commandParameters); + return SendCommand(MtCommandType.iHigh, commandParameters); } public int iHighest(string symbol, ChartPeriod timeframe, SeriesIdentifier type, int count, int start) { var commandParameters = new ArrayList { symbol, (int)timeframe, (int)type, count, start }; - return sendCommand(MtCommandType.iHighest, commandParameters); + return SendCommand(MtCommandType.iHighest, commandParameters); } public double iLow(string symbol, ChartPeriod timeframe, int shift) { var commandParameters = new ArrayList { symbol, (int)timeframe, shift }; - return sendCommand(MtCommandType.iLow, commandParameters); + return SendCommand(MtCommandType.iLow, commandParameters); } public int iLowest(string symbol, ChartPeriod timeframe, SeriesIdentifier type, int count, int start) { var commandParameters = new ArrayList { symbol, (int)timeframe, (int)type, count, start }; - return sendCommand(MtCommandType.iLowest, commandParameters); + return SendCommand(MtCommandType.iLowest, commandParameters); } public double iOpen(string symbol, ChartPeriod timeframe, int shift) { var commandParameters = new ArrayList { symbol, (int)timeframe, shift }; - return sendCommand(MtCommandType.iOpen, commandParameters); + return SendCommand(MtCommandType.iOpen, commandParameters); } public DateTime iTime(string symbol, ChartPeriod timeframe, int shift) { var commandParameters = new ArrayList { symbol, (int)timeframe, shift }; - var commandResponse = sendCommand(MtCommandType.iTime, commandParameters); + var commandResponse = SendCommand(MtCommandType.iTime, commandParameters); return MtApiTimeConverter.ConvertFromMtTime(commandResponse); } public double iVolume(string symbol, ChartPeriod timeframe, int shift) { var commandParameters = new ArrayList { symbol, (int)timeframe, shift }; - return sendCommand(MtCommandType.iVolume, commandParameters); + return SendCommand(MtCommandType.iVolume, commandParameters); } //public double[] iCloseArray(string symbol, ChartPeriod timeframe, int shift, int valueCount) //{ - // int doubleArraySendLimit = DOUBLE_ARRAY_LIMIT; + // int doubleArraySendLimit = DoubleArrayLimit; // int limitCount = valueCount / doubleArraySendLimit; // int valueCountModulo = valueCount - limitCount * doubleArraySendLimit; @@ -1164,7 +1153,7 @@ namespace MtApi // for (int i = 0; i < limitCount; i++) // { // var commandParameters = new ArrayList { symbol, (int)timeframe, i * doubleArraySendLimit, doubleArraySendLimit }; - // var result = sendCommand(MtCommandType.iCloseArray, commandParameters); + // var result = SendCommand(MtCommandType.iCloseArray, commandParameters); // if (result != null) // Array.Copy(result, 0, resultArray, i * doubleArraySendLimit, doubleArraySendLimit); // } @@ -1172,44 +1161,44 @@ namespace MtApi // if (valueCountModulo > 0) // { // var commandParameters = new ArrayList { symbol, (int)timeframe, limitCount * doubleArraySendLimit, valueCountModulo }; - // var result = sendCommand(MtCommandType.iCloseArray, commandParameters); + // var result = SendCommand(MtCommandType.iCloseArray, commandParameters); // if (result != null) // Array.Copy(result, 0, resultArray, limitCount * doubleArraySendLimit, valueCountModulo); // } // return resultArray; // var commandParameters = new ArrayList { symbol, (int)timeframe, shift, valueCount }; - // return sendCommand(MtCommandType.iCloseArray, commandParameters); + // return SendCommand(MtCommandType.iCloseArray, commandParameters); //} public double[] iCloseArray(string symbol, ChartPeriod timeframe) { var commandParameters = new ArrayList { symbol, (int)timeframe }; - return sendCommand(MtCommandType.iCloseArray, commandParameters); + return SendCommand(MtCommandType.iCloseArray, commandParameters); } public double[] iHighArray(string symbol, ChartPeriod timeframe) { var commandParameters = new ArrayList { symbol, (int)timeframe }; - return sendCommand(MtCommandType.iHighArray, commandParameters); + return SendCommand(MtCommandType.iHighArray, commandParameters); } public double[] iLowArray(string symbol, ChartPeriod timeframe) { var commandParameters = new ArrayList { symbol, (int)timeframe }; - return sendCommand(MtCommandType.iLowArray, commandParameters); + return SendCommand(MtCommandType.iLowArray, commandParameters); } public double[] iOpenArray(string symbol, ChartPeriod timeframe) { var commandParameters = new ArrayList { symbol, (int)timeframe }; - return sendCommand(MtCommandType.iOpenArray, commandParameters); + return SendCommand(MtCommandType.iOpenArray, commandParameters); } public double[] iVolumeArray(string symbol, ChartPeriod timeframe) { var commandParameters = new ArrayList { symbol, (int)timeframe }; - return sendCommand(MtCommandType.iVolumeArray, commandParameters); + return SendCommand(MtCommandType.iVolumeArray, commandParameters); } public DateTime[] iTimeArray(string symbol, ChartPeriod timeframe) @@ -1218,7 +1207,7 @@ namespace MtApi var commandParameters = new ArrayList { symbol, (int)timeframe }; - var response = sendCommand(MtCommandType.iTimeArray, commandParameters); + var response = SendCommand(MtCommandType.iTimeArray, commandParameters); if (response != null) { @@ -1235,7 +1224,7 @@ namespace MtApi public bool RefreshRates() { - return sendCommand(MtCommandType.RefreshRates, null); + return SendCommand(MtCommandType.RefreshRates, null); } #endregion @@ -1244,13 +1233,13 @@ namespace MtApi public string TerminalInfoString(ENUM_TERMINAL_INFO_STRING property_id) { var commandParameters = new ArrayList { (int)property_id }; - return sendCommand(MtCommandType.TerminalInfoString, commandParameters); + return SendCommand(MtCommandType.TerminalInfoString, commandParameters); } public string SymbolInfoString(string name, ENUM_SYMBOL_INFO_STRING prop_id) { var commandParameters = new ArrayList { name, (int)prop_id }; - return sendCommand(MtCommandType.SymbolInfoString, commandParameters); ; + return SendCommand(MtCommandType.SymbolInfoString, commandParameters); ; } #endregion @@ -1312,7 +1301,44 @@ namespace MtApi ConnectionState = MtConnectionState.Disconnected; ConnectionStateChanged.FireEvent(this, new MtConnectionEventArgs(MtConnectionState.Disconnected, "Disconnected")); } - private T sendCommand(MtCommandType commandType, ArrayList commandParameters) + + private int InternalOrderSend(string symbol, TradeOperation cmd, double volume, double? price, + int? slippage, double? stoploss, double? takeprofit, string comment, int? magic, DateTime? expiration, Color? arrowColor) + { + var response = SendRequest(new OrderSendRequest + { + Symbol = symbol, + Cmd = (int)cmd, + Volume = volume, + Price = price, + Slippage = slippage, + Stoploss = stoploss, + Takeprofit = takeprofit, + Comment = comment, + Magic = magic, + Expiration = expiration.HasValue ? MtApiTimeConverter.ConvertToMtTime(expiration.Value) : default(int?), + ArrowColor = arrowColor.HasValue ? MtApiColorConverter.ConvertToMtColor(arrowColor.Value) : default(int?) + }); + return response != null ? response.Ticket : -1; + } + + public bool InternalOrderClose(int ticket, double? lots, double? price, int? slippage, Color? color) + { + //var commandParameters = new ArrayList { ticket, lots, price, slippage, MtApiColorConverter.ConvertToMtColor(color) }; + //return SendCommand(MtCommandType.OrderClose, commandParameters); + + var response = SendRequest(new OrderCloseRequest + { + Ticket = ticket, + Lots = lots, + Price = price, + Slippage = slippage, + ArrowColor = color.HasValue ? MtApiColorConverter.ConvertToMtColor(color.Value) : default(int?) + }); + return response != null; + } + + private T SendCommand(MtCommandType commandType, ArrayList commandParameters) { var response = mClient.SendCommand((int)commandType, commandParameters); diff --git a/MtApi/MtCommandType.cs b/MtApi/MtCommandType.cs index 164044dd..66cf81ca 100755 --- a/MtApi/MtCommandType.cs +++ b/MtApi/MtCommandType.cs @@ -13,8 +13,9 @@ namespace MtApi // OrderSend = 1, // OrderSendBuy = 1001, // OrderSendSell = 1002, - OrderClose = 2, - OrderCloseByCurrentPrice = 152, +// OrderClose = 2, +// OrderCloseByCurrentPrice = 152, + OrderCloseBy = 3, OrderClosePrice = 4, OrderClosePriceByTicket = 1004, diff --git a/MtApi/Requests/OrderCloseRequest.cs b/MtApi/Requests/OrderCloseRequest.cs new file mode 100644 index 00000000..2b34b829 --- /dev/null +++ b/MtApi/Requests/OrderCloseRequest.cs @@ -0,0 +1,17 @@ +namespace MtApi.Requests +{ + public class OrderCloseRequest: RequestBase + { + public int Ticket { get; set; } + + public double? Lots { get; set; } + public double? Price { get; set; } + public int? Slippage { get; set; } + public int? ArrowColor { get; set; } + + public override RequestType RequestType + { + get { return RequestType.OrderClose; } + } + } +} \ No newline at end of file diff --git a/MtApi/Requests/RequestType.cs b/MtApi/Requests/RequestType.cs index a93ceb59..eb1794e9 100644 --- a/MtApi/Requests/RequestType.cs +++ b/MtApi/Requests/RequestType.cs @@ -5,6 +5,7 @@ Unknown = 0, GetOrder = 1, GetOrders = 2, - OrderSend = 3 + OrderSend = 3, + OrderClose = 4, } } \ No newline at end of file diff --git a/TestApiClientUI/Form1.Designer.cs b/TestApiClientUI/Form1.Designer.cs index d29bbcc4..d09d0870 100755 --- a/TestApiClientUI/Form1.Designer.cs +++ b/TestApiClientUI/Form1.Designer.cs @@ -48,6 +48,7 @@ this.contextMenuStrip1 = new System.Windows.Forms.ContextMenuStrip(this.components); this.tabControl1 = new System.Windows.Forms.TabControl(); this.tabPage2 = new System.Windows.Forms.TabPage(); + this.button20 = new System.Windows.Forms.Button(); this.button17 = new System.Windows.Forms.Button(); this.comboBox2 = new System.Windows.Forms.ComboBox(); this.comboBox1 = new System.Windows.Forms.ComboBox(); @@ -60,6 +61,8 @@ this.listBoxClosedOrders = new System.Windows.Forms.ListBox(); this.button2 = new System.Windows.Forms.Button(); this.groupBox3 = new System.Windows.Forms.GroupBox(); + this.button19 = new System.Windows.Forms.Button(); + this.button18 = new System.Windows.Forms.Button(); this.comboBoxOrderColor = new System.Windows.Forms.ComboBox(); this.textBox2 = new System.Windows.Forms.TextBox(); this.textBoxOrderMagic = new System.Windows.Forms.TextBox(); @@ -118,11 +121,9 @@ this.button6 = new System.Windows.Forms.Button(); this.listBoxProceHistory = new System.Windows.Forms.ListBox(); this.tabPage6 = new System.Windows.Forms.TabPage(); - this.button15 = new System.Windows.Forms.Button(); this.button14 = new System.Windows.Forms.Button(); this.button13 = new System.Windows.Forms.Button(); - this.button18 = new System.Windows.Forms.Button(); - this.button19 = new System.Windows.Forms.Button(); + this.checkBox1 = new System.Windows.Forms.CheckBox(); this.groupBox1.SuspendLayout(); this.statusStrip1.SuspendLayout(); this.groupBox2.SuspendLayout(); @@ -303,6 +304,8 @@ // // tabPage2 // + this.tabPage2.Controls.Add(this.checkBox1); + this.tabPage2.Controls.Add(this.button20); this.tabPage2.Controls.Add(this.button17); this.tabPage2.Controls.Add(this.comboBox2); this.tabPage2.Controls.Add(this.comboBox1); @@ -324,9 +327,19 @@ this.tabPage2.Text = "Trade Operations"; this.tabPage2.UseVisualStyleBackColor = true; // + // button20 + // + this.button20.Location = new System.Drawing.Point(216, 185); + this.button20.Name = "button20"; + this.button20.Size = new System.Drawing.Size(75, 23); + this.button20.TabIndex = 13; + this.button20.Text = "CloseOrder"; + this.button20.UseVisualStyleBackColor = true; + this.button20.Click += new System.EventHandler(this.button20_Click); + // // button17 // - this.button17.Location = new System.Drawing.Point(601, 290); + this.button17.Location = new System.Drawing.Point(601, 158); this.button17.Name = "button17"; this.button17.Size = new System.Drawing.Size(75, 23); this.button17.TabIndex = 12; @@ -341,7 +354,7 @@ this.comboBox2.Items.AddRange(new object[] { "MODE_TRADES", "MODE_HISTORY"}); - this.comboBox2.Location = new System.Drawing.Point(481, 261); + this.comboBox2.Location = new System.Drawing.Point(341, 158); this.comboBox2.Name = "comboBox2"; this.comboBox2.Size = new System.Drawing.Size(114, 21); this.comboBox2.TabIndex = 11; @@ -353,14 +366,14 @@ this.comboBox1.Items.AddRange(new object[] { "SELECT_BY_POS", "SELECT_BY_TICKET"}); - this.comboBox1.Location = new System.Drawing.Point(356, 261); + this.comboBox1.Location = new System.Drawing.Point(216, 158); this.comboBox1.Name = "comboBox1"; this.comboBox1.Size = new System.Drawing.Size(119, 21); this.comboBox1.TabIndex = 10; // // button16 // - this.button16.Location = new System.Drawing.Point(601, 261); + this.button16.Location = new System.Drawing.Point(461, 158); this.button16.Name = "button16"; this.button16.Size = new System.Drawing.Size(75, 23); this.button16.TabIndex = 9; @@ -371,7 +384,7 @@ // label21 // this.label21.AutoSize = true; - this.label21.Location = new System.Drawing.Point(213, 264); + this.label21.Location = new System.Drawing.Point(213, 135); this.label21.Name = "label21"; this.label21.Size = new System.Drawing.Size(71, 13); this.label21.TabIndex = 8; @@ -379,9 +392,9 @@ // // textBoxIndexTicket // - this.textBoxIndexTicket.Location = new System.Drawing.Point(290, 261); + this.textBoxIndexTicket.Location = new System.Drawing.Point(290, 132); this.textBoxIndexTicket.Name = "textBoxIndexTicket"; - this.textBoxIndexTicket.Size = new System.Drawing.Size(60, 20); + this.textBoxIndexTicket.Size = new System.Drawing.Size(109, 20); this.textBoxIndexTicket.TabIndex = 7; this.textBoxIndexTicket.Text = "0"; // @@ -413,7 +426,7 @@ "OrderTakeProfit", "OrderTicket", "OrderType"}); - this.comboBoxSelectedCommand.Location = new System.Drawing.Point(216, 292); + this.comboBoxSelectedCommand.Location = new System.Drawing.Point(213, 344); this.comboBoxSelectedCommand.Name = "comboBoxSelectedCommand"; this.comboBoxSelectedCommand.Size = new System.Drawing.Size(121, 21); this.comboBoxSelectedCommand.TabIndex = 6; @@ -430,7 +443,7 @@ // label16 // this.label16.AutoSize = true; - this.label16.Location = new System.Drawing.Point(213, 135); + this.label16.Location = new System.Drawing.Point(210, 69); this.label16.Name = "label16"; this.label16.Size = new System.Drawing.Size(42, 13); this.label16.TabIndex = 4; @@ -441,14 +454,14 @@ this.listBoxClosedOrders.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right))); this.listBoxClosedOrders.FormattingEnabled = true; - this.listBoxClosedOrders.Location = new System.Drawing.Point(213, 151); + this.listBoxClosedOrders.Location = new System.Drawing.Point(213, 85); this.listBoxClosedOrders.Name = "listBoxClosedOrders"; - this.listBoxClosedOrders.Size = new System.Drawing.Size(463, 108); + this.listBoxClosedOrders.Size = new System.Drawing.Size(463, 43); this.listBoxClosedOrders.TabIndex = 5; // // button2 // - this.button2.Location = new System.Drawing.Point(343, 292); + this.button2.Location = new System.Drawing.Point(356, 342); this.button2.Name = "button2"; this.button2.Size = new System.Drawing.Size(87, 23); this.button2.TabIndex = 2; @@ -490,6 +503,26 @@ this.groupBox3.TabStop = false; this.groupBox3.Text = "Send Order"; // + // button19 + // + this.button19.Location = new System.Drawing.Point(87, 336); + this.button19.Name = "button19"; + this.button19.Size = new System.Drawing.Size(75, 23); + this.button19.TabIndex = 15; + this.button19.Text = "Sell"; + this.button19.UseVisualStyleBackColor = true; + this.button19.Click += new System.EventHandler(this.button19_Click); + // + // button18 + // + this.button18.Location = new System.Drawing.Point(6, 336); + this.button18.Name = "button18"; + this.button18.Size = new System.Drawing.Size(75, 23); + this.button18.TabIndex = 14; + this.button18.Text = "Buy"; + this.button18.UseVisualStyleBackColor = true; + this.button18.Click += new System.EventHandler(this.button18_Click); + // // comboBoxOrderColor // this.comboBoxOrderColor.FormattingEnabled = true; @@ -708,7 +741,7 @@ this.listBoxSendedOrders.Location = new System.Drawing.Point(213, 23); this.listBoxSendedOrders.Name = "listBoxSendedOrders"; this.listBoxSendedOrders.SelectionMode = System.Windows.Forms.SelectionMode.MultiSimple; - this.listBoxSendedOrders.Size = new System.Drawing.Size(463, 121); + this.listBoxSendedOrders.Size = new System.Drawing.Size(463, 43); this.listBoxSendedOrders.TabIndex = 1; // // tabPage1 @@ -721,7 +754,7 @@ this.tabPage1.Location = new System.Drawing.Point(4, 22); this.tabPage1.Name = "tabPage1"; this.tabPage1.Padding = new System.Windows.Forms.Padding(3); - this.tabPage1.Size = new System.Drawing.Size(682, 362); + this.tabPage1.Size = new System.Drawing.Size(682, 377); this.tabPage1.TabIndex = 2; this.tabPage1.Text = "Check Status"; this.tabPage1.UseVisualStyleBackColor = true; @@ -799,7 +832,7 @@ this.tabPage3.Location = new System.Drawing.Point(4, 22); this.tabPage3.Name = "tabPage3"; this.tabPage3.Padding = new System.Windows.Forms.Padding(3); - this.tabPage3.Size = new System.Drawing.Size(682, 362); + this.tabPage3.Size = new System.Drawing.Size(682, 377); this.tabPage3.TabIndex = 3; this.tabPage3.Text = "Account Information"; this.tabPage3.UseVisualStyleBackColor = true; @@ -910,7 +943,7 @@ this.tabPage4.Location = new System.Drawing.Point(4, 22); this.tabPage4.Name = "tabPage4"; this.tabPage4.Padding = new System.Windows.Forms.Padding(3); - this.tabPage4.Size = new System.Drawing.Size(682, 362); + this.tabPage4.Size = new System.Drawing.Size(682, 377); this.tabPage4.TabIndex = 4; this.tabPage4.Text = "MarketInfo"; this.tabPage4.UseVisualStyleBackColor = true; @@ -992,7 +1025,7 @@ this.tabPage5.Location = new System.Drawing.Point(4, 22); this.tabPage5.Name = "tabPage5"; this.tabPage5.Padding = new System.Windows.Forms.Padding(3); - this.tabPage5.Size = new System.Drawing.Size(682, 362); + this.tabPage5.Size = new System.Drawing.Size(682, 377); this.tabPage5.TabIndex = 5; this.tabPage5.Text = "Timeframes"; this.tabPage5.UseVisualStyleBackColor = true; @@ -1110,27 +1143,16 @@ // // tabPage6 // - this.tabPage6.Controls.Add(this.button15); this.tabPage6.Controls.Add(this.button14); this.tabPage6.Controls.Add(this.button13); this.tabPage6.Location = new System.Drawing.Point(4, 22); this.tabPage6.Name = "tabPage6"; this.tabPage6.Padding = new System.Windows.Forms.Padding(3); - this.tabPage6.Size = new System.Drawing.Size(682, 362); + this.tabPage6.Size = new System.Drawing.Size(682, 377); this.tabPage6.TabIndex = 6; this.tabPage6.Text = "Client Terminal"; this.tabPage6.UseVisualStyleBackColor = true; // - // button15 - // - this.button15.Location = new System.Drawing.Point(223, 184); - this.button15.Name = "button15"; - this.button15.Size = new System.Drawing.Size(75, 23); - this.button15.TabIndex = 2; - this.button15.Text = "button15"; - this.button15.UseVisualStyleBackColor = true; - this.button15.Click += new System.EventHandler(this.button15_Click); - // // button14 // this.button14.Location = new System.Drawing.Point(13, 45); @@ -1151,25 +1173,15 @@ this.button13.UseVisualStyleBackColor = true; this.button13.Click += new System.EventHandler(this.button13_Click); // - // button18 + // checkBox1 // - this.button18.Location = new System.Drawing.Point(6, 336); - this.button18.Name = "button18"; - this.button18.Size = new System.Drawing.Size(75, 23); - this.button18.TabIndex = 14; - this.button18.Text = "Buy"; - this.button18.UseVisualStyleBackColor = true; - this.button18.Click += new System.EventHandler(this.button18_Click); - // - // button19 - // - this.button19.Location = new System.Drawing.Point(87, 336); - this.button19.Name = "button19"; - this.button19.Size = new System.Drawing.Size(75, 23); - this.button19.TabIndex = 15; - this.button19.Text = "Sell"; - this.button19.UseVisualStyleBackColor = true; - this.button19.Click += new System.EventHandler(this.button19_Click); + this.checkBox1.AutoSize = true; + this.checkBox1.Location = new System.Drawing.Point(297, 191); + this.checkBox1.Name = "checkBox1"; + this.checkBox1.Size = new System.Drawing.Size(205, 17); + this.checkBox1.TabIndex = 14; + this.checkBox1.Text = "Close order by current price on market"; + this.checkBox1.UseVisualStyleBackColor = true; // // Form1 // @@ -1298,7 +1310,6 @@ private System.Windows.Forms.Button button13; private System.Windows.Forms.Button button14; private System.Windows.Forms.Button buttonRefreshRates; - private System.Windows.Forms.Button button15; private System.Windows.Forms.Button button16; private System.Windows.Forms.ComboBox comboBox1; private System.Windows.Forms.ComboBox comboBox2; @@ -1306,6 +1317,8 @@ private System.Windows.Forms.ContextMenuStrip contextMenuStrip1; private System.Windows.Forms.Button button19; private System.Windows.Forms.Button button18; + private System.Windows.Forms.Button button20; + private System.Windows.Forms.CheckBox checkBox1; } } diff --git a/TestApiClientUI/Form1.cs b/TestApiClientUI/Form1.cs index 0eea5392..81ddc4e6 100755 --- a/TestApiClientUI/Form1.cs +++ b/TestApiClientUI/Form1.cs @@ -17,20 +17,17 @@ namespace TestApiClientUI { public partial class Form1 : Form { - delegate void PerformCommandHandler(); - - private List GroupOrderCommands = new List(); - - private MtApiClient apiClient = new MtApiClient(); + private readonly List _groupOrderCommands = new List(); + private readonly MtApiClient _apiClient = new MtApiClient(); public Form1() { InitializeComponent(); - apiClient.QuoteUpdated += apiClient_QuoteUpdated; - apiClient.QuoteAdded += apiClient_QuoteAdded; - apiClient.QuoteRemoved += apiClient_QuoteRemoved; - apiClient.ConnectionStateChanged += apiClient_ConnectionStateChanged; + _apiClient.QuoteUpdated += apiClient_QuoteUpdated; + _apiClient.QuoteAdded += apiClient_QuoteAdded; + _apiClient.QuoteRemoved += apiClient_QuoteRemoved; + _apiClient.ConnectionStateChanged += apiClient_ConnectionStateChanged; initOrderCommandsGroup(); @@ -40,30 +37,30 @@ namespace TestApiClientUI private void initOrderCommandsGroup() { - GroupOrderCommands.Add(new PerformCommandHandler(closeOrders)); - GroupOrderCommands.Add(new PerformCommandHandler(closeOrdersBy)); - GroupOrderCommands.Add(new PerformCommandHandler(orderClosePrice)); - GroupOrderCommands.Add(new PerformCommandHandler(orderCloseTime)); - GroupOrderCommands.Add(new PerformCommandHandler(orderComment)); - GroupOrderCommands.Add(new PerformCommandHandler(orderCommission)); - GroupOrderCommands.Add(new PerformCommandHandler(orderDelete)); - GroupOrderCommands.Add(new PerformCommandHandler(orderExpiration)); - GroupOrderCommands.Add(new PerformCommandHandler(orderLots)); - GroupOrderCommands.Add(new PerformCommandHandler(orderMagicNumber)); - GroupOrderCommands.Add(new PerformCommandHandler(orderModify)); - GroupOrderCommands.Add(new PerformCommandHandler(orderOpenPrice)); - GroupOrderCommands.Add(new PerformCommandHandler(orderOpenTime)); - GroupOrderCommands.Add(new PerformCommandHandler(orderPrint)); - GroupOrderCommands.Add(new PerformCommandHandler(orderProfit)); - GroupOrderCommands.Add(new PerformCommandHandler(orderSelect)); - GroupOrderCommands.Add(new PerformCommandHandler(ordersHistoryTotal)); - GroupOrderCommands.Add(new PerformCommandHandler(orderStopLoss)); - GroupOrderCommands.Add(new PerformCommandHandler(ordersTotal)); - GroupOrderCommands.Add(new PerformCommandHandler(orderSwap)); - GroupOrderCommands.Add(new PerformCommandHandler(orderSymbol)); - GroupOrderCommands.Add(new PerformCommandHandler(orderTakeProfit)); - GroupOrderCommands.Add(new PerformCommandHandler(orderTicket)); - GroupOrderCommands.Add(new PerformCommandHandler(orderType)); + _groupOrderCommands.Add(closeOrders); + _groupOrderCommands.Add(closeOrdersBy); + _groupOrderCommands.Add(orderClosePrice); + _groupOrderCommands.Add(orderCloseTime); + _groupOrderCommands.Add(orderComment); + _groupOrderCommands.Add(orderCommission); + _groupOrderCommands.Add(orderDelete); + _groupOrderCommands.Add(orderExpiration); + _groupOrderCommands.Add(orderLots); + _groupOrderCommands.Add(orderMagicNumber); + _groupOrderCommands.Add(orderModify); + _groupOrderCommands.Add(orderOpenPrice); + _groupOrderCommands.Add(orderOpenTime); + _groupOrderCommands.Add(orderPrint); + _groupOrderCommands.Add(orderProfit); + _groupOrderCommands.Add(orderSelect); + _groupOrderCommands.Add(ordersHistoryTotal); + _groupOrderCommands.Add(orderStopLoss); + _groupOrderCommands.Add(ordersTotal); + _groupOrderCommands.Add(orderSwap); + _groupOrderCommands.Add(orderSymbol); + _groupOrderCommands.Add(orderTakeProfit); + _groupOrderCommands.Add(orderTicket); + _groupOrderCommands.Add(orderType); } private void RunOnUiThread(Action action) @@ -175,7 +172,7 @@ namespace TestApiClientUI private void onConnected() { - var quotes = apiClient.GetQuotes(); + var quotes = _apiClient.GetQuotes(); if (quotes != null) { @@ -199,21 +196,21 @@ namespace TestApiClientUI int.TryParse(textBoxPort.Text, out port); if (string.IsNullOrEmpty(serverName)) - apiClient.BeginConnect(port); + _apiClient.BeginConnect(port); else - apiClient.BeginConnect(serverName, port); + _apiClient.BeginConnect(serverName, port); onConnected(); } private void buttonDisconnect_Click(object sender, EventArgs e) { - apiClient.BeginDisconnect(); + _apiClient.BeginDisconnect(); } private void Form1_FormClosing(object sender, FormClosingEventArgs e) { - apiClient.BeginDisconnect(); + _apiClient.BeginDisconnect(); } private void sendOrder(string symbol, TradeOperation command, double volume, double price, int slippage, double stoploss, double takeprofit @@ -223,7 +220,7 @@ namespace TestApiClientUI int ticket = 0; try { - ticket = apiClient.OrderSend(symbol, command, volume, price, slippage, stoploss, takeprofit, comment, magic, expiration, arrow_color); + ticket = _apiClient.OrderSend(symbol, command, volume, price, slippage, stoploss, takeprofit, comment, magic, expiration, arrow_color); } catch (MtConnectionException ex) { @@ -315,7 +312,7 @@ namespace TestApiClientUI foreach (var item in listBoxSendedOrders.SelectedItems) { int orderId = (int)item; - var result = apiClient.OrderClose(orderId, volume, price, slippage); + var result = _apiClient.OrderClose(orderId, volume, price, slippage); if (result == true) { @@ -354,7 +351,7 @@ namespace TestApiClientUI break; } - var result = apiClient.OrderCloseBy(ticket, opposite, color); + var result = _apiClient.OrderCloseBy(ticket, opposite, color); addToLog(string.Format("ClosedBy order result: {0}, ticketId = {1}, opposite {2}", result, ticket, opposite)); } @@ -362,27 +359,27 @@ namespace TestApiClientUI private void orderClosePrice() { - var result = apiClient.OrderClosePrice(); + var result = _apiClient.OrderClosePrice(); textBoxOrderPrice.Text = result.ToString(); addToLog(string.Format("OrderClosePrice result: {0}", result)); } private void orderCloseTime() { - var result = apiClient.OrderCloseTime(); + var result = _apiClient.OrderCloseTime(); addToLog(string.Format("OrderCloseTime result: {0}", result)); } private void orderComment() { - var result = apiClient.OrderComment(); + var result = _apiClient.OrderComment(); textBoxOrderComment.Text = result.ToString(); addToLog(string.Format("OrderComment result: {0}", result)); } private void orderCommission() { - var result = apiClient.OrderCommission(); + var result = _apiClient.OrderCommission(); addToLog(string.Format("OrderCommission result: {0}", result)); } @@ -392,7 +389,7 @@ namespace TestApiClientUI { int ticket = (int)listBoxSendedOrders.SelectedItems[0]; - var result = apiClient.OrderDelete(ticket); + var result = _apiClient.OrderDelete(ticket); addToLog(string.Format("Delete order result: {0}, ticketId = {1}", result, ticket)); } @@ -400,19 +397,19 @@ namespace TestApiClientUI private void orderExpiration() { - var result = apiClient.OrderExpiration(); + var result = _apiClient.OrderExpiration(); addToLog(string.Format("Expiration order result: {0}", result)); } private void orderLots() { - var result = apiClient.OrderLots(); + var result = _apiClient.OrderLots(); addToLog(string.Format("Lots order result: {0}", result)); } private void orderMagicNumber() { - var result = apiClient.OrderMagicNumber(); + var result = _apiClient.OrderMagicNumber(); addToLog(string.Format("MagicNumber order result: {0}", result)); } @@ -447,7 +444,7 @@ namespace TestApiClientUI break; } - var result = apiClient.OrderModify(ticket, price, stoploss, takeprofit, expiration, arrow_color); + var result = _apiClient.OrderModify(ticket, price, stoploss, takeprofit, expiration, arrow_color); addToLog(string.Format("OrderModify result: {0}", result)); } @@ -455,24 +452,24 @@ namespace TestApiClientUI private void orderOpenPrice() { - var result = apiClient.OrderOpenPrice(); + var result = _apiClient.OrderOpenPrice(); addToLog(string.Format("OrderOpenPrice result: {0}", result)); } private void orderOpenTime() { - var result = apiClient.OrderOpenTime(); + var result = _apiClient.OrderOpenTime(); addToLog(string.Format("OpenTime order result: {0}", result)); } private void orderPrint() { - apiClient.OrderPrint(); + _apiClient.OrderPrint(); } private void orderProfit() { - var result = apiClient.OrderProfit(); + var result = _apiClient.OrderProfit(); addToLog(string.Format("Profit order result: {0}", result)); } @@ -489,7 +486,7 @@ namespace TestApiClientUI if (ticket >= 0) { - var result = apiClient.OrderSelect(ticket, OrderSelectMode.SELECT_BY_POS); + var result = _apiClient.OrderSelect(ticket, OrderSelectMode.SELECT_BY_POS); addToLog(string.Format("OrderSelect result: {0}", result)); } @@ -497,59 +494,59 @@ namespace TestApiClientUI private void ordersHistoryTotal() { - var result = apiClient.OrdersHistoryTotal(); + var result = _apiClient.OrdersHistoryTotal(); addToLog(string.Format("OrdersHistoryTotal result: {0}", result)); } private void orderStopLoss() { - var result = apiClient.OrderStopLoss(); + var result = _apiClient.OrderStopLoss(); textBoxOrderStoploss.Text = result.ToString(); addToLog(string.Format("OrderStopLoss result: {0}", result)); } private void ordersTotal() { - var result = apiClient.OrdersTotal(); + var result = _apiClient.OrdersTotal(); addToLog(string.Format("OrdersTotal result: {0}", result)); } private void orderSwap() { - var result = apiClient.OrderSwap(); + var result = _apiClient.OrderSwap(); addToLog(string.Format("OrderSwap result: {0}", result)); } private void orderSymbol() { - var result = apiClient.OrderSymbol(); + var result = _apiClient.OrderSymbol(); textBoxOrderSymbol.Text = result; addToLog(string.Format("OrderSymbol result: {0}", result)); } private void orderTakeProfit() { - var result = apiClient.OrderTakeProfit(); + var result = _apiClient.OrderTakeProfit(); textBoxOrderProffit.Text = result.ToString(); addToLog(string.Format("OrderTakeProfit result: {0}", result)); } private void orderTicket() { - var result = apiClient.OrderTicket(); + var result = _apiClient.OrderTicket(); addToLog(string.Format("OrderTicket result: {0}", result)); } private void orderType() { - var result = apiClient.OrderType(); + var result = _apiClient.OrderType(); comboBoxOrderCommand.SelectedIndex = (int)result; addToLog(string.Format("OrderType result: {0}", result)); } private void button2_Click(object sender, EventArgs e) { - GroupOrderCommands[comboBoxSelectedCommand.SelectedIndex](); + _groupOrderCommands[comboBoxSelectedCommand.SelectedIndex](); } private void addToLog(string msg) @@ -568,79 +565,79 @@ namespace TestApiClientUI { case 0: { - var result = apiClient.GetLastError(); + var result = _apiClient.GetLastError(); addToLog(string.Format("GetLastError result: {0}", result)); } break; case 1: { - var result = apiClient.IsConnected(); + var result = _apiClient.IsConnected(); addToLog(string.Format("IsConnected result: {0}", result)); } break; case 2: { - var result = apiClient.IsDemo(); + var result = _apiClient.IsDemo(); addToLog(string.Format("IsDemo result: {0}", result)); } break; case 3: { - var result = apiClient.IsDllsAllowed(); + var result = _apiClient.IsDllsAllowed(); addToLog(string.Format("IsDllsAllowed result: {0}", result)); } break; case 4: { - var result = apiClient.IsExpertEnabled(); + var result = _apiClient.IsExpertEnabled(); addToLog(string.Format("IsExpertEnabled result: {0}", result)); } break; case 5: { - var result = apiClient.IsLibrariesAllowed(); + var result = _apiClient.IsLibrariesAllowed(); addToLog(string.Format("IsLibrariesAllowed result: {0}", result)); } break; case 6: { - var result = apiClient.IsOptimization(); + var result = _apiClient.IsOptimization(); addToLog(string.Format("IsOptimization result: {0}", result)); } break; case 7: { - var result = apiClient.IsStopped(); + var result = _apiClient.IsStopped(); addToLog(string.Format("IsStopped result: {0}", result)); } break; case 8: { - var result = apiClient.IsTesting(); + var result = _apiClient.IsTesting(); addToLog(string.Format("IsTesting result: {0}", result)); } break; case 9: { - var result = apiClient.IsTradeAllowed(); + var result = _apiClient.IsTradeAllowed(); addToLog(string.Format("IsTradeAllowed result: {0}", result)); } break; case 10: { - var result = apiClient.IsTradeContextBusy(); + var result = _apiClient.IsTradeContextBusy(); addToLog(string.Format("IsTradeContextBusy result: {0}", result)); } break; case 11: { - var result = apiClient.IsVisualMode(); + var result = _apiClient.IsVisualMode(); addToLog(string.Format("IsVisualMode result: {0}", result)); } break; case 12: { - var result = apiClient.UninitializeReason(); + var result = _apiClient.UninitializeReason(); addToLog(string.Format("UninitializeReason result: {0}", result)); } break; @@ -648,7 +645,7 @@ namespace TestApiClientUI { int errorCode = -1; int.TryParse(textBoxErrorCode.Text, out errorCode); - var result = apiClient.ErrorDescription(errorCode); + var result = _apiClient.ErrorDescription(errorCode); addToLog(string.Format("ErrorDescription result: {0}", result)); } break; @@ -661,97 +658,97 @@ namespace TestApiClientUI { case 0: { - var result = apiClient.AccountBalance(); + var result = _apiClient.AccountBalance(); addToLog(string.Format("AccountBalance result: {0}", result)); } break; case 1: { - var result = apiClient.AccountCredit(); + var result = _apiClient.AccountCredit(); addToLog(string.Format("AccountCredit result: {0}", result)); } break; case 2: { - var result = apiClient.AccountCompany(); + var result = _apiClient.AccountCompany(); addToLog(string.Format("AccountCompany result: {0}", result)); } break; case 3: { - var result = apiClient.AccountCurrency(); + var result = _apiClient.AccountCurrency(); addToLog(string.Format("AccountCurrency result: {0}", result)); } break; case 4: { - var result = apiClient.AccountEquity(); + var result = _apiClient.AccountEquity(); addToLog(string.Format("AccountEquity result: {0}", result)); } break; case 5: { - var result = apiClient.AccountFreeMargin(); + var result = _apiClient.AccountFreeMargin(); addToLog(string.Format("AccountFreeMargin result: {0}", result)); } break; case 6: { - var result = apiClient.AccountFreeMarginCheck(textBoxAccountInfoSymbol.Text, (TradeOperation)comboBoxAccountInfoCmd.SelectedIndex, int.Parse(textBoxAccountInfoVolume.Text)); + var result = _apiClient.AccountFreeMarginCheck(textBoxAccountInfoSymbol.Text, (TradeOperation)comboBoxAccountInfoCmd.SelectedIndex, int.Parse(textBoxAccountInfoVolume.Text)); addToLog(string.Format("AccountFreeMarginCheck result: {0}", result)); } break; case 7: { - var result = apiClient.AccountFreeMarginMode(); + var result = _apiClient.AccountFreeMarginMode(); addToLog(string.Format("AccountFreeMarginMode result: {0}", result)); } break; case 8: { - var result = apiClient.AccountLeverage(); + var result = _apiClient.AccountLeverage(); addToLog(string.Format("AccountLeverage result: {0}", result)); } break; case 9: { - var result = apiClient.AccountMargin(); + var result = _apiClient.AccountMargin(); addToLog(string.Format("AccountMargin result: {0}", result)); } break; case 10: { - var result = apiClient.AccountName(); + var result = _apiClient.AccountName(); addToLog(string.Format("AccountName result: {0}", result)); } break; case 11: { - var result = apiClient.AccountNumber(); + var result = _apiClient.AccountNumber(); addToLog(string.Format("AccountNumber result: {0}", result)); } break; case 12: { - var result = apiClient.AccountProfit(); + var result = _apiClient.AccountProfit(); addToLog(string.Format("AccountProfit result: {0}", result)); } break; case 13: { - var result = apiClient.AccountServer(); + var result = _apiClient.AccountServer(); addToLog(string.Format("AccountServer result: {0}", result)); } break; case 14: { - var result = apiClient.AccountStopoutLevel(); + var result = _apiClient.AccountStopoutLevel(); addToLog(string.Format("AccountStopoutLevel result: {0}", result)); } break; case 15: { - var result = apiClient.AccountStopoutMode(); + var result = _apiClient.AccountStopoutMode(); addToLog(string.Format("AccountStopoutMode result: {0}", result)); } break; @@ -763,7 +760,7 @@ namespace TestApiClientUI if (listBoxMarketInfo.SelectedIndex < 0) return; - var result = apiClient.MarketInfo(txtMarketInfoSymbol.Text, (MarketInfoModeType)listBoxMarketInfo.SelectedIndex); + var result = _apiClient.MarketInfo(txtMarketInfoSymbol.Text, (MarketInfoModeType)listBoxMarketInfo.SelectedIndex); addToLog(string.Format("MarketInfo result: {0}", result)); } @@ -779,11 +776,11 @@ namespace TestApiClientUI //for (int i = 0; i < COUNT; i++) //{ - // var price = apiClient.iOpen(symbol, ChartPeriod.PERIOD_M1, i); + // var price = _apiClient.iOpen(symbol, ChartPeriod.PERIOD_M1, i); // openPriceList.Add(price); //} - var prices = apiClient.iCloseArray(symbol, ChartPeriod.PERIOD_M1); + var prices = _apiClient.iCloseArray(symbol, ChartPeriod.PERIOD_M1); openPriceList = new List(prices); @@ -803,14 +800,14 @@ namespace TestApiClientUI private void button7_Click(object sender, EventArgs e) { string symbol = textBoxSelectedSymbol.Text; - var barCount = apiClient.iBars(symbol, ChartPeriod.PERIOD_M1); + var barCount = _apiClient.iBars(symbol, ChartPeriod.PERIOD_M1); textBoxTimeframesCount.Text = barCount.ToString(); } private void button8_Click(object sender, EventArgs e) { string symbol = textBoxSelectedSymbol.Text; - var prices = apiClient.iHighArray(symbol, ChartPeriod.PERIOD_M1); + var prices = _apiClient.iHighArray(symbol, ChartPeriod.PERIOD_M1); var items = new List(prices); listBoxProceHistory.DataSource = items; } @@ -818,7 +815,7 @@ namespace TestApiClientUI private void button9_Click(object sender, EventArgs e) { string symbol = textBoxSelectedSymbol.Text; - var prices = apiClient.iLowArray(symbol, ChartPeriod.PERIOD_M1); + var prices = _apiClient.iLowArray(symbol, ChartPeriod.PERIOD_M1); var items = new List(prices); listBoxProceHistory.DataSource = items; } @@ -826,7 +823,7 @@ namespace TestApiClientUI private void button10_Click(object sender, EventArgs e) { string symbol = textBoxSelectedSymbol.Text; - var prices = apiClient.iOpenArray(symbol, ChartPeriod.PERIOD_M1); + var prices = _apiClient.iOpenArray(symbol, ChartPeriod.PERIOD_M1); var items = new List(prices); listBoxProceHistory.DataSource = items; } @@ -834,7 +831,7 @@ namespace TestApiClientUI private void button11_Click(object sender, EventArgs e) { string symbol = textBoxSelectedSymbol.Text; - var prices = apiClient.iVolumeArray(symbol, ChartPeriod.PERIOD_M1); + var prices = _apiClient.iVolumeArray(symbol, ChartPeriod.PERIOD_M1); var items = new List(prices); listBoxProceHistory.DataSource = items; } @@ -842,7 +839,7 @@ namespace TestApiClientUI private void button12_Click(object sender, EventArgs e) { string symbol = textBoxSelectedSymbol.Text; - var times = apiClient.iTimeArray(symbol, ChartPeriod.PERIOD_M1); + var times = _apiClient.iTimeArray(symbol, ChartPeriod.PERIOD_M1); var items = new List(times); listBoxProceHistory.DataSource = items; } @@ -851,145 +848,93 @@ namespace TestApiClientUI { string symbol = textBoxSelectedSymbol.Text; int[] param = {11, 12, 13}; - var retVal = apiClient.iCustom(symbol, (int)ChartPeriod.PERIOD_H1, "Zigzag", param, 1, 0); + var retVal = _apiClient.iCustom(symbol, (int)ChartPeriod.PERIOD_H1, "Zigzag", param, 1, 0); addToLog(string.Format("ICustom result: {0}", retVal)); } private void button13_Click(object sender, EventArgs e) { - var retVal = apiClient.TimeCurrent(); + var retVal = _apiClient.TimeCurrent(); addToLog(string.Format("TimeCurrent result: {0}", retVal)); } private void button14_Click(object sender, EventArgs e) { - var retVal = apiClient.TimeLocal(); + var retVal = _apiClient.TimeLocal(); addToLog(string.Format("TimeLocal result: {0}", retVal)); } private void buttonRefreshRates_Click(object sender, EventArgs e) { - var retVal = apiClient.RefreshRates(); + var retVal = _apiClient.RefreshRates(); addToLog(string.Format("RefreshRates result: {0}", retVal)); } - private void button15_Click(object sender, EventArgs e) - { - for (var i = 0; i < 4; i++) - { - var ticket = i; - Task.Factory.StartNew(() => - { - MtOrder order = null; - try - { - order = apiClient.GetOrder(ticket, OrderSelectMode.SELECT_BY_POS, OrderSelectSource.MODE_TRADES); - } - catch (MtConnectionException ex) - { - addToLog("MtExecutionException: " + ex.Message); - } - catch (MtExecutionException ex) - { - addToLog("MtExecutionException: " + ex.Message); - } - - string result; - if (order != null) - { - result = - string.Format( - "Order: Ticket = {0}, Symbol = {1}, Operation = {2}, OpenPrice = {3}, ClosePrice = {4}, Lots = {5}, Profit = {6}, Comment = {7}, Commission = {8}, MagicNumber = {9}, OpenTime = {10}, CloseTime = {11}", - order.Ticket, order.Symbol, order.Operation, order.OpenPrice, order.ClosePrice, order.Lots, order.Profit, order.Comment, order.Commission, order.MagicNumber, order.OpenTime, order.CloseTime); - } - else - { - result = "Order is null"; - } - - addToLog(result); - }); - } - } - - private void button16_Click(object sender, EventArgs e) - { - var ticket = int.Parse(textBoxIndexTicket.Text); - var selectMode = (OrderSelectMode) comboBox1.SelectedIndex; - var selectSource = (OrderSelectSource) comboBox2.SelectedIndex; - - MtOrder order = null; - try - { - order = apiClient.GetOrder(ticket, selectMode, selectSource); - } - catch (MtConnectionException ex) - { - addToLog("MtExecutionException: " + ex.Message); - return; - } - catch (MtExecutionException ex) - { - addToLog("MtExecutionException: " + ex.Message + "; ErrorCode = " + ex.ErrorCode); - return; - } - - if (order == null) - { - addToLog("Order is null"); - return; - } - - var result = - string.Format( - "Order: Ticket = {0}, Symbol = {1}, Operation = {2}, OpenPrice = {3}, ClosePrice = {4}, Lots = {5}, Profit = {6}, Comment = {7}, Commission = {8}, MagicNumber = {9}, OpenTime = {10}, CloseTime = {11}", - order.Ticket, order.Symbol, order.Operation, order.OpenPrice, order.ClosePrice, order.Lots, order.Profit, order.Comment, order.Commission, order.MagicNumber, order.OpenTime, order.CloseTime); - addToLog(result); - } - - private void button17_Click(object sender, EventArgs e) - { - var selectSource = (OrderSelectSource)comboBox2.SelectedIndex; - - - IEnumerable orders = null; - try - { - orders = apiClient.GetOrders(selectSource); - } - catch (MtConnectionException ex) - { - addToLog("MtExecutionException: " + ex.Message); - return; - } - catch (MtExecutionException ex) - { - addToLog("MtExecutionException: " + ex.Message + "; ErrorCode = " + ex.ErrorCode); - return; - } - - if (orders == null) - { - addToLog("Orders is null"); - return; - - } - - foreach (var order in orders) - { - var result = - string.Format( - "Order: Ticket = {0}, Symbol = {1}, Operation = {2}, OpenPrice = {3}, ClosePrice = {4}, Lots = {5}, Profit = {6}, Comment = {7}, Commission = {8}, MagicNumber = {9}, OpenTime = {10}, CloseTime = {11}", - order.Ticket, order.Symbol, order.Operation, order.OpenPrice, order.ClosePrice, order.Lots, order.Profit, order.Comment, order.Commission, order.MagicNumber, order.OpenTime, order.CloseTime); - addToLog(result); - } - } - private void listBoxEventLog_MouseDoubleClick(object sender, MouseEventArgs e) { listBoxEventLog.Items.Clear(); } + private TResult Execute(Func func) + { + var result = default(TResult); + try + { + result = func(); + } + catch (MtConnectionException ex) + { + addToLog("MtExecutionException: " + ex.Message); + } + catch (MtExecutionException ex) + { + addToLog("MtExecutionException: " + ex.Message + "; ErrorCode = " + ex.ErrorCode); + } + + return result; + } + + //GetOrder + private void button16_Click(object sender, EventArgs e) + { + var ticket = int.Parse(textBoxIndexTicket.Text); + var selectMode = (OrderSelectMode) comboBox1.SelectedIndex; + var selectSource = (OrderSelectSource) comboBox2.SelectedIndex; + + var order = Execute(() => _apiClient.GetOrder(ticket, selectMode, selectSource)); + + if (order != null) + { + var result = + string.Format( + "Order: Ticket = {0}, Symbol = {1}, Operation = {2}, OpenPrice = {3}, ClosePrice = {4}, Lots = {5}, Profit = {6}, Comment = {7}, Commission = {8}, MagicNumber = {9}, OpenTime = {10}, CloseTime = {11}", + order.Ticket, order.Symbol, order.Operation, order.OpenPrice, order.ClosePrice, order.Lots, order.Profit, order.Comment, order.Commission, order.MagicNumber, order.OpenTime, order.CloseTime); + addToLog(result); + } + } + + //GetOrders + private void button17_Click(object sender, EventArgs e) + { + var selectSource = (OrderSelectSource)comboBox2.SelectedIndex; + + + var orders = Execute(() => _apiClient.GetOrders(selectSource)); ; + + if (orders != null) + { + foreach (var order in orders) + { + var result = + string.Format( + "Order: Ticket = {0}, Symbol = {1}, Operation = {2}, OpenPrice = {3}, ClosePrice = {4}, Lots = {5}, Profit = {6}, Comment = {7}, Commission = {8}, MagicNumber = {9}, OpenTime = {10}, CloseTime = {11}", + order.Ticket, order.Symbol, order.Operation, order.OpenPrice, order.ClosePrice, order.Lots, order.Profit, order.Comment, order.Commission, order.MagicNumber, order.OpenTime, order.CloseTime); + addToLog(result); + } + } + } + + //OrderSendBuy private void button18_Click(object sender, EventArgs e) { var symbol = textBoxOrderSymbol.Text; @@ -999,26 +944,13 @@ namespace TestApiClientUI var slippage = (int)numericOrderSlippage.Value; - int ticket = 0; - try - { - ticket = apiClient.OrderSendBuy(symbol, volume, slippage); - } - catch (MtConnectionException ex) - { - addToLog("MtExecutionException: " + ex.Message); - return; - } - catch (MtExecutionException ex) - { - addToLog("MtExecutionException: " + ex.Message + "; ErrorCode = " + ex.ErrorCode); - return; - } + var ticket = Execute(() => _apiClient.OrderSendBuy(symbol, volume, slippage)); addToLog(string.Format("Sended order result: ticketId = {0}, symbol = {1}, volume = {2}, slippage = {3}", ticket, symbol, volume, slippage)); } + //OrderSendSell private void button19_Click(object sender, EventArgs e) { var symbol = textBoxOrderSymbol.Text; @@ -1028,24 +960,35 @@ namespace TestApiClientUI var slippage = (int)numericOrderSlippage.Value; - int ticket = 0; - try - { - ticket = apiClient.OrderSendSell(symbol, volume, slippage); - } - catch (MtConnectionException ex) - { - addToLog("MtExecutionException: " + ex.Message); - return; - } - catch (MtExecutionException ex) - { - addToLog("MtExecutionException: " + ex.Message + "; ErrorCode = " + ex.ErrorCode); - return; - } + var ticket = Execute(() => _apiClient.OrderSendSell(symbol, volume, slippage)); addToLog(string.Format("Sended order result: ticketId = {0}, symbol = {1}, volume = {2}, slippage = {3}", ticket, symbol, volume, slippage)); } + + //OrderClose + private void button20_Click(object sender, EventArgs e) + { + var ticket = int.Parse(textBoxIndexTicket.Text); + var slippage = (int)numericOrderSlippage.Value; + + var closed = false; + + if (checkBox1.Checked) + { + closed = Execute(() => _apiClient.OrderClose(ticket, slippage)); + } + else + { + double volume; + double.TryParse(textBoxOrderVolume.Text, out volume); + + double price; + double.TryParse(textBoxOrderPrice.Text, out price); + closed = Execute(() => _apiClient.OrderClose(ticket, volume, price, slippage)); + } + + addToLog(string.Format("Close order result: {0}, ticket = {1}", closed, ticket)); + } } } diff --git a/mq4/MtApi.ex4 b/mq4/MtApi.ex4 index 4974266d..98387f51 100755 Binary files a/mq4/MtApi.ex4 and b/mq4/MtApi.ex4 differ diff --git a/mq4/MtApi.mq4 b/mq4/MtApi.mq4 index 44c62cbd..5820cae3 100755 --- a/mq4/MtApi.mq4 +++ b/mq4/MtApi.mq4 @@ -248,254 +248,31 @@ int executeCommand() //NoCommand break; - case 155: //SignalServiceCommand + case 155: //Request { if (!getStringValue(ExpertHandle, 0, requestValue)) { - PrintParamError("Signal"); + PrintParamError("Request"); } string response = ""; if (requestValue != "") { - Print("executeCommand: incoming signal = ", requestValue); + Print("executeCommand: incoming request = ", requestValue); response = OnRequest(requestValue); } sendStringResponse(ExpertHandle, response); } break; - case 1: // OrderSend - if (!getStringValue(ExpertHandle, 0, symbolValue)) - { - PrintParamError("symbol"); - } - - if (!getIntValue(ExpertHandle, 1, cmdValue)) - { - PrintParamError("cmd"); - } - - if (!getDoubleValue(ExpertHandle, 2, volumeValue)) - { - PrintParamError("volume"); - } - - if (!getDoubleValue(ExpertHandle, 3, priceValue)) - { - PrintParamError("price"); - } - - if (!getIntValue(ExpertHandle, 4, slippageValue)) - { - PrintParamError("slippage"); - } - - if (!getDoubleValue(ExpertHandle, 5, stoplossValue)) - { - PrintParamError("stoploss"); - } - - if (!getDoubleValue(ExpertHandle, 6, takeprofitValue)) - { - PrintParamError("takeprofit"); - } - - if (!getStringValue(ExpertHandle, 7, commentValue)) - { - PrintParamError("comment"); - } - - if (!getIntValue(ExpertHandle, 8, magicValue)) - { - PrintParamError("magic"); - } - - if (!getIntValue(ExpertHandle, 9, expirationValue)) - { - PrintParamError("expiration"); - } - - if (!getIntValue(ExpertHandle, 10, arrow_colorValue)) - { - PrintParamError("arrow_color"); - } - - if (!sendIntResponse(ExpertHandle, OrderSend(symbolValue, cmdValue, volumeValue, priceValue - , slippageValue, stoplossValue, takeprofitValue - , commentValue, magicValue, expirationValue, arrow_colorValue))) - { - PrintResponseError("OrderSend"); - } - - break; - - case 1001: // OrderSendBuy - if (!getStringValue(ExpertHandle, 0, symbolValue)) - { - PrintParamError("symbol"); - } - - if (!getDoubleValue(ExpertHandle, 1, volumeValue)) - { - PrintParamError("volume"); - } - - if (!getIntValue(ExpertHandle, 2, slippageValue)) - { - PrintParamError("slippage"); - } - - if (!getDoubleValue(ExpertHandle, 3, stoplossValue)) - { - PrintParamError("stoploss"); - } - - if (!getDoubleValue(ExpertHandle, 4, takeprofitValue)) - { - PrintParamError("takeprofit"); - } - - if (!getStringValue(ExpertHandle, 5, commentValue)) - { - PrintParamError("comment"); - } - - if (!getIntValue(ExpertHandle, 6, magicValue)) - { - PrintParamError("magic"); - } - - priceValue = MarketInfo(symbolValue, MODE_ASK); - - if (!sendIntResponse(ExpertHandle, OrderSend(symbolValue, OP_BUY, volumeValue, priceValue - , slippageValue, stoplossValue, takeprofitValue - , commentValue, magicValue))) - { - PrintResponseError("OrderSend"); - } - - break; - - case 1002: // OrderSendSell - if (!getStringValue(ExpertHandle, 0, symbolValue)) - { - PrintParamError("symbol"); - } - - if (!getDoubleValue(ExpertHandle, 1, volumeValue)) - { - PrintParamError("volume"); - } - - if (!getIntValue(ExpertHandle, 2, slippageValue)) - { - PrintParamError("slippage"); - } - - if (!getDoubleValue(ExpertHandle, 3, stoplossValue)) - { - PrintParamError("stoploss"); - } - - if (!getDoubleValue(ExpertHandle, 4, takeprofitValue)) - { - PrintParamError("takeprofit"); - } - - if (!getStringValue(ExpertHandle, 5, commentValue)) - { - PrintParamError("comment"); - } - - if (!getIntValue(ExpertHandle, 6, magicValue)) - { - PrintParamError("magic"); - } - - priceValue = MarketInfo(symbolValue, MODE_BID); - - if (!sendIntResponse(ExpertHandle, OrderSend(symbolValue, OP_SELL, volumeValue, priceValue - , slippageValue, stoplossValue, takeprofitValue - , commentValue, magicValue))) - { - PrintResponseError("OrderSend"); - } - - break; - - case 2: // OrderClose - if (!getIntValue(ExpertHandle, 0, ticketValue)) - { - PrintParamError("ticket"); - } - - if (!getDoubleValue(ExpertHandle, 1, lotsValue)) - { - PrintParamError("lots"); - } - - if (!getDoubleValue(ExpertHandle, 2, priceValue)) - { - PrintParamError("price"); - } - - if (!getIntValue(ExpertHandle, 3, slippageValue)) - { - PrintParamError("slippage"); - } - - if (!getIntValue(ExpertHandle, 4, colorValue)) - { - PrintParamError("color"); - } - - if (!sendBooleanResponse(ExpertHandle, OrderClose(ticketValue, lotsValue, priceValue, slippageValue, colorValue))) - { - PrintResponseError("OrderClose"); - } - break; case 151: //OrderCloseAll if (!sendBooleanResponse(ExpertHandle, OrderCloseAll())) { PrintResponseError("OrderCloseAll"); } - break; - - case 152: //OrderCloseByCurrentPrice - if (!getIntValue(ExpertHandle, 0, ticketValue)) - { - PrintParamError("ticket"); - } - - if (!getIntValue(ExpertHandle, 1, slippageValue)) - { - PrintParamError("slippage"); - } - - lotsValue = 0; - if (OrderSelect(ticketValue, SELECT_BY_TICKET)) - { - symbolValue = OrderSymbol(); - lotsValue = OrderLots(); - if (OrderType() == OP_SELL) - { - priceValue = MarketInfo(symbolValue, MODE_ASK); - } - else - { - priceValue = MarketInfo(symbolValue, MODE_BID); - } - } - - if (!sendBooleanResponse(ExpertHandle, OrderClose(ticketValue, lotsValue, priceValue, slippageValue))) - { - PrintResponseError("OrderClose"); - } - break; - + break; case 3: // OrderCloseBy if (!getIntValue(ExpertHandle, 0, ticketValue)) @@ -3567,14 +3344,13 @@ bool OrderCloseAll() { if (OrderSelect(i, SELECT_BY_POS)) { - int type = OrderType(); - + int type = OrderType(); switch(type) { //Close opened long positions case OP_BUY: OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 5, Red ); break; - //Close opened short positions + //Close opened short positions case OP_SELL: OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 5, Red ); break; } @@ -3640,6 +3416,9 @@ string OnRequest(string json) case 3: //OrderSend response = ExecuteRequestOrderSend(jo); break; + case 4: //OrderClose + response = ExecuteRequestOrderClose(jo); + break; default: Print("OnRequest [WARNING]: Unknown request type ", requestType); response = CreateErrorResponse(-1, "Unknown request type"); @@ -3755,13 +3534,11 @@ string ExecuteRequestOrderSend(JSONObject *jo) if (jvPrice != NULL) { price = jvPrice.getDouble(); - Print("Take price from request ", price); } else { int mode = isLongOperation(cmd) ? MODE_ASK : MODE_BID; price = MarketInfo(symbol, mode); - Print("Take price by mode ", mode, " and choosen price ", price); } JSONValue *jvSlippage = jo.getValue("Slippage"); @@ -3792,3 +3569,59 @@ string ExecuteRequestOrderSend(JSONObject *jo) JSONValue* jvTicket = new JSONNumber(ticket); return CreateSuccessResponse("Ticket", jvTicket); } + +string ExecuteRequestOrderClose(JSONObject *jo) +{ + if (jo.getValue("Ticket") == NULL) + return CreateErrorResponse(-1, "Undefinded mandatory parameter Ticket"); + + int ticket = jo.getInt("Ticket"); + + double price; + JSONValue *jvPrice = jo.getValue("Price"); + if (jvPrice != NULL) + { + price = jvPrice.getDouble(); + } + else + { + if (OrderSelect(ticket, SELECT_BY_TICKET)) + { + string symbol = OrderSymbol(); + int mode = isLongOperation(OrderType()) ? MODE_BID : MODE_ASK; + price = MarketInfo(symbol, mode); + } + else + { + return CreateErrorResponse(-1, "Failed select order to get current price"); + } + } + + double lots; + JSONValue *jvLots = jo.getValue("Lots"); + if (jvLots != NULL) + { + lots = jvLots.getDouble(); + } + else + { + if (OrderSelect(ticket, SELECT_BY_TICKET)) + { + lots = OrderLots(); + } + else + { + return CreateErrorResponse(-1, "Failed select order to get lots of order"); + } + } + + JSONValue *jvSlippage = jo.getValue("Slippage"); + double slippage = (jvSlippage != NULL) ? jvSlippage.getDouble() : 1; + + JSONValue *jvArrowColor = jo.getValue("ArrowColor"); + int arrowcolor = (jvArrowColor != NULL) ? jvArrowColor.getInt() : clrNONE; + + if (!OrderClose(ticket, lots, price, slippage, arrowcolor)) + return CreateErrorResponse(GetLastError(), "OrderClose failed"); + return CreateSuccessResponse("", NULL); +} \ No newline at end of file