Skip to content

Commit

Permalink
Merged fix of issue #175 from master
Browse files Browse the repository at this point in the history
  • Loading branch information
vdemydiuk committed Nov 6, 2019
2 parents 43ef628 + a4df4bb commit d621312
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 15 deletions.
1 change: 1 addition & 0 deletions MtApi5/Mt5CommandType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ internal enum Mt5CommandType
//CTrade
PositionClose = 64,
PositionOpen = 65,
PositionModify = 6066,
//PositionOpenWithResult = 1065,

//Backtesting
Expand Down
26 changes: 22 additions & 4 deletions MtApi5/MtApi5Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,20 @@ public bool PositionClose(ulong ticket, ulong deviation = ulong.MaxValue)
return SendCommand<bool>(Mt5CommandType.PositionClose, commandParameters);
}

/// <summary>
/// Modifies existing position
/// </summary>
/// <param name="ticket">>Ticket of the position</param>
/// <param name="sl">Stop loss</param>
/// <param name="tp">Take profit</param>
/// <returns></returns>
public bool PositionModify(ulong ticket, double sl, double tp)
{
var commandParameters = new ArrayList { ticket, sl,tp };

return SendCommand<bool>(Mt5CommandType.PositionModify, commandParameters);
}

///<summary>
///Closes a position with the specified ticket.
///</summary>
Expand Down Expand Up @@ -2113,13 +2127,17 @@ public bool ObjectCreate(long chartId, string name, ENUM_OBJECT type, int nwin,
}

int nParameter = 6 + iAdditionalCoordinates * 2;

var commandParameters = new ArrayList { nParameter, chartId, name, (int)type, nwin, Mt5TimeConverter.ConvertToMtTime(time), price };
foreach (Tuple<DateTime, double> coordinateTuple in listOfCoordinates)

if (iAdditionalCoordinates > 0)
{
commandParameters.Add(Mt5TimeConverter.ConvertToMtTime(coordinateTuple.Item1));
commandParameters.Add(coordinateTuple.Item2);
foreach (Tuple<DateTime, double> coordinateTuple in listOfCoordinates)
{
commandParameters.Add(Mt5TimeConverter.ConvertToMtTime(coordinateTuple.Item1));
commandParameters.Add(coordinateTuple.Item2);
}
}

return SendCommand<bool>(Mt5CommandType.ObjectCreate, commandParameters);
}

Expand Down
Binary file modified mq5/MtApi5.ex5
100755 → 100644
Binary file not shown.
95 changes: 84 additions & 11 deletions mq5/MtApi5.mq5
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,9 @@ int executeCommand()
// case 1065: //PositionOpenWithResult
// Execute_PositionOpen(true);
// break;
case 6066: //PositionModify
Execute_PositionModify();
break;
case 66: //BacktestingReady
Execute_BacktestingReady();
break;
Expand Down Expand Up @@ -3258,6 +3261,41 @@ void Execute_MarketBookRelease()
}
}

void Execute_PositionModify()
{
ulong ticket;
double sl;
double tp;

if (!getULongValue(ExpertHandle, 0, ticket, _error))
{
PrintParamError("PositionModify", "ticket", _error);
sendErrorResponse(ExpertHandle, -1, _error, _response_error);
return;
}
if (!getDoubleValue(ExpertHandle, 1, sl, _error))
{
PrintParamError("PositionModify", "sl", _error);
sendErrorResponse(ExpertHandle, -1, _error, _response_error);
return;
}
if (!getDoubleValue(ExpertHandle, 2, tp, _error))
{
PrintParamError("PositionModify", "tp", _error);
sendErrorResponse(ExpertHandle, -1, _error, _response_error);
return;
}

CTrade trade;
bool ok = trade.PositionModify(ticket,sl,tp);
Print("command PositionModify: result = ", ok);

if (!sendBooleanResponse(ExpertHandle, ok, _response_error))
{
PrintResponseError("PositionModify", _response_error);
}
}

void Execute_PositionOpen(bool isTradeResultRequired)
{
string symbol;
Expand Down Expand Up @@ -3402,52 +3440,87 @@ void Execute_PositionSelectByTicket()

void Execute_ObjectCreate()
{
int nParameter = 0;
long chartId;
string name;
int type;
int nwin;
int time;
double price;
int time1;
double price1;
int arrTime[29];
double arrPrice[29];
StringInit(name, 200, 0);

if (!getLongValue(ExpertHandle, 0, chartId, _error))
if (!getIntValue(ExpertHandle, 0, nParameter, _error))
{
PrintParamError("ObjectCreate", "nParameter", _error);
sendErrorResponse(ExpertHandle, -1, _error, _response_error);
return;
}

if (!getLongValue(ExpertHandle, 1, chartId, _error))
{
PrintParamError("ObjectCreate", "chartId", _error);
sendErrorResponse(ExpertHandle, -1, _error, _response_error);
return;
}
if (!getStringValue(ExpertHandle, 1, name, _error))
if (!getStringValue(ExpertHandle, 2, name, _error))
{
PrintParamError("ObjectCreate", "name", _error);
sendErrorResponse(ExpertHandle, -1, _error, _response_error);
return;
}
if (!getIntValue(ExpertHandle, 2, type, _error))
if (!getIntValue(ExpertHandle, 3, type, _error))
{
PrintParamError("ObjectCreate", "type", _error);
sendErrorResponse(ExpertHandle, -1, _error, _response_error);
return;
}
if (!getIntValue(ExpertHandle, 3, nwin, _error))
if (!getIntValue(ExpertHandle, 4, nwin, _error))
{
PrintParamError("ObjectCreate", "nwin", _error);
sendErrorResponse(ExpertHandle, -1, _error, _response_error);
return;
}
if (!getIntValue(ExpertHandle, 4, time, _error))
if (!getIntValue(ExpertHandle, 5, time1, _error))
{
PrintParamError("ObjectCreate", "time", _error);
PrintParamError("ObjectCreate", "time1", _error);
sendErrorResponse(ExpertHandle, -1, _error, _response_error);
return;
}
if (!getDoubleValue(ExpertHandle, 5, price, _error))
if (!getDoubleValue(ExpertHandle, 6, price1, _error))
{
PrintParamError("ObjectCreate", "price", _error);
PrintParamError("ObjectCreate", "price1", _error);
sendErrorResponse(ExpertHandle, -1, _error, _response_error);
return;
}

if (!sendBooleanResponse(ExpertHandle, ObjectCreate(chartId, name, (ENUM_OBJECT)type, nwin, (datetime)time, price), _response_error))
ArrayInitialize(arrTime, 0);
ArrayInitialize(arrPrice, 0);
for(int i = 0; i * 2 + 6 < nParameter; i++)
{
if (!getIntValue(ExpertHandle, i * 2 + 7, arrTime[i], _error))
{
PrintParamError("ObjectCreate", "time" + IntegerToString(i * 2 + 7), _error);
sendErrorResponse(ExpertHandle, -1, _error, _response_error);
return;
}
else if (!getDoubleValue(ExpertHandle, i * 2 + 8, arrPrice[i], _error))
{
PrintParamError("ObjectCreate", "price" + IntegerToString(i * 2 + 8), _error);
sendErrorResponse(ExpertHandle, -1, _error, _response_error);
return;
}
}

if (!sendBooleanResponse(ExpertHandle
,ObjectCreate(chartId, name, (ENUM_OBJECT)type, nwin, (datetime)time1, price1
,(datetime)arrTime[0], arrPrice[0],(datetime)arrTime[1], arrPrice[1],(datetime)arrTime[2], arrPrice[2],(datetime)arrTime[3], arrPrice[3],(datetime)arrTime[4], arrPrice[4],(datetime)arrTime[5], arrPrice[5]
,(datetime)arrTime[6], arrPrice[6],(datetime)arrTime[7], arrPrice[7],(datetime)arrTime[8], arrPrice[8],(datetime)arrTime[9], arrPrice[9],(datetime)arrTime[10], arrPrice[10],(datetime)arrTime[11], arrPrice[11]
,(datetime)arrTime[12], arrPrice[12],(datetime)arrTime[13], arrPrice[13],(datetime)arrTime[14], arrPrice[14],(datetime)arrTime[15], arrPrice[15],(datetime)arrTime[16], arrPrice[16],(datetime)arrTime[17], arrPrice[17]
,(datetime)arrTime[18], arrPrice[18],(datetime)arrTime[19], arrPrice[19],(datetime)arrTime[20], arrPrice[20],(datetime)arrTime[21], arrPrice[21],(datetime)arrTime[22], arrPrice[22],(datetime)arrTime[23], arrPrice[23]
,(datetime)arrTime[24], arrPrice[24],(datetime)arrTime[25], arrPrice[25],(datetime)arrTime[26], arrPrice[26],(datetime)arrTime[27], arrPrice[27],(datetime)arrTime[28], arrPrice[28])
, _response_error))
{
PrintResponseError("ObjectCreate", _response_error);
}
Expand Down

0 comments on commit d621312

Please sign in to comment.