Issue #82: Refactored request responses (MtAp5). Added iCustom functions with string and boolean parameters. Added button iCustom in test application (MT5).

This commit is contained in:
vdemydiuk
2018-02-19 11:07:37 +02:00
parent 521d7ea2c0
commit 29f501b055
11 changed files with 92 additions and 50 deletions
+2 -4
View File
@@ -71,10 +71,8 @@
<Compile Include="Requests\OrderSendRequest.cs" />
<Compile Include="Requests\RequestBase.cs" />
<Compile Include="Requests\RequestType.cs" />
<Compile Include="Responses\CopyTicksResponse.cs" />
<Compile Include="Responses\ICustomResponse.cs" />
<Compile Include="Responses\OrderSendResponse.cs" />
<Compile Include="Responses\ResponseBase.cs" />
<Compile Include="Responses\OrderSendResult.cs" />
<Compile Include="Responses\Response.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\MTApiService\MTApiService.csproj">
+56 -13
View File
@@ -116,13 +116,13 @@ namespace MtApi5
return false;
}
var response = SendRequest<OrderSendResponse>(new OrderSendRequest
var response = SendRequest<OrderSendResult>(new OrderSendRequest
{
TradeRequest = request
});
result = response?.Value?.TradeResult;
return response?.Value != null && response.Value.RetVal;
result = response?.TradeResult;
return response != null && response.RetVal;
}
///<summary>
@@ -1238,11 +1238,14 @@ namespace MtApi5
///<see href="https://www.mql5.com/en/docs/series/copyticks"/>
public List<MqlTick> CopyTicks(string symbolName, CopyTicksFlag flags = CopyTicksFlag.All, ulong from = 0, uint count = 0)
{
var response = SendRequest<CopyTicksResponse>(new CopyTicksRequest
var response = SendRequest<List<MqlTick>>(new CopyTicksRequest
{
SymbolName = symbolName, Flags = (int)flags, From = from, Count = count
SymbolName = symbolName,
Flags = (int)flags,
From = from,
Count = count
});
return response.Ticks;
return response;
}
#endregion
@@ -2178,7 +2181,7 @@ namespace MtApi5
///<param name="parameters">input-parameters of a custom indicator. If there is no parameters specified, then default values will be used.</param>
public int iCustom(string symbol, ENUM_TIMEFRAMES period, string name, double[] parameters)
{
var response = SendRequest<ICustomResponse>(new ICustomRequest
var response = SendRequest<int>(new ICustomRequest
{
Symbol = symbol,
Timeframe = (int)period,
@@ -2186,7 +2189,7 @@ namespace MtApi5
Params = new ArrayList(parameters),
ParamsType = ICustomRequest.ParametersType.Double
});
return response?.Value ?? 0;
return response;
}
///<summary>
@@ -2198,7 +2201,7 @@ namespace MtApi5
///<param name="parameters">input-parameters of a custom indicator. If there is no parameters specified, then default values will be used.</param>
public int iCustom(string symbol, ENUM_TIMEFRAMES period, string name, int[] parameters)
{
var response = SendRequest<ICustomResponse>(new ICustomRequest
var response = SendRequest<int>(new ICustomRequest
{
Symbol = symbol,
Timeframe = (int)period,
@@ -2206,7 +2209,47 @@ namespace MtApi5
Params = new ArrayList(parameters),
ParamsType = ICustomRequest.ParametersType.Int
});
return response?.Value ?? 0;
return response;
}
///<summary>
///The function returns the handle of the Volumes indicator.
///</summary>
///<param name="symbol">The symbol name of the security, the data of which should be used to calculate the indicator.</param>
///<param name="period">The value of the period can be one of the ENUM_TIMEFRAMES enumeration values, 0 means the current timeframe.</param>
///<param name="name">The name of the custom indicator, with path relative to the root directory of indicators (MQL5/Indicators/). If an indicator is located in a subdirectory, for example, in MQL5/Indicators/Examples, its name must be specified like: "Examples\\indicator_name" (it is necessary to use a double slash instead of the single slash as a separator).</param>
///<param name="parameters">input-parameters of a custom indicator. If there is no parameters specified, then default values will be used.</param>
public int iCustom(string symbol, ENUM_TIMEFRAMES period, string name, string[] parameters)
{
var response = SendRequest<int>(new ICustomRequest
{
Symbol = symbol,
Timeframe = (int)period,
Name = name,
Params = new ArrayList(parameters),
ParamsType = ICustomRequest.ParametersType.Int
});
return response;
}
///<summary>
///The function returns the handle of the Volumes indicator.
///</summary>
///<param name="symbol">The symbol name of the security, the data of which should be used to calculate the indicator.</param>
///<param name="period">The value of the period can be one of the ENUM_TIMEFRAMES enumeration values, 0 means the current timeframe.</param>
///<param name="name">The name of the custom indicator, with path relative to the root directory of indicators (MQL5/Indicators/). If an indicator is located in a subdirectory, for example, in MQL5/Indicators/Examples, its name must be specified like: "Examples\\indicator_name" (it is necessary to use a double slash instead of the single slash as a separator).</param>
///<param name="parameters">input-parameters of a custom indicator. If there is no parameters specified, then default values will be used.</param>
public int iCustom(string symbol, ENUM_TIMEFRAMES period, string name, bool[] parameters)
{
var response = SendRequest<int>(new ICustomRequest
{
Symbol = symbol,
Timeframe = (int)period,
Name = name,
Params = new ArrayList(parameters),
ParamsType = ICustomRequest.ParametersType.Int
});
return response;
}
#endregion // Public Methods
@@ -2400,7 +2443,7 @@ namespace MtApi5
return (T) responseValue;
}
private T SendRequest<T>(RequestBase request) where T : ResponseBase, new()
private T SendRequest<T>(RequestBase request)
{
if (request == null)
return default(T);
@@ -2419,13 +2462,13 @@ namespace MtApi5
throw new ExecutionException(ErrorCode.ErrCustom, "Response from MetaTrader is null");
}
var response = JsonConvert.DeserializeObject<T>(res);
var response = JsonConvert.DeserializeObject<Response<T>>(res);
if (response.ErrorCode != 0)
{
throw new ExecutionException((ErrorCode)response.ErrorCode, response.ErrorMessage);
}
return response;
return response.Value;
}
+8
View File
@@ -0,0 +1,8 @@
namespace MtApi5.Requests
{
internal class OrderSendResult
{
public bool RetVal { get; set; }
public MqlTradeResult TradeResult { get; set; }
}
}
+4 -2
View File
@@ -1,8 +1,10 @@
namespace MtApi5.Responses
namespace MtApi5.Requests
{
internal class ResponseBase
internal class Response<T>
{
public int ErrorCode { get; set; }
public string ErrorMessage { get; set; }
public T Value { get; set; }
}
}
-9
View File
@@ -1,9 +0,0 @@
using System.Collections.Generic;
namespace MtApi5.Responses
{
internal class CopyTicksResponse: ResponseBase
{
public List<MqlTick> Ticks { get; set; }
}
}
-8
View File
@@ -1,8 +0,0 @@
// ReSharper disable InconsistentNaming
namespace MtApi5.Responses
{
internal class ICustomResponse: ResponseBase
{
public int Value { get; set; }
}
}
-13
View File
@@ -1,13 +0,0 @@
namespace MtApi5.Responses
{
internal class OrderSendResult
{
public bool RetVal { get; set; }
public MqlTradeResult TradeResult { get; set; }
}
internal class OrderSendResponse: ResponseBase
{
public OrderSendResult Value { get; set; }
}
}
@@ -402,6 +402,12 @@
<Button Command="{Binding PositionOpenCommand}" Content="PositionOpen" Margin="2"/>
</WrapPanel>
</TabItem>
<TabItem Header="Indicators">
<WrapPanel VerticalAlignment="Top" Margin="5">
<Button Command="{Binding iCustomCommand}" Content="iCustom" Margin="2"/>
</WrapPanel>
</TabItem>
</TabControl>
<Expander Grid.Row="2" Header="History" IsExpanded="True">
+15
View File
@@ -55,6 +55,8 @@ namespace MtApi5TestClient
public DelegateCommand PositionOpenCommand { get; private set; }
public DelegateCommand PrintCommand { get; private set; }
public DelegateCommand iCustomCommand { get; private set; }
#endregion
#region Properties
@@ -237,6 +239,8 @@ namespace MtApi5TestClient
PositionOpenCommand = new DelegateCommand(ExecutePositionOpen);
PrintCommand = new DelegateCommand(ExecutePrint);
iCustomCommand = new DelegateCommand(ExecuteICustom);
}
private bool CanExecuteConnect(object o)
@@ -832,6 +836,17 @@ namespace MtApi5TestClient
AddLog($"Print: message print in MetaTrader - {retVal}");
}
private async void ExecuteICustom(object o)
{
const string symbol = "EURUSD";
const ENUM_TIMEFRAMES timeframe = ENUM_TIMEFRAMES.PERIOD_H1;
const string name = @"Examples\Custom Moving Average";
int[] parameters = { 0, 21, (int)ENUM_APPLIED_PRICE.PRICE_CLOSE };
var retVal = await Execute(() => _mtApiClient.iCustom(symbol, timeframe, name, parameters));
AddLog($"Custom Moving Average: result - {retVal}");
}
private static void RunOnUiThread(Action action)
{
Application.Current?.Dispatcher.Invoke(action);
BIN
View File
Binary file not shown.
+1 -1
View File
@@ -5667,7 +5667,7 @@ string ExecuteRequest_CopyTicks(JSONObject *jo)
jaTicks.put(i, Serialize(ticks[i]));
}
return CreateSuccessResponse("Ticks", jaTicks);;
return CreateSuccessResponse("Value", jaTicks);;
}
string ExecuteRequest_iCustom(JSONObject *jo)