mirror of
https://github.com/vdemydiuk/mtapi.git
synced 2026-07-27 18:47:55 +00:00
Issue #92: Added functions IndicatorCreate and IndicatorRelease
This commit is contained in:
@@ -0,0 +1,10 @@
|
||||
namespace MtApi5
|
||||
{
|
||||
public class MqlParam
|
||||
{
|
||||
public ENUM_DATATYPE DataType { get; set; }
|
||||
public long? IntegerValue { get; set; }
|
||||
public double? DoubleValue { get; set; }
|
||||
public string StringValue { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -177,6 +177,8 @@ namespace MtApi5
|
||||
TimeCurrent = 127,
|
||||
TimeTradeServer = 128,
|
||||
TimeLocal = 129,
|
||||
TimeGMT = 130
|
||||
TimeGMT = 130,
|
||||
|
||||
IndicatorRelease = 131
|
||||
}
|
||||
}
|
||||
|
||||
@@ -784,4 +784,68 @@ namespace MtApi5
|
||||
}
|
||||
|
||||
#endregion //Smoothing Methods
|
||||
|
||||
#region Indicator constants
|
||||
|
||||
public enum ENUM_INDICATOR
|
||||
{
|
||||
IND_AC = 5, // Accelerator Oscillator
|
||||
IND_AD = 6, // Accumulation/Distribution
|
||||
IND_ADX = 8, // Average Directional Index
|
||||
IND_ADXW = 9, // ADX by Welles Wilder
|
||||
IND_ALLIGATOR = 7, // Alligator
|
||||
IND_AMA = 40, // Adaptive Moving Average
|
||||
IND_AO = 11, // Awesome Oscillator
|
||||
IND_ATR = 10, // Average True Range
|
||||
IND_BANDS = 13, // Bollinger Bands®
|
||||
IND_BEARS = 12, // Bears Power
|
||||
IND_BULLS = 14, // Bulls Power
|
||||
IND_BWMFI = 22, // Market Facilitation Index
|
||||
IND_CCI = 15, // Commodity Channel Index
|
||||
IND_CHAIKIN = 41, // Chaikin Oscillator
|
||||
IND_CUSTOM = 43, // Custom indicator
|
||||
IND_DEMA = 36, // Double Exponential Moving Average
|
||||
IND_DEMARKER = 16, // DeMarker
|
||||
IND_ENVELOPES = 17, // Envelopes
|
||||
IND_FORCE = 18, // Force Index
|
||||
IND_FRACTALS = 19, // Fractals
|
||||
IND_FRAMA = 39, // Fractal Adaptive Moving Average
|
||||
IND_GATOR = 20, // Gator Oscillator
|
||||
IND_ICHIMOKU = 21, // Ichimoku Kinko Hyo
|
||||
IND_MA = 26, // Moving Average
|
||||
IND_MACD = 23, // MACD
|
||||
IND_MFI = 25, // Money Flow Index
|
||||
IND_MOMENTUM = 24, // Momentum
|
||||
IND_OBV = 28, // On Balance Volume
|
||||
IND_OSMA = 27, // OsMA
|
||||
IND_RSI = 30, // Relative Strength Index
|
||||
IND_RVI = 31, // Relative Vigor Index
|
||||
IND_SAR = 29, // Parabolic SAR
|
||||
IND_STDDEV = 32, // Standard Deviation
|
||||
IND_STOCHASTIC = 33, // Stochastic Oscillator
|
||||
IND_TEMA = 37, // Triple Exponential Moving Average
|
||||
IND_TRIX = 38, // Triple Exponential Moving Averages Oscillator
|
||||
IND_VIDYA = 42, // Variable Index Dynamic Average
|
||||
IND_VOLUMES = 34, // Volumes
|
||||
IND_WPR = 35 // Williams' Percent Ranges
|
||||
}
|
||||
|
||||
public enum ENUM_DATATYPE
|
||||
{
|
||||
TYPE_BOOL = 1,
|
||||
TYPE_CHAR = 2,
|
||||
TYPE_UCHAR = 3,
|
||||
TYPE_SHORT = 4,
|
||||
TYPE_USHORT = 5,
|
||||
TYPE_COLOR = 6,
|
||||
TYPE_INT = 7,
|
||||
TYPE_UINT = 8,
|
||||
TYPE_DATETIME = 9,
|
||||
TYPE_LONG = 10,
|
||||
TYPE_ULONG = 11,
|
||||
TYPE_FLOAT = 12,
|
||||
TYPE_DOUBLE = 13,
|
||||
TYPE_STRING = 14
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
|
||||
@@ -49,6 +49,7 @@
|
||||
<ItemGroup>
|
||||
<Compile Include="CopyTicksFlag.cs" />
|
||||
<Compile Include="MqlBookInfo.cs" />
|
||||
<Compile Include="MqlParam.cs" />
|
||||
<Compile Include="MqlRates.cs" />
|
||||
<Compile Include="MqlTick.cs" />
|
||||
<Compile Include="MqlTradeCheckResult.cs" />
|
||||
@@ -68,6 +69,7 @@
|
||||
<Compile Include="Mt5Quote.cs" />
|
||||
<Compile Include="Requests\CopyTicksRequest.cs" />
|
||||
<Compile Include="Requests\ICustomRequest.cs" />
|
||||
<Compile Include="Requests\IndicatorCreateRequest.cs" />
|
||||
<Compile Include="Requests\MarketBookGetRequest.cs" />
|
||||
<Compile Include="Requests\OrderCheckRequest.cs" />
|
||||
<Compile Include="Requests\OrderCheckResult.cs" />
|
||||
|
||||
@@ -1262,6 +1262,32 @@ namespace MtApi5
|
||||
});
|
||||
return response;
|
||||
}
|
||||
|
||||
///<summary>
|
||||
///The function returns the handle of a specified technical indicator created based on the array of parameters of MqlParam type.
|
||||
///</summary>
|
||||
///<param name="symbol">Name of a symbol, on data of which the indicator is calculated. NULL means the current symbol.</param>
|
||||
///<param name="period">The value of the timeframe can be one of values of the ENUM_TIMEFRAMES enumeration, 0 means the current timeframe.</param>
|
||||
///<param name="indicatorType">Indicator type, can be one of values of the ENUM_INDICATOR enumeration.</param>
|
||||
///<param name="parameters">An array of MqlParam type, whose elements contain the type and value of each input parameter of a technical indicator.</param>
|
||||
public int IndicatorCreate(string symbol, ENUM_TIMEFRAMES period, ENUM_INDICATOR indicatorType, List<MqlParam> parameters = null)
|
||||
{
|
||||
var response = SendRequest<int>(new IndicatorCreateRequest
|
||||
{
|
||||
Symbol = symbol,
|
||||
Period = period,
|
||||
IndicatorType = indicatorType,
|
||||
Parameters = parameters
|
||||
});
|
||||
return response;
|
||||
}
|
||||
|
||||
public bool IndicatorRelease(int indicatorHandle)
|
||||
{
|
||||
var commandParameters = new ArrayList { indicatorHandle };
|
||||
|
||||
return SendCommand<bool>(Mt5CommandType.IndicatorRelease, commandParameters);
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Market Info
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace MtApi5.Requests
|
||||
{
|
||||
internal class IndicatorCreateRequest: RequestBase
|
||||
{
|
||||
public override RequestType RequestType => RequestType.IndicatorCreate;
|
||||
|
||||
public string Symbol { get; set; }
|
||||
public ENUM_TIMEFRAMES Period { get; set; }
|
||||
public ENUM_INDICATOR IndicatorType { get; set; }
|
||||
public List<MqlParam> Parameters { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -10,6 +10,7 @@ namespace MtApi5.Requests
|
||||
OrderSend = 3,
|
||||
PositionOpen = 4,
|
||||
OrderCheck = 5,
|
||||
MarketBookGet = 6
|
||||
MarketBookGet = 6,
|
||||
IndicatorCreate = 7
|
||||
}
|
||||
}
|
||||
@@ -352,6 +352,10 @@
|
||||
<Button Command="{Binding CopyTicksCommand}" Margin="1"
|
||||
Content="CopyTicks" HorizontalAlignment="Left" />
|
||||
|
||||
<Button Command="{Binding IndicatorCreateCommand}" Margin="1"
|
||||
Content="IndicatorCreate" HorizontalAlignment="Left" />
|
||||
<Button Command="{Binding IndicatorReleaseCommand}" Margin="1"
|
||||
Content="IndicatorRelease" HorizontalAlignment="Left" />
|
||||
</WrapPanel>
|
||||
</Grid>
|
||||
|
||||
|
||||
@@ -7,6 +7,7 @@ using MtApi5;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Globalization;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Documents;
|
||||
|
||||
namespace MtApi5TestClient
|
||||
{
|
||||
@@ -35,6 +36,8 @@ namespace MtApi5TestClient
|
||||
public DelegateCommand CopyHighCommand { get; private set; }
|
||||
public DelegateCommand CopyLowCommand { get; private set; }
|
||||
public DelegateCommand CopyCloseCommand { get; private set; }
|
||||
public DelegateCommand IndicatorCreateCommand { get; private set; }
|
||||
public DelegateCommand IndicatorReleaseCommand { get; private set; }
|
||||
|
||||
public DelegateCommand CopyTickVolumeCommand { get; private set; }
|
||||
public DelegateCommand CopyRealVolumeCommand { get; private set; }
|
||||
@@ -226,6 +229,8 @@ namespace MtApi5TestClient
|
||||
CopyHighCommand = new DelegateCommand(ExecuteCopyHigh);
|
||||
CopyLowCommand = new DelegateCommand(ExecuteCopyLow);
|
||||
CopyCloseCommand = new DelegateCommand(ExecuteCopyClose);
|
||||
IndicatorCreateCommand = new DelegateCommand(ExecuteIndicatorCreate);
|
||||
IndicatorReleaseCommand = new DelegateCommand(ExecuteIndicatorRelease);
|
||||
|
||||
CopyTickVolumeCommand = new DelegateCommand(ExecuteCopyTickVolume);
|
||||
CopyRealVolumeCommand = new DelegateCommand(ExecuteCopyRealVolume);
|
||||
@@ -548,6 +553,50 @@ namespace MtApi5TestClient
|
||||
AddLog("CopyClose: success");
|
||||
}
|
||||
|
||||
private async void ExecuteIndicatorCreate(object o)
|
||||
{
|
||||
//const string symbol = "EURUSD";
|
||||
//const ENUM_TIMEFRAMES timeframe = ENUM_TIMEFRAMES.PERIOD_H1;
|
||||
//const string name = @"Examples\c";
|
||||
//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}");
|
||||
|
||||
var parameters = new List<MqlParam>
|
||||
{
|
||||
new MqlParam
|
||||
{
|
||||
DataType = ENUM_DATATYPE.TYPE_INT,
|
||||
IntegerValue = 0
|
||||
},
|
||||
new MqlParam
|
||||
{
|
||||
DataType = ENUM_DATATYPE.TYPE_INT,
|
||||
IntegerValue = 21
|
||||
},
|
||||
new MqlParam
|
||||
{
|
||||
DataType = ENUM_DATATYPE.TYPE_INT,
|
||||
IntegerValue = (int)ENUM_APPLIED_PRICE.PRICE_CLOSE
|
||||
}
|
||||
};
|
||||
|
||||
const string symbol = "EURUSD";
|
||||
const ENUM_TIMEFRAMES timeframe = ENUM_TIMEFRAMES.PERIOD_H1;
|
||||
const ENUM_INDICATOR indicatorType = ENUM_INDICATOR.IND_MA;
|
||||
var retVal = await Execute(() => _mtApiClient.IndicatorCreate(symbol, timeframe, indicatorType, parameters));
|
||||
AddLog($"IndicatorCreate [IND_MA]: result - {retVal}");
|
||||
}
|
||||
|
||||
private async void ExecuteIndicatorRelease(object o)
|
||||
{
|
||||
const int indicatorHandle = 111;
|
||||
|
||||
var retVal = await Execute(() => _mtApiClient.IndicatorRelease(indicatorHandle));
|
||||
AddLog($"IndicatorRelease: result - {retVal}");
|
||||
}
|
||||
|
||||
private async void ExecuteCopyRates(object o)
|
||||
{
|
||||
if (string.IsNullOrEmpty(TimeSeriesValues?.SymbolValue)) return;
|
||||
|
||||
Binary file not shown.
+99
-2
@@ -39,7 +39,7 @@
|
||||
bool getBooleanValue(int expertHandle, int paramIndex, bool& res, string& err);
|
||||
#import
|
||||
|
||||
//#define __DEBUG_LOG__
|
||||
#define __DEBUG_LOG__
|
||||
|
||||
input int Port = 8228;
|
||||
|
||||
@@ -673,7 +673,10 @@ int executeCommand()
|
||||
break;
|
||||
case 130: //TimeGMT
|
||||
Execute_TimeGMT();
|
||||
break;
|
||||
break;
|
||||
case 131: //IndicatorRelease
|
||||
Execute_IndicatorRelease();
|
||||
break;
|
||||
default:
|
||||
Print("Unknown command type = ", commandType);
|
||||
sendVoidResponse(ExpertHandle, _response_error);
|
||||
@@ -5436,6 +5439,30 @@ void Execute_TimeGMT()
|
||||
PrintResponseError("TimeGMT", _response_error);
|
||||
}
|
||||
}
|
||||
|
||||
#define GET_VALUE_OR_RETURN_WITH_SENDING_ERROR(get_func, argument_id, argument, cmd_name, param_name) if (!get_func(ExpertHandle, argument_id, argument, _error)) \
|
||||
{ \
|
||||
PrintParamError(cmd_name, param_name, _error); \
|
||||
sendErrorResponse(ExpertHandle, -1, _error, _response_error); \
|
||||
return; \
|
||||
} \
|
||||
|
||||
#define GET_INTEGER_VALUE(argument_id, argument, cmd_name, param_name) GET_VALUE_OR_RETURN_WITH_SENDING_ERROR(getIntValue, argument_id, argument, cmd_name, param_name)
|
||||
|
||||
void Execute_IndicatorRelease()
|
||||
{
|
||||
int indicator_handle;
|
||||
GET_INTEGER_VALUE(0, indicator_handle, "IndicatorRelease", "indicator_handle");
|
||||
|
||||
#ifdef __DEBUG_LOG__
|
||||
PrintFormat("%s: indicator_handle = %d", __FUNCTION__, indicator_handle);
|
||||
#endif
|
||||
|
||||
if (!sendBooleanResponse(ExpertHandle, IndicatorRelease(indicator_handle), _error))
|
||||
{
|
||||
PrintResponseError("IndicatorRelease", _response_error);
|
||||
}
|
||||
}
|
||||
|
||||
void PrintParamError(string paramName)
|
||||
{
|
||||
@@ -5603,6 +5630,9 @@ string OnRequest(string json)
|
||||
case 6: //MarketBookGet
|
||||
response = ExecuteRequest_MarketBookGet(jo);
|
||||
break;
|
||||
case 7: //IndicatorCreate
|
||||
response = ExecuteRequest_IndicatorCreate(jo);
|
||||
break;
|
||||
default:
|
||||
PrintFormat("%s [WARNING]: Unknown request type %d", __FUNCTION__, requestType);
|
||||
response = CreateErrorResponse(-1, "Unknown request type");
|
||||
@@ -6040,4 +6070,71 @@ string ExecuteRequest_MarketBookGet(JSONObject *jo)
|
||||
#endif
|
||||
|
||||
return CreateSuccessResponse("Value", book_ja);
|
||||
}
|
||||
|
||||
string ExecuteRequest_IndicatorCreate(JSONObject *jo)
|
||||
{
|
||||
//Symbol
|
||||
string symbol;
|
||||
if (jo.getValue("Symbol") != NULL)
|
||||
{
|
||||
symbol = jo.getString("Symbol");
|
||||
}
|
||||
|
||||
CHECK_JSON_VALUE(jo, "Period", CreateErrorResponse(-1, "Undefinded mandatory parameter Period"));
|
||||
ENUM_TIMEFRAMES period = (ENUM_TIMEFRAMES) jo.getInt("Period");
|
||||
|
||||
CHECK_JSON_VALUE(jo, "IndicatorType", CreateErrorResponse(-1, "Undefinded mandatory parameter IndicatorType"));
|
||||
ENUM_INDICATOR indicator_type = (ENUM_INDICATOR) jo.getInt("IndicatorType");
|
||||
|
||||
int indicator_handle = -1;
|
||||
if (jo.getValue("Parameters") != NULL)
|
||||
{
|
||||
JSONArray parameters_ja = jo.getArray("Parameters");
|
||||
int size = parameters_ja.size();
|
||||
if (size > 0)
|
||||
{
|
||||
MqlParam parameters[];
|
||||
ArrayResize(parameters, size);
|
||||
|
||||
for (int i = 0; i < size; i++)
|
||||
{
|
||||
JSONObject param_jo = parameters_ja.getObject(i);
|
||||
|
||||
parameters[i].type = (ENUM_DATATYPE)param_jo.getInt("DataType");
|
||||
if (param_jo.getValue("IntegerValue") != NULL)
|
||||
{
|
||||
parameters[i].integer_value = param_jo.getLong("IntegerValue");
|
||||
}
|
||||
if (param_jo.getValue("DoubleValue") != NULL)
|
||||
{
|
||||
parameters[i].double_value = param_jo.getDouble("DoubleValue");
|
||||
}
|
||||
if (param_jo.getValue("StringValue") != NULL)
|
||||
{
|
||||
parameters[i].string_value = param_jo.getString("StringValue");
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef __DEBUG_LOG__
|
||||
PrintFormat("%s: symbol = %s, period = %d, indicator_type = %d, size = %d.", __FUNCTION__, symbol, period, indicator_type, size);
|
||||
#endif
|
||||
|
||||
indicator_handle = IndicatorCreate(symbol, period, indicator_type, size, parameters);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
#ifdef __DEBUG_LOG__
|
||||
PrintFormat("%s: symbol = %s, period = %d, indicator_type = %d.", __FUNCTION__, symbol, period, indicator_type);
|
||||
#endif
|
||||
|
||||
indicator_handle = IndicatorCreate(symbol, period, indicator_type);
|
||||
}
|
||||
|
||||
#ifdef __DEBUG_LOG__
|
||||
PrintFormat("%s: result indicator handle = %d", __FUNCTION__, indicator_handle);
|
||||
#endif
|
||||
|
||||
return CreateSuccessResponse("Value", new JSONNumber(indicator_handle));
|
||||
}
|
||||
Reference in New Issue
Block a user