mirror of
https://github.com/vdemydiuk/mtapi.git
synced 2026-08-02 21:47:47 +00:00
Issue #119: Added out parameter MqlTradeResult to function PositionClose
This commit is contained in:
@@ -85,6 +85,8 @@
|
||||
<Compile Include="Requests\OrderCheckRequest.cs" />
|
||||
<Compile Include="Requests\OrderCheckResult.cs" />
|
||||
<Compile Include="Requests\OrderSendRequest.cs" />
|
||||
<Compile Include="Requests\PositionCloseRequest.cs" />
|
||||
<Compile Include="Requests\PositionCloseResult.cs" />
|
||||
<Compile Include="Requests\PositionOpenRequest.cs" />
|
||||
<Compile Include="Requests\RequestBase.cs" />
|
||||
<Compile Include="Requests\RequestType.cs" />
|
||||
|
||||
+52
-6
@@ -536,6 +536,36 @@ namespace MtApi5
|
||||
return SendCommand<bool>(Mt5CommandType.PositionClose, commandParameters);
|
||||
}
|
||||
|
||||
///<summary>
|
||||
///Closes a position with the specified ticket.
|
||||
///</summary>
|
||||
///<param name="ticket">Ticket of the closed position.</param>
|
||||
///<param name="deviation">Maximal deviation from the current price (in points).</param>
|
||||
/// <param name="result">output result</param>
|
||||
public bool PositionClose(ulong ticket, ulong deviation, out MqlTradeResult result)
|
||||
{
|
||||
Log.Debug($"PositionClose: ticket = {ticket}, deviation = {deviation}");
|
||||
|
||||
var response = SendRequest<PositionCloseResult>(new PositionCloseRequest
|
||||
{
|
||||
Ticket = ticket,
|
||||
Deviation = deviation
|
||||
});
|
||||
|
||||
result = response?.TradeResult;
|
||||
return response != null && response.RetVal;
|
||||
}
|
||||
|
||||
///<summary>
|
||||
///Closes a position with the specified ticket.
|
||||
///</summary>
|
||||
///<param name="ticket">Ticket of the closed position.</param>
|
||||
/// <param name="result">output result</param>
|
||||
public bool PositionClose(ulong ticket, out MqlTradeResult result)
|
||||
{
|
||||
return PositionClose(ticket, ulong.MaxValue, out result);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Opens a position with the specified parameters.
|
||||
/// </summary>
|
||||
@@ -566,7 +596,7 @@ namespace MtApi5
|
||||
/// <param name="comment">comment</param>
|
||||
/// <param name="result">output result</param>
|
||||
/// <returns>true - successful check of the basic structures, otherwise - false.</returns>
|
||||
public bool PositionOpen(string symbol, ENUM_ORDER_TYPE orderType, double volume, double price, double sl, double tp, string comment , out MqlTradeResult result)
|
||||
public bool PositionOpen(string symbol, ENUM_ORDER_TYPE orderType, double volume, double price, double sl, double tp, string comment, out MqlTradeResult result)
|
||||
{
|
||||
Log.Debug($"PositionOpen: symbol = {symbol}, orderType = {orderType}, volume = {volume}, price = {price}, sl = {sl}, tp = {tp}, comment = {comment}");
|
||||
|
||||
@@ -584,15 +614,31 @@ namespace MtApi5
|
||||
result = response?.TradeResult;
|
||||
return response != null && response.RetVal;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Opens a position with the specified parameters.
|
||||
/// </summary>
|
||||
/// <param name="symbol">symbol</param>
|
||||
/// <param name="orderType">order type to open position </param>
|
||||
/// <param name="volume">position volume</param>
|
||||
/// <param name="price">execution price</param>
|
||||
/// <param name="sl">Stop Loss price</param>
|
||||
/// <param name="tp">Take Profit price</param>
|
||||
/// <param name="result">output result</param>
|
||||
/// <returns>true - successful check of the basic structures, otherwise - false.</returns>
|
||||
public bool PositionOpen(string symbol, ENUM_ORDER_TYPE orderType, double volume, double price, double sl, double tp, out MqlTradeResult result)
|
||||
{
|
||||
return PositionOpen(symbol, orderType, volume, price, sl, tp, "", out result);
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Account Information functions
|
||||
|
||||
///<summary>
|
||||
///Returns the value of the corresponding account property.
|
||||
///</summary>
|
||||
///<param name="propertyId">Identifier of the property.</param>
|
||||
public double AccountInfoDouble(ENUM_ACCOUNT_INFO_DOUBLE propertyId)
|
||||
///<summary>
|
||||
///Returns the value of the corresponding account property.
|
||||
///</summary>
|
||||
///<param name="propertyId">Identifier of the property.</param>
|
||||
public double AccountInfoDouble(ENUM_ACCOUNT_INFO_DOUBLE propertyId)
|
||||
{
|
||||
var commandParameters = new ArrayList { (int)propertyId };
|
||||
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
namespace MtApi5.Requests
|
||||
{
|
||||
internal class PositionCloseRequest : RequestBase
|
||||
{
|
||||
public override RequestType RequestType => RequestType.PositionClose;
|
||||
|
||||
public ulong Ticket { get; set; }
|
||||
public ulong Deviation { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
namespace MtApi5.Requests
|
||||
{
|
||||
internal class PositionCloseResult
|
||||
{
|
||||
public bool RetVal { get; set; }
|
||||
public MqlTradeResult TradeResult { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -14,6 +14,7 @@ namespace MtApi5.Requests
|
||||
IndicatorCreate = 7,
|
||||
SymbolInfoString = 8,
|
||||
ChartTimePriceToXY = 9,
|
||||
ChartXYToTimePrice = 10
|
||||
ChartXYToTimePrice = 10,
|
||||
PositionClose = 11
|
||||
}
|
||||
}
|
||||
@@ -481,10 +481,19 @@
|
||||
</WrapPanel>
|
||||
</TabItem>
|
||||
|
||||
<TabItem Header="CTrade">
|
||||
<WrapPanel VerticalAlignment="Top" Margin="5">
|
||||
<Button Command="{Binding PositionOpenCommand}" Content="PositionOpen" Margin="2"/>
|
||||
</WrapPanel>
|
||||
<TabItem Header="CTrade (Positions)">
|
||||
<Grid>
|
||||
<Grid.RowDefinitions>
|
||||
<RowDefinition Height="Auto"/>
|
||||
<RowDefinition Height="Auto"/>
|
||||
</Grid.RowDefinitions>
|
||||
<Button Grid.Row="0" Command="{Binding PositionOpenCommand}" Content="PositionOpen" Margin="2" HorizontalAlignment="Left"/>
|
||||
<StackPanel Grid.Row="1" Orientation="Horizontal" Margin="4">
|
||||
<TextBlock Text="Ticket:" VerticalAlignment="Center"/>
|
||||
<TextBox Text="{Binding PositionTicketValue}" Width="150" Margin="2"/>
|
||||
<Button Command="{Binding PositionCloseCommand}" Content="PositionClose" Margin="2"/>
|
||||
</StackPanel>
|
||||
</Grid>
|
||||
</TabItem>
|
||||
|
||||
<TabItem Header="Indicators">
|
||||
|
||||
@@ -66,6 +66,7 @@ namespace MtApi5TestClient
|
||||
public DelegateCommand MarketBookGetCommand { get; private set; }
|
||||
|
||||
public DelegateCommand PositionOpenCommand { get; private set; }
|
||||
public DelegateCommand PositionCloseCommand { get; private set; }
|
||||
|
||||
public DelegateCommand GetLastErrorCommand { get; private set; }
|
||||
public DelegateCommand ResetLastErrorCommand { get; private set; }
|
||||
@@ -263,6 +264,17 @@ namespace MtApi5TestClient
|
||||
OnPropertyChanged("GlobalVarValue");
|
||||
}
|
||||
}
|
||||
|
||||
private ulong _positionTicketValue;
|
||||
public ulong PositionTicketValue
|
||||
{
|
||||
get { return _positionTicketValue; }
|
||||
set
|
||||
{
|
||||
_positionTicketValue = value;
|
||||
OnPropertyChanged("PositionTicketValue");
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Public Methods
|
||||
@@ -358,6 +370,7 @@ namespace MtApi5TestClient
|
||||
MarketBookGetCommand = new DelegateCommand(ExecuteMarketBookGet);
|
||||
|
||||
PositionOpenCommand = new DelegateCommand(ExecutePositionOpen);
|
||||
PositionCloseCommand = new DelegateCommand(ExecutePositionClose);
|
||||
|
||||
PrintCommand = new DelegateCommand(ExecutePrint);
|
||||
AlertCommand = new DelegateCommand(ExecuteAlert);
|
||||
@@ -1098,6 +1111,15 @@ namespace MtApi5TestClient
|
||||
AddLog($"PositionOpen: symbol EURUSD retVal = {retVal}, result = {tradeResult}");
|
||||
}
|
||||
|
||||
private async void ExecutePositionClose(object obj)
|
||||
{
|
||||
var ticket = PositionTicketValue;
|
||||
MqlTradeResult tradeResult = null;
|
||||
|
||||
var retVal = await Execute(() => _mtApiClient.PositionClose(ticket, out tradeResult));
|
||||
AddLog($"PositionClose: ticket {ticket} retVal = {retVal}, result = {tradeResult}");
|
||||
}
|
||||
|
||||
private async void ExecutePrint(object obj)
|
||||
{
|
||||
var message = MessageText;
|
||||
|
||||
Binary file not shown.
+35
-1
@@ -41,7 +41,7 @@
|
||||
bool getBooleanValue(int expertHandle, int paramIndex, bool& res, string& err);
|
||||
#import
|
||||
|
||||
#define __DEBUG_LOG__
|
||||
//#define __DEBUG_LOG__
|
||||
|
||||
input int Port = 8228;
|
||||
|
||||
@@ -6700,6 +6700,9 @@ string OnRequest(string json)
|
||||
case 10: //ChartXYToTimePrice
|
||||
response = ExecuteRequest_ChartXYToTimePrice(jo);
|
||||
break;
|
||||
case 11: //PositionClose
|
||||
response = ExecuteRequest_PositionClose(jo);
|
||||
break;
|
||||
default:
|
||||
PrintFormat("%s [WARNING]: Unknown request type %d", __FUNCTION__, requestType);
|
||||
response = CreateErrorResponse(-1, "Unknown request type");
|
||||
@@ -7180,6 +7183,37 @@ string ExecuteRequest_ChartXYToTimePrice(JSONObject *jo)
|
||||
return CreateSuccessResponse("Value", result_value_jo);
|
||||
}
|
||||
|
||||
string ExecuteRequest_PositionClose(JSONObject *jo)
|
||||
{
|
||||
//Ticket
|
||||
CHECK_JSON_VALUE(jo, "Ticket", CreateErrorResponse(-1, "Undefinded mandatory parameter Ticket"));
|
||||
ulong ticket = jo.getLong("Ticket");
|
||||
|
||||
//Deviation
|
||||
CHECK_JSON_VALUE(jo, "Deviation", CreateErrorResponse(-1, "Undefinded mandatory parameter Deviation"));
|
||||
ulong deviation = jo.getLong("Deviation");
|
||||
|
||||
#ifdef __DEBUG_LOG__
|
||||
PrintFormat("%s: Ticket = %d, Deviation = %d", __FUNCTION__, ticket, deviation);
|
||||
#endif
|
||||
|
||||
CTrade trade;
|
||||
bool ok = trade.PositionClose(ticket, deviation);
|
||||
|
||||
MqlTradeResult trade_result={0};
|
||||
trade.Result(trade_result);
|
||||
|
||||
#ifdef __DEBUG_LOG__
|
||||
Print("ExecuteRequest_PositionClose: retcode = ", trade.ResultRetcode());
|
||||
#endif
|
||||
|
||||
JSONObject* result_value_jo = new JSONObject();
|
||||
result_value_jo.put("RetVal", new JSONBool(ok));
|
||||
result_value_jo.put("TradeResult", MqlTradeResultToJson(trade_result));
|
||||
|
||||
return CreateSuccessResponse("Value", result_value_jo);
|
||||
}
|
||||
|
||||
//------------ Events -------------------------------------------------------
|
||||
|
||||
enum MtEventTypes
|
||||
|
||||
Reference in New Issue
Block a user